Why Priority Queue in Dijkstra?

Shortest Path Algorithms DSA practice problem on Onlearn.

Difficulty: medium.

Topics: Finding the shortest distance from a source to all vertices in a weighted, undirected, and connected graph using Dijkstra's Algorithm, Graph, Weighted Graph, Undirected Graph, Adjacency List, Heap/Priority Queue, Algorithm, Time Complexity, Space Complexity, Distance Calculation, Graph Traversal, Dijkstra's Algorithm, graph representation, priority queue, graph algorithms, visited tracking, dijkstra's algorithm, graph traversal, time complexity analysis.

Given a weighted, undirected, and connected graph consisting of V vertices and an adjacency list adj where adj[i] contains lists of two integers. The first integer j denotes an edge between i and j, and the second integer corresponds to the weight of that edge. You are given a source vertex S. Your task is to find the shortest distance from the source vertex S to all other vertices in the graph. You should return a list of integers, where the i th integer denotes the shortest distance between node i and the source vertex S. Note: The graph does not contain any negative weight cycles. All edge weights are non negative. Example 1: Example 2: