MATLAB Examples

(3) Graph Analysis and Metrics



Created by Brighton Ancelin

This toolbox has four main objectives:

  1. Help the user import a graph from a file or matrix into MATLAB as a graph object
  2. Help the user perform common operations and alterations on graph objects
  3. Provide tools for basic analysis of graph objects and calculation of key metrics
  4. Provide a variety of different visualization means for graph objects, both time-varying and time-invariant

In this page, we will discuss how to generate a graph metrics struct.

All-in-One Metrics Struct

A metrics struct contains many different fields of data about a graph. A brief overview of each field can be seen by using the MATLAB command: help getGraphMetrics

There are two main functions associated with graph metrics fields: getGraphMetrics and exportMetricsFile. The prior can be used to obtain a metrics struct, and the latter can be used to export said metric data into a text file. The latter function will call the prior internally and return the struct, so there's seldom a reason to call the two in succession. An example of proper usage is as follows:

metrics = exportMetricsFile(graph([1,2,3],[2,3,1]),'Title of Graph','filename');
help getGraphMetrics;
  $Author Brighton Ancelin
  Returns a structure of various graph metrics.
 	graphObj: Graph object to be analyzed
    metrics: Structure with metrics data. Fields:
 		isDirected: true if graph is directed, false if graph is undirected
 		isFullyConn: true if graph is fully, strongly connected.
 			See also:
 		distances: NxN (N is the number of nodes) matrix where
 			distances(i,j) represents the shortest path distance between 
 			node i and node j. distances(i,i) will always equal 0,
 			regardless of self-edges.
 			See also:
 		nodeCt: Integer number of nodes in the graph.
 		edgeCt: Integer number of edges in the graph.
 		avgPathLength: Average of all shortest paths between distinct nodes
 			in the graph.
 		diameter: Maximum shortest path length in the graph, i.e. maximum 
 			value of the aforesaid 'distances' field after matrix 
 		clusterings: Column vector of local clustering coefficients.
 			clusterings(n) will return the local clustering coefficient of
 			the nth node. Nodes with degree 1 or less (i.e. 1 or fewer 
 			neighbors) are incapable of forming triangles and are given a
 			default clustering coefficient of 0.
 			See also:
 		avgClustering: Average of all local clustering coefficients as
 			defined above.
 		maxEigenvalue: Maximum eigenvalue of the adjacency matrix. By the
 			Perron-Frobenius Theorem, this eigenvalue has an associated
 			eigenvector whose entries are all nonnegative (for undirected 
 			graphs). This is useful for centrality measurements.
 			See also:
 		eigenCentralities: Eigenvector associated with the maxEigenvalue
 			referenced above. eigenCentralities(n) will return the 
 			eigencentrality of the nth node.
 			See also:
 		degrees: Column vector of degree values. degrees(n) will return the
 			degree of the nth node.
 			See also:
 		degreeCentralities: Column vector of degree centralities, defined
 			as the degree of each node divided by the maximum degree that
 			node could have. degreeCentralities(n) will return the degree 
 			centrality of the nth node.
 			See also:
 		closenessCentralities: Column vector of closeness centralities,
 			defined as the reciprocal of the average of all shortest paths 
 			originating from a given node. closenessCentralities(n) will 
 			return the closeness centrality of the nth node.
 			See also:
 		distanceDistribution: A table of distance distribution data.
 			The 'Distance' vector contains integer values that represent
 			the shortest path lengths. The 'QuantityOfNodePairs' vector
 			contains corresponding quantities of node pairs that have the
 			associated shortest path between them.
 			See also:
 		assortativityByNode: Column vector of assortativity data.
 			assortativityByNode(n) will return the average degree of all
 			neighbors of the nth node.
 			See also:
 		assortativityByDegree: Column vector of assortativity data.
 			assortativityByDegree(n) will return the average of the vector
 			assortativityByNode(arr) where arr contains the node indices of
 			all nodes in the graph of degree n.
 			See also:
 	- Unweighted