Presentation is loading. Please wait.

Presentation is loading. Please wait.

ADVANCED ALGORITHMS GRAPH ALGORITHMS (UNIT-2).

Similar presentations


Presentation on theme: "ADVANCED ALGORITHMS GRAPH ALGORITHMS (UNIT-2)."— 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) for i = 1 to |V| -1 for each edge (u,v) ∊ E RELAX(u,v,w) 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] RELAX (u, v, w) Ex-1 : Topologically sort the following DAG : t r x y s z

6 Ex-2 : Draw the Graph and Solve it for the following Weight Table :
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 s  x x  z r  t t  y t  z

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 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) = h(b) = δ(s,b) = -1 h(c) = δ(s,c) = 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) = – (-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 Draw the D-Matrix (5 x 5 ) : δ(u,v) = duv
The Output : Draw the D-Matrix (5 x 5 ) : δ(u,v) = duv δ(u,v) a b c d e a b c d e

13 A flow network G = (V,E) is a directed graph in
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
v v3 s t v v4 The following is FNW for |f| = 19 v /12 v3 11/ /20 s / /9 7/7 t 8/ /4 v /14 v4

15 4. The Ford-Fulkerson Method :
The following is FNW for |f| = 20 v /12 v3 8/ /20 S / / /7 T 12/ /4 v /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 : Residual Network : 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 = 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 (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 if (u,v) ∊ E (u,v) .f = (u.v).f + Cf(p) else (v,u).f = (v,u).f - Cf(p)

18 Example-5 : Consider the following FNW :

19

20

21

22

23 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

24 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’|.

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

26 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 }

27

28

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

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

31

32

33 The Max-Flow of the above Graph is : 28
Consider the above Original Graph. Find its Max-Flow : The Max-Flow of the above Graph is : 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 * * * * *


Download ppt "ADVANCED ALGORITHMS GRAPH ALGORITHMS (UNIT-2)."

Similar presentations


Ads by Google