CS 3343: Analysis of Algorithms

54
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort

description

CS 3343: Analysis of Algorithms. Lecture 24: Graph searching, Topological sort. Review of MST and shortest path problem. Run Kruskal’s algorithm Run Prim’s algorithm Run Dijkstra’s algorithm. 2. f. c. 4. 7. 8. 8. a. d. g. 6. 7. 6. 1. 3. 5. 6. b. e. Graph Searching. - PowerPoint PPT Presentation

Transcript of CS 3343: Analysis of Algorithms

Page 1: CS 3343: Analysis of Algorithms

CS 3343: Analysis of Algorithms

Lecture 24: Graph searching, Topological sort

Page 2: CS 3343: Analysis of Algorithms

Review of MST and shortest path problem

• Run Kruskal’s algorithm

• Run Prim’s algorithm

• Run Dijkstra’s algorithm

a

c

b

f

e

d g

4

5

6

1

6

2

8

6

8

3

7

7

Page 3: CS 3343: Analysis of Algorithms

Graph Searching

• Given: a graph G = (V, E), directed or undirected• Goal: methodically explore every vertex (and

every edge)• Ultimately: build a tree on the graph

– Pick a vertex as the root– Find (“discover”) its children, then their children, etc.– Note: might also build a forest if graph is not

connected– Here we only consider that the graph is connected

Page 4: CS 3343: Analysis of Algorithms

Breadth-First Search

• “Explore” a graph, turning it into a tree– Pick a source vertex to be the root– Expand frontier of explored vertices across

the breadth of the frontier

Page 5: CS 3343: Analysis of Algorithms

Breadth-First Search

• Associate vertex “colors” to guide the algorithm– White vertices have not been discovered

• All vertices start out white

– Grey vertices are discovered but not fully explored• They may be adjacent to white vertices

– Black vertices are discovered and fully explored• They are adjacent only to black and gray vertices

• Explore vertices by scanning adjacency list of grey vertices

Page 6: CS 3343: Analysis of Algorithms

Breadth-First Search

BFS(G, s) {

initialize vertices; // mark all vertices as white

Q = {s}; // Q is a queue; initialize to s

while (Q not empty) {

u = Dequeue(Q);

for each v adj[u] if (v.color == WHITE) {

v.color = GREY;

v.d = u.d + 1;

v.p = u;

Enqueue(Q, v);

}

u.color = BLACK;

}

}

What does v.p represent?

What does v.d represent?

Page 7: CS 3343: Analysis of Algorithms

Breadth-First Search: Example

r s t u

v w x y

Page 8: CS 3343: Analysis of Algorithms

Breadth-First Search: Example

0

r s t u

v w x y

sQ:

Page 9: CS 3343: Analysis of Algorithms

Breadth-First Search: Example

1

0

1

r s t u

v w x y

wQ: r

Page 10: CS 3343: Analysis of Algorithms

Breadth-First Search: Example

1

0

1

2

2

r s t u

v w x y

rQ: t x

Page 11: CS 3343: Analysis of Algorithms

Breadth-First Search: Example

1

2

0

1

2

2

r s t u

v w x y

Q: t x v

Page 12: CS 3343: Analysis of Algorithms

Breadth-First Search: Example

1

2

0

1

2

2

3

r s t u

v w x y

Q: x v u

Page 13: CS 3343: Analysis of Algorithms

Breadth-First Search: Example

1

2

0

1

2

2

3

3

r s t u

v w x y

Q: v u y

Page 14: CS 3343: Analysis of Algorithms

Breadth-First Search: Example

1

2

0

1

2

2

3

3

r s t u

v w x y

Q: u y

Page 15: CS 3343: Analysis of Algorithms

Breadth-First Search: Example

1

2

0

1

2

2

3

3

r s t u

v w x y

Q: y

Page 16: CS 3343: Analysis of Algorithms

Breadth-First Search: Example

1

2

0

1

2

2

3

3

r s t u

v w x y

Q: Ø

Page 17: CS 3343: Analysis of Algorithms

BFS: The Code Again

BFS(G, s) {

initialize vertices;

Q = {s};

while (Q not empty) {

u = Dequeue(Q);

for each v adj[u] if (v.color == WHITE) {

v.color = GREY;

v.d = u.d + 1;

v.p = u;

Enqueue(Q, v);

}

u.color = BLACK;

}

} What will be the running time?

Touch every vertex: Θ(n)

u = every vertex, but only once (Why?)

v = every vertex that appears in some other vert’s adjacency listTotal: Θ(m)

Total running time: Θ(n+m)

Page 18: CS 3343: Analysis of Algorithms

Depth-First Search

• Depth-first search is another strategy for exploring a graph– Explore “deeper” in the graph whenever

possible– Edges are explored out of the most recently

discovered vertex v that still has unexplored edges

– When all of v’s edges have been explored, backtrack to the vertex from which v was discovered

Page 19: CS 3343: Analysis of Algorithms

Depth-First Search

• Vertices initially colored white

• Then colored gray when discovered

• Then black when finished

Page 20: CS 3343: Analysis of Algorithms

Depth-First Search: The Code

DFS(G)

{

for each vertex u G->V {

u->color = WHITE;

}

time = 0;

for each vertex u G->V {

if (u->color == WHITE)

DFS_Visit(u);

}

}

DFS_Visit(u)

{

u->color = GREY;

time = time+1;

u->d = time;

for each v u->Adj[] {

if (v->color == WHITE)

DFS_Visit(v);

}

u->color = BLACK;

time = time+1;

u->f = time;

}

Page 21: CS 3343: Analysis of Algorithms

Depth-First Search: The Code

DFS(G)

{

for each vertex u G->V {

u->color = WHITE;

}

time = 0;

for each vertex u G->V {

if (u->color == WHITE)

DFS_Visit(u);

}

}

DFS_Visit(u)

{

u->color = GREY;

time = time+1;

u->d = time;

for each v u->Adj[] {

if (v->color == WHITE)

DFS_Visit(v);

}

u->color = BLACK;

time = time+1;

u->f = time;

}

What does u->d represent?

Page 22: CS 3343: Analysis of Algorithms

Depth-First Search: The Code

DFS(G)

{

for each vertex u G->V {

u->color = WHITE;

}

time = 0;

for each vertex u G->V {

if (u->color == WHITE)

DFS_Visit(u);

}

}

DFS_Visit(u)

{

u->color = GREY;

time = time+1;

u->d = time;

for each v u->Adj[] {

if (v->color == WHITE)

DFS_Visit(v);

}

u->color = BLACK;

time = time+1;

u->f = time;

}

What does u->f represent?

Page 23: CS 3343: Analysis of Algorithms

Depth-First Search: The Code

DFS(G)

{

for each vertex u G->V {

u->color = WHITE;

}

time = 0;

for each vertex u G->V {

if (u->color == WHITE)

DFS_Visit(u);

}

}

DFS_Visit(u)

{

u->color = GREY;

time = time+1;

u->d = time;

for each v u->Adj[] {

if (v->color == WHITE)

DFS_Visit(v);

}

u->color = BLACK;

time = time+1;

u->f = time;

}Will all vertices eventually be colored black?

(How about in BFS?)

Page 24: CS 3343: Analysis of Algorithms

Depth-First Search: The Code

DFS(G)

{

for each vertex u G->V {

u->color = WHITE;

}

time = 0;

for each vertex u G->V {

if (u->color == WHITE)

DFS_Visit(u);

}

}

DFS_Visit(u)

{

u->color = GREY;

time = time+1;

u->d = time;

for each v u->Adj[] {

if (v->color == WHITE)

DFS_Visit(v);

}

u->color = BLACK;

time = time+1;

u->f = time;

}

What will be the running time?

Page 25: CS 3343: Analysis of Algorithms

Depth-First Search: The Code

DFS(G)

{

for each vertex u G->V {

u->color = WHITE;

}

time = 0;

for each vertex u G->V {

if (u->color == WHITE)

DFS_Visit(u);

}

}

DFS_Visit(u)

{

u->color = GREY;

time = time+1;

u->d = time;

for each v u->Adj[] {

if (v->color == WHITE)

DFS_Visit(v);

}

u->color = BLACK;

time = time+1;

u->f = time;

}

How many times will DFS_Visit() be called?

Page 26: CS 3343: Analysis of Algorithms

Depth-First Search: The Code

DFS(G)

{

for each vertex u G->V {

u->color = WHITE;

}

time = 0;

for each vertex u G->V {

if (u->color == WHITE)

DFS_Visit(u);

}

}

DFS_Visit(u)

{

u->color = GREY;

time = time+1;

u->d = time;

for each v u->Adj[] {

if (v->color == WHITE)

DFS_Visit(v);

}

u->color = BLACK;

time = time+1;

u->f = time;

}

How much time is needed within each DFS_Visit()?

Page 27: CS 3343: Analysis of Algorithms

Depth-First Search: The Code

DFS(G)

{

for each vertex u G->V {

u->color = WHITE;

}

time = 0;

for each vertex u G->V {

if (u->color == WHITE)

DFS_Visit(u);

}

}

DFS_Visit(u)

{

u->color = GREY;

time = time+1;

u->d = time;

for each v u->Adj[] {

if (v->color == WHITE)

DFS_Visit(v);

}

u->color = BLACK;

time = time+1;

u->f = time;

}

So, running time of DFS = O(V+E)

Page 28: CS 3343: Analysis of Algorithms

DFS Examplesourcevertex

Page 29: CS 3343: Analysis of Algorithms

DFS Example

1 | | |

| | |

| |

sourcevertex

d f

Page 30: CS 3343: Analysis of Algorithms

DFS Example

1 | | |

| | |

2 | |

d f

sourcevertex

Page 31: CS 3343: Analysis of Algorithms

DFS Example

1 | | |

| | 3 |

2 | |

d f

sourcevertex

Page 32: CS 3343: Analysis of Algorithms

DFS Example

1 | | |

| | 3 | 4

2 | |

d f

sourcevertex

Page 33: CS 3343: Analysis of Algorithms

DFS Example

1 | | |

| 5 | 3 | 4

2 | |

d f

sourcevertex

Page 34: CS 3343: Analysis of Algorithms

DFS Example

1 | | |

| 5 | 63 | 4

2 | |

d f

sourcevertex

Page 35: CS 3343: Analysis of Algorithms

DFS Example

1 | | |

| 5 | 63 | 4

2 | 7 |

d f

sourcevertex

Page 36: CS 3343: Analysis of Algorithms

DFS Example

1 | 8 | |

| 5 | 63 | 4

2 | 7 |

d f

sourcevertex

Page 37: CS 3343: Analysis of Algorithms

DFS Example

1 | 8 | |

| 5 | 63 | 4

2 | 7 9 |

d f

What is the structure of the grey vertices? What do they represent?

sourcevertex

Page 38: CS 3343: Analysis of Algorithms

DFS Example

1 | 8 | |

| 5 | 63 | 4

2 | 7 9 |10

d f

sourcevertex

Page 39: CS 3343: Analysis of Algorithms

DFS Example

1 | 8 |11 |

| 5 | 63 | 4

2 | 7 9 |10

d f

sourcevertex

Page 40: CS 3343: Analysis of Algorithms

DFS Example

1 |12 8 |11 |

| 5 | 63 | 4

2 | 7 9 |10

d f

sourcevertex

Page 41: CS 3343: Analysis of Algorithms

DFS Example

1 |12 8 |11 13|

| 5 | 63 | 4

2 | 7 9 |10

d f

sourcevertex

Page 42: CS 3343: Analysis of Algorithms

DFS Example

1 |12 8 |11 13|

14| 5 | 63 | 4

2 | 7 9 |10

d f

sourcevertex

Page 43: CS 3343: Analysis of Algorithms

DFS Example

1 |12 8 |11 13|

14|155 | 63 | 4

2 | 7 9 |10

d f

sourcevertex

Page 44: CS 3343: Analysis of Algorithms

DFS Example

1 |12 8 |11 13|16

14|155 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

Page 45: CS 3343: Analysis of Algorithms

DFS and cycles in graph

• A graph G is acyclic iff a DFS of G yields no back edges

1 | | |

| | 3 |

2 | |

d f

sourcevertex

Page 46: CS 3343: Analysis of Algorithms

Directed Acyclic Graphs

• A directed acyclic graph or DAG is a directed graph with no directed cycles:

Acyclic Cyclic

Page 47: CS 3343: Analysis of Algorithms

Topological Sort

• Topological sort of a DAG:– Linear ordering of all vertices in graph G such

that vertex u comes before vertex v if edge (u, v) G

• Real-world example: getting dressed

Page 48: CS 3343: Analysis of Algorithms

Getting Dressed

Underwear Socks

ShoesPants

Belt

Shirt

Watch

Tie

Jacket

Page 49: CS 3343: Analysis of Algorithms

Getting Dressed

Underwear Socks

ShoesPants

Belt

Shirt

Watch

Tie

Jacket

Socks Underwear Pants Shoes Watch Shirt Belt Tie Jacket

Page 50: CS 3343: Analysis of Algorithms

Topological Sort Algorithm

Topological-Sort()

{ // condition: the graph is a DAG

Run DFS

When a vertex is finished, output it

Vertices are output in reverse topological order

}

• Time: O(V+E)

• Correctness: Want to prove that(u,v) G uf > vf

Page 51: CS 3343: Analysis of Algorithms

Correctness of Topological Sort

• Claim: (u,v) G uf > vf– When (u,v) is explored, u is grey

• v = grey (u,v) is back edge. Contradiction (Why?)

• v = white v becomes descendent of u vf < uf (since must finish v before backtracking and finishing u)

• v = black v already finished vf < uf

Page 52: CS 3343: Analysis of Algorithms

Another Algorithm• Store vertices in a priority min-queue, with the in-degree of the vertex as the key• While queue is not empty

• Extract minimum vertex v, and give it next number• Decrease keys of all adjacent vertices by 1

33 55 66

44

22

77

99

88110 22

1

1

3

1 10

Page 53: CS 3343: Analysis of Algorithms

Another Algorithm• Store vertices in a priority min-queue, with the in-degree of the vertex as the key• While queue is not empty

• Extract minimum vertex v, and give it next number• Decrease keys of all adjacent vertices by 1

33 55 66

44

22

77

99

88110 22

1

1

3

1 10

1

2

0

0

0

1

0

0

0

10

Page 54: CS 3343: Analysis of Algorithms

Topological Sort Runtime

Runtime:• O(|V|) to build heap + O(|E|) DECREASE-KEY ops O(|V| + |E| log |V|) with a binary heap O(|V|2) with an array

Compare to DFS: O(|V|+|E|)