Professional Documents
Culture Documents
Graphs Topological Sort Single Source Shortest Path: Manoj Kumar DTU, Delhi
Graphs Topological Sort Single Source Shortest Path: Manoj Kumar DTU, Delhi
Graphs Topological Sort Single Source Shortest Path: Manoj Kumar DTU, Delhi
Topological Sort
Single Source Shortest Path
Manoj Kumar
DTU, Delhi
Directed Acyclic Graph (DAG)
A Directed Graph without a cycle.
Undershorts
Socks
Watch
Pants Shoes
Shirt
a DAG implies an
Belt Tie ordering on events
In a complex DAG, it
can be hard to find a
Jacket schedule that obeys
all the constraints.
2
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.
Undershorts
Socks
Watch
Pants Shoes
Shirt
Belt Tie
Jacket
1 2
Impossible!
3
Watch 9/10
12/15 Pants Shoes
Shirt 13/14
1/8
Jacket 3/4
a b c d
e f g h
DFS on Graph GT (transpose of G)
SCC tree
abe cd
fg h
Articulation Points, bridges, and biconnected graph
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) = ∑i =1 w(vi − 1,vi)
k
RELAX(u,v) RELAX(u,v)
u v u v
2 2
5 7 5 6
u v
1
10 ∞
10 Π[s] = NIL
2 3 9 Π[u] = s
s 0 4 6 Π[v] = NIL
Π[x] = s
7
5 Π[y] = NIL
5 ∞
2
x y
Dijkstra: Example…
u v
1
8 14
10 Π[s] = NIL
2 3 9 Π[u] = x
s 0 4 6 Π[v] = x
Π[x] = s
7
5 Π[y] = x
5 7
2
x y
u v
1
8 13
10 Π[s] = NIL
Π[u] = x
2 3 9
s 0 4 6 Π[v] = y
Π[x] = s
7 Π[y] = x
5
5 7
2
x y
Dijkstra: Example…
u v
1
8 9
10
Π[s] = NIL
2 3 9 Π[u] = x
s 0 4 6
Π[v] = u
7 Π[x] = s
5 Π[y] = x
5 7
2
x y
u v
1
8 9
10 Π[s] = NIL d(s,s) = 0
3 Π[u] = x d(s,u) = 8
2 9
s 0 4 6 Π[v] = u d(s,v) = 9
Π[x] = s d(s,x) = 5
7 Π[y] = x d(s,y) = 7
5
5 7
2
x y
Shortest-path tree
u v
1
8 9
Π[s] = NIL d(s,s) = 0
3 Π[u] = x d(s,u) = 8
s 0 Π[v] = u d(s,v) = 9
Π[x] = s d(s,x) = 5
5 Π[y] = x d(s,y) = 7
5 7
2
x y