Session6 7 Graph NoAnimation
Session6 7 Graph NoAnimation
Graphs
Modified from
Slides by Kevin Wayne.
Copyright © 2005 Pearson-Addison Wesley.
All rights reserved.
1
3.1 Basic Definitions and Applications
Undirected Graphs
V = { 1, 2, 3, 4, 5, 6, 7, 8 }
E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 }
n=8
m = 11
3
Some Graph Applications
4
World Wide Web
Web graph.
Node: web page.
Edge: hyperlink from one page to another.
cnn.com
hbo.com
sorpranos.com
5
Graph Representation: Adjacency Matrix
1 2 3 4 5 6 7 8
1 0 1 1 0 0 0 0 0
2 1 0 1 1 1 0 0 0
3 1 1 0 0 1 0 1 1
4 0 1 0 1 1 0 0 0
5 0 1 1 1 0 1 0 0
6 0 0 0 0 1 0 0 0
7 0 0 1 0 0 0 0 1
8 0 0 1 0 0 0 1 0
7
Graph Representation: Adjacency List
1 2 3
2 1 3 4 5
3 1 2 5 7 8
4 2 5
5 2 3 4 6
6 5
7 3 8
8 3 7
8
Paths and Connectivity
9
Cycles
Def. A cycle is a path v1, v2, …, vk-1, vk in which v1 = vk, k > 2, and the
first k-1 nodes are all distinct.
cycle C = 1-2-4-5-3-1
10
Trees
11
Rooted Trees
Rooted tree. Given a tree T, choose a root node r and orient each edge
away from r.
root r
parent of v
child of v
12
Phylogeny Trees
13
GUI Containment Hierarchy
Reference: http://java.sun.com/docs/books/tutorial/uiswing/overview/anatomy.html
14
3.2 Graph Traversal
Connectivity
s-t shortest path problem. Given two node s and t, what is the length
of the shortest path between s and t?
Applications.
Social network
Fewest number of hops in a communication network
Navigation (shortest route)
16
Breadth First Search
s L1 L L
BFS algorithm. 2 n-1
L0 = { s }.
L1 = all neighbors of L0.
L2 = all nodes that do not belong to L0 or L1, and that have an edge
to a node in L1.
Li+1 = all nodes that do not belong to an earlier layer, and that have
an edge to a node in Li.
17
Breadth First Search
18
Breadth First Search
19
Breadth First Search
Property. Let T be a BFS tree of G = (V, E), and let (x, y) be an edge of
G. Then the level of x and y differ by at most 1.
L0
L1
L2
L3
20
Breadth First Search - Analysis
O(1) O(V)
O(1)
O(V)
O(deg(v)
)
21
Breadth First Search - Analysis
O(1) O(V)
O(1)
O(2E
)
Pf.
Easy to prove O(n2) running time:
– at most n lists L[i]
– each node occurs on at most one list; for loop runs n times
– when we consider node u, there are n incident edges (u, v),
and we spend O(1) processing each edge
23
BFS Live Poll 1
A. 𝑂(𝑉𝐸)
B. 𝑂(𝑉2)
C. 𝑂(𝐸2)
D. 𝑂(𝑉2 + 𝐸2)
24
BFS Live Poll 1
A. O(VE)
B. O(V2)
C. O(E2)
D. O(V2 + E2)
25
Connected Component
26
Flood Fill
Flood fill. Given lime green pixel in an image, change color of entire
blob of neighboring lime pixels to blue.
Node: pixel.
Edge: two neighboring lime pixels.
Blob: connected component of lime pixels.
recolor lime green blob to blue
27
Flood Fill
Flood fill. Given lime green pixel in an image, change color of entire
blob of neighboring lime pixels to blue.
Node: pixel.
Edge: two neighboring lime pixels.
Blob: connected component of lime pixels.
recolor lime green blob to blue
28
Connected Component
R
s
u v
29
3.4 Testing Bipartiteness
Bipartite Graphs
Applications.
Stable matching: courses = blues, TAs = red.
Scheduling: jobs = blue, machines = red
a bipartite graph
31
Testing Bipartiteness
v2
v2 v3
v1
v4
v6 v5 v4 v3
v5
v6
v7 v1
v7
32
An Obstruction to Bipartiteness
33
Bipartite Graphs
L1 L2 L3 L1 L2 L3
Pf. (i)
Suppose no edge joins two nodes in adjacent layers.
By previous lemma, this implies all edges join nodes on same level.
Bipartition: red = nodes on odd levels, blue = nodes on even levels.
L1 L2 L3
Case (i)
35
Bipartite Graphs
Pf. (ii)
Suppose (x, y) is an edge with x, y in same level Lj.
Let z = lca(x, y) = lowest common ancestor. z = lca(x, y)
Let Li be level containing z.
Consider cycle that takes edge from x to y,
then path from y to z, then path from z to x.
Its length is 1 + (j-i) + (j-i), which is odd. ▪
36
Obstruction to Bipartiteness
5-cycle C
37
3.5 Connectivity in Directed Graphs
Directed Graphs
Ex. Web graph - hyperlink points from one web page to another.
Directedness of graph is crucial.
Modern web search engines exploit hyperlink structure to rank web
pages by importance.
39
Graph Search
Directed s-t shortest path problem. Given two node s and t, what is
the length of the shortest path between s and t?
Web crawler. Start from web page s. Find all web pages linked from s,
either directly or indirectly.
40
Strong Connectivity
ok if paths overlap
s u
41
Strong Connectivity: Algorithm
42
3.6 DAGs and Topological Ordering
Directed Acyclic Graphs
Ex. Precedence constraints: edge (vi, vj) means vi must precede vj.
v2 v3
v6 v5 v4 v1 v2 v3 v4 v5 v6 v7
v7 v1
44
Precedence Constraints
Precedence constraints. Edge (vi, vj) means task vi must occur before vj.
Applications.
Course prerequisite graph: course vi must be taken before vj.
Compilation: module vi must be compiled before vj. Pipeline of
computing jobs: output of job vi needed to determine input of job vj.
45
Directed Acyclic Graphs
v1 vi vj vn
46
Directed Acyclic Graphs
47
Directed Acyclic Graphs
w x u v
48
Directed Acyclic Graphs
DAG
49
Topological Sorting Algorithm: Running Time
Pf.
Maintain the following information:
– count[w] = remaining number of incoming edges
50