using System; using System.Collections.Generic; class Edge { public int Src, Dest, Weight; public Edge(int s, int d, int w) { Src = s; Dest = d; Weight = w; } } class Graph { public int V, E; public List<Edge> edges; public Graph(int v, int e) { V = v; E = e; edges = new List<Edge>(); } public int[] BellmanFord(int src) { int[] dist = new int[V]; for (int i = 0; i < V; i++) dist[i] = int.MaxValue; dist[src] = 0; for (int i = 1; i <= V - 1; i++) { foreach (var edge in edges) { int u = edge.Src, v = edge.Dest, w = edge.Weight; if (dist[u] != int.MaxValue && dist[u] + w < dist[v]) { dist[v] = dist[u] + w; } } } return dist; } } class Program { static void Main() { Graph g = new Graph(5, 8); g.edges.Add(new Edge(0, 1, -1)); g.edges.Add(new Edge(0, 2, 4)); g.edges.Add(new Edge(1, 2, 3)); g.edges.Add(new Edge(1, 3, 2)); g.edges.Add(new Edge(1, 4, 2)); g.edges.Add(new Edge(3, 2, 5)); g.edges.Add(new Edge(3, 1, 1)); g.edges.Add(new Edge(4, 3, -3)); int[] dist = g.BellmanFord(0); Console.WriteLine(string.Join(",", dist)); } }
The Bellman-Ford algorithm relaxes edges repeatedly. After 4 iterations (V-1), the shortest distances from source 0 to all vertices are [0, -1, 2, -2, 1].
If after V-1 iterations, any edge can still reduce the distance, it means a cycle with negative total weight exists.
for (int i = 1; i <= V; i++) { foreach (var edge in edges) { int u = edge.Src, v = edge.Dest, w = edge.Weight; if (dist[u] != int.MaxValue && dist[u] + w < dist[v]) { dist[v] = dist[u] + w; } } }
The Bellman-Ford algorithm requires relaxing edges exactly V-1 times. Running V times is unnecessary and may cause incorrect behavior.
using System; using System.Collections.Generic; class Edge { public int Src, Dest, Weight; public Edge(int s, int d, int w) { Src = s; Dest = d; Weight = w; } } class Graph { public int V, E; public List<Edge> edges; public Graph(int v, int e) { V = v; E = e; edges = new List<Edge>(); } public bool BellmanFord(int src, out int[] dist) { dist = new int[V]; for (int i = 0; i < V; i++) dist[i] = int.MaxValue; dist[src] = 0; for (int i = 1; i <= V - 1; i++) { foreach (var edge in edges) { int u = edge.Src, v = edge.Dest, w = edge.Weight; if (dist[u] != int.MaxValue && dist[u] + w < dist[v]) { dist[v] = dist[u] + w; } } } foreach (var edge in edges) { int u = edge.Src, v = edge.Dest, w = edge.Weight; if (dist[u] != int.MaxValue && dist[u] + w < dist[v]) { return false; // Negative cycle detected } } return true; } } class Program { static void Main() { Graph g = new Graph(3, 3); g.edges.Add(new Edge(0, 1, 1)); g.edges.Add(new Edge(1, 2, -1)); g.edges.Add(new Edge(2, 0, -1)); bool noNegativeCycle = g.BellmanFord(0, out int[] dist); Console.WriteLine(noNegativeCycle ? string.Join(",", dist) : "Negative cycle detected"); } }
The algorithm detects the negative cycle and returns false, printing "Negative cycle detected".
Dijkstra's algorithm assumes all edge weights are non-negative. It fails or gives incorrect results if negative edges exist. Bellman-Ford handles negative weights and detects negative cycles.
