Lecture 03
Lecture 03
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
2
Some Graph Applications
Web graph.
Node: web page.
Edge: hyperlink from one page to another.
cnn.com
hbo.com
sorpranos.com
4
9-11 Terrorist Network
Reference: http://www.twingroves.district96.k12.il.us/Wetlands/Salamander/SalGraphics/salfoodweb.giff
6
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
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
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
Reference: http://java.sun.com/docs/books/tutorial/uiswing/overview/anatomy.html
14
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.
Friendster.
Maze traversal.
Kevin Bacon number.
Fewest number of hops in a communication network.
15
s L1 L2 L
BFS algorithm. 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.
16
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
17
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
18
Connected Component
19
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
20
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
21
Connected Component
R
s
u v
22
Bipartite Graphs
Applications.
Stable marriage: men = red, women = blue.
Scheduling: machines = red, jobs = blue.
a bipartite graph
23
Testing Bipartiteness
v2
v2 v3
v1
v4
v6 v5 v4 v3
v5
v6
v7 v1
v7
24
An Obstruction to Bipartiteness
25
Bipartite Graphs
L1 L2 L3 L1 L2 L3
Pf. (i)
Suppose no edge joins two nodes in the same layer.
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)
27
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. ▪
28
Obstruction to Bipartiteness
5-cycle C
29
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.
30
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.
31
Strong Connectivity
ok if paths overlap
s u
32
Strong Connectivity: Algorithm
33
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
34
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.
35
v1 vi vj vn
36
Directed Acyclic Graphs
37
w x u v
38
Directed Acyclic Graphs
DAG
39
Pf.
Maintain the following information:
– count[w] = remaining number of incoming edges
– S = set of remaining nodes with no incoming edges
Initialization: O(m + n) via single scan through graph.
Update: to delete v
– remove v from S
– decrement count[w] for all edges from v to w, and add w to S if c
count[w] hits 0
– this is O(1) per edge ▪
40