What is the shortest distance array output after running Dijkstra's algorithm from source node 0 on the given graph?
int[][] graph = { {0, 4, 0, 0, 0, 0, 0, 8, 0}, {4, 0, 8, 0, 0, 0, 0, 11, 0}, {0, 8, 0, 7, 0, 4, 0, 0, 2}, {0, 0, 7, 0, 9, 14, 0, 0, 0}, {0, 0, 0, 9, 0, 10, 0, 0, 0}, {0, 0, 4, 14, 10, 0, 2, 0, 0}, {0, 0, 0, 0, 0, 2, 0, 1, 6}, {8, 11, 0, 0, 0, 0, 1, 0, 7}, {0, 0, 2, 0, 0, 0, 6, 7, 0} }; int source = 0; int[] dist = dijkstra(graph, source); System.out.println(Arrays.toString(dist));
Trace the shortest paths step-by-step from the source node 0.
Dijkstra's algorithm finds the shortest path from source 0 to all other nodes. The distances array shows the minimum distances after processing all nodes.
What is the main purpose of using a priority queue in Dijkstra's algorithm?
Think about how the algorithm decides which node to explore next.
The priority queue helps pick the node with the smallest current distance, ensuring the shortest paths are found efficiently.
Given the following snippet from a Dijkstra's algorithm implementation, what error will occur when running it?
int[] dist = new int[V]; boolean[] visited = new boolean[V]; for (int i = 0; i < V; i++) { dist[i] = Integer.MAX_VALUE; } dist[source] = 0; for (int count = 0; count < V - 1; count++) { int u = minDistance(dist, visited); visited[u] = true; for (int v = 0; v < V; v++) { if (!visited[v] && graph[u][v] != 0 && dist[u] != Integer.MAX_VALUE && dist[u] + graph[u][v] < dist[v]) { dist[v] = dist[u] + graph[u][v]; } } } // Missing minDistance method implementation
Check if all methods used are defined.
The code calls minDistance but does not define it, causing a compilation error.
Given the weighted graph below, what is the shortest path distance from node 2 to node 6?
Graph edges with weights: (2-3:7), (2-5:4), (3-4:9), (5-6:2), (6-7:1)
Consider direct and indirect paths and sum weights.
The shortest path from 2 to 6 is 2->5->6 with total weight 6.
Why does Dijkstra's algorithm not work correctly if the graph contains edges with negative weights?
Think about the algorithm's assumption about distances once visited.
Dijkstra's algorithm assumes that once a node is processed, its shortest distance is final. Negative edges can later reduce that distance, breaking this assumption.
