-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBellmanFord.java
More file actions
72 lines (56 loc) · 2.26 KB
/
BellmanFord.java
File metadata and controls
72 lines (56 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.mycompany.algorithm_final_project;
import java.util.Arrays;
import java.util.Scanner;
/**
*
* @author israkkayumchowdhury
*/
public class BellmanFord {
public static int[] bellmanFord(int numVertices, int numEdges, int[][] edges, int source) {
int[] distances = new int[numVertices+1];
Arrays.fill(distances, Integer.MAX_VALUE);
distances[source] = 0;
for (int i = 1; i < numVertices; i++) {
for (int j = 0; j < numEdges; j++) {
int u = edges[j][0];
int v = edges[j][1];
int weight = edges[j][2];
if (distances[u] != Integer.MAX_VALUE && distances[u] + weight < distances[v]) {
distances[v] = distances[u] + weight;
}
}
}
for (int j = 0; j < numEdges; j++) {
int u = edges[j][0];
int v = edges[j][1];
int weight = edges[j][2];
if (distances[u] != Integer.MAX_VALUE && distances[u] + weight < distances[v]) {
System.out.println(" Negative cycle detected. The graph contains negative-weight cycles.");
return null;
}
}
return distances;
}
public void main_func() {
Scanner s = new Scanner(System.in);
System.out.print(" Enter the number of vertices in the graph: ");
int numVertices = s.nextInt();
System.out.print(" Enter the number of edges in the graph: ");
int numEdges = s.nextInt();
int[][] edges = new int[numEdges+1][3];
System.out.println(" Enter the edges in the following format (source, destination, weight):");
for (int i = 0; i < numEdges; i++) {
System.out.print("Edge " + (i + 1) + ": ");
edges[i][0] = s.nextInt();
edges[i][1] = s.nextInt();
edges[i][2] = s.nextInt();
}
System.out.print(" Enter the source vertex: ");
int source = s.nextInt();
int[] distances = bellmanFord(numVertices, numEdges, edges, source);
System.out.println(" Shortest distances from source vertex " + source + ":");
for (int i = 0; i < numVertices; i++) {
System.out.println("Vertex " + i + ": " + distances[i]);
}
}
}