CSE 431/531: Algorithm Analysis and Design (Fall 2021 ...
Transcript of CSE 431/531: Algorithm Analysis and Design (Fall 2021 ...
CSE 431/531: Algorithm Analysis and Design (Fall 2021)
Graph Basics
Lecturer: Shi Li
Department of Computer Science and EngineeringUniversity at Buffalo
2/37
Outline
1 Graphs
2 Connectivity and Graph TraversalTesting Bipartiteness
3 Topological Ordering
4 Properties of BFS and DFS trees
3/37
Examples of Graphs
Figure: Road Networks
Figure: Social Networks
Figure: Internet
Figure: Transition Graphs
4/37
(Undirected) Graph G = (V,E)
1
2 3
4 5
7
8
6
V : set of vertices (nodes);
V = {1, 2, 3, 4, 5, 6, 7, 8}
E: pairwise relationships among V ;
(undirected) graphs: relationship is symmetric, E contains subsets ofsize 2
E = {{1, 2}, {1, 3}, {2, 3}, {2, 4}, {2, 5}, {3, 5}, {3, 7}, {3, 8},{4, 5}, {5, 6}, {7, 8}}
4/37
(Undirected) Graph G = (V,E)
1
2 3
4 5
7
8
6
V : set of vertices (nodes);
V = {1, 2, 3, 4, 5, 6, 7, 8}E: pairwise relationships among V ;
(undirected) graphs: relationship is symmetric, E contains subsets ofsize 2E = {{1, 2}, {1, 3}, {2, 3}, {2, 4}, {2, 5}, {3, 5}, {3, 7}, {3, 8},{4, 5}, {5, 6}, {7, 8}}
4/37
Directed Graph G = (V,E)
1
2 3
4 5
7
8
6
V : set of vertices (nodes);
V = {1, 2, 3, 4, 5, 6, 7, 8}E: pairwise relationships among V ;
directed graphs: relationship is asymmetric, E contains ordered pairs
E = {(1, 2), (1, 3), (3, 2), (4, 2), (2, 5), (5, 3), (3, 7), (3, 8),(4, 5), (5, 6), (6, 5), (8, 7)}
4/37
Directed Graph G = (V,E)
1
2 3
4 5
7
8
6
V : set of vertices (nodes);
V = {1, 2, 3, 4, 5, 6, 7, 8}E: pairwise relationships among V ;
directed graphs: relationship is asymmetric, E contains ordered pairsE = {(1, 2), (1, 3), (3, 2), (4, 2), (2, 5), (5, 3), (3, 7), (3, 8),(4, 5), (5, 6), (6, 5), (8, 7)}
5/37
Abuse of Notations
For (undirected) graphs, we often use (i, j) to denote the set{i, j}.We call (i, j) an unordered pair; in this case (i, j) = (j, i).
1
2 3
4 5
7
8
6
E = {(1, 2), (1, 3), (2, 3), (2, 4), (2, 5), (3, 5), (3, 7), (3, 8),(4, 5), (5, 6), (7, 8)}
6/37
Social Network : Undirected
Transition Graph : Directed
Road Network : Directed or Undirected
Internet : Directed or Undirected
7/37
Representation of Graphs
1
2 3
4 5
7
8
6
0 1
1 0
1 0
1 1
1 1
0 1
0 0
0 0
0 0
1 0
0 0
0 0
1 0
1 0
1 1
0 0
0 1
0 0
1 1
0 0
0 0
0 0
1 0
1 0
0 1
1 0
0 0
0 0
0 0
0 0
0 1
1 0
1 2 3 4 5 6 7 8
1
2
3
4
5
6
7
8
Adjacency matrix
n× n matrix, A[u, v] = 1 if (u, v) ∈ E and A[u, v] = 0 otherwiseA is symmetric if graph is undirected
Linked lists
For every vertex v, there is a linked list containing all neighbours of v.
7/37
Representation of Graphs
1
2 3
4 5
7
8
6
2 3
1 3
1 2
2 5
5
3 8
8
3 7
2 3
5 7
4 5
4 6
1:
2:
3:
4:
5:
6:
7:
8:
Adjacency matrix
n× n matrix, A[u, v] = 1 if (u, v) ∈ E and A[u, v] = 0 otherwiseA is symmetric if graph is undirected
Linked lists
For every vertex v, there is a linked list containing all neighbours of v.
8/37
Comparison of Two Representations
Assuming we are dealing with undirected graphs
n: number of vertices
m: number of edges, assuming n− 1 ≤ m ≤ n(n− 1)/2
dv: number of neighbors of v
Matrix Linked Lists
memory usage
O(n2) O(m)
time to check (u, v) ∈ E
O(1) O(du)
time to list all neighbours of v
O(n) O(dv)
8/37
Comparison of Two Representations
Assuming we are dealing with undirected graphs
n: number of vertices
m: number of edges, assuming n− 1 ≤ m ≤ n(n− 1)/2
dv: number of neighbors of v
Matrix Linked Lists
memory usage O(n2)
O(m)
time to check (u, v) ∈ E
O(1) O(du)
time to list all neighbours of v
O(n) O(dv)
8/37
Comparison of Two Representations
Assuming we are dealing with undirected graphs
n: number of vertices
m: number of edges, assuming n− 1 ≤ m ≤ n(n− 1)/2
dv: number of neighbors of v
Matrix Linked Lists
memory usage O(n2) O(m)
time to check (u, v) ∈ E
O(1) O(du)
time to list all neighbours of v
O(n) O(dv)
8/37
Comparison of Two Representations
Assuming we are dealing with undirected graphs
n: number of vertices
m: number of edges, assuming n− 1 ≤ m ≤ n(n− 1)/2
dv: number of neighbors of v
Matrix Linked Lists
memory usage O(n2) O(m)
time to check (u, v) ∈ E O(1)
O(du)
time to list all neighbours of v
O(n) O(dv)
8/37
Comparison of Two Representations
Assuming we are dealing with undirected graphs
n: number of vertices
m: number of edges, assuming n− 1 ≤ m ≤ n(n− 1)/2
dv: number of neighbors of v
Matrix Linked Lists
memory usage O(n2) O(m)
time to check (u, v) ∈ E O(1) O(du)
time to list all neighbours of v
O(n) O(dv)
8/37
Comparison of Two Representations
Assuming we are dealing with undirected graphs
n: number of vertices
m: number of edges, assuming n− 1 ≤ m ≤ n(n− 1)/2
dv: number of neighbors of v
Matrix Linked Lists
memory usage O(n2) O(m)
time to check (u, v) ∈ E O(1) O(du)
time to list all neighbours of v O(n)
O(dv)
8/37
Comparison of Two Representations
Assuming we are dealing with undirected graphs
n: number of vertices
m: number of edges, assuming n− 1 ≤ m ≤ n(n− 1)/2
dv: number of neighbors of v
Matrix Linked Lists
memory usage O(n2) O(m)
time to check (u, v) ∈ E O(1) O(du)
time to list all neighbours of v O(n) O(dv)
9/37
Outline
1 Graphs
2 Connectivity and Graph TraversalTesting Bipartiteness
3 Topological Ordering
4 Properties of BFS and DFS trees
10/37
Connectivity Problem
Input: graph G = (V,E), (using linked lists)
two vertices s, t ∈ VOutput: whether there is a path connecting s to t in G
Algorithm: starting from s, search for all vertices that arereachable from s and check if the set contains t
Breadth-First Search (BFS)Depth-First Search (DFS)
10/37
Connectivity Problem
Input: graph G = (V,E), (using linked lists)
two vertices s, t ∈ VOutput: whether there is a path connecting s to t in G
Algorithm: starting from s, search for all vertices that arereachable from s and check if the set contains t
Breadth-First Search (BFS)Depth-First Search (DFS)
10/37
Connectivity Problem
Input: graph G = (V,E), (using linked lists)
two vertices s, t ∈ VOutput: whether there is a path connecting s to t in G
Algorithm: starting from s, search for all vertices that arereachable from s and check if the set contains t
Breadth-First Search (BFS)
Depth-First Search (DFS)
10/37
Connectivity Problem
Input: graph G = (V,E), (using linked lists)
two vertices s, t ∈ VOutput: whether there is a path connecting s to t in G
Algorithm: starting from s, search for all vertices that arereachable from s and check if the set contains t
Breadth-First Search (BFS)Depth-First Search (DFS)
11/37
Breadth-First Search (BFS)
Build layers L0, L1, L2, L3, · · ·L0 = {s}Lj+1 contains all nodes that are not in L0 ∪ L1 ∪ · · · ∪ Lj andhave an edge to a vertex in Lj
11/37
Breadth-First Search (BFS)
Build layers L0, L1, L2, L3, · · ·L0 = {s}Lj+1 contains all nodes that are not in L0 ∪ L1 ∪ · · · ∪ Lj andhave an edge to a vertex in Lj
1
2 3
4 5
7
8
6
11/37
Breadth-First Search (BFS)
Build layers L0, L1, L2, L3, · · ·L0 = {s}Lj+1 contains all nodes that are not in L0 ∪ L1 ∪ · · · ∪ Lj andhave an edge to a vertex in Lj
1
2 3
4 5
7
8
6
11/37
Breadth-First Search (BFS)
Build layers L0, L1, L2, L3, · · ·L0 = {s}Lj+1 contains all nodes that are not in L0 ∪ L1 ∪ · · · ∪ Lj andhave an edge to a vertex in Lj
1
2 3
4 5
7
8
6
11/37
Breadth-First Search (BFS)
Build layers L0, L1, L2, L3, · · ·L0 = {s}Lj+1 contains all nodes that are not in L0 ∪ L1 ∪ · · · ∪ Lj andhave an edge to a vertex in Lj
1
2 3
4 5
7
8
6
12/37
Implementing BFS using a Queue
BFS(s)
1: head← 1, tail← 1, queue[1]← s2: mark s as “visited” and all other vertices as “unvisited”3: while head ≥ tail do4: v ← queue[tail], tail← tail + 15: for all neighbours u of v do6: if u is “unvisited” then7: head← head+ 1, queue[head] = u8: mark u as “visited”
Running time: O(n+m).
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
1
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
v
1
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
v
21
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
v
2 31
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
v 2 31
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
v 2 3 41
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
v 2 3 4 51
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
v2 3 4 51
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
v2 3 4 5 71
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
v2 3 4 5 7 81
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
v
2 3 4 5 7 81
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tailv
2 3 4 5 7 81
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tailv
2 3 4 5 7 8 61
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
v
2 3 4 5 7 8 61
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tailv
2 3 4 5 7 8 61
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
v
2 3 4 5 7 8 61
14/37
Depth-First Search (DFS)
Starting from s
Travel through the first edge leading out of the current vertex
When reach an already-visited vertex (“dead-end”), go back
Travel through the next edge
If tried all edges leading out of the current vertex, go back
14/37
Depth-First Search (DFS)
Starting from s
Travel through the first edge leading out of the current vertex
When reach an already-visited vertex (“dead-end”), go back
Travel through the next edge
If tried all edges leading out of the current vertex, go back
1
2 3
4 5
7
8
6
14/37
Depth-First Search (DFS)
Starting from s
Travel through the first edge leading out of the current vertex
When reach an already-visited vertex (“dead-end”), go back
Travel through the next edge
If tried all edges leading out of the current vertex, go back
1
2 3
4 5
7
8
6
14/37
Depth-First Search (DFS)
Starting from s
Travel through the first edge leading out of the current vertex
When reach an already-visited vertex (“dead-end”), go back
Travel through the next edge
If tried all edges leading out of the current vertex, go back
1
2 3
4 5
7
8
6
14/37
Depth-First Search (DFS)
Starting from s
Travel through the first edge leading out of the current vertex
When reach an already-visited vertex (“dead-end”), go back
Travel through the next edge
If tried all edges leading out of the current vertex, go back
1
2 3
4 5
7
8
6
14/37
Depth-First Search (DFS)
Starting from s
Travel through the first edge leading out of the current vertex
When reach an already-visited vertex (“dead-end”), go back
Travel through the next edge
If tried all edges leading out of the current vertex, go back
1
2 3
4 5
7
8
6
14/37
Depth-First Search (DFS)
Starting from s
Travel through the first edge leading out of the current vertex
When reach an already-visited vertex (“dead-end”), go back
Travel through the next edge
If tried all edges leading out of the current vertex, go back
1
2 3
4 5
7
8
6
14/37
Depth-First Search (DFS)
Starting from s
Travel through the first edge leading out of the current vertex
When reach an already-visited vertex (“dead-end”), go back
Travel through the next edge
If tried all edges leading out of the current vertex, go back
1
2 3
4 5
7
8
6
14/37
Depth-First Search (DFS)
Starting from s
Travel through the first edge leading out of the current vertex
When reach an already-visited vertex (“dead-end”), go back
Travel through the next edge
If tried all edges leading out of the current vertex, go back
1
2 3
4 5
7
8
6
14/37
Depth-First Search (DFS)
Starting from s
Travel through the first edge leading out of the current vertex
When reach an already-visited vertex (“dead-end”), go back
Travel through the next edge
If tried all edges leading out of the current vertex, go back
1
2 3
4 5
7
8
6
14/37
Depth-First Search (DFS)
Starting from s
Travel through the first edge leading out of the current vertex
When reach an already-visited vertex (“dead-end”), go back
Travel through the next edge
If tried all edges leading out of the current vertex, go back
1
2 3
4 5
7
8
6
14/37
Depth-First Search (DFS)
Starting from s
Travel through the first edge leading out of the current vertex
When reach an already-visited vertex (“dead-end”), go back
Travel through the next edge
If tried all edges leading out of the current vertex, go back
1
2 3
4 5
7
8
6
15/37
Implementing DFS using Recurrsion
DFS(s)
1: mark all vertices as “unvisited”2: recursive-DFS(s)
recursive-DFS(v)
1: mark v as “visited”2: for all neighbours u of v do3: if u is unvisited then recursive-DFS(u)
16/37
Outline
1 Graphs
2 Connectivity and Graph TraversalTesting Bipartiteness
3 Topological Ordering
4 Properties of BFS and DFS trees
17/37
Testing Bipartiteness: Applications of BFS
Def. A graph G = (V,E) is a bipartitegraph if there is a partition of V into twosets L and R such that for every edge(u, v) ∈ E, we have either u ∈ L, v ∈ Ror v ∈ L, u ∈ R.
18/37
Testing Bipartiteness
Taking an arbitrary vertex s ∈ V
Assuming s ∈ L w.l.o.g
Neighbors of s must be in R
Neighbors of neighbors of s must be in L
· · ·Report “not a bipartite graph” if contradiction was found
If G contains multiple connected components, repeat abovealgorithm for each component
18/37
Testing Bipartiteness
Taking an arbitrary vertex s ∈ VAssuming s ∈ L w.l.o.g
Neighbors of s must be in R
Neighbors of neighbors of s must be in L
· · ·Report “not a bipartite graph” if contradiction was found
If G contains multiple connected components, repeat abovealgorithm for each component
18/37
Testing Bipartiteness
Taking an arbitrary vertex s ∈ VAssuming s ∈ L w.l.o.g
Neighbors of s must be in R
Neighbors of neighbors of s must be in L
· · ·Report “not a bipartite graph” if contradiction was found
If G contains multiple connected components, repeat abovealgorithm for each component
18/37
Testing Bipartiteness
Taking an arbitrary vertex s ∈ VAssuming s ∈ L w.l.o.g
Neighbors of s must be in R
Neighbors of neighbors of s must be in L
· · ·Report “not a bipartite graph” if contradiction was found
If G contains multiple connected components, repeat abovealgorithm for each component
18/37
Testing Bipartiteness
Taking an arbitrary vertex s ∈ VAssuming s ∈ L w.l.o.g
Neighbors of s must be in R
Neighbors of neighbors of s must be in L
· · ·
Report “not a bipartite graph” if contradiction was found
If G contains multiple connected components, repeat abovealgorithm for each component
18/37
Testing Bipartiteness
Taking an arbitrary vertex s ∈ VAssuming s ∈ L w.l.o.g
Neighbors of s must be in R
Neighbors of neighbors of s must be in L
· · ·Report “not a bipartite graph” if contradiction was found
If G contains multiple connected components, repeat abovealgorithm for each component
18/37
Testing Bipartiteness
Taking an arbitrary vertex s ∈ VAssuming s ∈ L w.l.o.g
Neighbors of s must be in R
Neighbors of neighbors of s must be in L
· · ·Report “not a bipartite graph” if contradiction was found
If G contains multiple connected components, repeat abovealgorithm for each component
19/37
Test Bipartiteness
19/37
Test Bipartiteness
19/37
Test Bipartiteness
19/37
Test Bipartiteness
19/37
Test Bipartiteness
19/37
Test Bipartiteness
19/37
Test Bipartiteness
19/37
Test Bipartiteness
19/37
Test Bipartiteness
19/37
Test Bipartiteness
19/37
Test Bipartiteness
bad edges!
20/37
Testing Bipartiteness using BFS
BFS(s)
1: head← 1, tail← 1, queue[1]← s2: mark s as “visited” and all other vertices as “unvisited”3: while head ≥ tail do4: v ← queue[tail], tail← tail + 15: for all neighbours u of v do6: if u is “unvisited” then7: head← head+ 1, queue[head] = u8: mark u as “visited”
20/37
Testing Bipartiteness using BFS
test-bipartiteness(s)
1: head← 1, tail← 1, queue[1]← s2: mark s as “visited” and all other vertices as “unvisited”3: color[s]← 04: while head ≥ tail do5: v ← queue[tail], tail← tail + 16: for all neighbours u of v do7: if u is “unvisited” then8: head← head+ 1, queue[head] = u9: mark u as “visited”10: color[u]← 1− color[v]11: else if color[u] = color[v] then12: print(“G is not bipartite”) and exit
21/37
Testing Bipartiteness using BFS
1: mark all vertices as “unvisited”2: for each vertex v ∈ V do3: if v is “unvisited” then4: test-bipartiteness(v)
5: print(“G is bipartite”)
Obs. Running time of algorithm = O(n+m)
21/37
Testing Bipartiteness using BFS
1: mark all vertices as “unvisited”2: for each vertex v ∈ V do3: if v is “unvisited” then4: test-bipartiteness(v)
5: print(“G is bipartite”)
Obs. Running time of algorithm = O(n+m)
22/37
Outline
1 Graphs
2 Connectivity and Graph TraversalTesting Bipartiteness
3 Topological Ordering
4 Properties of BFS and DFS trees
23/37
Topological Ordering Problem
Input: a directed acyclic graph (DAG) G = (V,E)
Output: 1-to-1 function π : V → {1, 2, 3 · · · , n}, so that
if (u, v) ∈ E then π(u) < π(v)
a b
c d e f
g h i
23/37
Topological Ordering Problem
Input: a directed acyclic graph (DAG) G = (V,E)
Output: 1-to-1 function π : V → {1, 2, 3 · · · , n}, so that
if (u, v) ∈ E then π(u) < π(v)
1
2
3
4 5
6 7
89
24/37
Topological Ordering
Algorithm: each time take a vertex without incoming edges, thenremove the vertex and all its outgoing edges.
a b
c d e f
g h i
24/37
Topological Ordering
Algorithm: each time take a vertex without incoming edges, thenremove the vertex and all its outgoing edges.
1
a b
d e f
g h i
24/37
Topological Ordering
Algorithm: each time take a vertex without incoming edges, thenremove the vertex and all its outgoing edges.
1
a b
d e f
g h i
24/37
Topological Ordering
Algorithm: each time take a vertex without incoming edges, thenremove the vertex and all its outgoing edges.
1
2
a b
d e f
h i
24/37
Topological Ordering
Algorithm: each time take a vertex without incoming edges, thenremove the vertex and all its outgoing edges.
1
2
a b
d e f
h i
24/37
Topological Ordering
Algorithm: each time take a vertex without incoming edges, thenremove the vertex and all its outgoing edges.
1
2
3
a b
e f
h i
24/37
Topological Ordering
Algorithm: each time take a vertex without incoming edges, thenremove the vertex and all its outgoing edges.
1
2
3
a b
e f
h i
24/37
Topological Ordering
Algorithm: each time take a vertex without incoming edges, thenremove the vertex and all its outgoing edges.
1
2
3
4 b
e f
h i
24/37
Topological Ordering
Algorithm: each time take a vertex without incoming edges, thenremove the vertex and all its outgoing edges.
1
2
3
4 b
e f
h i
24/37
Topological Ordering
Algorithm: each time take a vertex without incoming edges, thenremove the vertex and all its outgoing edges.
1
2
3
4 5
e f
h i
24/37
Topological Ordering
Algorithm: each time take a vertex without incoming edges, thenremove the vertex and all its outgoing edges.
1
2
3
4 5
e f
h i
24/37
Topological Ordering
Algorithm: each time take a vertex without incoming edges, thenremove the vertex and all its outgoing edges.
1
2
3
4 5
6 f
h i
24/37
Topological Ordering
Algorithm: each time take a vertex without incoming edges, thenremove the vertex and all its outgoing edges.
1
2
3
4 5
6 f
h i
24/37
Topological Ordering
Algorithm: each time take a vertex without incoming edges, thenremove the vertex and all its outgoing edges.
1
2
3
4 5
6 7
h i
24/37
Topological Ordering
Algorithm: each time take a vertex without incoming edges, thenremove the vertex and all its outgoing edges.
1
2
3
4 5
6 7
h i
24/37
Topological Ordering
Algorithm: each time take a vertex without incoming edges, thenremove the vertex and all its outgoing edges.
1
2
3
4 5
6 7
8h
24/37
Topological Ordering
Algorithm: each time take a vertex without incoming edges, thenremove the vertex and all its outgoing edges.
1
2
3
4 5
6 7
8h
24/37
Topological Ordering
Algorithm: each time take a vertex without incoming edges, thenremove the vertex and all its outgoing edges.
1
2
3
4 5
6 7
89
24/37
Topological Ordering
Algorithm: each time take a vertex without incoming edges, thenremove the vertex and all its outgoing edges.
1
2
3
4 5
6 7
89
25/37
Topological Ordering
Algorithm: each time take a vertex without incoming edges, thenremove the vertex and all its outgoing edges.
Q: How to make the algorithm as efficient as possible?
A:
Use linked-lists of outgoing edges
Maintain the in-degree dv of vertices
Maintain a queue (or stack) of vertices v with dv = 0
25/37
Topological Ordering
Algorithm: each time take a vertex without incoming edges, thenremove the vertex and all its outgoing edges.
Q: How to make the algorithm as efficient as possible?
A:
Use linked-lists of outgoing edges
Maintain the in-degree dv of vertices
Maintain a queue (or stack) of vertices v with dv = 0
25/37
Topological Ordering
Algorithm: each time take a vertex without incoming edges, thenremove the vertex and all its outgoing edges.
Q: How to make the algorithm as efficient as possible?
A:
Use linked-lists of outgoing edges
Maintain the in-degree dv of vertices
Maintain a queue (or stack) of vertices v with dv = 0
26/37
topological-sort(G)
1: let dv ← 0 for every v ∈ V2: for every v ∈ V do3: for every u such that (v, u) ∈ E do4: du ← du + 1
5: S ← {v : dv = 0}, i← 06: while S 6= ∅ do7: v ← arbitrary vertex in S, S ← S \ {v}8: i← i+ 1, π(v)← i9: for every u such that (v, u) ∈ E do10: du ← du − 111: if du = 0 then add u to S
12: if i < n then output “not a DAG”
S can be represented using a queue or a stack
Running time = O(n+m)
27/37
S as a Queue or a Stack
DS Queue Stack
Initialization head← 0, tail← 1 top← 0
Non-Empty? head ≥ tail top > 0
Add(v) head← head+ 1S[head]← v
top← top+ 1S[top]← v
Retrieve v v ← S[tail]tail← tail + 1
v ← S[top]top← top− 1
28/37
Example
a b c d f g
degree
e
queue:
a
b c
d
e
f
g0 1 1 1 12 3
head
tail
a
28/37
Example
a b c d f g
degree
e
queue:
a
b c
d
e
f
g0 1 1 1 12 3
head
tail
a
28/37
Example
a b c d f g
degree
e
queue:b c
d
e
f
g0 0 0 1 12 3
head
tail
a
28/37
Example
a b c d f g
degree
e
queue:b c
d
e
f
g0 0 0 1 12 3
head
tail
a b c
28/37
Example
a b c d f g
degree
e
queue:b c
d
e
f
g0 0 0 1 12 3
head
tail
a b c
28/37
Example
a b c d f g
degree
e
queue:c
d
e
f
g0 0 0 1 1 1 3
head
tail
a b c
28/37
Example
a b c d f g
degree
e
queue:c
d
e
f
g0 0 0 1 1 1 3
head
tail
a b c
28/37
Example
a b c d f g
degree
e
queue:
d
e
f
g0 0 0 0 01 3
head
tail
a b c
28/37
Example
a b c d f g
degree
e
queue:
d
e
f
g0 0 0 0 01 3
head
tail
a b c d f
28/37
Example
a b c d f g
degree
e
queue:
d
e
f
g0 0 0 0 01 3
head
tail
a b c d f
28/37
Example
a b c d f g
degree
e
queue:
e
f
g0 0 0 0 0 0 2
head
tail
a b c d f
28/37
Example
a b c d f g
degree
e
queue:
e
f
g0 0 0 0 0 0 2
head
tail
a b c d ef
28/37
Example
a b c d f g
degree
e
queue:
e
f
g0 0 0 0 0 0 2
head
tail
a b c d ef
28/37
Example
a b c d f g
degree
e
queue:
e g0 0 0 0 0 0 1
head
tail
a b c d ef
28/37
Example
a b c d f g
degree
e
queue:
e g0 0 0 0 0 0 1
head
tail
a b c d ef
28/37
Example
a b c d f g
degree
e
queue:
g0 0 0 0 0 0 0
head
tail
a b c d ef
28/37
Example
a b c d f g
degree
e
queue:
g0 0 0 0 0 0 0
head
tail
a b c d ef g
28/37
Example
a b c d f g
degree
e
queue:
g0 0 0 0 0 0 0
head
a b c d ef g
tail
29/37
Outline
1 Graphs
2 Connectivity and Graph TraversalTesting Bipartiteness
3 Topological Ordering
4 Properties of BFS and DFS trees
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
u
v
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
u
v
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
u
v
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
u
v
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
u
v
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
u
v
30/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G
Can there be a vertical edge(u, v), u ≥ 2 levels above v?
No. v should be a child of u
Can there be a horizontal edge(u, v) u ≥ 2 levels above v?
No. v should be a child of u.
Can there be a horizontal edge(u, v), where u is 1 level abovev, but v’s parent is to the rightof u?
No. v should be a child of u.
31/37
Properties of a BFS Tree
Given a BFS tree T of a connected graph G, other than the treeedges, we only have horizontal edges (u, v), where
either u and v are at the samelevel
or u is 1 level above v, and v’sparent is to the left of u, (orvice versa)
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
32/37
Properties of a DFS Tree
Given a tree DFS tree T of a graph (connected) G,
Can there be a horizontal edge(u, v)?
No.
All non-tree edges are verticaledges.
A vertical edge (u, v) and itsthe edges in the path from u tov in T form a cycle; we call it acanonical cycle.
33/37
Properties of a DFS Tree
Lemma If G contains a cycle,then it has a canonical cycle.
Proof.If G contains a cycle, then itmust have at least non-treeedge.
W.r.t DFS tree T , we can onlyhave vertical + tree edges
∃ at least one vertical edge
There is a canonical cycle
There might or might not be non-canonical ones.
33/37
Properties of a DFS Tree
Lemma If G contains a cycle,then it has a canonical cycle.
Proof.If G contains a cycle, then itmust have at least non-treeedge.
W.r.t DFS tree T , we can onlyhave vertical + tree edges
∃ at least one vertical edge
There is a canonical cycle
There might or might not be non-canonical ones.
33/37
Properties of a DFS Tree
Lemma If G contains a cycle,then it has a canonical cycle.
Proof.If G contains a cycle, then itmust have at least non-treeedge.
W.r.t DFS tree T , we can onlyhave vertical + tree edges
∃ at least one vertical edge
There is a canonical cycle
There might or might not be non-canonical ones.
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
34/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Can there be a horizontal(directed) edge (u, v) where uis visited before v?
No.
However, there can behorizontal edges (u, v) where uis visited after v.
s∗
35/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Other than tree edges, thereare two types of edges:
vertical edges directed toancestorshorizontal edges (u, v) where uis visited after v.
An vertical edge (u, v) and thetree edges in the tree pathfrom v to u form a cycle, andwe call it a canonical cycle.
s∗
35/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Other than tree edges, thereare two types of edges:
vertical edges directed toancestorshorizontal edges (u, v) where uis visited after v.
An vertical edge (u, v) and thetree edges in the tree pathfrom v to u form a cycle, andwe call it a canonical cycle.
s∗
35/37
Properties of a DFS Tree Over a Directed Graph
Given a tree DFS tree T of a directed graph G, assuming all verticescan be reached from the starting vertex s∗
Other than tree edges, thereare two types of edges:
vertical edges directed toancestorshorizontal edges (u, v) where uis visited after v.
An vertical edge (u, v) and thetree edges in the tree pathfrom v to u form a cycle, andwe call it a canonical cycle.
s∗
36/37
Properties of a DFS Tree Over a Directed Graph
Lemma If there is a cycle in thedirected graph G, then there mustbe a canonical one.
Proof.
Focus on tree edges andhorizontal edges
post-order-traversal of T givesa reversed topological ordering
Without vertical edges, G hasno cycles
s∗
Again, there might be non-canonical cycles.
36/37
Properties of a DFS Tree Over a Directed Graph
Lemma If there is a cycle in thedirected graph G, then there mustbe a canonical one.
Proof.Focus on tree edges andhorizontal edges
post-order-traversal of T givesa reversed topological ordering
Without vertical edges, G hasno cycles
s∗
Again, there might be non-canonical cycles.
36/37
Properties of a DFS Tree Over a Directed Graph
Lemma If there is a cycle in thedirected graph G, then there mustbe a canonical one.
Proof.Focus on tree edges andhorizontal edges
post-order-traversal of T givesa reversed topological ordering
Without vertical edges, G hasno cycles
1 2
3 4
5
6
7
8 9
10 11 12
13
15
16
17 18
19
20
21
14
s∗
Again, there might be non-canonical cycles.
36/37
Properties of a DFS Tree Over a Directed Graph
Lemma If there is a cycle in thedirected graph G, then there mustbe a canonical one.
Proof.Focus on tree edges andhorizontal edges
post-order-traversal of T givesa reversed topological ordering
Without vertical edges, G hasno cycles
1 2
3 4
5
6
7
8 9
10 11 12
13
15
16
17 18
19
20
21
14
s∗
Again, there might be non-canonical cycles.
36/37
Properties of a DFS Tree Over a Directed Graph
Lemma If there is a cycle in thedirected graph G, then there mustbe a canonical one.
Proof.Focus on tree edges andhorizontal edges
post-order-traversal of T givesa reversed topological ordering
Without vertical edges, G hasno cycles
s∗
Again, there might be non-canonical cycles.
37/37
Cycle Detection Using DFS in Directed Graphs
Algorithm 1 Check-Cycle-Directed1: add a source s∗ to G and edges from s∗ to all other vertices.2: visited← boolean array over V , with visited[v] = false,∀v3: instack ← boolean array over V , with instack[v] = false,∀v4: DFS(s∗)5: return “no cycle”
Algorithm 2 DFS(v)
1: visited[v]← true, instack[v]← true2: for every outgoing edge (v, u) of v do3: if inqueue[u] then . Find a vertical edge4: exit the whole algorithm, by returning “there is a cycle”5: else if visited[u] = false then6: DFS(u)
7: instack[v]← false