deep a kuni yal 10801934

Download Deep a Kuni Yal 10801934

Post on 18-Jul-2015

62 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

TERM PAPER SUBJECT: (CSE408)TOPIC: - Bellman

Ford

D.O.S-02/04/2012

Submitted to: - Ms. Shivani Malhotra

Submitted by:-

Deepak uniyal

Sec:-RK1R09 Roll no: - A13 Reg. no: - 10801934

ACKNOWLEDGEMENTI take this opportunity to express my acknowledgement and deep sense of gratitude to the following personalities for rendering me value able guidance to me for the successful completion of my TERM PAPER on Bellman Ford

I am thankful to Miss SHIVANI MALHOTRA for assigning me this topic which not only enhances my knowledge in the subject but also helps me to manage my work.

I owe whole heartedly thanks to all those who are directly and indirectly involved in the fulfillment of my term paper.

DEEPAK UNIYAL

INTRODUCTIONThe BellmanFord algorithm computes single-source shortest paths in a weighted digraph. For graphs with only non-negative edge weights, the faster Dijkstra's algorithm also solves the problem. Thus, BellmanFord is used primarily for graphs with negative edge weights. The algorithm is named after its developers, Richard Bellman and Lester Ford, Jr. If a graph contains a "negative cycle", i.e., a cycle whose edges sum to a negative value, then walks of arbitrarily low weight can be constructed, i.e., there may be no shortest path. Bellman-Ford can detect negative cycles and report their existence, but it cannot produce a correct answer if a negative cycle is not reachable from the source. According to Robert Sedgewick, "Negative weights are not merely a mathematical curiosity; [...] [they] arise in a natural way when we reduce other problems to shortest-paths problems". Let G be a graph containing a negative cycle. One NP-Complete variant of the shortest-path problem asks for the shortest path in G (containing a negative cycle) such that no edge is repeated. Sedgewick gives areduction from the Hamiltonian path problem to this variant of the problem. Bellman-Ford algorithm is a graph search algorithm that can handle edges with negative weight of traversal (this means, better to pass through such an edge than not to pass). Edges with negative weights may arise after transforming many real world problems into graph search algorithm. Bellman-Ford algorithm runs in O(V E) (number of nodes multiplied by the number of vertices) time. Hence it is less efficient than Dijkstra algorithm and only should be used when graph edges may have negative weights (Dijkstra algorithm cannot handle such graphs). Definition: An efficient algorithm to solve the single-source shortest-path problem. Weights may be negative. The algorithm initializes the distance to the source vertex to 0 and all other vertices to . It then does V-1 passes (V is the number of vertices) over all edges relaxing, or updating, the distance to the destination of each edge. Finally it checks each edge again to detect negative weight cycles, in which case it returns false. The time complexity is O(VE), where E is the number of edges. Also known as Ford-Bellman. Bellman-Ford algorithm solves the single-source shortest-path problem in the general case in which edges of a given digraph can have negative weight as long as G contains no negative cycles. This algorithm, like Dijkstra's algorithm uses the notion of edge relaxation but does not use with greedy method. Again, it uses d[u] as an upper bound on the distance d[u, v] from u to v. The algorithm progressively decreases an estimate d[v] on the weight of the shortest path from the source vertex s to each vertex v in V until it achieve the actual shortest-path. The algorithm returns Boolean TRUE if the given digraph contains no negative cycles that are reachable from source vertex s otherwise it returns Boolean FALSE.

BELLMAN-FORD (G, w, s)1. INITIALIZE-SINGLE-SOURCE (G, s) 2. for each vertex i = 1 to V[G] - 1 do 3. for each edge (u, v) in E[G] do 4. RELAX (u, v, w) 5. For each edge (u, v) in E[G] do 6. if d[u] + w(u, v) < d[v] then

7. return FALSE 8. return TRUE

Analysis

The initialization in line 1 takes (v) time For loop of lines 2-4 takes O(E) time and For-loop of line 5-7 takes O(E) time.

Thus, the Bellman-Ford algorithm runs in O(E) time.

AlgorithmBellmanFord is in its basic structure very similar to Dijkstra's algorithm, but instead of greedily selecting the minimum-weight node not yet processed to relax, it simply relaxes all the edges, and does this |V | 1 times, where |V | is the number of vertices in the graph. The repetitions allow minimum distances to accurately propagate throughout the graph, since, in the absence of negative cycles, the shortest path can only visit each node at most once. Unlike the greedy approach, which depends on certain structural assumptions derived from positive weights, this straightforward approach extends to the general case. BellmanFord runs in O(|V||E|) time, where |V| and |E| are the number of vertices and edges respectively. procedure BellmanFord(list vertices, list edges, vertex source) // This implementation takes in a graph, represented as lists of vertices // and edges, and modifies the vertices so that their distance and // predecessor attributes store the shortest paths. // Step 1: initialize graph for each vertex v in vertices: if v is source then v.distance := 0 else v.distance := infinity

v.predecessor := null // Step 2: relax edges repeatedly for i from 1 to size(vertices)-1: for each edge uv in edges: // uv is the edge from u to v u := uv.source v := uv.destination if u.distance + uv.weight < v.distance: v.distance := u.distance + uv.weight v.predecessor := u // Step 3: check for negative-weight cycles for each edge uv in edges: u := uv.source v := uv.destination if u.distance + uv.weight < v.distance: error "Graph contains a negative-weight cycle"

PROOF OF CORRECTNESSThe correctness of the algorithm can be shown by induction. The precise statement shown by induction is: Lemma. After i repetitions of for cycle: If Distance(u) is not infinity, it is equal to the length of some path from s to u; If there is a path from s to u with at most i edges, then Distance(u) is at most the length of the shortest path from s to u with at most i edges.

Proof. For the base case of induction, consider i=0 and the moment before for cycle is executed for the first time. Then, for the source vertex, source.distance = 0, which is correct. For other vertices u, u.distance = infinity, which is also correct because there is no path from source to u with 0 edges.

For the inductive case, we first prove the first part. Consider a moment when a vertex's distance is updated by v.distance := u.distance + uv.weight. By inductive assumption, u.distance is the length of some path from source to u. Then u.distance + uv.weight is the length of the path from source to v that follows the path from source tou and then goes to v. For the second part, consider the shortest path from source to u with at most i edges. Let v be the last vertex before u on this path. Then, the part of the path from source to v is the shortest path from source to v with at most i-1 edges. By inductive assumption, v.distance after i1 cycles is at most the length of this path. Therefore, uv.weight + v.distanceis at most the length of the path from s to u. In the ith cycle, u.distance gets compared with uv.weight + v.distance, and is set equal to it if uv.weight + v.distance was smaller. Therefore, after i cycles, u.distance is at most the length of the shortest path from source to u that uses at most i edges. If there are no negative-weight cycles, then every shortest path visits each vertex at most once, so at step 3 no further improvements can be made. Conversely, suppose no improvement can be made. Then for any cycle with vertices v[0], ..., v[k1], v[i].distance 4 -> 3 -> 2. The length of the path is 7 3 2 = 2, which is less than 6. BTW, you dont need negative edge weights to get such a situation, but they do clarify the problem. This also suggests a property of shortest path algorithms: to find the shortest path form x to y, you need to know, beforehand, the shortest paths to ys neighbours. For this, you need to know the paths to ys neighbours neighbours In the end, you must calculate the shortest path to the connected component of the graph in which x and y are found. That said, you usually calculate the shortest path to all nodes and then pick the ones youre intrested in.

The Algorithm

The Bellman-Ford algorithm is one of the classic solutions to this problem. It calculates the shortest path to all nodes in the graph from a single source. The basic idea is simple: Start by considering that the shortest path to all nodes, less the source, is infinity. Mark the length of the path to the source as 0: Take every edge and try to relax it: Relaxing an edge means checking to see if the path to the node the edge is pointing to cant be shortened, and if so, doing it. In the above graph, by checking the edge 1 -> 2 of length 6, you find that the length of the shortest path to node 1 plus the length of the edge 1 -> 2 is less then infinity. So, you replace infinity in node 2 with 6. The same can be said for edge 1 -> 4 of length 7. Its also worth noting that, practically, you cant relax the edges whose start has the shortest path of length infinity to it. Now, you apply the previous step n 1 times, where n is the number of nodes in the graph. In this example, you have to apply it 4 times (thats 3 more times). Thats it, heres the algorithm in a condensed form: void bellman_ford(int s) { int i, j; for (i = 0; i < n; ++i) d[i] = INFINITY; d[s] = 0; for (i = 0; i < n - 1; ++i) for (j = 0; j < e; ++j) if (d[edges[j].u] + edges[j].w < d[edges[j].v]) d[edges[j].v] = d[edges[j].u] + edges[j].w; }

Here, d[i] is the shortest path to node i, e is the number of edges and edges[i] is the i-th edge. It ma