Professional Documents
Culture Documents
3
Protein-protein interaction network
http://en.wikipedia.org/wiki/Internet
5
Map of science clickstreams
http://www.plosone.org/article/info:doi/10.1371/journal.pone.0004803
6
10 million Facebook friends
7
One week of Enron emails
8
The evolution of FCC lobbying coalitions
“The Evolution of FCC Lobbying Coalitions” by Pierre de Vries in JoSS Visualization Symposium 2010 9
weight status controlled for homophily.25 The smoking status of egos and alters at times t and
key variable of interest was an alter’s obesity at t + 1 to the foregoing models. We also analyzed
time t + 1. A significant coefficient for this vari- the role of geographic distance between egos
Framingham heart study
able would suggest either that an alter’s weight
affected an ego’s weight or that an ego and an
and alters by adding such a variable.
We calculated 95% confidence intervals by sim-
alter experienced contemporaneous events affect- ulating the first difference in the alter’s contem-
Figure 1. Largest Connected Subcomponent of the Social Network in the Framingham Heart Study in the Year 2000.
Each circle (node) represents one person in the data set. There are 2200 persons in this subcomponent of the social
network. Circles with red borders denote women, and circles with blue borders denote men. The size of each circle
is proportional to the person’s body-mass index. The interior color of the circles indicates the person’s obesity status:
yellow denotes an obese person (body-mass index, ≥30) and green denotes a nonobese person. The colors of the
ties between the nodes indicate the relationship between them: purple denotes a friendship or marital tie and orange
denotes a familial tie.
“The Spread of Obesity in a Large Social Network over 32 Years” by Christakis and Fowler in New England Journal of Medicine, 2007 10
n engl j med 357;4 www.nejm.org july 26, 2007 373
Graph applications
11
Graph terminology
vertex
edge
cycle of
length 5
path of
length 4
vertex of
degree 3
connected
components
Anatomy of a graph
12
Some graph-processing problems
Planarity. Can you draw the graph in the plane with no crossing edges
Graph isomorphism. Do two adjacency lists represent the same graph?
13
4.1 U NDIRECTED G RAPHS
‣ introduction
‣ graph API
‣ depth-first search
Algorithms
‣ breadth-first search
Vertex representation.
・This lecture: use integers between 0 and V – 1.
・Applications: convert between names and integers with symbol table.
A 0
B C G 1 2 6
symbol table
D E 3 4
F 5
self-loop parallel
edges
Anomalies.
Anomalies
17
Graph API
0 0 1
0 2
0 5
1 2 6
0 6
3 4
3 4 3 5
4 5
5 4 6
7 8
9 10 9 10
9 11
7 8 9 12
11 12 11 12
21
Adjacency-matrix graph representation
two entries
for each edge
0
0 1 2 3 4 5 6 7 8 9 10 11 12
0 0 1 1 0 0 1 1 0 0 0 0 0 0
1 2 6 1 1 0 0 0 0 0 0 0 0 0 0 0 0
2 1 0 0 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 1 1 0 0 0 0 0 0 0
3 4 4 0 0 0 1 0 1 1 0 0 0 0 0 0
5 1 0 0 1 1 0 0 0 0 0 0 0 0
5 6 1 0 0 0 1 0 0 0 0 0 0 0 0
7 0 0 0 0 0 0 0 0 1 0 0 0 0
8 0 0 0 0 0 0 0 1 0 0 0 0 0
9 10 9 0 0 0 0 0 0 0 0 0 0 1 1 1
10 0 0 0 0 0 0 0 0 0 1 0 0 0
7 8
11 0 0 0 0 0 0 0 0 0 1 0 0 1
11 12 12 0 0 0 0 0 0 0 0 0 1 0 1 0
22
Adjacency-list graph representation
6 2 1 5
0
Bag objects
0
0
adj[]
0 5 4
1
1 2 6 2 5 6 3
3
4 3 4 0
3 4 5
6 0 4
7
5 8 8
representations
9 of the same edge
10 7
9 10 11
12 11 10 12
7 8
9
11 12
9 12
11 9
public Graph(int V)
{
this.V = V;
create empty graph
adj = (Bag<Integer>[]) new Bag[V]; with V vertices
for (int v = 0; v < V; v++)
adj[v] = new Bag<Integer>();
}
{ return adj[v]; }
}
24
Graph representations
25
Graph representations
list of edges E 1 E E
adjacency matrix V2 1* 1 V
26
4.1 U NDIRECTED G RAPHS
‣ introduction
‣ graph API
‣ depth-first search
Algorithms
‣ breadth-first search
Maze graph.
・Vertex = intersection.
・Edge = passage.
intersection passage
Algorithm.
・Unroll a ball of string behind you.
・Mark each visited intersection and each visited passage.
・Retrace steps when no unvisited options.
Tremaux exploration
30
Trémaux maze exploration
Algorithm.
・Unroll a ball of string behind you.
・Mark each visited intersection and each visited passage.
・Retrace steps when no unvisited options.
First use? Theseus entered Labyrinth to kill the monstrous Minotaur;
Ariadne instructed Theseus to use a ball of string to find his way back out.
31
Maze exploration
32
Maze exploration
33
Depth-first search
Mark v as visited.
Recursively visit all unmarked
vertices w adjacent to v.
Typical applications.
・Find all vertices connected to a given source vertex.
・Find a path between two vertices.
35
Depth-first search demo
To visit a vertex v :
・Mark vertex v as visited.
・Recursively visit all unmarked vertices adjacent to v.
tinyG.txt
0 7 8 V
13
E
13
0 5
4 3
0 1
1 2 6 9 10 9 12
6 4
5 4
0 2
11 12
3 4 11 12
9 10
0 6
7 8
9 11
5
5 3
graph G
36
Depth-first search demo
To visit a vertex v :
・Mark vertex v as visited.
・Recursively visit all unmarked vertices adjacent to v.
0 7 8 v marked[] edgeTo[v]
0 T –
1 T 0
2 T 0
1 2 6 9 10 3 T 5
4 T 6
5 T 4
6 T 0
3 4 11 12 7 F –
8 F –
9 F –
5 10 F –
11 F –
12 F –
37
Depth-first search
Algorithm.
・Use recursion (ball of string).
・Mark each visited vertex (and keep track of edge taken to visit it).
・Return (retrace steps) when no unvisited options.
Data structures.
・ boolean[] marked to mark visited vertices.
・ int[] edgeTo to keep tree of paths.
(edgeTo[w] == v) means that edge v-w taken to visit w for first time
Depth-first search
40
Depth-first search properties
edgeTo[]
public Iterable<Integer> pathTo(int v)
0
{
1 2
if (!hasPathTo(v)) return null; 2 0
Stack<Integer> path = new Stack<Integer>(); 3 2
for (int x = v; x != s; x = edgeTo[x]) 4 3
path.push(x); 5 3
path.push(s); x path
return path; 5 5
} 3 3 5
2 2 3 5
0 0 2 3 5
http://xkcd.com/761/
42
Depth-first search application: flood fill
tinyCG.txt standard d
0 2
V
6 E
8
0 5
1 2 4
2 3
1 2 drawing w
0 1
3 3 4
3 5
5 4 0 2
adjacency lists
2
graph G
0
adj[]
46
0
0
Breadth-first search demo
0 2
v edgeTo[] distTo[]
0 – 0
1 0 1
1 2 0 1
3 2 2
4 2 2
3 5 0 1
5 4
done
47
Breadth-first search
Shortest path. Find path from s to t that uses fewest number of edges.
Breadth-first
maze exploration
Intuition. BFS examines vertices in increasing distance from s.
48
Breadth-first search properties
1
4
0 2
1 0 2
3
3
5 4
5
50
Breadth-first search application: routing
51
Breadth-first search application: Kevin Bacon numbers
52
Kevin Bacon graph
・Include one vertex for each performer and one for each movie.
・Connect a movie to all performers that appear in that movie.
・Compute shortest path from s = Kevin Bacon.
Patrick Dial M Grace
Caligola Kelly
Allen for Murder
Murder on the
Orient Express
Cold Donald
Sutherland Kathleen Joe Versus
Mountain
Quinlan the Volcano
53
V and E
movies.txt
not explicitly
specified
Breadth-first search application: Erdös numbers
public class CC
63 connected components
59
Connected components
Connected components
To visit a vertex v :
・Mark vertex v as visited.
・Recursively visit all unmarked vertices adjacent to v.
0 7 8 v marked[] id[]
0 F –
1 F –
2 F –
1 2 6 9 10 3 F –
4 F –
5 F –
6 F –
3 4 11 12 7 F –
8 F –
9 F –
5 10 F –
11 F –
12 F –
graph G
61
Connected components demo
To visit a vertex v :
・Mark vertex v as visited.
・Recursively visit all unmarked vertices adjacent to v.
0 7 8 v marked[] id[]
0 T 0
1 T 0
2 T 0
1 2 6 9 10 3 T 0
4 T 0
5 T 0
6 T 0
3 4 11 12 7 T 1
8 T 1
9 T 2
5 10 T 2
11 T 2
12 T 2
done
62
Finding connected components with DFS
public class CC
{
private boolean[] marked;
private int[] id; id[v] = id of component containing v
private int count; number of components
public CC(Graph G)
{
marked = new boolean[G.V()];
id = new int[G.V()];
for (int v = 0; v < G.V(); v++)
{
if (!marked[v])
{ run DFS from one vertex in
dfs(G, v); each component
count++;
}
}
}
}
63
Finding connected components with DFS (continued)
64
Connected components application: study spread of STDs
Peter Bearman, James Moody, and Katherine Stovel. Chains of affection: The structure of
adolescent romantic and sexual networks. American Journal of Sociology, 110(1): 44–99, 2004.
65
Connected components application: particle detection
0 0-1
0-2
0-5
1 2 6
0-6
1-3
How difficult? 3 4 2-3
・Any programmer could do it. 5
2-4
4-5
✓・Typical diligent algorithms student could do it. 4-6
・Hire an expert.
・Intractable. simple DFS-based solution
(see textbook) 0 0-1
・No one knows. 0-2
0-5
・Impossible. 1 2 6
0-6
1-3
3 4 2-3
2-4
5 4-5
4-6
{ 0, 3, 4 }
69
Bipartiteness application: is dating graph bipartite?
70
Graph-processing challenge 2
0 0-1
0-2
0-5
1 2 6
0-6
1-3
How difficult? 3 4 2-3
・Any programmer could do it. 5
2-4
4-5
✓・Typical diligent algorithms student could do it. 4-6
71
Bridges of Königsberg
Euler tour. Is there a (general) cycle that uses each edge exactly once?
Answer. A connected graph is Eulerian iff all vertices have even degree.
72
Graph-processing challenge 3
Problem. Find a (general) cycle that uses every edge exactly once.
0 0-1
0-2
0-5
1 2 6
0-6
1-2
How difficult? 3 4 2-3
・Any programmer could do it. 5
2-4
3-4
✓・Typical diligent algorithms student could do it. 4-5
73
Graph-processing challenge 4
0 0-1
0-2
0-5
1 2 6
0-6
1-2
How difficult? 3 4 2-6
・Any programmer could do it. 5
3-4
3-5
・Typical diligent algorithms student could do it. 4-5
✓・Intractable.
Hamiltonian cycle
・No one knows. (classical NP-complete problem)
・Impossible.
74
Graph-processing challenge 5
0 0-1
0-2
0-5
1 2 6
0-6
3-4
How difficult? 3 4 3-5
・Any programmer could do it. 5
4-5
4-6
・Typical diligent algorithms student could do it.
・Hire an expert.
・Intractable. 3 0-4
✓・No one knows. 2
0-5
0-6
・Impossible. graph isomorphism is
4
1
1-4
1-5
6
longstanding open problem
5 2-4
3-4
0 5-6
75
Graph-processing challenge 6
1 0-1
0-2
2
0-5
0 0-6
6
3-4
How difficult? 3
4 3-5
・Any programmer could do it. 4-5
4-6
・Typical diligent algorithms student could do it. 5
✓ ・Hire an expert.
・Intractable. linear-time DFS-based planarity algorithm
0
3 4
76
4.1 U NDIRECTED G RAPHS
‣ introduction
‣ graph API
‣ depth-first search
Algorithms
‣ breadth-first search