Get Started

Network density and average path length

The first graph level metric you will explore is the density of a graph. This is essentially the proportion of all potential edges between vertices that actually exist in the network graph. It is an indicator of how well connected the vertices of the graph are.

Another measure of how interconnected a network is average path length. This is calculated by determining the mean of the lengths of the shortest paths between all pairs of vertices in the network. The longest path length between any pair of vertices is called the diameter of the network graph. You will calculate the diameter and average path length of the original graph g.

This is a part of the course

“Network Analysis in R”

View Course

Exercise instructions

  • Using the function edge_density() calculate the density of the graph gand assign this value to the vector gd.
  • Use diameter() to calculate the diameter of the original graph g.
  • Assign the average path length of g to g.apl with the function mean_distance().

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

library(igraph)

# Get density of a graph
gd <- ___(g)

# Get the diameter of the graph g
___(g, directed = FALSE)

# Get the average path length of the graph g
g.apl <- ___(g, directed = FALSE)
g.apl

This exercise is part of the course

Network Analysis in R

IntermediateSkill Level
4.8+
6 reviews

Learn to analyze and visualize network data with the igraph package and create interactive network plots with threejs.

This module will show how to characterize global network structures and sub-structures. It will also introduce generating random network graphs.

Exercise 1: IntroductionExercise 2: Forrest Gump networkExercise 3: Network density and average path length
Exercise 4: Graph density quizExercise 5: Understanding network structuresExercise 6: Random graphsExercise 7: Network randomizationsExercise 8: Randomization quizExercise 9: Network substructuresExercise 10: Triangles and transitivityExercise 11: Transitivity randomizationsExercise 12: CliquesExercise 13: Visualize largest cliques

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free