41UndirectedGraphs 2x2
41UndirectedGraphs 2x2
3 4
Protein-protein interaction network Map of science clickstreams
8 “The Evolution of FCC Lobbying Coalitions” by Pierre de Vries in JoSS Visualization Symposium 2010 9
use of a lagged independent variable for an alter’s the spread of obesity by adding variables for the
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-
The Internet as mapped by the Opte Project
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.
http://en.wikipedia.org/wiki/Internet
“The Spread of Obesity in a Large Social Network over 32 Years” by Christakis and Fowler in New England Journal of Medicine, 2007 10 11
n engl j med 357;4 www.nejm.org july 26, 2007 373
circuit gate, register, processor wire Two vertices are connected if there is a path between them.
12 Anatomy of a graph 13
Some graph-processing problems
problem description
Graph drawing. Provides intuition about the structure of the graph. 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
two drawings of the same graph
Two drawings of the same graph
self-loop parallel
edges
Anomalies.
Caveat. Intuition can be misleading.
Anomalies
16 17
Two drawings of the same graph
Graph API Graph API: sample client
Graph(int V) create an empty graph with V vertices tinyG.txt % java Test tinyG.txt
mediumG.txt
V V 0-6250
13
E E
Graph(In in) create a graph from input stream 13 0-21273
0 5 244 246
0-1
void addEdge(int v, int w) 4 3 239 240
add an edge v-w 0-5238 245
0 1
9 12 1-0235 238
Iterable<Integer> adj(int v) vertices adjacent to v 6 4 2-0233 240
5 4 232 248
3-5
int V() number of vertices 0 2 231 248
11 12 3-4229 249
int E() number of edges 9 10 ⋮ 228 241
0 6 226 231
12-11
7 8 ...
9 11 12-9
(1261 additional lines)
5 3
Input format for Graph constructor (two examples)
In in = new In(args[0]); read graph from In in = new In(args[0]); read graph from
Graph G = new Graph(in); input stream Graph G = new Graph(in); input stream
0
two entries Bag objects
0 for each edge 0
0
0 1 2 3 4 5 6 7 8 9 10 11 12 adj[]
0 5 4
1 2 6 0 0 1 1 0 0 1 1 0 0 0 0 0 0 1 2 6 1
1 1 0 0 0 0 0 0 0 0 0 0 0 0 2 5 6 3
2 1 0 0 0 0 0 0 0 0 0 0 0 0 3
3 4 3 0 0 0 0 1 1 0 0 0 0 0 0 0 3 4 4 3 4 0
4 0 0 0 1 0 1 1 0 0 0 0 0 0 5
6 0 4
5 5 1 0 0 1 1 0 0 0 0 0 0 0 0 5 7
6 1 0 0 0 1 0 0 0 0 0 0 0 0 8
8
7 0 0 0 0 0 0 0 0 1 0 0 0 0 representations
9 of the same edge
9 10 8 0 0 0 0 0 0 0 1 0 0 0 0 0 9 10 10 7
9 0 0 0 0 0 0 0 0 0 0 1 1 1 11
7 8 10 0 0 0 0 0 0 0 0 0 1 0 0 0 7 8 12 11 10 12
11 12 11 0 0 0 0 0 0 0 0 0 1 0 0 1 11 12 9
12 0 0 0 0 0 0 0 0 0 1 0 1 0
9 12
Q. How long to iterate over vertices adjacent to v ? Q. How long to iterate over vertices adjacent to v ? 11 9
list of edges E 1 E E
adjacency matrix V2 1* 1 V
24 25
Adjacency-list graph representation: Java implementation
public Graph(int V)
{ 4.1 U NDIRECTED G RAPHS
this.V = V;
create empty graph
adj = (Bag<Integer>[]) new Bag[V]; with V vertices ‣ introduction
for (int v = 0; v < V; v++)
adj[v] = new Bag<Integer>(); ‣ graph API
}
‣ depth-first search
Algorithms
public void addEdge(int v, int w)
‣ breadth-first search
{ add edge v-w
adj[v].add(w); (parallel edges and
R OBERT S EDGEWICK | K EVIN W AYNE
‣ connected components
adj[w].add(v); self-loops allowed)
} http://algs4.cs.princeton.edu ‣ challenges
{ return adj[v]; }
}
26
intersection passage
Tremaux exploration
Goal. Explore every intersection in the maze.
28 29
Trémaux maze exploration Maze exploration: easy
Algorithm.
Unroll a ball of string behind you.
Mark each visited intersection and each visited passage.
Retrace steps when no unvisited options.
30 31
32 33
Depth-first search Depth-first search demo
35
To visit a vertex v : Design pattern. Decouple graph data type from graph processing.
Mark vertex v as visited. Create a Graph object.
Recursively visit all unmarked vertices adjacent to v. Pass the Graph to a graph-processing routine.
Query the graph-processing routine for information.
0 7 8 v marked[] edgeTo[]
0 T –
public class Paths
1 T 0
2 T 0 Paths(Graph G, int s) find paths in G from source s
1 2 6 9 10 3 T 5
4 T 6 boolean hasPathTo(int v) is there a path from s to v?
5 T 4
6 T 0 Iterable<Integer> pathTo(int v) path from s to v; null if no such path
3 4 11 12 7 F –
8 F –
9 F –
5 10 F – Paths paths = new Paths(G, s);
11 F – for (int v = 0; v < G.V(); v++)
12 F – if (paths.hasPathTo(v))
print all vertices
StdOut.println(v); connected to s
vertices reachable from 0
36 37
Depth-first search: data structures Depth-first search: Java implementation
marked[v] = true;
for (int w : G.adj(v))
if (!marked[w])
{
dfs(G, w);
edgeTo[w] = v;
}
}
}
39
Proposition. DFS marks all vertices connected to s in time proportional to Proposition. After DFS, can check if vertex v is connected to s in constant
the sum of their degrees (plus time to initialize the marked[] array). time and can find v–s path (if one exists) in time proportional to its length.
Pf. [correctness] source set of marked Pf. edgeTo[] is parent-link representation of a tree rooted at vertex s.
vertices
If w marked, then w connected to s (why?) s
If w connected to s, then w marked.
(if w unmarked, then consider last edge
public boolean hasPathTo(int v)
on a path from s to w that goes from a v { return marked[v]; }
no such edge
set of can exist
marked vertex to an unmarked one). unmarked edgeTo[]
public Iterable<Integer> pathTo(int v)
vertices x 0
{
1 2
if (!hasPathTo(v)) return null; 2 0
Pf. [running time] Stack<Integer> path = new Stack<Integer>(); 3 2
Each vertex connected to s is visited once. for (int x = v; x != s; x = edgeTo[x]) 4 3
w 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
42 43
‣ graph API 1
0
2
5
4
2 3
‣ depth-first search
Algorithms 1
0
2
1
drawing with bo
‣ breadth-first search 3 3 4
3 5
http://algs4.cs.princeton.edu ‣ challenges
adjacency lists
2 1
graph G
0 2
adj[]
45
0
0 1
1
Breadth-first search demo Breadth-first search
Breadth-first
maze exploration
done
46 47
50 51
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.
Murder on the
Orient Express
Cold Donald
Sutherland Kathleen Joe Versus
Mountain
Quinlan the Volcano
http://algs4.cs.princeton.edu ‣ challenges
The relation "is connected to" is an equivalence relation: Def. A connected component is a maximal set of connected vertices.
Reflexive: v is connected to v.
Symmetric: if v is connected to w, then w is connected to v.
Transitive: if v connected to w and w connected to x, then v connected to x.
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.
62 63
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. 2-4
5 4-5
✓ Typical diligent algorithms student could do it. 4-6
Hire an expert.
Intractable. simple DFS-based solution
0
(see textbook) 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
Image created by Mark Newman.
4-6
{ 0, 3, 4 }
66 67
Problem. Find a cycle. The Seven Bridges of Königsberg. [Leonhard Euler 1736]
Euler cycle. Is there a (general) cycle that uses each edge exactly once?
Answer. A connected graph is Eulerian iff all vertices have even degree.
68 69
Graph-processing challenge 3 Graph-processing challenge 4
Problem. Find a (general) cycle that uses every edge exactly once. Problem. Find a cycle that visits every vertex exactly once.
0 0-1 0 0-1
0-2 0-2
0-5 0-5
1 2 6 1 2 6
0-6 0-6
1-2 1-2
How difficult? 3 4
How difficult? 3 4
2-3 2-6
Any programmer could do it. 2-4 Any programmer could do it. 3-4
5 3-4 5 3-5
✓ Typical diligent algorithms student could do it. 4-5
Typical diligent algorithms student could do it. 4-5
Hire an expert. 4-6 Hire an expert. 4-6
0-1-2-3-4-2-0-6-4-5-0 0-5-3-4-6-2-1-0
Intractable. Euler cycle ✓ Intractable.
(classic graph-processing problem) Hamilton cycle
No one knows. No one knows. (classical NP-complete problem)
Impossible. Impossible.
70 71
Problem. Are two graphs identical except for vertex names? Problem. Lay out a graph in the plane without crossing edges?
0 0-1 1 0-1
0-2 0-2
2
0-5 0-5
1 2 6 0
0-6 0-6
6
3-4 3-4
How difficult? 3 4
How difficult? 3
3-5 4 3-5
Any programmer could do it. 4-5 Any programmer could do it. 4-5
5 4-6 5 4-6
Typical diligent algorithms student could do it. Typical diligent algorithms student could do it.
Hire an expert. ✓ Hire an expert.
Intractable. 3 0-4 Intractable. 0
linear-time DFS-based planarity algorithm
✓ No one knows. 2
0-5 No one knows. discovered by Tarjan in 1970s
0-6
Impossible. 4 1-4 Impossible. (too complicated for most practitioners) 1 2 6
graph isomorphism is 1
6 1-5
longstanding open problem
5 2-4 3 4
3-4
0 5-6 5
72 73
Graph traversal summary
BFS and DFS enables efficient solution of many (but not all) graph problems.
cycle ✔ ✔ E+V
planarity ✔ E+V
74