Upload
others
View
5
Download
0
Embed Size (px)
Citation preview
Graphs
Topological Sort
Single Source Shortest Path
Manoj Kumar
DTU, Delhi
Directed Acyclic Graph (DAG)
�A Directed Graph without a cycle.
Watch
Socks
Shoes
Undershorts
Pants
2
ShoesPants
Belt Tie
Shirt
Jacket
a DAG implies anordering on events
In a complex DAG, it
can be hard to find a schedule that obeys
all the constraints.
Topological Sort
• For a directed acyclic graph G = (V,E), a topological
sort is a linear ordering of all vertices of G such that
if G contains an edge (u,v), then u appears before v in
the ordering.
• A topological sort of a graph can be viewed as an
ordering of its vertices along a horizontal line so that
all directed edges go from left to right.
Topological Sort:Example
Socks
Shoes
Undershorts
Pants
Belt
Shirt
Watch
Socks ShoesUndershorts Pants Belt TieShirt JacketWatch
Belt Tie
Jacket
Topological sort
• There are often many possible topological sorts of a given DAG (Directed Acyclic Graph)
• Topological orders for this DAG :
� 1,2,5,4,3,6,7
� 2,1,5,4,7,3,6
1 2
� 2,1,5,4,7,3,6
� 2,5,1,4,7,3,6
� Etc.
• Each topological order is a feasible schedule.
4
76
3 5
Topological Sorts for Cyclic Graphs?
Impossible!1 2
3
� If v and w are two vertices on a cycle, there
exist paths from v to w and from w to v.
� Any ordering will contradict one of these
paths
Topological Sort: Algorithm
TOPOLOGICAL-SORT(G)
1. Call DFS(G) to compute finishing time f[v] for each vertex v.
2. As each vertex is finished, insert it onto the front of a linked list.
3. Return the linked list of vertices.
Topological Sort
Socks
Shoes
Undershorts
Pants
Shirt
Watch
11/16
1/8
9/1012/15
17/18
13/14
Belt Tie
Jacket
2/5
3/4
6/7
Socks ShoesUndershorts Pants Belt TieShirt JacketWatch
17/18 11/16 12/15 13/14 9/10 1/8 6/7 2/5 3/4
All edges of G are going from left to right only
Strongly Connected Components
• Strongly connected component of a directed graph
G=(V,E) is a maximal set of vertices U ⊆ V such that
for every pair of vertices u and v in U, we have both
u v and v u, that is u and v are reachable from
each other.each other.
Strongly Connected Components: Algorithm
STRONGLY-CONNECTED-COMPONENTS(G)
1. Call DFS(G) to compute finishing time f[u] for
each vertex u.
2. Compute GT, transpose of G by reversing all edges.
3. Call DFS(GT), but in the main loop of DFS, 3. Call DFS(GT), but in the main loop of DFS,
consider the vertices in order of decreasing f[u] as
computed in line 1.
4. Output the vertices of each tree in the depth-first
forest of step 3 as a separate strongly connected
component.
Strongly Connected Components: Example
13/14
a
11/16
b
3/4
f
2/7
g
12/15
e
8/9
d
1/10
c
5/6
hf ge h
a b
f ge
dc
h
DFS on graph G with 4 SCCs, tree edges are in RED
DFS on Graph GT (transpose of G)
SCC tree
abe cd
fg hh
Articulation Points, bridges, and biconnected graph
• Let G be a connected, undirected graph.
• An articulation point of G is a vertex whose removal
disconnects G.
• A bridge of G is an edge whose removal disconnects
G.G.
• A graph is biconnected if it contains no articulation
point.
• A biconnected component of G is a maximal
biconnected subgraph.
Biconnected Graph
Articulation Points, bridges, and biconnected graph
Articulation points
bridge
Single Source Shortest Path
• Given a weighted directed graph G=(V,E), with weight function w:E�R mapping edges to real valued weights.
• Weight of path p=<v0,v1,v2,…vk> is the sum of the weights of its constituent edges.
w(p) = ∑k
)v,w(vw(p) =
• We define the shortest path weight from u to v by
min{w(p): u v)} if there is a path from
δ(u,v) = u to v.
∞ otherwise.
∑ = −
k
i i1i )v,w(v1
p
Representing shortest path
Shortest path tree
• Rooted at source vertex s,
• A directed graph G′ =(V′,E′), where V′⊆ V and
E′⊆ E, such that
1. V′ is the set of vertices reachable from s in G.1. V′ is the set of vertices reachable from s in G.
2. G′ forms a rooted tree with root s, and
3. For all v ϵ V′, the unique simple path from s to v in G
is a shortest path from s to v in G.
For each vertex v, we store Π[v] pointing to its
predecessor vertex. For source vertex s, Π[s] =NIL
Representing shortest path
0
3
5 11
93
5
6
6
712
3
2s
u v
Π[s] = NILΠ[u] = sΠ[v] = uΠ[x] = sΠ[y] = x
4
6
x y δ(s,s) = 0δ(s,u) = 3δ(s,v) = 9δ(s,x) = 5δ(s,y) = 11
Relaxation
• Algorithms keep track of d[v], π[v]. Initialized as
follows:INITIALIZE-SINGLE-SOURCE(G, s)1. for each v ∈ V[G] do2. d[v] � ∞; //distance from source 3. π[v] � NIL //parent node4. d[s] � 0 // source to source distance =0
• These values are changed when an edge (u, v) is
relaxed:
4. d[s] � 0 // source to source distance =0
RELAX(u, v, w)1. if d[v] > d[u] + w(u, v) then //new path from s to v through u
is smaller2. d[v] � d[u] + w(u, v); //set new path3. π[v] � u
Relaxation
5 92
u v
5 72
u v
RELAX(u,v)
5 62
u v
5 62
u v
RELAX(u,v)
5 72
5 62
d[v] > d[u]+w(u,v) d[v] <= d[u]+w(u,v)
Dijkstra’s Algorithm
DIJKSTRA(G, w, s)1.2.3.4.5.
DIJKSTRA(G, w, s)1. INITIALIZE-SINGLE-SOURCE(G,s)2. S←∅3. Q←V4. while Q ≠∅5. do u← EXTRACT-MIN(Q)
∪
∈
INITIALIZE-SINGLE-SOURCE(G, s)1. for each v ∈ V[G] do2. d[v] � ∞;3. π[v] � NIL4. d[s] � 0
5.6.7.8.
∅
5. do u← EXTRACT-MIN(Q) 6. S←S ∪ {u}7. for each vertex v ∈ Adj[u]8. do RELAX(u, v, w)
RELAX(u, v, w)1. if d[v] > d[u] + w(u, v) then2. d[v] � d[u] + w(u, v);3. π[v] � u
Dijkstra’s AlgorithmDIJKSTRA(G, w, s)1.2.3.4.5.6.7.
DIJKSTRA(G, w, s)1. for each v ∈ V[G] do
2. d[v] � ∞; INITIALIZE-3. π[v] � NIL SINGLE-SOURCE4. d[s] � 05. S←∅6. Q←V7. while Q ≠∅ // Q is priority queue using minHeap
∪
7.8.9.10.11.12.13.
∅
7. while Q ≠∅ // Q is priority queue using minHeap8. do u← EXTRACT-MIN(Q) 9. S←S ∪ {u}10. for each vertex v ∈ Adj[u]11. do if d[v] > d[u] + w(u, v)
12. then d[v] � d[u] + w(u, v); RELAX13. π[v] � u
Dijkstra: Example
0
∞
∞ ∞
∞10
5
1
2
632
7
4s
u v
9 Π[s] = NILΠ[u] = NILΠ[v] = NILΠ[x] = NILΠ[y] = NIL2
x y
0
10
5 ∞
∞10
5
1
2
632
7
4s
x y
u v
9
Π[y] = NIL
Π[s] = NILΠ[u] = sΠ[v] = NILΠ[x] = sΠ[y] = NIL
Dijkstra: Example…
0
8
5 7
1410
5
1
2
632
7
4s
u v
9
Π[s] = NILΠ[u] = xΠ[v] = xΠ[x] = sΠ[y] = x
2x y
0
8
5 7
1310
5
1
2
632
7
4s
x y
u v
9
Π[s] = NILΠ[u] = xΠ[v] = yΠ[x] = sΠ[y] = x
Dijkstra: Example…
0
8
5 7
910
5
1
2
632
7
4s
u v
9Π[s] = NILΠ[u] = xΠ[v] = uΠ[x] = sΠ[y] = x
2x y
0
8
5 7
910
5
1
2
632
7
4s
x y
u v
9
Π[s] = NIL d(s,s) = 0Π[u] = x d(s,u) = 8Π[v] = u d(s,v) = 9Π[x] = s d(s,x) = 5Π[y] = x d(s,y) = 7
Shortest-path tree
0
8 91
3s
u v
Π[s] = NIL d(s,s) = 0Π[u] = x d(s,u) = 8Π[v] = u d(s,v) = 9Π[x] = s d(s,x) = 5
5 75
2x y
Π[x] = s d(s,x) = 5Π[y] = x d(s,y) = 7