330 likes | 498 Views
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,
E N D
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, 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.
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
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.
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
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
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.
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)
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) ]
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
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
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
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
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
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.
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 }
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)
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
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’|.
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.
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 }
6. Max Flow - Min Cut Theorem : Ex-7 : Consider the following Graph :
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.
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 * * * * *