Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 3343: Analysis of Algorithms

Similar presentations


Presentation on theme: "CS 3343: Analysis of Algorithms"— Presentation transcript:

1 CS 3343: Analysis of Algorithms
Graph search, Topological sort

2 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 6 d 7 g 6 1 5 3 6 b e

3 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

4 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

5 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

6 Breadth-First Search What does v.d represent? What does v.p represent?
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.d represent? What does v.p represent?

7 Breadth-First Search: Example
u v w x y

8 Breadth-First Search: Example
u v w x y Q: s

9 Breadth-First Search: Example
u 1 1 v w x y Q: w r

10 Breadth-First Search: Example
u 1 2 1 2 v w x y Q: r t x

11 Breadth-First Search: Example
u 1 2 2 1 2 v w x y Q: t x v

12 Breadth-First Search: Example
u 1 2 3 2 1 2 v w x y Q: x v u

13 Breadth-First Search: Example
u 1 2 3 2 1 2 3 v w x y Q: v u y

14 Breadth-First Search: Example
u 1 2 3 2 1 2 3 v w x y Q: u y

15 Breadth-First Search: Example
u 1 2 3 2 1 2 3 v w x y Q: y

16 Breadth-First Search: Example
u 1 2 3 2 1 2 3 v w x y Q: Ø

17 BFS: The Code Again Touch every vertex: Θ(n)
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; 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) What will be the running time? Total running time: Θ(n+m)

18 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

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

20 Depth-First Search: The Code
DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; 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; u->f = time;

21 Depth-First Search: The Code
DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; 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; u->f = time; What does u->d represent?

22 Depth-First Search: The Code
DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; 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; u->f = time; What does u->f represent?

23 Depth-First Search: The Code
DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; 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; u->f = time; Will all vertices eventually be colored black? (How about in BFS?)

24 Depth-First Search: The Code
DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; 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; u->f = time; What will be the running time?

25 Depth-First Search: The Code
DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; 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; u->f = time; How many times will DFS_Visit() be called?

26 Depth-First Search: The Code
DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; 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; u->f = time; How much time is needed within each DFS_Visit()?

27 Depth-First Search: The Code
DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; 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; u->f = time; So, running time of DFS = O(V+E)

28 DFS Example source vertex

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

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

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

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

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

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

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

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

37 What is the structure of the grey vertices? What do they represent?
DFS Example source vertex d f 1 | 8 | | 2 | 7 9 | 3 | 4 5 | 6 | What is the structure of the grey vertices? What do they represent?

38 DFS Example source vertex d f 1 | 8 | | 2 | 7 9 |10 3 | 4 5 | 6 |

39 DFS Example source vertex d f 1 | 8 |11 | 2 | 7 9 |10 3 | 4 5 | 6 |

40 DFS Example source vertex d f 1 |12 8 |11 | 2 | 7 9 |10 3 | 4 5 | 6 |

41 DFS Example d f 1 |12 8 |11 13| 2 | 7 9 |10 3 | 4 5 | 6 |
source vertex d f 1 |12 8 |11 13| 2 | 7 9 |10 3 | 4 5 | 6 |

42 DFS Example d f 1 |12 8 |11 13| 2 | 7 9 |10 3 | 4 5 | 6 14|
source vertex d f 1 |12 8 |11 13| 2 | 7 9 |10 3 | 4 5 | 6 14|

43 DFS Example d f 1 |12 8 |11 13| 2 | 7 9 |10 3 | 4 5 | 6 14|15
source vertex d f 1 |12 8 |11 13| 2 | 7 9 |10 3 | 4 5 | 6 14|15

44 DFS Example d f 1 |12 8 |11 13|16 2 | 7 9 |10 3 | 4 5 | 6 14|15
source vertex d f 1 |12 8 |11 13|16 2 | 7 9 |10 3 | 4 5 | 6 14|15

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

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

47 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

48 Getting Dressed Underwear Socks Watch Pants Shoes Shirt Belt Tie
Jacket

49 Getting Dressed Underwear Socks Watch Pants Shoes Shirt Belt Tie
Jacket Socks Underwear Pants Shoes Watch Shirt Belt Tie Jacket

50 Topological Sort Algorithm
{ // 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

51 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

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

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

54 Topological Sort 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|)


Download ppt "CS 3343: Analysis of Algorithms"

Similar presentations


Ads by Google