Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Simple Network Analysis

with MatLab

Gergana Bounova

ESD.342

February 23, 2006

Gergana Bounova
ESD.342
Feb 23, 2006

MatLab Basics

Official MathWorks tutorial:


http://www.mathworks.com/academia/student_center/tutorials/launchpad.html
List of all MatLab functions
http://www.mathworks.com/support/functions/alpha_list.html

Search by name, topic, description

MatLab prompt:

>> date

>> help what

>> lookfor something

>> help lookfor

LOOKFOR Search all M-files for keyword.


See also dir, help, who, what, which.

>> diary (filename,on,off)

>> load mydata.mat

>> type filename.m (.txt)

Loading data: ExcelLink, etc

Gergana Bounova
ESD.342
Feb 23, 2006

Working Example: Bike

References: Daniel Whitney, Degree Correlations and Motifs in Technological


Networks, Source: http://esd.mit.edu/WPS/esd-wp-2005-10.pdf

Gergana Bounova
ESD.342
Feb 23, 2006

Graph Representation in MatLab

Depends on what you are going to do! Computation, extracting


data/properties, visualization
Adjacency matrix A

Incidence matrix C

node by node (nxn), if i and j are connected A(i,j)=1, otherwise A(i,j)=0; for
multiple edges A(i,j)=2,3,
sum(A) = graph degree sequence (self-loops give an exception)
node by edge (nxm), if node i is an endpoint for edge j, then C(i,j)=1,
otherwise C(i,j)=0

sum(C) = [2 2 2 2] every edge has 2 endpoints

Cool formula: A = CxCT - 2I

List: every node points to the nodes its connected to


Text: for other programs input (Pajek)

Gergana Bounova
ESD.342
Feb 23, 2006

01110000000000000000000000001010000000000100100100000000000000000000000000000000000000000000001000000001110000110111111010000110011
10010010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
10010000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
11101111111110100000000010100000000100110000000000000000000000000000000000000000000000000000000000000000001011000000000000000000000
00010100010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00011000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
01010001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00010010001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00110000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00011000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00010001000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00010000001010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00010000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000100000111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00010000000001011011000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000101100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000110100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000011000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000100001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000100010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000001000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000001000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00010000000000000000000001110000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000010110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00010000000000000000000011000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000011000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
10000000000000000000000000000111010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000001011000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
10000000000000000000000000001100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000001100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000100010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000010000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000011111111110000000000000000000000000000000000000000000000000000000000000
00010000000000000000000000000000000011110000000000111111111100000000000000000000000000000000000000000000000010000000000000000000000
00000000000000000000000000000000000100100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000100010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00010000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000
00010000000000000000000000000000000101000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000111111111100000000000000000000000000000000000000000
Gergana
Bounova
10000000000000000000000000000000000000000011111111000000000000000000001111111111000000000000000000000000000000010000000000000000000
ESD.342
00000000000000000000000000000000000000000101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Feb00000000000000000000000000000000000000000110000000000000000000000000000000000000000000000010000000000000000000000000000000000000000
23, 2006
10000000000000000000000000000000000000000100010000000000000000000000000000000000000000000000000000000000000000010000000000000000000

MatLab Code I

>> clear all


>> % Load bike data

>> load bike_data

>> who

>> size(adj_bike)

>> size(deg_bike)

>> deg_bike sum(adj_bike)

>> % Graph Representations **************************************************

>> inc = adj2inc(adj_bike);

>> size(inc)

>> sum(inc) % should give a vector of 2s

>> % num_edges = sum(sum(adj_bike))/2 for an undirected graph

>> % should be same as second dimension of incidence matrix

>> numedges = num_edges(adj_bike)

>> % Lists

>> str = adj2str(adj_bike);

>> % number of children and number of degrees should be the same

>> str(1).child

>> degrees(1)

Gergana Bounova
ESD.342
Feb 23, 2006

Graph Diagnostics

issymmetric.m: A=AT?

issimple.m: are there self-loops or double edges?

isdirected: A=AT?

isconnected.m: is there a path from every node to

every other node?


issparse.m: k << nxm, k # of non-zero entries
All of the above return Boolean variables

Gergana Bounova
ESD.342
Feb 23, 2006

MatLab Code II

>> issymmetric(adj_bike)

ans =

>> issimple(adj_bike)

ans =

>> isdirected(adj_bike)

ans =

>> isconnected(adj_bike)

ans =

1
Gergana Bounova
ESD.342
Feb 23, 2006

bikenet (Pajek)

Graph Properties

LOCAL
shortest path (i-j)
betweenness (i)
degree (ave, max, in-out)
clustering coefficient (i)
harmonic path length (i-j)
number of k-neighbors (i)

Gergana Bounova
ESD.342
Feb 23, 2006

GLOBAL
mean path length
betweenness distribution
degree distributions
mean clustering coeff.
mean harmonic path
k-neighbors distribution
diameter

MatLab Code III

>> degree_dist(adj_bike)
>> clust_coeff(adj_bike)

ans = 0.3933
>> diagnose_powerlaw?
>>ave_path_length(adj_bike)

ans = 0.9973
>> diameter(adj_bike)
ans = 9

degree distribution

10

frequency

20 442

36

10 35
41
0

15
95
97
109
14
25
29
93
112
114
39
40
45
48
107
126
235789111
12
16
17
26
027
30
31
44
46
49
92
104
111
117
122
127
129
6118
19
20
21
22
3
28
32
33
34
37
38
43
47
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
94
99
100
101
102
103
105
106
108
110
115
116
118
119
123
124
125
128
130
131
23
24
96
98
113
120
121
50

100

histogram

Gergana Bounova

ESD.342

Feb 23, 2006

vertex ordered index

100

50

10

20

10

10

degree

150

10

10
0
10

150

30

cumulative degree distribution

vertex degree

30

10

10

10

10
0
10

10

degree

degree_dist.m

10

Graph Construction/Structure

min spanning tree


connected components
dual graphs (dual.m)
subgraph and motifs
growth models
Random, preferential, min-cost, max-span

k-regular graphs

random_graph.m

Gergana Bounova
ESD.342
Feb 23, 2006

MatLab Code IV: Random Graphs

>> adj = random_graph(10)

>> adj = random_graph(10,0.1)

>> adj = random_graph(10,0.1,20)

>> adj = random_graph(10,0.1,20,normal)

>> adj = random_graph(10,0.1,20,custom,@mypdf)

>> degrees = [3 1 1 1];

>> adj = graph_from_degrees(degrees)

2 versions

~ 50% success rate (in producing simple graphs)

Gergana Bounova

ESD.342

Feb 23, 2006

Bike Distribution Random Graphs

Gergana Bounova
ESD.342
Feb 23, 2006

Network Visualization - Pajek

http://vlado.fmf.uni-lj.si/pub/networks/pajek/
draw_circ_graph.m

MatLab-Pajek:
>> adj2pajek(adj,graph.net)
Open graph.net in Pajek
Gtrl + G

Gergana Bounova
ESD.342
Feb 23, 2006

v36
v35
v34
v33
v32
v37
v31
v30
v38
v39
v29
v28
v40
v27
v41
v26
v42
v25
v43
v24
v44
v23
v45
v22
v46
v21
v47
v20
v48
v19
v49
v18
v50
v17
v51
v16
v52
v15
v53
v14
v54
v13
v55
0.5
v12
v56
v11
v57
v10
v58
v9
v59
v8
v60
v7
v61
v6
v62
v5
v63
v4
v64
v3
v65
v2
v66
0 v67
v1
v131
v68
v130
v69
v129
v70
v128
v71
v127
v72
v126
v73
v125
v74
v124
v75
v123
v76
v122
v77
-0.5
v121
v78
v120
v79
v119
v80
v118
v81
v117
v82
v116
v83
v115
v84
v114
v85
v113
v86
v112
v87
v111
v88
v110
v89
v109
v90
v108
v91
v107
v92
v106
v93
v105
v94
v104
v95
v96
v102
v103
v97
v98
v99
v100
v101
-1
-1
-0.5
0
0.5
1

Specialized (Engineering) Metrics

Robustness:
Topological: if you knock out x% of nodes/edges,
how many % survive
Functional: by how much (%-wise) function degrades

Span, coverage, geometry considerations


Cost, performance

total launch mass (space), network throughput, comm


link costs, number of cities served

Gergana Bounova
ESD.342
Feb 23, 2006

Off the Shelf (MatLab Functions)

dist, pdist, linkage, dendrogram, cluster, kmeans


Simulink (circuits), neural net toolbox, comp bio
toolbox
Statistics toolbox: distributions
List of all MatLab functions
http://www.mathworks.com/support/functions/alp
ha_list.html

Gergana Bounova
ESD.342
Feb 23, 2006

Your Own Toolbox

Write functions
Save them in the same directory
Set workpath (accessible from anywhere)
Have Fun!

Gergana Bounova
ESD.342
Feb 23, 2006

Appendix: tutorial.m

% MatLab tutorial script

% Gergana Bounova, February 22, 2006

clear all
% Load bike data
load bike_data

who

size(adj_bike)

size(deg_bike)

% Graph Representations ********************************


inc = adj2inc(adj_bike);

size(inc)

sum(inc) % should give a vector of 2s

% num_edges = sum(sum(adj_bike))/2 for an undirected graph

% should be same as second dimension of incidence matrix

numedges = num_edges(adj_bike)
% Lists
str = adj2str(adj_bike);
% number of children and number of degrees should be the same
str(1).child

degrees(1)

Gergana Bounova
ESD.342
Feb 23, 2006

% Graph Diagnostics ***********************************

issymmetric(adj_bike)

issimple(adj_bike)

isdirected(adj_bike)

isconnected(adj_bike)

% Global metrics

degree_dist(adj_bike)

clust_coeff(adj_bike)

diameter(adj_bike)

ave_path_length(adj_bike)

betweenness(adj_bike)

harmonic_path_length(adj_bike)

% Graph construction ***********************************

random_graph(10)

random_graph(10,0.1,20)

random_graph(10,0,0,'normal')

random_graph(10,0,0,'custom',@mypdf)

degs = [3 1 1 1];

random_graph(10,0,0,'custom',@mypdf,degs)

adj_bike1 = graph_from_degrees(degs);

% Graph visualization ***********************************

adj2pajek(adj_bike,'bike.net');

You might also like