Breadth First Search of Graphs

893 views
782 views

Published on

Applications of Breadth-First Search of Graphs,
Reading on Breadth-First Search of Graphs,
Breadth-First Search Algorithm Input,
Breadth-First Search Algorithm,
Breadth-First Search Algorithm Output,
Edges in Breadth-First Search,
Breadth-First Search Tree,
Single Source Shortest Paths Problem,

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
893
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
54
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Breadth First Search of Graphs

  1. 1. Breadth-First Search of Graphs Analysis of Algorithms Breadth-First Search of Graphs Prepared by Limani Bhavik N. HNGU University Analysis of Algorithms
  2. 2. Applications of Breadth-First Search of Graphs a) Single Source Shortest Path b) Graph Matching
  3. 3. Reading on Breadth-First Search of Graphs • Reading Selection: – CLR, Chapter 24
  4. 4. Breadth-First Search Algorithm Input : undirected graph G (V,E) with root r V input = ∈
  5. 5. Breadth-First Search Algorithm : L 0 each V visit(v) LEVEL(0) {r}; visit (r) LEVEL(L) {} LEVEL(L+1) {} initialize for do false true while do begin ¬ ∈ ¬ ¬ ¬ ≠ ¬ each v LEVEL(L) each {v,u} E s.t. not visit(u) add u to for do begin for do ∈ ∈ LEVEL(L+1) visit (u) L L 1 true od end end ¬ ¬ +
  6. 6. Breadth-First Search Algorithm Output : LEVEL(0), LEVEL(1), ..., LEVEL(L-1) O(|V|+|E|) time cost output
  7. 7. Edges in Breadth-First Search • All edges {u,v} ∈ E have level distance  1 32 4 1 6 5 87 LEVEL(0) LEVEL(1) LEVEL(2) Example root r
  8. 8. Breadth-First Search Tree • Breadth First Search Tree T 32 4 1 6 87 LEVEL(0) LEVEL(1) LEVEL(2) r 5
  9. 9. Single Source Shortest Paths Problem : digraph G (V,E) with root r V weighting d: E positive reals input = ∈ → problem: For each vertex v, determine D(v) = min length path from root r to v
  10. 10. Dijkstra’s Algorithm for Single Source Shortest Paths : Q {} each v V-{r} D(v) D(r) 0 choose a vertex u V-Q with minimum D(u) initialize for do until no change do ¬ ∈ ¬ ∞ ¬ ∈ v add u to Q each (u,v) E s.t. v V-Q D(v) min (D(v), D(u) + d(u,v)) : V D(v) weight of min. path from r to v for do output ∈ ∈ ¬ ∀ ∈ =
  11. 11. Example Single Source Shortest Paths Problem • example 100 root r 2 3 1 4 5 50 40 20 30 10
  12. 12. Example Execution of Dijkstra’s Algorithm Q u D(1) D(2) D(3) D(4) D(5)  1 0     {1} 2 0 10 30  100 {1,2} 3 0 10 30 60 100 {1,2,3} 4 0 10 30 50 100 {1,2,3,4} 5 0 10 30 50 90
  13. 13. Proof of Dijkstra’s Algorithm • Use induction hypothesis: v v (1) V, D(v) is of the minimum cost of path p from r to v, where p visits only vertices of Q {v} (2) Q, D(v) minimum cost path from r to v D(r) 0 for weight basis ∀ ∈ ∪ ∀ ∈ = = Q {r}           =
  14. 14. Proof of Dijkstra’s Algorithm (cont’d) if D(u) is minimum for all u V-Q then : (1) D(u) is minimum cost of path from r to u in G suppose not: then path p with weight induction step claim ∈ (u,v) E D(u) and such that p visits a vertex w V-(Q {u}). Then D(w) D(u), contradiction. (2) is satisfied by D(v) min (D(v), ∈ < ∈ ∪ < = D(u) d(u,v)) for all v Q {u} + ∈ ∪
  15. 15. Time Cost of Dijkstra’s Algorithm on a RAM Model • Time cost: per iteration • Since there are |V| iterations, Total Time O( |V| (log |V| ) + |E|) - O(log|V|) to find u V-Q with min D(u) - O(degree(u)) to update weights ∈    
  16. 16. Graph Matching • Graph G = (V,E) • Graph Matching M is a subset of E – if e1, e2 distinct edges in M – Then they have no vertex in common Vertex v is matched if v is in an edge of M example
  17. 17. Graph Matching Problem Graph Matching Problem: Find a maximum size matching • Suppose: – G = (V,E) has matching M Goal: – find a larger matching
  18. 18. Augmenting Path in G given Graph Matching M • An augmenting path p = (e1, e2, …, ek) 1 3 5 k 2 4 k-1 begins and ends at unmatched vertices require e , e , e , ..., e E-M e , e , ..., e M     ∈  ∈
  19. 19. Graph Matching (cont’d) • Initial matching M in G • Augmenting path p = ((5,2), (2,6), (6,4), (4,7), (7,3)) |M| = 2 1 2 3 4 5 6 7 8
  20. 20. Graph Matching (cont’d) • New matching M‘ = P  M = (P  M) – (P  M) |P  M| = 3 1 2 3 4 5 6 7 8
  21. 21. Characterization of a Maximum Graph Matching via Lack of Augmented Path • Theorem M is maximum matching iff there is no augmenting path relative to M
  22. 22. Proof of Characterization of Maximum Graph Matching (cont’d) • Proof (1) If M is smaller matching and p is an augmenting path for M, then M  P is a matching size > |M| (2) If M, M ' are matchings with |M| < |M '|
  23. 23. Claim: M  M' contains an augmenting path for M. Proof • The graph G' = (V, M  M ' ) has only paths with edges alternating between M and M '. • Repeatedly delete a cycle in G ' (with equal number of edges in M, M ') • Since |M| < |M '| must eventually get augmenting path remaining for M.
  24. 24. Maximum Matching Algorithm • Algorithm graph G (V,E) [1] M {} [2] there exists an augmenting path p relative to M M M P [3] maximum matching M input while do output = ¬ ¬ ⊕
  25. 25. Maximum Matching (cont’d) • Remaining problem: Find augmenting path • Assume weighting d: E  R+ = pos. reals
  26. 26. Maximum Weighted Matching Algorithm • Assume – weighting d: E  R+ = positive reals • Theorem – Let M be maximum weight among matchings of size |M|. – Let P be an augmenting path for M of maximum weight. – Then matching M  P is of maximum weight among matchings of size |M|+1.
  27. 27. Proof of Maximum Weighted Matching Algorithm • Proof – Let M' be any maximum weight matching of size |M|+ 1. – Consider the graph G' = (V, M  M ' ). – Then the maximum weight augmenting path p in G' gives a matching M  P of the same weight as M'.
  28. 28. Bipartite Graph • Bipartite Graph G = (V,E) 1 2 1 2 1 2 V V V , V V E is a subset of { {u,v} | u V , v V } = ∪ ∩ = Φ ∈ ∈ V1 V2
  29. 29. Breadth-First Search Algorithm for Augmented Path • Assume G is bipartite graph with matching M. • Use Breadth-First Search: LEVEL(0) = some unmatched vertex r for odd L > 0, LEVEL(L) = {u|{v,u}  E – M when v  LEVEL(L -1) and when u in no lower level} For even L > 0, LEVEL(L) = {u|{v,u}  M when v  LEVEL(L -1) and u in no lower level}
  30. 30. Proof of Breadth-First Search Algorithm for Augmented Path • Cases (1) If for some odd L >0, LEVEL(L) contains an unmatched vertex u then the Breadth First Search tree T has an augmenting path from r to u (2) Otherwise no augmenting path exists, so M is maximal.
  31. 31. Finding a Maximal Matching in a Bipartite Graph • Theorem If G = (V,E) is a bipartite graph, Then the maximum matching can be constructed in O(|V||E|) time. • Proof Each stage requires O(|E|) time for Breadth First Search construction of augmenting path.
  32. 32. Generalizations of Matching Algorithm • Generalizations: (1) Compute Edge Weighted Maximum Matching (2) Edmonds gives a polynomial time algorithm for maximum matching of graphany       
  33. 33. Computing Augmented Paths in General Graphs • Let M be matching in general graph G • Fix starting vertex r to be an unmatched vertex Let vertex v V be if even length augmenting path from r to v and if odd length augmenting path from r to v. even odd ∈ ∃ ∃
  34. 34. Why Algorithm for Augmented Paths in Bipartite Graphs does not work for General Graphs G is bipartite vertex is both even and odd G is bipartite some vertices may be both even and odd! Case no Case not ⇒ ⇒
  35. 35. Edmond’s Algorithm for Augmented Paths in General Graphs P is STEMr v t' t base w even vertex BLOSSOM even vertex t w, v t' Shrink Blossom  STEM BLOSSOM BASE augmenting path from r to v is subpath of p from r to v is subpath of p from v to w plus edge {w,v} is vertex w
  36. 36. Blossom Shrinking Maintains the Existence of Augmented Paths • Theorem If G' is formed from G by shrinking of blossom B, then G contains an augmenting path iff G' does.
  37. 37. Proof of Blossom Shrinking • Proof (1) If G' contains an augmenting path p, then if p visits blossom B we can insert an augmenting subpath p' within blossom into p to get a new augmenting path for G (2) If G contains an augmenting path, then apply Edmond’s blossom shrinking algorithm to find an augmenting path in G'.
  38. 38. Edmond’s Blossom Shrinking Algorithm • comment Edmond’s algorithm will construct a forest of trees whose paths are partial augmenting paths Graph G=(V,E) with matching M E {(v,w), (w,v) | {v,w} E} input initialization → = ∈
  39. 39. Edmond’s Blossom Shrinking Algorithm (cont’d) • Note: We will let P(v) = parent of vertex v [0] each unmatched vertex v V label v as "even" [1] each matched v V label v "unreached" set p(v) v is matched edge {v,w} mate (v) w for do for do null if then ∈ ∈ = ¬ od           
  40. 40. Main Loop • Edmond’s Main Loop: an unexplored edge (v,w) E where v is "even" ( none exists, then terminate and output current matching M, since there is no augmenting path) Choose if → ∈
  41. 41. Main Loop (cont’d) • Case 1 if w is “odd” then do nothing. • Case 2 if w is “unreached” and matched then set w “odd” and set mate (w) “even” P(w)  v , P(mate (w))  w even odd even wv mate(w)
  42. 42. Main Loop (cont’d) • Case 3 if w is “even” and v, w are in distinct trees T, T' then output augmenting path p from root of T to v, through {v,w}, in T' to root. eveneven v w T T' Unmatched root Unmatched root Unmatched edge
  43. 43. Main Loop (cont’d) • Case 4 w is “even” and v,w in same tree T then {v,w} forms a blossom B containing all vertices which are both (i) a descendant of LCA(v,w) and (ii) an ancestor of v or w where LCA(v,w) = least common ancestor of v,w in T
  44. 44. Main Loop (cont’d) • Shrink all vertices of B to a single vertex b. Define p(b) = p(LCA(v,w)) and p(x) = b for all x ∈ B LCA (v,w) v w ** Blossom
  45. 45. Proof Edmond’s blossom-shrinking algorithm succeeds • Lemma Edmond’s blossom-shrinking algorithm succeeds iff • Proof Uses an induction on blossom shrinking stages an augmenting path in G∃
  46. 46. Time Bounds for Matching in General Graphs • Edmond’s blossom-shrinking algorithm costs time O(n4 ) • [Gabow and Tarjan] implement in time O(nm) all O(n) stages of matching algorithmtaking O(m) time per stage for blossom shrinking • [Micali and Vazirani] using network flow to find augmented paths and reduce time to O(n m) for unweighted matching in general graphs
  47. 47. Breadth-First Search of Graphs Prepared by Limani Bhavik N. HNGU University Analysis of Algorithms

×