cse 431/531: algorithm analysis and design (fall 2021
TRANSCRIPT
![Page 1: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/1.jpg)
CSE 431/531: Algorithm Analysis and Design (Fall 2021)
Graph Basics
Lecturer: Shi Li
Department of Computer Science and EngineeringUniversity at Buffalo
![Page 2: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/2.jpg)
2/37
Outline
1 Graphs
2 Connectivity and Graph TraversalTesting Bipartiteness
3 Topological Ordering
4 Properties of BFS and DFS trees
![Page 3: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/3.jpg)
3/37
Examples of Graphs
Figure: Road Networks
Figure: Social Networks
Figure: Internet
Figure: Transition Graphs
![Page 4: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/4.jpg)
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}}
![Page 5: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/5.jpg)
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}}
![Page 6: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/6.jpg)
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)}
![Page 7: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/7.jpg)
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)}
![Page 8: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/8.jpg)
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)}
![Page 9: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/9.jpg)
6/37
Social Network : Undirected
Transition Graph : Directed
Road Network : Directed or Undirected
Internet : Directed or Undirected
![Page 10: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/10.jpg)
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.
![Page 11: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/11.jpg)
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.
![Page 12: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/12.jpg)
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)
![Page 13: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/13.jpg)
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)
![Page 14: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/14.jpg)
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)
![Page 15: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/15.jpg)
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)
![Page 16: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/16.jpg)
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)
![Page 17: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/17.jpg)
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)
![Page 18: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/18.jpg)
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)
![Page 19: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/19.jpg)
9/37
Outline
1 Graphs
2 Connectivity and Graph TraversalTesting Bipartiteness
3 Topological Ordering
4 Properties of BFS and DFS trees
![Page 20: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/20.jpg)
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)
![Page 21: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/21.jpg)
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)
![Page 22: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/22.jpg)
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)
![Page 23: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/23.jpg)
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)
![Page 24: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/24.jpg)
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
![Page 25: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/25.jpg)
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
![Page 26: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/26.jpg)
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
![Page 27: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/27.jpg)
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
![Page 28: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/28.jpg)
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
![Page 29: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/29.jpg)
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).
![Page 30: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/30.jpg)
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
1
![Page 31: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/31.jpg)
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
v
1
![Page 32: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/32.jpg)
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
v
21
![Page 33: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/33.jpg)
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
v
2 31
![Page 34: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/34.jpg)
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
v 2 31
![Page 35: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/35.jpg)
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
v 2 3 41
![Page 36: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/36.jpg)
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
v 2 3 4 51
![Page 37: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/37.jpg)
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
v2 3 4 51
![Page 38: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/38.jpg)
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
v2 3 4 5 71
![Page 39: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/39.jpg)
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
v2 3 4 5 7 81
![Page 40: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/40.jpg)
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tail
v
2 3 4 5 7 81
![Page 41: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/41.jpg)
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tailv
2 3 4 5 7 81
![Page 42: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/42.jpg)
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tailv
2 3 4 5 7 8 61
![Page 43: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/43.jpg)
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
![Page 44: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/44.jpg)
13/37
Example of BFS via Queue
1
2 3
4 5
7
8
6
head
tailv
2 3 4 5 7 8 61
![Page 45: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/45.jpg)
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
![Page 46: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/46.jpg)
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
![Page 47: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/47.jpg)
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
![Page 48: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/48.jpg)
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
![Page 49: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/49.jpg)
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
![Page 50: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/50.jpg)
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
![Page 51: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/51.jpg)
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
![Page 52: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/52.jpg)
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
![Page 53: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/53.jpg)
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
![Page 54: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/54.jpg)
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
![Page 55: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/55.jpg)
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
![Page 56: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/56.jpg)
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
![Page 57: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/57.jpg)
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
![Page 58: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/58.jpg)
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)
![Page 59: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/59.jpg)
16/37
Outline
1 Graphs
2 Connectivity and Graph TraversalTesting Bipartiteness
3 Topological Ordering
4 Properties of BFS and DFS trees
![Page 60: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/60.jpg)
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.
![Page 61: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/61.jpg)
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
![Page 62: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/62.jpg)
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
![Page 63: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/63.jpg)
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
![Page 64: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/64.jpg)
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
![Page 65: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/65.jpg)
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
![Page 66: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/66.jpg)
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
![Page 67: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/67.jpg)
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
![Page 68: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/68.jpg)
19/37
Test Bipartiteness
![Page 69: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/69.jpg)
19/37
Test Bipartiteness
![Page 70: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/70.jpg)
19/37
Test Bipartiteness
![Page 71: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/71.jpg)
19/37
Test Bipartiteness
![Page 72: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/72.jpg)
19/37
Test Bipartiteness
![Page 73: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/73.jpg)
19/37
Test Bipartiteness
![Page 74: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/74.jpg)
19/37
Test Bipartiteness
![Page 75: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/75.jpg)
19/37
Test Bipartiteness
![Page 76: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/76.jpg)
19/37
Test Bipartiteness
![Page 77: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/77.jpg)
19/37
Test Bipartiteness
![Page 78: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/78.jpg)
19/37
Test Bipartiteness
bad edges!
![Page 79: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/79.jpg)
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”
![Page 80: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/80.jpg)
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
![Page 81: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/81.jpg)
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)
![Page 82: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/82.jpg)
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)
![Page 83: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/83.jpg)
22/37
Outline
1 Graphs
2 Connectivity and Graph TraversalTesting Bipartiteness
3 Topological Ordering
4 Properties of BFS and DFS trees
![Page 84: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/84.jpg)
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
![Page 85: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/85.jpg)
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
![Page 86: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/86.jpg)
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
![Page 87: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/87.jpg)
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
![Page 88: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/88.jpg)
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
![Page 89: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/89.jpg)
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
![Page 90: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/90.jpg)
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
![Page 91: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/91.jpg)
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
![Page 92: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/92.jpg)
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
![Page 93: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/93.jpg)
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
![Page 94: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/94.jpg)
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
![Page 95: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/95.jpg)
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
![Page 96: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/96.jpg)
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
![Page 97: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/97.jpg)
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
![Page 98: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/98.jpg)
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
![Page 99: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/99.jpg)
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
![Page 100: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/100.jpg)
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
![Page 101: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/101.jpg)
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
![Page 102: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/102.jpg)
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
![Page 103: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/103.jpg)
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
![Page 104: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/104.jpg)
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
![Page 105: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/105.jpg)
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
![Page 106: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/106.jpg)
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
![Page 107: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/107.jpg)
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
![Page 108: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/108.jpg)
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)
![Page 109: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/109.jpg)
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
![Page 110: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/110.jpg)
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
![Page 111: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/111.jpg)
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
![Page 112: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/112.jpg)
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
![Page 113: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/113.jpg)
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
![Page 114: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/114.jpg)
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
![Page 115: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/115.jpg)
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
![Page 116: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/116.jpg)
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
![Page 117: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/117.jpg)
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
![Page 118: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/118.jpg)
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
![Page 119: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/119.jpg)
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
![Page 120: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/120.jpg)
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
![Page 121: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/121.jpg)
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
![Page 122: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/122.jpg)
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
![Page 123: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/123.jpg)
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
![Page 124: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/124.jpg)
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
![Page 125: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/125.jpg)
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
![Page 126: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/126.jpg)
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
![Page 127: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/127.jpg)
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
![Page 128: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/128.jpg)
29/37
Outline
1 Graphs
2 Connectivity and Graph TraversalTesting Bipartiteness
3 Topological Ordering
4 Properties of BFS and DFS trees
![Page 129: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/129.jpg)
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.
![Page 130: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/130.jpg)
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.
![Page 131: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/131.jpg)
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.
![Page 132: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/132.jpg)
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.
![Page 133: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/133.jpg)
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.
![Page 134: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/134.jpg)
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.
![Page 135: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/135.jpg)
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.
![Page 136: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/136.jpg)
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.
![Page 137: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/137.jpg)
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.
![Page 138: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/138.jpg)
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.
![Page 139: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/139.jpg)
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.
![Page 140: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/140.jpg)
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.
![Page 141: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/141.jpg)
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.
![Page 142: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/142.jpg)
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.
![Page 143: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/143.jpg)
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.
![Page 144: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/144.jpg)
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.
![Page 145: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/145.jpg)
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.
![Page 146: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/146.jpg)
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.
![Page 147: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/147.jpg)
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.
![Page 148: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/148.jpg)
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.
![Page 149: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/149.jpg)
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.
![Page 150: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/150.jpg)
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
![Page 151: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/151.jpg)
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
![Page 152: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/152.jpg)
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.
![Page 153: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/153.jpg)
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
![Page 154: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/154.jpg)
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
![Page 155: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/155.jpg)
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.
![Page 156: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/156.jpg)
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
![Page 157: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/157.jpg)
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
![Page 158: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/158.jpg)
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.
![Page 159: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/159.jpg)
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)
![Page 160: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/160.jpg)
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.
![Page 161: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/161.jpg)
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.
![Page 162: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/162.jpg)
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.
![Page 163: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/163.jpg)
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.
![Page 164: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/164.jpg)
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.
![Page 165: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/165.jpg)
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.
![Page 166: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/166.jpg)
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.
![Page 167: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/167.jpg)
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.
![Page 168: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/168.jpg)
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.
![Page 169: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/169.jpg)
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.
![Page 170: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/170.jpg)
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.
![Page 171: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/171.jpg)
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.
![Page 172: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/172.jpg)
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.
![Page 173: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/173.jpg)
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.
![Page 174: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/174.jpg)
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.
![Page 175: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/175.jpg)
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.
![Page 176: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/176.jpg)
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.
![Page 177: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/177.jpg)
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.
![Page 178: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/178.jpg)
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.
![Page 179: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/179.jpg)
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.
![Page 180: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/180.jpg)
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.
![Page 181: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/181.jpg)
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.
![Page 182: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/182.jpg)
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.
![Page 183: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/183.jpg)
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.
![Page 184: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/184.jpg)
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.
![Page 185: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/185.jpg)
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.
![Page 186: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/186.jpg)
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.
![Page 187: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/187.jpg)
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.
![Page 188: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/188.jpg)
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.
![Page 189: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/189.jpg)
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.
![Page 190: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/190.jpg)
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∗
![Page 191: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/191.jpg)
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∗
![Page 192: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/192.jpg)
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∗
![Page 193: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/193.jpg)
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∗
![Page 194: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/194.jpg)
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∗
![Page 195: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/195.jpg)
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∗
![Page 196: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/196.jpg)
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∗
![Page 197: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/197.jpg)
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∗
![Page 198: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/198.jpg)
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∗
![Page 199: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/199.jpg)
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∗
![Page 200: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/200.jpg)
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∗
![Page 201: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/201.jpg)
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∗
![Page 202: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/202.jpg)
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∗
![Page 203: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/203.jpg)
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∗
![Page 204: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/204.jpg)
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∗
![Page 205: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/205.jpg)
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∗
![Page 206: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/206.jpg)
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∗
![Page 207: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/207.jpg)
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∗
![Page 208: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/208.jpg)
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∗
![Page 209: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/209.jpg)
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∗
![Page 210: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/210.jpg)
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∗
![Page 211: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/211.jpg)
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∗
![Page 212: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/212.jpg)
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∗
![Page 213: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/213.jpg)
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∗
![Page 214: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/214.jpg)
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∗
![Page 215: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/215.jpg)
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∗
![Page 216: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/216.jpg)
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∗
![Page 217: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/217.jpg)
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∗
![Page 218: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/218.jpg)
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∗
![Page 219: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/219.jpg)
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∗
![Page 220: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/220.jpg)
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.
![Page 221: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/221.jpg)
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.
![Page 222: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/222.jpg)
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.
![Page 223: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/223.jpg)
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.
![Page 224: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/224.jpg)
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.
![Page 225: CSE 431/531: Algorithm Analysis and Design (Fall 2021](https://reader031.vdocument.in/reader031/viewer/2022012519/619443221e8dc038ae2d6b5d/html5/thumbnails/225.jpg)
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