1 / 33

ADVANCED ALGORITHMS

ADVANCED ALGORITHMS. GRAPH ALGORITHMS (UNIT-2 ). The Bellman-Ford Algorithm (BFA) The BFA solves the single-source shortest-paths problem in general case in which edge weights may be negative. Given a weighted directed graph G = (V,E). with source s,

blenda
Download Presentation

ADVANCED ALGORITHMS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ADVANCED ALGORITHMS GRAPH ALGORITHMS (UNIT-2)

  2. The Bellman-Ford Algorithm (BFA) The BFA solves the single-source shortest-paths problem in general case in which edge weights may be negative. Given a weighted directed graph G = (V,E). with source s, and weight function w, the BFA returns a boolean value indicating whether or not there is a negative-weight cycle that is reachable from the source.

  3. Bellman-Ford(G,w,s) • Initialize-Single-Source(G,s) • fori = 1 to |V| -1 • for each edge (u,v) ∊ E • RELAX(u,v,w) • foreach edge (u,v) ∊ E • if v.d > u.d + w(u,v) • return FALSE • 8 return TRUE RELAX (u,v,w) if v.d > u.d + w(u,v) 2 v.d = u.d + w(u,v) 3 v.∏ = u

  4. Single –Source Shortest Paths in DAG: DAG stands for ‘Directed Acyclic Graph’. There is no cycle in a directed acyclic graph. Topological Sort : Topological Sort of a DAG, G = (V,E) is a linear ordering of all its nodes such that if G has an edge (u,v), then u appears before v in the ordering. The algorithm starts by topologically sorting the DAG to impose a linear ordering on the vertices. If a DAG contains a path from u to v, then u preceds v in the topological sort.

  5. DAG-SHORTEST-PATHS(G,w,s) Topologically sort the vertices of G INITIALIZE-SINGLE-SOURCE(G,s) for each vertex u, taken in topologically sorted order for each vertex v ∊G.Adj[u] 5. RELAX (u, v, w) Ex-1 : Topologically sort the following DAG : t r x y s z

  6. r  s  t  x  y  z Ex-2 : Draw the Graph and Solve it for the following Weight Table : ------------------------------------------------ r  s s  t t  x x  y y  z 5 2 7 -1 -2 ------------------------------------------------ s  x x  z r  t t  y t  z 6 1 3 4 2

  7. 3. Johnson’s Algorithm for Sparse Graphs: This algorithm finds shortest paths between all pairs. This algorithm returns a matrix of shortest-path weights for all pairs of vertices or reports that the that the input graph contains a negative-weight cycle. This algorithm uses the technique of ’reweighting’. If all edge weights w in a graph G = (V,E) are non-negative, we can find shortest paths between all pairs of vertices by running Dijkstra’s algorithm once from each matrix. b) If G has negative –weight edges, then compute a new set of non-negative edge weights (reweighting) that allows us to use the same method.

  8. JOHNSON(G,w) Compute G’, where G’.V = G.V U {s} G’.E = G.E U { (s,v) : v ∊ G.V w(s,v) = 0 for all v ∊ G.V if BELLMAN-FORD(G’, w, s) = = FALSE print “the input graph contains a negative- weight cycle”. else for each vertex v ∊ G’.V set h(v) to the value of δ(s,v) computed by the BF algorithm. for each edge (u,v) ∊ G’. E 7 w’(u,v) = w(u,v) + h(u) – h(v)

  9. Let D = (duv) be a new matrix. for each vertex u ∊ G.V 10. run DIJKSTRA(G,w’,u) to compute δ’(s,v) for all v ∊ G.V for each vertex v ∊ G.V duv = δ’(u,v) + h(v) – h(u) return D Ex-3: Consider the following Graph. G(V) = [ a, b, c, d, e] G(E) = [ (a  b : 3), (a  c : 8), (a  e : -4), (b  d : 1), (b  e : 7), (c  b : 4), (d  c : -5), (d  a : 2), (e  d : 6) ]

  10. Consider new vertex s. Draw edges from s to a, b, c, d, e with weights zero. Draw the Graph G’ (V’,E’), where V’ = V U (s). and E’ = E U [(s,a), (s,b), (s,c), (s,d),(s,e)] for G, we have |V| = 5 |E| = 9 for G’, we have |V| = 6 |E| = 14 And h(a) = δ(s,a) = 0 h(b) = δ(s,b) = -1 h(c) = δ(s,c) = -5 h(d) = δ(s,d) = 0 h(e) = δ(s,e) = -4

  11. Bellman-Ford Algorithm : TRUE REWEIGHTING : w’(a,b) = w(a,b) + h(a) – h(b) = 3 + 0 – (-1) = 4 w’(a,c) = w(a,c) + h(a) – h(c) = 13 w’(a,e) = w(a,e) + h(a) – h(e) = 0 w’(b,d) = w(b,d) + h(b) – h(d) = 0 w’(b,e) = w(b,e) + h(b) – h(e) = 10 Similarily w’(c,b) = 0 w’(d,c) = 0 w’(d,a) = 2 w’(e,d) = 2 w’(s,a) = 0 w’(s,b) = 1 w’(s,c) = 5 w’(s,d) = 0 w’(s,e) = 4

  12. The Output : Draw the D-Matrix (5 x 5 ) : δ(u,v) = duv δ(u,v) a b c d e --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- a 0 1 - 3 2 - 4 b 3 0 - 4 1 - 1 c 7 4 0 5 3 d 2 - 1 - 5 0 - 2 e 8 5 1 6 0

  13. 4. FLOW NETWORKS : A flow network G = (V,E) is a directed graph in which each edge (u,v) ∊ E has a non-negative capacity c(u,v)  0. There are two distinguished vertices : source s sink t A flow in G has two following properties : a) Capacity Constraint : 0 ≤ f(u,v) ≤ c(u,v) b) Flow Conservation : For all u ∊ V – {s,t}  f(u,v) =  f(v,u) v ∊ V v∊ V

  14. Ex-4 : Consider the following Flow Network v1 12 v3 1620 s 4 97 t 13 4 v2 14 v4 The following is FNW for |f| = 19 v1 12/12 v3 11/1615/20 s 1/44/97/7 t 8/134/4 v2 11/14 v4

  15. The following is FNW for |f| = 20 v1 12/12 v3 8/1616/20 S 4/43/9 7/7 T 12/134/4 v2 11/14 v4 4. The Ford-Fulkerson Method : This FF Method is used for solving the maximum- flow problem. This method iteratively increases the value of the flow.

  16. Residual Capacity : Let f be a flow in G. Consider a pair of vertices u,v∊ V The Residual Capacity Cf(u,v) is : = c(u,v) – f(u,v) if (u,v) ∊ E Cf(u,v) = f(v,u) if (v,u) ∊ E = 0 otherwise Residual Network : Given a flow network G = (V,E) and a flow f, the residual network of G induced by f is Gf = (V,Ef), where Ef = { (u,v) ∊ V X V : Cf(u,v) > 0 }

  17. The Ford-Fulkerson Algorithm : FORD-FULKERSON(G,s,t) 1 for each edge (u,v) ∊ E 2 (u,v).f = 0 3 while there exists a path p from s to t in the residual network Gf Cf(p) = min {Cf(u,v) : (u,v) is in p} for each edge (u,v) in p 6 if (u,v) ∊ E 7 (u,v) .f = (u.v).f + Cf(p) 8 else (v,u).f = (v,u).f - Cf(p)

  18. Example-5 : Consider the following FNW :

  19. The above Residual network has no augmenting paths, and the flow f shown above is therefore a maximum flow. The value of the maximum flow found is  23

  20. Maximum Bipartite Matching : Let there is an Undirected Graph : G = (V,E) A matching is a subset of edges M  E such that for all vertices v ∊ V, at most one edge of M is incident on v. A vertex v ∊ V is matched by the matching M, if some edge in M is incident on v, otherwise, v is unmatched. A maximum matching is a matching of maximum cardinality, i.e., a matching M such that for any matching M’, it is |M|  |M’|.

  21. Let the vertex set V is partitioned in to L and R : V = L U R where L and R are disjoint and all edges in E are in either L or R. Ex-6 : Let there are L machines. Let there are R tasks. Here the edge (u,v) in E is to mean that a particular machine u ∊ L is capable of performing a particular task v ∊ R. A maximum matching provides work for as many machines as possible.

  22. Finding a Maximum Bipartite Matching : Let there is a Bipartite Graph G = (V, E) . The Ford-Fulkerson method can be used here. Construct a FNW, G’ = (V’,E’) , where V’ = V U {s,t} E’ = E U all edges from s to L U all edges from R to t. Here, E’ = { (s,u) : u ∊ L } U {(u,v) : (u,v) ∊ E } U {(v,t) ∊ R }

  23. 6. Max Flow - Min Cut Theorem : Ex-7 : Consider the following Graph :

  24. A cut is a node partition (S,T) such that s is in S, and t is in T. capacity(S,T) = sum of weights of edges leaving S.

  25. Consider the above Original Graph. Find its Max-Flow : The Max-Flow of the above Graph is : 28 Max-Flow Min-Cut Theorem : The value of the max flow is equal to the capacity of the min cut. i.e., Max-Flow = Min-Cut * * * * *

More Related