Download presentation

Presentation is loading. Please wait.

Published byJoy Nicholson Modified over 2 years ago

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

2
Midterm 2 overview

3
Midterm 2 overview: overall

4
Semester overview A BCD

5
Review of MST and shortest path problem Run Kruskal’s algorithm Run Prim’s algorithm Run Dijkstra’s algorithm a c b f e dg 4 5 6 1 6 2 8 6 8 3 7 7

6
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

7
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

8
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

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

10
Breadth-First Search: Example rstu vwxy

11
0 rstu vwxy s Q:

12
Breadth-First Search: Example 1 0 1 rstu vwxy w Q: r

13
Breadth-First Search: Example 1 0 1 2 2 rstu vwxy r Q: tx

14
Breadth-First Search: Example 1 2 0 1 2 2 rstu vwxy Q: txv

15
Breadth-First Search: Example 1 2 0 1 2 2 3 rstu vwxy Q: xvu

16
Breadth-First Search: Example 1 2 0 1 2 2 3 3 rstu vwxy Q: vuy

17
Breadth-First Search: Example 1 2 0 1 2 2 3 3 rstu vwxy Q: uy

18
Breadth-First Search: Example 1 2 0 1 2 2 3 3 rstu vwxy Q: y

19
Breadth-First Search: Example 1 2 0 1 2 2 3 3 rstu vwxy Q: Ø

20
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 list Total: Θ(m) Total running time: Θ(n+m)

21
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

22
Depth-First Search Vertices initially colored white Then colored gray when discovered Then black when finished

23
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; }

24
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?

25
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?

26
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?)

27
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?

28
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?

29
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()?

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

31
DFS Example source vertex

32
DFS Example 1 | | | | | | | | source vertex d f

33
DFS Example 1 | | | | | | 2 | | d f source vertex

34
DFS Example 1 | | | | |3 | 2 | | d f source vertex

35
DFS Example 1 | | | | |3 | 4 2 | | d f source vertex

36
DFS Example 1 | | | |5 |3 | 4 2 | | d f source vertex

37
DFS Example 1 | | | |5 | 63 | 4 2 | | d f source vertex

38
DFS Example 1 | | | |5 | 63 | 4 2 | 7 | d f source vertex

39
DFS Example 1 |8 | | |5 | 63 | 4 2 | 7 | d f source vertex

40
DFS Example 1 |8 | | |5 | 63 | 4 2 | 79 | d f What is the structure of the grey vertices? What do they represent? source vertex

41
DFS Example 1 |8 | | |5 | 63 | 4 2 | 79 |10 d f source vertex

42
DFS Example 1 |8 |11 | |5 | 63 | 4 2 | 79 |10 d f source vertex

43
DFS Example 1 |128 |11 | |5 | 63 | 4 2 | 79 |10 d f source vertex

44
DFS Example 1 |128 |1113| |5 | 63 | 4 2 | 79 |10 d f source vertex

45
DFS Example 1 |128 |1113| 14|5 | 63 | 4 2 | 79 |10 d f source vertex

46
DFS Example 1 |128 |1113| 14|155 | 63 | 4 2 | 79 |10 d f source vertex

47
DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f

48
DFS and cycles in graph A graph G is acyclic iff a DFS of G yields no back edges 1 | | | | |3 | 2 | | d f source vertex

49
Directed Acyclic Graphs A directed acyclic graph or DAG is a directed graph with no directed cycles: Acyclic Cyclic

50
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

51
Getting Dressed UnderwearSocks ShoesPants Belt Shirt Watch Tie Jacket

52
Getting Dressed UnderwearSocks ShoesPants Belt Shirt Watch Tie Jacket SocksUnderwearPantsShoesWatchShirtBeltTieJacket

53
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 u f > v f

54
Correctness of Topological Sort Claim: (u,v) G u f > v f –When (u,v) is explored, u is grey v = grey (u,v) is back edge. Contradiction (Why?) v = white v becomes descendent of u v f < u f (since must finish v before backtracking and finishing u) v = black v already finished v f < u f

55
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 3 3 5 5 6 6 4 4 2 2 7 7 9 9 8 8 1 1 0 2 2 1 1 3 1 1 0

56
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 3 3 5 5 6 6 4 4 2 2 7 7 9 9 8 8 1 1 0 2 2 1 1 3 1 1 0 1 2 0 0 0 1 0 0 0 1 0

57
Topological Sort Runtime Runtime: O(|V|) to build heap + O(|E|) D ECREASE -K EY ops O(|V| + |E| log |V|) with a binary heap O(|V| 2 ) with an array Compare to DFS: O(|V|+|E|)

Similar presentations

OK

Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.

Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on power system deregulation Ppt on exploring number system Ppt on ict in education in india Knowledge based view ppt on iphone Make ppt online Ppt on telephone etiquette training Ppt on event handling in javascript which term Ppt on artificial intelligence for cse Ppt on the digestive system of human Ppt on retail marketing