April14SCCs%2BBFS
April14SCCs%2BBFS
proof:
suppose (u,v) is an edge in a dag then it can’t be a back
edge, therefore it can only be a forward edge/tree edge or
a cross edge.
Both of which have the property that post(v)<post(u).
Edge types and pre/post numbers
The different types of edges can be determined from the
pre/post numbers for the edge (𝑢, 𝑣)
• (𝑢, 𝑣) is a tree/forward edge then
𝑝𝑟𝑒 𝑢 < 𝑝𝑟𝑒 𝑣 < 𝑝𝑜𝑠𝑡 𝑣 < 𝑝𝑜𝑠𝑡(𝑢)
• (𝑢, 𝑣) is a back edge then
𝑝𝑟𝑒 𝑣 < 𝑝𝑟𝑒 𝑢 < 𝑝𝑜𝑠𝑡 𝑢 < 𝑝𝑜𝑠𝑡(𝑣)
• (𝑢, 𝑣) is a cross edge then
𝑝𝑟𝑒 𝑣 < 𝑝𝑜𝑠𝑡 𝑣 < 𝑝𝑟𝑒 𝑢 < 𝑝𝑜𝑠𝑡(𝑢)
Property of DAGS
Linearization of a DAG:
Since we know that edges go in the direction of decreasing
post numbers, if we order the vertices by decreasing post
numbers then we will have a linearization
D E F
Sources and Sinks
• Property of DAGs:
• Any DAG has at least one source and at least one sink.
• (Why?)
Strongly connected vertices
C
D
Two vertices 𝑢 and 𝑣 in a directed graph are B
strongly connected if there exists a path
F
from 𝑢 to 𝑣 and a path from 𝑣 to 𝑢. A
K M
L
Strongly connected component
C
D
What are the strongly connected components of B
this graph?
F
A
I
G
E H
K M
L
Strongly connected components as
vertices. (Meta-graph)
C
B,C,F,I D
D
B A
F G
A
H
I
G
E H E
J,K,L,M
J
K M
L
Directed Graphs as DAGs of SCCs
Every Directed graph is a DAG of its strongly connected
components.
Some SCCs are sink SCCs and some are source SCCs.
Decomposition
There is a linear time algorithm that decomposes a
directed graph into its strongly connected components.
The vertex with the greatest post number in any DFS output tree
belongs to a source SCC.
The vertex with the least post number in a dfs output does not
necessarily belong to a sink SCC.
Vertices in Source SCCs
The vertex with the greatest post number in any DFS
output tree belongs to a source SCC.
C C’
Proof
Case 2: DFS searches 𝐶′ before 𝐶:
Then DFS will visit all vertices of C’ before getting stuck
and assign a post number to all vertices of C’.
Then it will visit some vertex of C later and assign post numbers to those
vertices.
C C’
Corollary
The strongly connected components can be linearized by
arranging them in decreasing order of their highest post
numbers.
How to find sink SCCs C
D
B
F
A
I
G
E H
K M
L
How to find sink SCCs
Given a graph 𝐺, let 𝐺 ! be the reverse graph of 𝐺.
Then the sources of 𝐺 ! are the sinks of 𝐺.
F
G
E
H I
M
K
L
1 2 3 26
A B C
4 25 D
F B
F
5
20
24 A
21
C I
I
6 23
G
22 H
19 E
D H
7
18 J
G
8
17 K M
10 11
J E
13 14 L
9 16 15
12
K L
M
cc = 1 cc = 2 cc = 3
B H D C
D
B
C G F
A
I
F I G
E H
cc = 4 cc = 5 cc = 6
J E A
J
L K M
L
M
K
B, F, I, H, C, D, G, J, K, M, L, E, A
The SCC algorithm
• Run DFS on 𝐺 ! and keep track of the post numbers.
• Run DFS on 𝐺 and order the vertices in decreasing order of
the postnumbers from the previous step. Every time DFS
increments cc, you have found a new SCC!!
G F
BFS explores the closest vertices first
• So the intuition is that it should find shortest paths.
• How can we keep track of shortest paths/minimum
distances?
Augmented BFS
procedure BFS(G, s)
Input: Graph G = (V,E), (directed or undirected) and a vertex s in V.
Output: For all vertices u reachable from s, dist(u) is the distance from s to u. and for all
vertices u not reachable from s, dist(u) = ∞
for each vertex u in V:
dist(u)=∞
dist(s) = 0
Q = [s] (queue just containing s)
while Q is not empty
v = Q.dequeue
for all edges (v,u) in E
if dist(u)=∞ then
Q.enqueue(u); prev(u)=v;
dist(v)=dist(u) + 1
Example
A
B
C
H
E A B C D E F G H
G F
Proof of correctness
• For each vertex v, we want to show that dist(v) is the
minimum distance of all paths from s to v.
• Claim: at the first time when the head of the queue has
distance marked d:
• (1) all vertices with distances at distance ≤ 𝑑 from s have their
distance values correctly set.
• (2). All vertices in the queue are exactly those of distance d.
• (2) All other vertices are marked distance infinity
Claim: for each distance value d = 0, 1, 2,…:
•Corollary:
For each vertex 𝑣, dist(𝑣) is set at most one time.
Edge lengths (weights)
edges can be given values such as
• Distance
• Cost
• Time
• Bandwidth
• Value
BFS on weighted graphs.
• BFS only works to find shortest distances on graphs in
which each edge has equal weight.
A 3 B 2 C
2 1 4 1
D 4 E F
BFS on weighted graphs.
• Discuss how we can use a reduction to solve the
problem of shortest paths with edge lengths.
A 3 B 2 C
2 1 4 1
D 4 E F
BFS on weighted graphs.
• On a graph 𝐺 with integer edge lengths, form 𝐺 ! by adding ℓ" − 1 many new
vertices between 𝑢 and 𝑣 for every edge 𝑒 = (𝑢, 𝑣). Then run BFS on 𝐺′.
A 3 B 2 C A B C
2 1 4 1
D 4 E F D E F
𝐺 𝐺′
Problems with this method
If the edge lengths
(weights) are large
integers then it is
impractical.
In this example
with 10 vertices,
we must add 1,783
more vertices!!!!!!!!
Next class
• Simulating bfs in weighted graphs
• Dijkstra’s algorithm
• Priority queues