38
2015-11-09 © ETH Zürich | Modeling and Simulating Social Systems with MATLAB Lecture 8 – Introduction to Graphs/Networks © ETH Zürich | Chair of Sociology, in particular of Modeling and Simulation Olivia Woolley, Stefano Balietti, Lloyd Sanders, Dirk Helbing

Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 © ETH Zürich |

Modeling and Simulating Social Systems with MATLAB

Lecture 8 – Introduction to Graphs/Networks

© ETH Zürich |

Chair of Sociology, in particular of

Modeling and Simulation

Olivia Woolley, Stefano Balietti, Lloyd Sanders, Dirk Helbing

Page 2: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB 2

Schedule of the course 21.09. 28.09. 05.10. 12.10. 19.10. 26.10. 02.11. 09.11. 16.11. 23.11. 30.12. 07.12. 14.12.

Introduction to MATLAB

Introduction to social-science modeling and simulations

Working on projects (seminar thesis)

Handing in seminar thesis and giving a presentation

Flash Talks

Modeling overview

Page 3: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

Seven Bridges of Königsberg §  Graph Theory was born in 1736, when Euler

posted the following problem: Is it possible to have a walk in the city of Königsberg, that crosses each of the seven bridges only once?

3

Page 4: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

Seven Bridges of Königsberg (II) §  In order to approach the problem, Euler

represented the important information as a graph:

Source: wikipedia.org

4

Page 5: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

Definition of Graph A graph consists of two entities:

§  Nodes (vertices): N

§  Links: L Edge: undirected link Arc: directed link

The graph is defined as G = (N,L)

Source: Batagelj

5

Page 6: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

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)

6

Page 7: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

The social network

7

Page 8: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

Graphs - examples

Internet Map [opte project]Food Web [Martinez ’91]

Protein Interactions [genomebiology.com]

Friendship Network [Moody ’01]

8

Page 9: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 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 Networks

Authors Papers

9

Page 10: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 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

10

Page 11: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

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)

11

Page 12: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

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) Not Connected

(Forest) Connected with loops

12

Page 13: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

Giant Component §  The giant component connects the vast majority of the nodes

of a Graph.

13

Page 14: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

Shortest paths §  The shortest path between i and j is minimum

number of traversed edges

§  Distance l(i,j) = shortest path between i and j §  Diameter D of the graph = max(l(i,j))

I J A

X H

B

D

I J A

X H

B

D

14

Page 15: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

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.

Page 16: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

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.

16

Page 17: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

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 highbetweeness centrality

v

17

σst

= number of shortest paths from s to t

σst

(v) = number of shortest paths from s to t passing through v

Page 18: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

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.com18

Page 19: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

Examples of different network topologies

19

Source: Wang (2003)

Page 20: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

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 ?

Page 21: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

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.

21

Page 22: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

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.

22

Page 23: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

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

23

Page 24: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

Small World model

24

Source: Watts, D. J., &Strogatz, S. H. (1998)

Page 25: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

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;

25

Page 26: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

Graphs §  If the nodes are cities and the links define

connections and travel times for the SBB network it looks like this:

13

2

4Geneva

Basel

BernZurich

26

Page 27: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

Graphs §  If the nodes are cities and the links define

connections and travel times for the SBB network it looks like this:

A = [0 1 1 0; 1 0 1 0; 1 1 0 1; 0 0 1 0];

13

2

4Geneva

Basel0 1 1 0

1 0 1 0

1 1 0 1

0 0 1 0

A =

BernZurich

1 2 3 41234

27

Page 28: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

Graphs §  If the nodes are cities and the links define

connections and travel times for the SBB network it looks like this:

13

2

4Geneva

Bern

Basel

Zurich0:57

0:540:55

1:41

28

Page 29: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

Graphs §  If the nodes are cities and the links define

connections and travel times for the SBB network it looks like this:

13

2

4Geneva

Bern

Basel

Zurich

0 54 57 0

54 0 55 0

57 55 0 101

0 0 101 0

A =

1 2 3 41234

0:57

0:540:55

1:41

29

Page 30: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

Alternatives Ways to Store Network Data §  Edge/Arc lists can easily stored to a file and

loaded when needed

13

2

4Geneva

Basel

BernZurich

1 2 1 3 2 1 2 3 3 1 3 2 3 4 4 3

30

Page 31: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

Alternatives Ways to Store Network Data §  Cell arrays can contain vectors of different size

13

2

4Geneva

Basel

BernZurich

>> A = [2 3]; >> B = [1 3]; >> C = [1 2 4]; >> D = [3]; >> Net = {A;B;C;D}; >> Net{1}(1) >> ans = 2

31

Page 32: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

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.

0 54 57 0

54 0 55 0

57 55 0 101

0 0 101 0

1 2 3 41234

>> A = [2,54; 3,57]; >> B = [1,54; 3,55]; >> C = [1,57; 2,55; 4,101]; >> D = [3,101]; >> Net = {A;B;C;D};

32

Page 33: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

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.

0 54 57 0

54 0 55 0

57 55 0 101

0 0 101 0

1 2 3 41234

>> A = [2,54; 3,57]; >> B = [1,54; 3,55]; >> C = [1,57; 2,55, 4,101]; >> D = [3,101]; >> Net = {A;B;C;D};

Warning: you must validate your own data structure !

33

Page 34: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

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!!

34

Page 35: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

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

35

Page 36: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

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:

36

Page 37: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

2015-11-09 Modeling and Simulation of Social Systems with MATLAB

Live demo which should get this as a final result

Page 38: Modeling and Simulating Social Systems with MATLABethz.ch/content/dam/ethz/special-interest/gess/computational-social-science...2015-11-09 Modeling and Simulation of Social Systems

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 ‘small-

world’ 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: Small-

World, Scale-Free and Beyond §  GEPHI:

§  https://gephi.org/users/supported-graph-formats/csv-format/ §  https://gephi.org/users/ §  https://wiki.gephi.org/index.php/Datasets

38