Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms Searching in a Graph.

Similar presentations


Presentation on theme: "Algorithms Searching in a Graph."— Presentation transcript:

1 Algorithms Searching in a Graph

2 Searching in a Graph Graph searching = systematically follow the edges of the graph so as to visit the vertices of the graph

3 Breadth-First Search (BFS)
Input: A graph G = (V, E) (directed or undirected) A source vertex s  V Goal: Explore the edges of G to “discover” every vertex reachable from s, taking the ones closest to s first Output: d[v] = distance (smallest # of edges) from s to v, for all v  V A “breadth-first tree” rooted at s that contains all reachable vertices

4 Breadth-First Search (cont.)
Discover vertices in increasing order of distance from the source s – search in breadth not depth Find all vertices at 1 edge from s, then all vertices at 2 edges from s, and so on 1 2 5 4 3 11 12 6 7 9

5 BFS A D E F C B Not seen/visited G Currently traversing Seen/Visited H
Finished traversing

6 BFS A D E F C B Not seen/visited G Currently traversing Seen/Visited H
Finished traversing

7 BFS A D E F B Not seen/visited G Currently traversing Seen/Visited H I
Finished traversing

8 BFS A D E F C B Not seen/visited G Currently traversing Seen/Visited H
Finished traversing

9 BFS A D E F C B Not seen/visited G Currently traversing Seen/Visited H
Finished traversing

10 BFS A D E F C B Not seen/visited G Currently traversing Seen/Visited H
Finished traversing

11 BFS A D E F C B Not seen/visited G Currently traversing Seen/Visited H
Finished traversing

12 BFS A D E F C B Not seen/visited G Currently traversing Seen/Visited H
Finished traversing

13 BFS A D E F C B Not seen/visited G Currently traversing Seen/Visited H
Finished traversing

14 BFS A D E F C B Not seen/visited G Currently traversing Seen/Visited H
Finished traversing

15 BFS A D E F C B Not seen/visited G Currently traversing Seen/Visited H
Finished traversing

16 BFS A D E F C B Not seen/visited G Currently traversing Seen/Visited H
Finished traversing

17 BFS A D E F C B Not seen/visited G Currently traversing Seen/Visited H
Finished traversing

18 BFS A D E F C B Not seen/visited G Currently traversing Seen/Visited H
Finished traversing

19 BFS A D E F C B Not seen/visited G Currently traversing Seen/Visited H
Finished traversing

20 BFS A D E F C B Not seen/visited G Currently traversing Seen/Visited H
Finished traversing

21 BFS A D E F C B Not seen/visited G Currently traversing Seen/Visited H
Finished traversing

22 BFS A D E F C B Not seen/visited G Currently traversing Seen/Visited H
Finished traversing

23 BFS A D E F C B Not seen/visited G Currently traversing Seen/Visited H
Finished traversing

24 Analysis of BFS for each u  V - {s} do color[u]  WHITE d[u] ← 
[u] = NIL color[s]  GRAY d[s] ← 0 [s] = NIL Q   Q ← ENQUEUE(Q, s) O(V) (1)

25 Analysis of BFS Total running time for BFS = O(V + E) while Q  
do u ← DEQUEUE(Q) for each v  Adj[u] do if color[v] = WHITE then color[v] = GRAY d[v] ← d[u] + 1 [v] = u ENQUEUE(Q, v) color[u]  BLACK Scan Adj[u] for all vertices in the graph Each vertex is scanned only once, when the vertex is dequeued Sum of lengths of all adjacency lists = (E) Scanning operations: O(E) (1) (1) Total running time for BFS = O(V + E)

26 Depth-First Search Input: Goal: Output:
G = (V, E) (No source vertex given!) Goal: Explore the edges of G to “discover” every vertex in V starting at the most current visited node Search may be repeated from multiple sources Output: 2 timestamps on each vertex: d[v] = discovery time f[v] = finishing time (done with examining v’s adjacency list) Depth-first forest 1 2 5 4 3

27 Depth-First Search Search “deeper” in the graph whenever possible
Edges are explored out of the most recently discovered vertex v that still has unexplored edges 1 2 5 4 3 After all edges of v have been explored, the search “backtracks” from the parent of v The process continues until all vertices reachable from the original source have been discovered If undiscovered vertices remain, choose one of them as a new source and repeat the search from that vertex DFS creates a “depth-first forest”

28 DFS A D E F C B G H I

29 DFS A D E F C B G H I

30 DFS A D E F C B G H I

31 DFS A D E F C B G H I

32 DFS A D E F C B G H I

33 DFS A D E F C B G H I

34 DFS A D E F C B G H I

35 DFS A D E F C B G H I

36 DFS A D E F C B G H I

37 DFS A D E F C B G H I

38 DFS A D E F C B G H I

39 DFS A D E F C B G H I

40 DFS A D E F C B G H I

41 DFS A D E F C B G H I

42 DFS A D E F C B G H I

43 DFS A D E F C B G H I

44 DFS A D E F C B G H I

45 DFS A D E F C B G H I

46 DFS A D E F C B G H I

47 Analysis of DFS(G) for each u  V[G] do color[u] ← WHITE [u] ← NIL
time ← 0 do if color[u] = WHITE then DFS-VISIT(u) (V) (V) – exclusive of time for DFS-VISIT

48 Analysis of DFS-VISIT(u)
color[u] ← GRAY time ← time+1 d[u] ← time for each v  Adj[u] do if color[v] = WHITE then [v] ← u DFS-VISIT(v) color[u] ← BLACK time ← time + 1 f[u] ← time DFS-VISIT is called exactly once for each vertex Each loop takes |Adj[v]| Total: ΣvV |Adj[v]| + (V) = (V + E) (E)

49 Shortest path algorithms

50 Shortest Paths Problems
Input is G(V, E) with edge weights Shortest Paths from a single source s to all/one destination in DAGs (DP Solution) Shortest Paths from a single source s to all/one destination in general graphs with no negative edge weights (Dijkstra: Greedy) Shortest Paths from all sources to all dests (Floyd Warshall: DP).

51 SSSP In General Graphs Without Neg. Edges
Input: A directed/undirected graph G(V, E): n nodes (one is the source), m edges (u,v) and costs cu,v X S A Y B C 1 3 5 2 4 Output: For each node v in the graph, shortest s-v path. Assumption 1: Graph is connected (all s-v paths exist) Assumption 2: Edge costs are non-negative, i.e., w(u, v) ≥ 0

52 Shortest Path Example 1 5 1 1 2 1 4 3 INPUT OUTPUT Dst Path Distance X
B Y Dst Path Distance X S->X 1 Y S->X->Y 2 A S->B->A 3 B S->B Dst Path Distance X S->X 1 Y S->X->Y 2 A S->B->A 3 B S->B C S->B->A->C 4 Dst Path Distance X S->X 1 Y S->X->Y 2 A S->B->A 3 Dst Path Distance X S->X 1 Dst Path Distance X S->X 1 Y S->X->Y 2 OUTPUT

53 Shortest Path Example 1 5 1 1 2 1 4 3 INPUT OUTPUT Dst Path Distance X
B Y Dst Path Distance X S->X 1 Y S->X->Y 2 A S->B->A 3 B S->B C S->B->A->C 4 OUTPUT

54 Dijkstra’s Algorithm A

55 Dijkstra’s Algorithm A D 1 8 4 C B

56 Dijkstra’s Algorithm A D E 1 2 1 8 9 4 C B

57 Dijkstra’s Algorithm A D E 1 2 1 3 3 8 9 4 F C B

58 Dijkstra’s Algorithm A D E 1 2 1 3 3 8 9 4 4 F 3 C B

59 Dijkstra’s Algorithm A D E 1 2 1 3 3 8 9 4 5 4 F 6 3 C B 2 G

60 Dijkstra’s Algorithm A D E 1 2 1 3 3 8 9 4 5 4 F 7 6 3 C B 4 2 H G

61 Dijkstra’s Algorithm A D E 1 2 3 8 9 4 5 F 3 C B 4 2 H G 3 1 3 4 7 6 8
1 3 3 8 9 4 5 4 F 7 6 3 C B 4 2 H 11 8 G 3

62 Dijkstra’s Algorithm A D E 1 2 3 8 9 4 5 F 3 C B 4 2 H G 3 1 3 4 7 6 8
1 3 3 8 9 4 5 4 F 7 6 3 C B 4 2 H 11 8 G 11 3

63 Dijkstra’s Algorithm Run time: O(mlog(n))
procedure dijkstra(G(V,E),s, weights w(u, v)) : L = {s}; R=V-{s} shortestDist is an array initialized to null parent is an array initialized to null distSoFar = priority queue of size n distSoFar[s] = 0; distSoFar[v] = +∞ for other v for i = 1 to n-1: let v* = extract-min from distSoFar remove v* from R and add to L shortestDist[v*] = distSoFar[v*] for each (v*, w) s.t. w∈R: decrement distSoFar[w] = min{distSoFar[w], shortestDist[v*] + w(v*, w) if distSoFar[w] decreased: set parent[w] = v* return shortestDist O(log(n)) O(log(n)) Run time: O(mlog(n))

64 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

65 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 2 10 15 3 8 Run on example graph

66 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 2 10 15 3 8 Run on example graph

67 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 2 10 15 r 3 8 Pick a start vertex r

68 Red vertices have been removed from Q
Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 2 10 15 u 3 8 Red vertices have been removed from Q

69 Red arrows indicate parent pointers
Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 2 10 15 u 3 8 3 Red arrows indicate parent pointers

70 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 14 2 10 15 u 3 8 3

71 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 14 2 10 15 3 8 3 u

72 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 14 2 10 15 8 3 8 3 u

73 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 10 14 2 10 15 8 3 8 3 u

74 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 10 14 2 10 15 8 3 8 3 u

75 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 10 2 14 2 10 15 8 3 8 3 u

76 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 10 2 14 2 10 15 8 15 3 8 3 u

77 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u 6 4 9 5 10 2 14 2 10 15 8 15 3 8 3

78 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u 6 4 9 5 10 2 9 14 2 10 15 8 15 3 8 3

79 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u 4 6 4 9 5 10 2 9 14 2 10 15 8 15 3 8 3

80 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u 4 6 4 9 5 5 2 9 14 2 10 15 8 15 3 8 3

81 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u 4 6 4 9 5 5 2 9 14 2 10 15 8 15 3 8 3

82 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u 4 6 4 9 5 5 2 9 14 2 10 15 8 15 3 8 3

83 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u 4 6 4 9 5 5 2 9 14 2 10 15 8 15 3 8 3

84 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 6 4 9 5 5 2 9 14 2 u 10 15 8 15 3 8 3

85 Review: Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); What is the hidden cost in this code?

86 Review: Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; DecreaseKey(v, w(u,v));

87 Review: Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); What will be the running time? A: Depends on queue binary heap: O(E lg V) Fibonacci heap: O(V lg V + E)

88 Walk-Through F C A B D H G E Initialize array K dv pv A F   B C D E
2 Initialize array 3 F C K dv pv A F B C D E G H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

89 F C A B D H G E Start with any node, say D K dv pv A B C D T  E F G H
2 Start with any node, say D 3 F C K dv pv A B C D T E F G H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

90 F C A B D H G E Update distances of adjacent, unselected nodes K dv pv
2 3 F C K dv pv A B C 3 D T E 25 F 18 G 2 H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

91 F C A B D H G E Select node with minimum distance K dv pv A B C 3 D T
2 3 F C K dv pv A B C 3 D T E 25 F 18 G 2 H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

92 F C A B D H G E Update distances of adjacent, unselected nodes K dv pv
2 3 F C K dv pv A B C 3 D T E 7 G F 18 2 H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

93 F C A B D H G E Select node with minimum distance K dv pv A B C T 3 D
2 3 F C K dv pv A B C T 3 D E 7 G F 18 2 H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

94 F C A B D H G E Update distances of adjacent, unselected nodes K dv pv
2 3 F C K dv pv A B 4 C T 3 D E 7 G F 2 H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

95 F C A B D H G E Select node with minimum distance K dv pv A B 4 C T 3
2 3 F C K dv pv A B 4 C T 3 D E 7 G F 2 H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

96 F C A B D H G E Update distances of adjacent, unselected nodes K dv pv
2 3 F C K dv pv A 10 F B 4 C T 3 D E 2 G H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

97 F C A B D H G E Select node with minimum distance K dv pv A 10 F B 4 C
2 3 F C K dv pv A 10 F B 4 C T 3 D E 2 G H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

98 F C A B D H G E Update distances of adjacent, unselected nodes
2 3 F C K dv pv A 10 F B 4 C T 3 D E 2 G H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7 Table entries unchanged

99 F C A B D H G E Select node with minimum distance K dv pv A 10 F B 4 C
2 3 F C K dv pv A 10 F B 4 C T 3 D E 2 G H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

100 F C A B D H G E Update distances of adjacent, unselected nodes K dv pv
2 3 F C K dv pv A 4 H B C T 3 D E 2 F G 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

101 F C A B D H G E Select node with minimum distance K dv pv A T 4 H B C
2 3 F C K dv pv A T 4 H B C 3 D E 2 F G 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

102 F C A B D H G E Update distances of adjacent, unselected nodes
2 3 F C K dv pv A T 4 H B C 3 D E 2 F G 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7 Table entries unchanged

103 F C A B D H G E Select node with minimum distance K dv pv A T 4 H B C
2 3 F C K dv pv A T 4 H B C 3 D E 2 F G 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

104 F C A B D H G E Done Cost of Minimum Spanning Tree =  dv = 21 K dv pv
3 F C K dv pv A T 4 H B C 3 D E 2 F G A 3 4 4 B D H 2 3 G E Done

105 Example a b c d e h g f i 4 8 7 11 1 2 14 9 10 6


Download ppt "Algorithms Searching in a Graph."

Similar presentations


Ads by Google