CSE 431/531: Algorithm Analysis and Design (Fall 2021 ...

225
CSE 431/531: Algorithm Analysis and Design (Fall 2021) Graph Basics Lecturer: Shi Li Department of Computer Science and Engineering University at Buffalo

Transcript of CSE 431/531: Algorithm Analysis and Design (Fall 2021 ...

Page 1: CSE 431/531: Algorithm Analysis and Design (Fall 2021 ...

CSE 431/531: Algorithm Analysis and Design (Fall 2021)

Graph Basics

Lecturer: Shi Li

Department of Computer Science and EngineeringUniversity at Buffalo

Page 2: CSE 431/531: Algorithm Analysis and Design (Fall 2021 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

19/37

Test Bipartiteness

Page 69: CSE 431/531: Algorithm Analysis and Design (Fall 2021 ...

19/37

Test Bipartiteness

Page 70: CSE 431/531: Algorithm Analysis and Design (Fall 2021 ...

19/37

Test Bipartiteness

Page 71: CSE 431/531: Algorithm Analysis and Design (Fall 2021 ...

19/37

Test Bipartiteness

Page 72: CSE 431/531: Algorithm Analysis and Design (Fall 2021 ...

19/37

Test Bipartiteness

Page 73: CSE 431/531: Algorithm Analysis and Design (Fall 2021 ...

19/37

Test Bipartiteness

Page 74: CSE 431/531: Algorithm Analysis and Design (Fall 2021 ...

19/37

Test Bipartiteness

Page 75: CSE 431/531: Algorithm Analysis and Design (Fall 2021 ...

19/37

Test Bipartiteness

Page 76: CSE 431/531: Algorithm Analysis and Design (Fall 2021 ...

19/37

Test Bipartiteness

Page 77: CSE 431/531: Algorithm Analysis and Design (Fall 2021 ...

19/37

Test Bipartiteness

Page 78: CSE 431/531: Algorithm Analysis and Design (Fall 2021 ...

19/37

Test Bipartiteness

bad edges!

Page 79: CSE 431/531: Algorithm Analysis and Design (Fall 2021 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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