Sei sulla pagina 1di 38

Modeling and Simulating Social Systems

with MATLAB
Lecture 8 Introduction to Graphs/Networks
Olivia Woolley, Stefano Balietti, Lloyd Sanders, Dirk Helbing
Chair of Sociology, in particular of
Modeling and Simulation

ETH Zrich |

2015-11-09

Schedule of the course


Introduction to
MATLAB

21.09.
28.09.
05.10.

Modeling overview

12.10.

Flash Talks

19.10.
26.10.

Working on
projects
(seminar
thesis)

02.11.
09.11.
16.11.

Introduction to
social-science
modeling and
simulations

23.11.
30.12.
07.12.
14.12.

2015-11-09

Handing in seminar thesis


and giving a presentation

Modeling and Simulation of Social Systems with MATLAB

Seven Bridges of Knigsberg


Graph Theory was born in 1736, when Euler
posted the following problem: Is it possible to
have a walk in the city of Knigsberg, that
crosses each of the seven bridges only once?

2015-11-09

Modeling and Simulation of Social Systems with MATLAB

Seven Bridges of Knigsberg (II)


In order to approach the problem, Euler
represented the important information as a
graph:

Source: wikipedia.org

2015-11-09

Modeling and Simulation of Social Systems with MATLAB

Definition of Graph
A graph consists of two entities:

Source: Batagelj

Nodes (vertices): N
Links: L
Edge: undirected link
Arc: directed link
The graph is defined as
G = (N,L)
2015-11-09

Modeling and Simulation of Social Systems with MATLAB

Properties of Links and Nodes


A link can either be encoded as a:

boolean flag (connection vs. no connection), or


value or weight (distance, traveling time, etc.)

Links of different types can exist (multiplex


networks)

A node can also contain information (attributes)

2015-11-09

Modeling and Simulation of Social Systems with MATLAB

The social network

2015-11-09

Modeling and Simulation of Social Systems with MATLAB

Graphs - examples

Internet Map [opte project]

Friendship Network
[Moody 01]
2015-11-09

Food Web
[Martinez 91]

Protein Interactions
[genomebiology.com]

Modeling and Simulation of Social Systems with MATLAB

Graphs - Examples
NODES

LINKS

Protein interaction

Proteins

Metabolic reactions

Internet

Routers

Communication
channels

Social networks

Individuals

Social relations

WWW

Web pages

Hyperlinks

Scientific Coauthorship Authors


Networks

2015-11-09

Papers

Modeling and Simulation of Social Systems with MATLAB

Characterizing networks
1. Path properties
2. Node centrality measures and distributions
3. Local structure e.g. clustering

2015-11-09

Modeling and Simulation of Social Systems with MATLAB

10

Paths
Path of length n = ordered collection of

n+1 nodes. Eg: A,C,D,E in G =(N,L)


n links. Eg: (A,C), (C,D),(D,E) in G =(N,L)

Circuit = closed path (last node = first node)

2015-11-09

Modeling and Simulation of Social Systems with MATLAB

11

Paths and connectedness


A graph G=(N,L)is connected if and only if there
exists a path connecting any two nodes in G

is not connected
Connected
(Tree)

2015-11-09

Not Connected
(Forest)

Modeling and Simulation of Social Systems with MATLAB

Connected
with loops

12

Giant Component
The giant component connects the vast majority of the nodes
of a Graph.

2015-11-09

Modeling and Simulation of Social Systems with MATLAB

13

Shortest paths
The shortest path between i and j is minimum
number of traversed edges
A

J
H

D
X

J
H

D
X

Distance l(i,j) = shortest path between i and j


Diameter D of the graph = max(l(i,j))
2015-11-09

Modeling and Simulation of Social Systems with MATLAB

14

Shortest paths: Average Path Length


Average path length is the average number of
steps along the shortest paths for all possible
pairs of network nodes.

It is a measure of the efficiency of transport


through a network, e.g. how quick an epidemics
can spread.

2015-11-09

Modeling and Simulation of Social Systems with MATLAB

Centrality Measures
The importance of a node can be captured by:
Degree: number of connections
Flux or strength: Sum of strength of all
connections

Closeness: Average distance (inverse of


connection strength) form others.

Eigenvector centrality (e.g. PageRank):


Centrality score is higher the more high-scoring
others a node is connected to.
2015-11-09

Modeling and Simulation of Social Systems with MATLAB

16

Centrality Measures: Betweeness Centrality


Idea: Controlling network flows
The number of shortest paths passing through a
node v. Namely,
Example of a node v with high
betweeness centrality
v
= number of shortest paths from s to t
st
(v) = number of shortest paths from s to t passing through v
st
2015-11-09

Modeling and Simulation of Social Systems with MATLAB

17

Statistical description of network


topology: Degree Distribution
Probability distribution function P(k) of the
degree k of nodes

Random graph: P(k) = binomial distribution


Scale-free graph: P(k) = k- (power law)

Source: www.computerworld.com
2015-11-09

Modeling and Simulation of Social Systems with MATLAB

18

Examples of different network topologies

Source:
Wang (2003)
2015-11-09

Modeling and Simulation of Social Systems with MATLAB

19

Local structure: Clustering Coefficient


Local clustering coefficient C(i): fraction of
pairs of neighbors of a node that are also
neighbors of each other.

Global clustering coefficient: network average


It measures how clickish a network is.
Source Costa (2008)

Question: What is the local clustering coefficient for the node i ?


2015-11-09

Modeling and Simulation of Social Systems with MATLAB

Small Worlds: Clustering & small diameter


Graphs are useful for modeling social networks,
disease spreading, transportation, and so on

One of the most famous graph studies is the


Small World Experiment (S. Milgram), which
shows that the minimum
distance between any two
persons in the world is
almost never longer than
through 5 friends.
2015-11-09

Modeling and Simulation of Social Systems with MATLAB

21

Small World Example: Oracle of Bacon


There is a web page http://oracleofbacon.org/
finding the path from any
actor at any time to the
Hollywood actor Kevin
Bacon.

It can also be used


to find the shortest path
between any two actors.

2015-11-09

Modeling and Simulation of Social Systems with MATLAB

22

Small World Network Properties


High clustered networks, like regular lattices,
and small path lengths, like random graphs.

A small-world network is defined to be a network


where the typical distance L between two
randomly chosen nodes grows logarithmically
with total number of nodes

2015-11-09

Modeling and Simulation of Social Systems with MATLAB

23

Small World model

Source: Watts, D. J., &


Strogatz, S. H. (1998)

2015-11-09

Modeling and Simulation of Social Systems with MATLAB

24

MATLAB Implementation
A graph can be implemented in MATLAB via its
adjacency matrix, i.e. an N x N matrix, defining
how N nodes are connected to the other N-1
nodes:
N = 10;
A = zeros(N, N);
A(1,2) = 1;
A(10,4) = 1;

2015-11-09

Modeling and Simulation of Social Systems with MATLAB

25

Graphs
If the nodes are cities and the links define
connections and travel times for the SBB
network it looks like this:
Basel
2
3
Bern

1
Zurich

4
Geneva
2015-11-09

Modeling and Simulation of Social Systems with MATLAB

26

Graphs
If the nodes are cities and the links define
connections and travel times for the SBB
network it looks like this:
Basel

2
3
Bern
4
Geneva
2015-11-09

1
Zurich

A =

A = [0 1 1 0; 1 0 1 0; 1 1 0 1; 0 0 1 0];
Modeling and Simulation of Social Systems with MATLAB

27

Graphs
If the nodes are cities and the links define
connections and travel times for the SBB
network it looks like this:
Basel
2
0:55
1:41

3
Bern

0:54

1
0:57 Zurich

4
Geneva
2015-11-09

Modeling and Simulation of Social Systems with MATLAB

28

Graphs
If the nodes are cities and the links define
connections and travel times for the SBB
network it looks like this:
Basel
2
0:55
1:41

3
Bern

1
0:54

1
0:57 Zurich

A =

54

57

54

55

57

55

101

101 0

4
Geneva
2015-11-09

Modeling and Simulation of Social Systems with MATLAB

29

Alternatives Ways to Store Network Data


Edge/Arc lists can easily stored to a file and
loaded when needed
Basel
2
3
Bern

1
Zurich

4
Geneva

2015-11-09

Modeling and Simulation of Social Systems with MATLAB

1
1
2
2
3
3
3
4

2
3
1
3
1
2
4
3

30

Alternatives Ways to Store Network Data


Cell arrays can contain vectors of different size
Basel
2
3
Bern
4
Geneva

>>
>>
>>
1
Zurich >>

A
B
C
D

=
=
=
=

[2 3];
[1 3];
[1 2 4];
[3];

>> Net = {A;B;C;D};


>> Net{1}(1)
>> ans = 2

2015-11-09

Modeling and Simulation of Social Systems with MATLAB

31

Alternatives Ways to Store Network Data


Cell arrays grants more freedom in representing
data structures, in spite of losing the simplicity and
clarity of the matrix notation.
1

54

57

54

55

57

55

101

101 0

2015-11-09

>>
>>
>>
>>

A
B
C
D

=
=
=
=

[2,54; 3,57];
[1,54; 3,55];
[1,57; 2,55; 4,101];
[3,101];

>> Net = {A;B;C;D};

Modeling and Simulation of Social Systems with MATLAB

32

Alternatives Ways to Store Network Data


Cell arrays grants more freedom in representing
data structures, in spite of loosing the simplicity
and clarity of the matrix notation.
1

54

57

54

55

57

55

101

101 0

2015-11-09

>> A = [2,54; 3,57];


>> B = [1,54; 3,55];
Warning:
you must
validate
>> C = [1,57;
2,55,
4,101];
your
data structure !
>> Down
= [3,101];
>> Net = {A;B;C;D};

Modeling and Simulation of Social Systems with MATLAB

33

Software Packages for Graph Visualization


The following programs are valuable tools for
representing and and visualizing networks:

Pajek (http://pajek.imfm.si/doku.php) -> Easy to use


NWB (http://nwb.cns.iu.edu/) -> Good for Analysis
Gephi (http://gephi.org/) -> New
Visone (http://visone.info/) -> made in Konstanz
JUNG (http://jung.sourceforge.net/) -> library
Net Draw (http://www.analytictech.com/netdraw/netdraw.htm)
Pegasus (http://www.cs.cmu.edu/~pegasus/) -> for huge data

Use them!!
2015-11-09

Modeling and Simulation of Social Systems with MATLAB

34

Exporting and visualizing a graph in Gephi


csvwrite (filename,matrix)writes a
matrix as a list of comma seperated values

but works only with adjacency matrixes.


Often we need an edge list (cell array).
Download two files from the web site:
cell2csv.m export.m

2015-11-09

Modeling and Simulation of Social Systems with MATLAB

35

Exporting and visualizing a graph in Gephi


Download Gephi
Open the .csv edge list that you just exported
Visualize the network
Compute the modularity score:

2015-11-09

Modeling and Simulation of Social Systems with MATLAB

36

Live demo which should get this as a


final result

2015-11-09

Modeling and Simulation of Social Systems with MATLAB

References
Handbook of graphs and networks: from the Genome to the Internet,

edited by S. Bornholdt, H. G. Schuster. John Wiley and Sons, 2003.


Watts,D.J.,& Strogatz, S.H. (1998).Collective dynamics of smallworld networks. nature, 393(6684), 440-442.
Newman, M.E. (2003).The structure and function of complex
networks. SIAM review, 45(2), 167-256.
Newman, M. E. (2009). Networks: an introduction. Oxford University
Press.
Easley,D., &Kleinberg,J. (2010). Networks, crowds, and markets.
Cambridge: Cambridge University Press.
Xiao Fan Wang and Guanrong Chen Complex Networks: SmallWorld, Scale-Free and Beyond
GEPHI:

2015-11-09

https://gephi.org/users/supported-graph-formats/csv-format/
https://gephi.org/users/
https://wiki.gephi.org/index.php/Datasets
Modeling and Simulation of Social Systems with MATLAB

38

Potrebbero piacerti anche