Network Analysis Part 1 Exercises

[This article was first published on R-exercises, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


In this set of exercises we shall create an empty graph and practice the functions for basic manipulation with vertices and edges, using the package igraph. If you don’t have the package already installed, install it using the following code:


and load it into the session using the following code:


before proceeding. You can find more info about the package and graphs in general here

Answers to the exercises are available here.

If you have different solution, feel free to post it.

Exercise 1

Create an empty directed graph with 5 nodes. Set color of all nodes to yellow and shape to sphere.

Exercise 2

Add the following edges to the graph: 1->2, 1->3, 2->4, 3->4, 4->5.

Exercise 3

Add a vertex to the graph, color it to red and add edges: 3->6, 6->5. Set vertex shape to sphere.

Exercise 4

Replace edge 1->3 with the edge 3->1.

Exercise 5

Name vertices with letters A-F. List all vertices and edges.

Exercise 6

What is the shortest path between vertices A and E.

Exercise 7

Plot the graph. The size of a vertex should depend on the number of incoming edges.

Exercise 8

Plot the distribution of vertices degrees.

Exercise 9

Create a heatmap from the data.

Exercise 10

Create graph from subset of vertices with 1 or more incoming edges and plot it. Set the vertice shape to green box. The size of a vertice should depend on the number of outcoming edges.

To leave a comment for the author, please follow the link and comment on their blog: R-exercises. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)