0% found this document useful (0 votes)
5 views8 pages

Day 19 Bellman Ford Algorithm

The Bellman-Ford algorithm finds the shortest paths from a source vertex to all other vertices in a graph, even with negative weight edges. It initializes distances, relaxes edges multiple times, and checks for negative cycles. If a negative cycle exists or if there is no path to a destination, it outputs -1; otherwise, it prints the shortest paths.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Day 19 Bellman Ford Algorithm

The Bellman-Ford algorithm finds the shortest paths from a source vertex to all other vertices in a graph, even with negative weight edges. It initializes distances, relaxes edges multiple times, and checks for negative cycles. If a negative cycle exists or if there is no path to a destination, it outputs -1; otherwise, it prints the shortest paths.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Bellman-Ford Algorithm

Bellman-Ford Algorithm
Given a graph and a source vertex src(0) in graph, find shortest paths from src to all vertices in
the given graph. The graph may contain negative weight edges.
If the graph contains a negative cycle, print -1.
If there is no path to a destination, print -1 there.
Algorithm
• Initialize distance array dist[] for each vertex ‘v‘ as dist[v] = INFINITY.
• Assume any vertex (let’s say ‘0’) as source and assign dist = 0.
• Relax all the edges(u,v,weight) N-1 times as per the below condition:
• dist[v] = minimum(dist[v], distance[u] + weight)
• Now, Relax all the edges one more time i.e. the Nth time and based on the below two cases
we can detect the negative cycle:
• Case 1 (Negative cycle exists): For any edge(u, v, weight), if dist[u] + weight <
dist[v]
• Case 2 (No Negative cycle) : case 1 fails for all the edges.
Bellman-Ford Algorithm
Input Format:
First line - n and m, the number of nodes and edges
Then there are m lines each containing u, v, and w.
u and v are the nodes and w is the weight of the edge.
Output Format:
Print shortest paths from src to all vertices in the given graph. If there is no path to a
destination, print -1 there. If the graph contains a negative cycle, print -1.

Sample Input:
58
0 1 -1
024
123
132
142
325
311
4 3 -3
Sample Output:
0 -1 2 -2 1
1 import java.util.*;
2 class Main {
3 class Edge {
4 int src, dest, weight;
5 Edge(){
6 src = dest = weight = 0;
7 }
8 };
9 int V, E;
10 Edge edge[];
11 Main(int v, int e){
12 V = v;
13 E = e;
14 edge = new Edge[e];
15 for (int i = 0; i < e; ++i)
16 edge[i] = new Edge();
17 }
18 void BellmanFord(Main graph, int src){
19 int V = graph.V, E = graph.E;
20 int dist[] = new int[V];
21
22
23 for (int i = 0; i < V; ++i)
24 dist[i] = Integer.MAX_VALUE;
25 dist[src] = 0;
26 for (int i = 1; i < V; ++i) {
27 for (int j = 0; j < E; ++j) {
28 int u = graph.edge[j].src;
29 int v = graph.edge[j].dest;
30 int weight = graph.edge[j].weight;
31 if (dist[u] != Integer.MAX_VALUE && dist[u] +
32 weight < dist[v])
33 dist[v] = dist[u] + weight;
34 }
35 }
36 for (int j = 0; j < E; ++j) {
37 int u = graph.edge[j].src;
38 int v = graph.edge[j].dest;
39 int weight = graph.edge[j].weight;
40 if(dist[u]!=Integer.MAX_VALUE && dist[u]
41 +weight<dist[v]){
42 System.out.println(-1);
43 return;
44 }
45 for(int i = 0; i < V; ++i)
46 if(dist[i]!=Integer.MAX_VALUE)
47 System.out.print(dist[i]+" ");
48 else
49 System.out.print(-1+" ");
50 }
51 public static void main(String[] args){
52 Scanner sc=new Scanner(System.in);
53 int V = sc.nextInt();
54 int E = sc.nextInt();
55 Main graph = new Main(V,E);
56 for(int i=0;i<E;i++){
57 int u=sc.nextInt();
58 int v=sc.nextInt();
59 int w=sc.nextInt();
60 graph.edge[i].src = u;
61 graph.edge[i].dest = v;
62 graph.edge[i].weight = w;
63 }
64 graph.BellmanFord(graph, 0);
65 }
66 }
THANK YOU

You might also like