using System; using System.Collections.Generic; class Program { static void Main() { int V = 6; List<(int, int)>[] graph = new List<(int, int)>[V]; for (int i = 0; i < V; i++) graph[i] = new List<(int, int)>(); graph[0].Add((1, 5)); graph[0].Add((2, 3)); graph[1].Add((3, 6)); graph[1].Add((2, 2)); graph[2].Add((4, 4)); graph[2].Add((5, 2)); graph[2].Add((3, 7)); graph[3].Add((4, -1)); graph[4].Add((5, -2)); int[] dist = new int[V]; for (int i = 0; i < V; i++) dist[i] = int.MaxValue; dist[0] = 0; Stack<int> stack = new Stack<int>(); bool[] visited = new bool[V]; void TopoSort(int u) { visited[u] = true; foreach (var (v, w) in graph[u]) { if (!visited[v]) TopoSort(v); } stack.Push(u); } for (int i = 0; i < V; i++) if (!visited[i]) TopoSort(i); while (stack.Count > 0) { int u = stack.Pop(); if (dist[u] != int.MaxValue) { foreach (var (v, w) in graph[u]) { if (dist[u] + w < dist[v]) dist[v] = dist[u] + w; } } } for (int i = 0; i < V; i++) { if (dist[i] == int.MaxValue) Console.Write("INF "); else Console.Write(dist[i] + " "); } } }
The code performs a topological sort and then relaxes edges in order. The shortest distances from node 0 are: 0 (start), 5 (0->1), 3 (0->2), 9 (0->2->3 or 0->1->3), 7 (0->2->4 or 0->2->3->4), 5 (0->2->5 or 0->2->4->5).
Topological order processes nodes only after all nodes leading to them are processed, so shortest distances can be updated correctly without revisiting nodes or cycles.
int V = 4; List<(int, int)>[] graph = new List<(int, int)>[V]; for (int i = 0; i < V; i++) graph[i] = new List<(int, int)>(); graph[0].Add((1, 1)); graph[1].Add((2, 2)); graph[2].Add((3, 3)); int[] dist = new int[V]; for (int i = 0; i < V; i++) dist[i] = int.MaxValue; dist[0] = 0; Stack<int> stack = new Stack<int>(); bool[] visited = new bool[V]; void TopoSort(int u) { visited[u] = true; foreach (var (v, w) in graph[u]) { if (!visited[v]) TopoSort(v); } stack.Push(u); } for (int i = 0; i < V; i++) TopoSort(i); while (stack.Count > 0) { int u = stack.Pop(); if (dist[u] != int.MaxValue) { foreach (var (v, w) in graph[u]) { if (dist[u] + w < dist[v]) dist[v] = dist[u] + w; } } } Console.WriteLine(string.Join(" ", dist));
The code calls TopoSort on every node without checking if it was visited, causing infinite recursion and StackOverflowException.
using System; using System.Collections.Generic; class Program { static void Main() { int V = 5; List<(int, int)>[] graph = new List<(int, int)>[V]; for (int i = 0; i < V; i++) graph[i] = new List<(int, int)>(); graph[0].Add((1, 2)); graph[0].Add((2, 3)); graph[1].Add((3, 4)); graph[2].Add((3, -2)); graph[3].Add((4, 2)); int[] dist = new int[V]; for (int i = 0; i < V; i++) dist[i] = int.MaxValue; dist[0] = 0; Stack<int> stack = new Stack<int>(); bool[] visited = new bool[V]; void TopoSort(int u) { visited[u] = true; foreach (var (v, w) in graph[u]) { if (!visited[v]) TopoSort(v); } stack.Push(u); } for (int i = 0; i < V; i++) if (!visited[i]) TopoSort(i); while (stack.Count > 0) { int u = stack.Pop(); if (dist[u] != int.MaxValue) { foreach (var (v, w) in graph[u]) { if (dist[u] + w < dist[v]) dist[v] = dist[u] + w; } } } for (int i = 0; i < V; i++) { if (dist[i] == int.MaxValue) Console.Write("INF "); else Console.Write(dist[i] + " "); } } }
The shortest path to node 3 is via node 2 with weight 3 + (-2) = 1. Then node 4 is reached from node 3 with +2 = 3.
Dijkstra's algorithm assumes no negative edges because it greedily picks the closest node, which can fail if a shorter path appears later due to negative edges.
