MATLAB Examples

(1) Importing Graphs from Files

Contents

Intro

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

To get started with this toolbox, we will first focus on importing graphs from files and matrices.

Note: A filename ending in .net is assumed to be a Pajek file. Please be careful to change extensions if needed. More info on the Pajek format here: http://vlado.fmf.uni-lj.si/pub/networks/pajek/

Simple Edge Files

Graph data comes in all kinds of formats, but most commonly is the edge-by-row format. In this format, every line of the file contains two positive integers. Each line in the file represents an edge in the graph, and the two integers on said line are the nodes which that edge connects. The first integer is the "from node" and the second integer is the "to node." In undirected graphs, the order of these nodes does not matter, as edges do not have direction. In directed graphs, however, the order does matter, and the edge points from the "from node" to the "to node." An example of one such file might be the following 'ex_edges-by-rows.txt':

1 5
2 3
4 6
12 8
9 12

Such files can easily be imported as graph objects using the importNet function. For more information on how this function works, use the MATLAB command: help importNet

graphObj = importNet('ex_edges-by-rows.txt',false);
graphObj.Edges
ans = 
    EndNodes
    ________
    1     5 
    2     3 
    4     6 
    8    12 
    9    12 

More Complicated Edge Files

While many edge files have each row representing a single edge, some files will have more than two columns. Two of the columns will contain node IDs, but other data will be written as well. Some of these columns may be useful to import into our graph objects, such as edge weights for weighted graphs. For example, let's look at 'ex_complicatedEdgeFile.txt', whose first column contains edge weights and whose second and third columns contain node IDs:

4 1 4
2 2 3
4 3 1
7 5 8
3 1 5

graphObj = importNet('ex_complicatedEdgeFile.txt',false,'nodeCols',[2,3],'weightCol',1);
graphObj.Edges
ans = 
    EndNodes    Weight
    ________    ______
    1    3      4     
    1    4      4     
    1    5      3     
    2    3      2     
    5    8      7     

Duplicate Edge Handling

Graph objects in MATLAB can't hold multiple identical edges, so importNet has four predefined methods for dealing with duplicate edges: 'first', 'ignore', 'sum', and 'average'. For more information on what each does, use the MATLAB command: help importNet

For directed graphs, the order of nodes matters, but for undirected graphs it does not. Let's observe how we can import the following graph using averaged weights from the file 'ex_duplicateEdges.txt':

1 2 500
3 4 400
2 4 600
2 1 200
3 4 700

graphObj = importNet('ex_duplicateEdges.txt',false,'nodeCols',[1,2],...
		'weightCol',3,'dupMode','average');
graphObj.Edges
ans = 
    EndNodes    Weight
    ________    ______
    1    2      350   
    2    4      600   
    3    4      550   

Preprocessing a Matrix for use with importNet

If the capabilities of importNet just aren't cutting it, you can always do some preprocessing and pass importNet the edge matrix. For example, let's say you have a file with 3 columns: the first two are node IDs, and the third represents the classification of the edge. We want to create a graph with only classification 7 edges. Here's the file 'ex_edgesClassified.txt':

1 2 7
3 2 3
3 2 7
5 7 4
4 2 7
2 5 7
2 2 2

Here's the code:

fileMatrix = str2num(fileread('ex_edgesClassified.txt'));
class7mask = 7 == fileMatrix(:,3);
graphObj = importNet(fileMatrix(class7mask,1:2),false);
graphObj.Edges
ans = 
    EndNodes
    ________
    1    2  
    2    3  
    2    4  
    2    5  

Importing a Graph as Frames

Graphs that change over time, or temporal graphs, can be imported as a cell vector of graph objects. However, this is not accomplished with the importNet function, rather it is accomplished with the getGraphFrames function. This function will create graph frames from a matrix of edges and a column vector of timestamps corresponding to the entries in the edge matrix. For more information on how to use this function, use the MATLAB command: help getGraphFrames

data = [1,2,  0;...
	    1,2,  1;...
	    2,3,  1;...
	    4,2,  2;...
	    4,2,  3;...
	    3,2,  0;...
	    1,4,  5];
edgeMat = data(:,1:2);
timestampVec = data(:,3);
graphFrames = getGraphFrames(edgeMat,timestampVec,true,1,99);
fprintf('Frame Count: %d\n',length(graphFrames));
fprintf('Frame 1:\n'); disp(graphFrames{1}.Edges.EndNodes);
fprintf('Frame 2:\n'); disp(graphFrames{2}.Edges.EndNodes);
fprintf('Frame 3:\n'); disp(graphFrames{3}.Edges.EndNodes);
fprintf('Frame 4:\n'); disp(graphFrames{4}.Edges.EndNodes);
fprintf('Frame 5:\n'); disp(graphFrames{5}.Edges.EndNodes);
fprintf('Frame 6:\n'); disp(graphFrames{6}.Edges.EndNodes);
Frame Count: 6
Frame 1:
     1     2
     3     2
Frame 2:
     1     2
     2     3
Frame 3:
     4     2
Frame 4:
     4     2
Frame 5:
Frame 6:
     1     4