Below are the solutions to these exercises on analysing networks.

#################### # # # Exercise 1 # # # #################### g <- make_empty_graph(n=5, directed=TRUE) V(g)$color = "yellow" V(g)$shape = "sphere" #################### # # # Exercise 2 # # # #################### g <- add.edges(g, c(1,2, 1,3, 2,4, 3,4, 4,5)) #################### # # # Exercise 3 # # # #################### g <- add.vertices(g, 1, color="red", shape="sphere") g <- add.edges(g, c(3,6, 6,5)) #################### # # # Exercise 4 # # # #################### E(g)

## + 7/7 edges: ## [1] 1->2 1->3 2->4 3->4 4->5 3->6 6->5

g <- delete.edges(g, c(2)) g <- add.edges(g, c(3,1)) #################### # # # Exercise 5 # # # #################### V(g)$name <- LETTERS[1:6] V(g)

## + 6/6 vertices, named: ## [1] A B C D E F

E(g)

## + 7/7 edges (vertex names): ## [1] A->B B->D C->D D->E C->F F->E C->A

#################### # # # Exercise 6 # # # #################### shortest_paths(g, "A", "E", output="epath")$epath[1]

## [[1]] ## + 3/7 edges (vertex names): ## [1] A->B B->D D->E

#################### # # # Exercise 7 # # # #################### plot(g, layout=layout_nicely, vertex.size=degree(g, V(g), "in")*15+15, vertex.label.dist=0.5, edge.arrow.size=0.5)

#################### # # # Exercise 8 # # # #################### plot(degree_distribution(g), main="Degree distribution", xlab="Degree", ylab="Frequency")

#################### # # # Exercise 9 # # # #################### pal <- colorRampPalette(c("lightblue", "blue")) a <- as.matrix(get.adjacency(g)) heatmap(a, Rowv=NA, Colv="Rowv", col=pal(100))

#################### # # # Exercise 10 # # # #################### sg <- induced_subgraph(g, V(g)[degree(g, V(g), "in") >= 1]) plot(sg, layout=layout_nicely, vertex.size=degree(sg, V(sg), "out")*10+15, vertex.color="green", vertex.shape="square", vertex.label.dist=0.5, edge.arrow.size=0.5)