Graph Basics Lecturer: Shi Li Department of Computer Science and - - PowerPoint PPT Presentation

graph basics
SMART_READER_LITE
LIVE PREVIEW

Graph Basics Lecturer: Shi Li Department of Computer Science and - - PowerPoint PPT Presentation

CSE 431/531: Analysis of Algorithms Graph Basics Lecturer: Shi Li Department of Computer Science and Engineering University at Buffalo Outline Graphs 1 Connectivity and Graph Traversal 2 Testing Bipartiteness Topological Ordering 3 2/28


slide-1
SLIDE 1

CSE 431/531: Analysis of Algorithms

Graph Basics

Lecturer: Shi Li

Department of Computer Science and Engineering University at Buffalo

slide-2
SLIDE 2

2/28

Outline

1

Graphs

2

Connectivity and Graph Traversal Testing Bipartiteness

3

Topological Ordering

slide-3
SLIDE 3

3/28

Examples of Graphs

Figure: Road Networks Figure: Social Networks Figure: Internet Figure: Transition Graphs

slide-4
SLIDE 4

4/28

(Undirected) Graph G = (V, E)

1 2 3 4 5 7 8 6

V : a set of vertices (nodes); E: pairwise relationships among V ;

(undirected) graphs: relationship is symmetric, E contains subsets of size 2

slide-5
SLIDE 5

4/28

(Undirected) Graph G = (V, E)

1 2 3 4 5 7 8 6

V : a 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 of size 2 E = {{1, 2}, {1, 3}, {2, 3}, {2, 4}, {2, 5}, {3, 5}, {3, 7}, {3, 8}, {4, 5}, {5, 6}, {7, 8}}

slide-6
SLIDE 6

4/28

Directed Graph G = (V, E)

1 2 3 4 5 7 8 6

V : a 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

slide-7
SLIDE 7

4/28

Directed Graph G = (V, E)

1 2 3 4 5 7 8 6

V : a 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)}

slide-8
SLIDE 8

5/28

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)}

slide-9
SLIDE 9

6/28

Social Network : Undirected Transition Graph : Directed Road Network : Directed or Undirected Internet : Directed or Undirected

slide-10
SLIDE 10

7/28

Representation of Graphs

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 7 8 6

Adjacency matrix

n × n matrix, A[u, v] = 1 if (u, v) ∈ E and A[u, v] = 0

  • therwise

A is symmetric if graph is undirected

slide-11
SLIDE 11

7/28

Representation of Graphs

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: 1 2 3 4 5 7 8 6

Adjacency matrix

n × n matrix, A[u, v] = 1 if (u, v) ∈ E and A[u, v] = 0

  • therwise

A is symmetric if graph is undirected

Linked lists

For every vertex v, there is a linked list containing all neighbours of v.

slide-12
SLIDE 12

8/28

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 time to check (u, v) ∈ E time to list all neighbours of v

slide-13
SLIDE 13

8/28

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) time to check (u, v) ∈ E time to list all neighbours of v

slide-14
SLIDE 14

8/28

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 time to list all neighbours of v

slide-15
SLIDE 15

8/28

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) time to list all neighbours of v

slide-16
SLIDE 16

8/28

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

slide-17
SLIDE 17

8/28

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)

slide-18
SLIDE 18

8/28

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)

slide-19
SLIDE 19

9/28

Outline

1

Graphs

2

Connectivity and Graph Traversal Testing Bipartiteness

3

Topological Ordering

slide-20
SLIDE 20

10/28

Connectivity Problem Input: graph G = (V, E), (using linked lists) two vertices s, t ∈ V Output: whether there is a path connecting s to t in G

slide-21
SLIDE 21

10/28

Connectivity Problem Input: graph G = (V, E), (using linked lists) two vertices s, t ∈ V Output: whether there is a path connecting s to t in G Algorithm: starting from s, search for all vertices that are reachable from s and check if the set contains t

slide-22
SLIDE 22

10/28

Connectivity Problem Input: graph G = (V, E), (using linked lists) two vertices s, t ∈ V Output: whether there is a path connecting s to t in G Algorithm: starting from s, search for all vertices that are reachable from s and check if the set contains t

Breadth-First Search (BFS)

slide-23
SLIDE 23

10/28

Connectivity Problem Input: graph G = (V, E), (using linked lists) two vertices s, t ∈ V Output: whether there is a path connecting s to t in G Algorithm: starting from s, search for all vertices that are reachable from s and check if the set contains t

Breadth-First Search (BFS) Depth-First Search (DFS)

slide-24
SLIDE 24

11/28

Breadth-First Search (BFS)

Build layers L0, L1, L2, L3, · · · L0 = {s} Lj+1 contains all nodes that are not in L0 ∪ L1 ∪ · · · ∪ Lj and have an edge to a vertex in Lj

slide-25
SLIDE 25

11/28

Breadth-First Search (BFS)

Build layers L0, L1, L2, L3, · · · L0 = {s} Lj+1 contains all nodes that are not in L0 ∪ L1 ∪ · · · ∪ Lj and have an edge to a vertex in Lj

1 2 3 4 5 7 8 6

slide-26
SLIDE 26

11/28

Breadth-First Search (BFS)

Build layers L0, L1, L2, L3, · · · L0 = {s} Lj+1 contains all nodes that are not in L0 ∪ L1 ∪ · · · ∪ Lj and have an edge to a vertex in Lj

1 2 3 4 5 7 8 6

slide-27
SLIDE 27

11/28

Breadth-First Search (BFS)

Build layers L0, L1, L2, L3, · · · L0 = {s} Lj+1 contains all nodes that are not in L0 ∪ L1 ∪ · · · ∪ Lj and have an edge to a vertex in Lj

1 2 3 4 5 7 8 6

slide-28
SLIDE 28

11/28

Breadth-First Search (BFS)

Build layers L0, L1, L2, L3, · · · L0 = {s} Lj+1 contains all nodes that are not in L0 ∪ L1 ∪ · · · ∪ Lj and have an edge to a vertex in Lj

1 2 3 4 5 7 8 6

slide-29
SLIDE 29

12/28

Implementing BFS using a Queue

BFS(s)

1

head ← 1, tail ← 1, queue[1] ← s

2

mark s as “visited” and all other vertices as “unvisited”

3

while head ≥ tail

4

v ← queue[tail], tail ← tail + 1

5

for all neighbours u of v

6

if u is “unvisited” then

7

head ← head + 1, queue[head] = u

8

mark u as “visited” Running time: O(n + m).

slide-30
SLIDE 30

13/28

Example of BFS via Queue

1 2 3 4 5 7 8 6

head tail

1

slide-31
SLIDE 31

13/28

Example of BFS via Queue

1 2 3 4 5 7 8 6

head tail

v 1

slide-32
SLIDE 32

13/28

Example of BFS via Queue

1 2 3 4 5 7 8 6

head tail

v 2 1

slide-33
SLIDE 33

13/28

Example of BFS via Queue

1 2 3 4 5 7 8 6

head tail

v 2 3 1

slide-34
SLIDE 34

13/28

Example of BFS via Queue

1 2 3 4 5 7 8 6

head tail

v 2 3 1

slide-35
SLIDE 35

13/28

Example of BFS via Queue

1 2 3 4 5 7 8 6

head tail

v 2 3 4 1

slide-36
SLIDE 36

13/28

Example of BFS via Queue

1 2 3 4 5 7 8 6

head tail

v 2 3 4 5 1

slide-37
SLIDE 37

13/28

Example of BFS via Queue

1 2 3 4 5 7 8 6

head tail

v 2 3 4 5 1

slide-38
SLIDE 38

13/28

Example of BFS via Queue

1 2 3 4 5 7 8 6

head tail

v 2 3 4 5 7 1

slide-39
SLIDE 39

13/28

Example of BFS via Queue

1 2 3 4 5 7 8 6

head tail

v 2 3 4 5 7 8 1

slide-40
SLIDE 40

13/28

Example of BFS via Queue

1 2 3 4 5 7 8 6

head tail

v 2 3 4 5 7 8 1

slide-41
SLIDE 41

13/28

Example of BFS via Queue

1 2 3 4 5 7 8 6

head tail

v 2 3 4 5 7 8 1

slide-42
SLIDE 42

13/28

Example of BFS via Queue

1 2 3 4 5 7 8 6

head tail

v 2 3 4 5 7 8 6 1

slide-43
SLIDE 43

13/28

Example of BFS via Queue

1 2 3 4 5 7 8 6

head tail

v 2 3 4 5 7 8 6 1

slide-44
SLIDE 44

13/28

Example of BFS via Queue

1 2 3 4 5 7 8 6

head tail

v 2 3 4 5 7 8 6 1

slide-45
SLIDE 45

13/28

Example of BFS via Queue

1 2 3 4 5 7 8 6

head tail

v 2 3 4 5 7 8 6 1

slide-46
SLIDE 46

14/28

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

slide-47
SLIDE 47

14/28

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

slide-48
SLIDE 48

14/28

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

slide-49
SLIDE 49

14/28

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

slide-50
SLIDE 50

14/28

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

slide-51
SLIDE 51

14/28

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

slide-52
SLIDE 52

14/28

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

slide-53
SLIDE 53

14/28

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

slide-54
SLIDE 54

14/28

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

slide-55
SLIDE 55

14/28

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

slide-56
SLIDE 56

14/28

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

slide-57
SLIDE 57

14/28

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

slide-58
SLIDE 58

15/28

Implementing DFS using a Stack

DFS(s)

1

head ← 1, stack[1] ← s

2

mark all vertices as “unexplored”

3

while head ≥ 1

4

v ← stack[head], head ← head − 1

5

if v is unexplored then

6

mark v as “explored”

7

for all neighbours u of v

8

if u is not explored then

9

head ← head + 1, stack[head] = u Running time: O(n + m).

slide-59
SLIDE 59

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

explored vertices:

1

head

slide-60
SLIDE 60

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

v

explored vertices: head

slide-61
SLIDE 61

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

v

explored vertices: head

1

slide-62
SLIDE 62

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

head

v

explored vertices:

1 3 2

slide-63
SLIDE 63

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

v

explored vertices: head

1 3

slide-64
SLIDE 64

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

v

explored vertices: head

1 3 2

slide-65
SLIDE 65

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

head

v

explored vertices:

1 3 2 5 4 3

slide-66
SLIDE 66

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

head

v

explored vertices:

1 3 2 5 4

slide-67
SLIDE 67

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

head

v

explored vertices:

1 3 2 5 4 3

slide-68
SLIDE 68

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

head

v

explored vertices:

1 3 2 5 4 3 8 7 5

slide-69
SLIDE 69

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

head

v

explored vertices:

1 3 2 5 4 3 8 7

slide-70
SLIDE 70

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

head

v

explored vertices:

1 3 2 5 4 3 8 7 5

slide-71
SLIDE 71

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

v

explored vertices:

1 3 2 5 4 3

head

8 7 5 6 4

slide-72
SLIDE 72

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

head

v

explored vertices:

1 3 2 5 4 3 8 7 5 6

slide-73
SLIDE 73

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

head

v

explored vertices:

1 3 2 5 4 3 8 7 5 6 4

slide-74
SLIDE 74

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

head

v

explored vertices:

1 3 2 5 4 3 8 7 5 4

slide-75
SLIDE 75

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

head

v

explored vertices:

1 3 2 5 4 3 8 7 5 4 6

slide-76
SLIDE 76

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

head

v

explored vertices:

1 3 2 5 4 3 8 5 4 6

slide-77
SLIDE 77

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

head

v

explored vertices:

1 3 2 5 4 3 8 5 4 6 7

slide-78
SLIDE 78

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

head

v

explored vertices:

1 3 2 5 4 3 8 5 4 6 7 8

slide-79
SLIDE 79

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

head

v

explored vertices:

1 3 2 5 4 3 8 5 4 6 7

slide-80
SLIDE 80

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

head

v

explored vertices:

1 3 2 5 4 3 8 5 4 6 7 8

slide-81
SLIDE 81

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

head

v

explored vertices:

1 3 2 5 4 3 5 4 6 7 8

slide-82
SLIDE 82

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

head

v

explored vertices:

1 3 2 5 3 5 4 6 7 8

slide-83
SLIDE 83

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

v

explored vertices: head

1 3 2 3 5 4 6 7 8

slide-84
SLIDE 84

16/28

Example of DFS using Stack

1 2 3 4 5 7 8 6

v

explored vertices: head

1 2 3 5 4 6 7 8

slide-85
SLIDE 85

17/28

Implementing DFS using Recurrsion

DFS(s)

1

mark all vertices as “unexplored”

2

recursive-DFS(s) recursive-DFS(v)

1

if v is explored then return

2

mark v as “explored”

3

for all neighbours u of v

4

recursive-DFS(u)

slide-86
SLIDE 86

18/28

Outline

1

Graphs

2

Connectivity and Graph Traversal Testing Bipartiteness

3

Topological Ordering

slide-87
SLIDE 87

19/28

Testing Bipartiteness: Applications of BFS

  • Def. A graph G = (V, E) is a

bipartite graph if there is a partition of V into two sets L and R such that for every edge (u, v) ∈ E, we have either u ∈ L, v ∈ R or v ∈ L, u ∈ R.

slide-88
SLIDE 88

20/28

Testing Bipartiteness

Taking an arbitrary vertex s ∈ V

slide-89
SLIDE 89

20/28

Testing Bipartiteness

Taking an arbitrary vertex s ∈ V Assuming s ∈ L w.l.o.g

slide-90
SLIDE 90

20/28

Testing Bipartiteness

Taking an arbitrary vertex s ∈ V Assuming s ∈ L w.l.o.g Neighbors of s must be in R

slide-91
SLIDE 91

20/28

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

slide-92
SLIDE 92

20/28

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

slide-93
SLIDE 93

20/28

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

slide-94
SLIDE 94

20/28

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 above algorithm for each component

slide-95
SLIDE 95

21/28

Test Bipartiteness

slide-96
SLIDE 96

21/28

Test Bipartiteness

slide-97
SLIDE 97

21/28

Test Bipartiteness

slide-98
SLIDE 98

21/28

Test Bipartiteness

slide-99
SLIDE 99

21/28

Test Bipartiteness

slide-100
SLIDE 100

21/28

Test Bipartiteness

slide-101
SLIDE 101

21/28

Test Bipartiteness

slide-102
SLIDE 102

21/28

Test Bipartiteness

slide-103
SLIDE 103

21/28

Test Bipartiteness

slide-104
SLIDE 104

21/28

Test Bipartiteness

slide-105
SLIDE 105

21/28

Test Bipartiteness

bad edges!

slide-106
SLIDE 106

22/28

Testing Bipartiteness using BFS

BFS(s)

1

head ← 1, tail ← 1, queue[1] ← s

2

mark s as “visited” and all other vertices as “unvisited”

3

while head ≥ tail

4

v ← queue[tail], tail ← tail + 1

5

for all neighbours u of v

6

if u is “unvisited” then

7

head ← head + 1, queue[head] = u

8

mark u as “visited”

slide-107
SLIDE 107

22/28

Testing Bipartiteness using BFS

test-bipartiteness(s)

1

head ← 1, tail ← 1, queue[1] ← s

2

mark s as “visited” and all other vertices as “unvisited”

3

color[s] ← 0

4

while head ≥ tail

5

v ← queue[tail], tail ← tail + 1

6

for all neighbours u of v

7

if u is “unvisited” then

8

head ← head + 1, queue[head] = u

9

mark u as “visited”

10

color[u] ← 1 − color[v]

11

elseif color[u] = color[v] then

12

print(“G is not bipartite”) and exit

slide-108
SLIDE 108

23/28

Testing Bipartiteness using BFS

1

mark all vertices as “unvisited”

2

for each vertex v ∈ V

3

if v is “unvisited” then

4

test-bipartiteness(v)

5

print(“G is bipartite”)

slide-109
SLIDE 109

23/28

Testing Bipartiteness using BFS

1

mark all vertices as “unvisited”

2

for each vertex v ∈ V

3

if v is “unvisited” then

4

test-bipartiteness(v)

5

print(“G is bipartite”)

  • Obs. Running time of algorithm = O(n + m)
slide-110
SLIDE 110

23/28

Testing Bipartiteness using BFS

1

mark all vertices as “unvisited”

2

for each vertex v ∈ V

3

if v is “unvisited” then

4

test-bipartiteness(v)

5

print(“G is bipartite”)

  • Obs. Running time of algorithm = O(n + m)

Homework problem: using DFS to implement test-bipartiteness.

slide-111
SLIDE 111

24/28

Outline

1

Graphs

2

Connectivity and Graph Traversal Testing Bipartiteness

3

Topological Ordering

slide-112
SLIDE 112

25/28

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

slide-113
SLIDE 113

25/28

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 8 9

slide-114
SLIDE 114

26/28

Topological Ordering

Algorithm: each time take a vertex without incoming edges, then remove the vertex and all its outgoing edges.

a b c d e f g h i

slide-115
SLIDE 115

26/28

Topological Ordering

Algorithm: each time take a vertex without incoming edges, then remove the vertex and all its outgoing edges.

1 a b d e f g h i

slide-116
SLIDE 116

26/28

Topological Ordering

Algorithm: each time take a vertex without incoming edges, then remove the vertex and all its outgoing edges.

1 a b d e f g h i

slide-117
SLIDE 117

26/28

Topological Ordering

Algorithm: each time take a vertex without incoming edges, then remove the vertex and all its outgoing edges.

1 2 a b d e f h i

slide-118
SLIDE 118

26/28

Topological Ordering

Algorithm: each time take a vertex without incoming edges, then remove the vertex and all its outgoing edges.

1 2 a b d e f h i

slide-119
SLIDE 119

26/28

Topological Ordering

Algorithm: each time take a vertex without incoming edges, then remove the vertex and all its outgoing edges.

1 2 3 a b e f h i

slide-120
SLIDE 120

26/28

Topological Ordering

Algorithm: each time take a vertex without incoming edges, then remove the vertex and all its outgoing edges.

1 2 3 a b e f h i

slide-121
SLIDE 121

26/28

Topological Ordering

Algorithm: each time take a vertex without incoming edges, then remove the vertex and all its outgoing edges.

1 2 3 4 b e f h i

slide-122
SLIDE 122

26/28

Topological Ordering

Algorithm: each time take a vertex without incoming edges, then remove the vertex and all its outgoing edges.

1 2 3 4 b e f h i

slide-123
SLIDE 123

26/28

Topological Ordering

Algorithm: each time take a vertex without incoming edges, then remove the vertex and all its outgoing edges.

1 2 3 4 5 e f h i

slide-124
SLIDE 124

26/28

Topological Ordering

Algorithm: each time take a vertex without incoming edges, then remove the vertex and all its outgoing edges.

1 2 3 4 5 e f h i

slide-125
SLIDE 125

26/28

Topological Ordering

Algorithm: each time take a vertex without incoming edges, then remove the vertex and all its outgoing edges.

1 2 3 4 5 6 f h i

slide-126
SLIDE 126

26/28

Topological Ordering

Algorithm: each time take a vertex without incoming edges, then remove the vertex and all its outgoing edges.

1 2 3 4 5 6 f h i

slide-127
SLIDE 127

26/28

Topological Ordering

Algorithm: each time take a vertex without incoming edges, then remove the vertex and all its outgoing edges.

1 2 3 4 5 6 7 h i

slide-128
SLIDE 128

26/28

Topological Ordering

Algorithm: each time take a vertex without incoming edges, then remove the vertex and all its outgoing edges.

1 2 3 4 5 6 7 h i

slide-129
SLIDE 129

26/28

Topological Ordering

Algorithm: each time take a vertex without incoming edges, then remove the vertex and all its outgoing edges.

1 2 3 4 5 6 7 8 h

slide-130
SLIDE 130

26/28

Topological Ordering

Algorithm: each time take a vertex without incoming edges, then remove the vertex and all its outgoing edges.

1 2 3 4 5 6 7 8 h

slide-131
SLIDE 131

26/28

Topological Ordering

Algorithm: each time take a vertex without incoming edges, then remove the vertex and all its outgoing edges.

1 2 3 4 5 6 7 8 9

slide-132
SLIDE 132

26/28

Topological Ordering

Algorithm: each time take a vertex without incoming edges, then remove the vertex and all its outgoing edges.

1 2 3 4 5 6 7 8 9

slide-133
SLIDE 133

27/28

Topological Ordering

Algorithm: each time take a vertex without incoming edges, then remove the vertex and all its outgoing edges.

slide-134
SLIDE 134

27/28

Topological Ordering

Algorithm: each time take a vertex without incoming edges, then remove the vertex and all its outgoing edges. Q: How to make the algorithm as efficient as possible?

slide-135
SLIDE 135

27/28

Topological Ordering

Algorithm: each time take a vertex without incoming edges, then remove 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

slide-136
SLIDE 136

28/28

topological-sort(G)

1

let dv ← 0 for every v ∈ V

2

for every v ∈ V

3

for every u such that (v, u) ∈ E

4

du ← du + 1

5

S ← {v : dv = 0}, i ← 0

6

while S = ∅

7

v ← arbitrary vertex in S, S ← S \ {v}

8

i ← i + 1, π(v) ← i

9

for every u such that (v, u) ∈ E

10

du ← du − 1

11

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)