advanced algorithms

33
ADVANCED ALGORITHMS 1 GRAPH ALGORITHMS (UNIT-2)

Upload: blenda

Post on 21-Mar-2016

53 views

Category:

Documents


2 download

DESCRIPTION

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, - PowerPoint PPT Presentation

TRANSCRIPT

ADVANCED ALGORITHMS

ADVANCED ALGORITHMS1GRAPHALGORITHMS(UNIT-2)2The Bellman-Ford Algorithm (BFA)The BFA solves the single-source shortest-pathsproblem 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.3Bellman-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)for each edge (u,v) Eif v.d > u.d + w(u,v)return FALSE8return TRUERELAX (u,v,w)if v.d > u.d + w(u,v)2 v.d = u.d + w(u,v) 3v. = u4Single 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 itsnodes 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.5DAG-SHORTEST-PATHS(G,w,s)Topologically sort the vertices of GINITIALIZE-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

rx

ys

z6r s t x y z

Ex-2 : Draw the Graph and Solve it for the following Weight Table :------------------------------------------------r s s tt x x y y z 5 2 7 -1 -2------------------------------------------------s xx z r t t y t z 6 1 3 4 273. Johnsons 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) arenon-negative, we can find shortest paths between all pairs of vertices by running Dijkstras 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.8JOHNSON(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) 9Let 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.Vfor each vertex v G.V duv = (u,v) + h(v) h(u)return DEx-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) ]10Consider new vertex s.

Draw edges from s to a, b, c, d, e with weights zero.

Draw the Graph G (V,E), whereV = V U (s).and E = E U [(s,a), (s,b), (s,c), (s,d),(s,e)]for G, we have |V| = 5|E| = 9for G, we have |V| = 6|E| = 14And h(a) = (s,a) = 0 h(b) = (s,b) = -1

h(c) = (s,c) = -5 h(d) = (s,d) = 0

h(e) = (s,e) = -411Bellman-Ford Algorithm : TRUEREWEIGHTING : 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) = 0w(b,e) = w(b,e) + h(b) h(e) = 10Similarilyw(c,b) = 0w(d,c) = 0 w(d,a) = 2w(e,d) = 2

w(s,a) = 0w(s,b) = 1 w(s,c) = 5w(s,d) = 0 w(s,e) = 412The Output :

Draw the D-Matrix (5 x 5 ) : (u,v) = duv (u,v)abcde--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- a 01- 32- 4

b30- 41- 1

c74053

d2- 1- 50- 2

e85160134. 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 ssink tA 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 Vv V14Ex-4 : Consider the following Flow Network v1 12v316 20s 4 9 7t 13 4v2 14v4The following is FNW for |f| = 19

v1 12/12v3 11/16 15/20s 1/4 4/9 7/7t 8/13 4/4v2 11/14v415The following is FNW for |f| = 20

v1 12/12v3 8/16 16/20S 4/4 3/9 7/7T 12/13 4/4v2 11/14v4

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.16Residual 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 = 0otherwiseResidual 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 }

17The Ford-Fulkerson Algorithm :

FORD-FULKERSON(G,s,t)

1 for each edge (u,v) E2 (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) E7 (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

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 2324Maximum 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 anymatching M, it is |M| |M|.25Let 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.26Finding 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

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

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