Complete the code to initialize the distance array with infinity values.
int[] dist = new int[V]; for (int i = 0; i < V; i++) { dist[i] = [1]; }
We use int.MaxValue to represent infinity for initial distances.
Complete the code to set the source vertex distance to zero.
dist[[1]] = 0;
The source vertex distance is set to zero because the shortest path from source to itself is zero.
Fix the error in the loop that relaxes edges after topological sorting.
foreach (int u in topoOrder) { if (dist[u] != [1]) { foreach (var edge in graph[u]) { int v = edge.Item1; int weight = edge.Item2; if (dist[v] > dist[u] + weight) { dist[v] = dist[u] + weight; } } } }
We check if the distance is not infinity before relaxing edges to avoid invalid calculations.
Fill both blanks to correctly perform relaxation of edges in the shortest path algorithm.
foreach (var edge in graph[[1]]) { int v = edge.[2]; int weight = edge.Item2; if (dist[v] > dist[u] + weight) { dist[v] = dist[u] + weight; } }
The variable u is the current vertex, and Item1 is the adjacent vertex in the edge tuple.
Fill all three blanks to create a dictionary of shortest distances for vertices reachable from source.
var shortestPaths = new Dictionary<int, int> {
{ [1], [2] }
};
for (int i = 0; i < V; i++) {
if (dist[i] != [3] && i != [1]) {
shortestPaths[i] = dist[i];
}
}We start the dictionary with the source vertex and its distance zero, and add other reachable vertices with distances not equal to infinity.
