githubEdit

13(July) Shortest Path in Weighted undirected graph

13. Shortest Path in Weighted Undirected Graph

The problem can be found at the following link: Question Linkarrow-up-right

Problem Description

You are given a weighted undirected graph having n vertices numbered from 1 to n and m edges along with their weights. Find the shortest path between the vertex 1 and the vertex n, if there exists a path, and return a list of integers whose first element is the weight of the path, and the rest consist of the nodes on that path. If no path exists, then return a list containing a single element -1.

Examples:

Input:

n = 5, m = 6
edges = [[1, 2, 2], [2, 5, 5], [2, 3, 4], [1, 4, 1], [4, 3, 3], [3, 5, 1]]

Output:

5

Explanation: The shortest path from 1 to 5 is by the path 1 -> 4 -> 3 -> 5 whose weight is 5.

My Approach

  1. Graph Representation:

  • Create a graph using an adjacency list to store the vertices and their respective weights.

  1. Dijkstra's Algorithm:

  • Use Dijkstra's algorithm to find the shortest path from vertex 1 to vertex n.

  • Initialize the distance for the starting vertex 1 as 0 and all other vertices as infinity.

  • Use a priority queue to explore the vertices based on the minimum distance.

  1. Path Reconstruction:

  • Keep track of the parent of each vertex to reconstruct the path once the shortest path to vertex n is found.

  • If the distance to vertex n remains infinity, return [-1] indicating no path exists.

Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(m * log(n)), as Dijkstra's algorithm explores each edge in logarithmic time.

  • Expected Auxiliary Space Complexity: O(n + m), as we store the graph representation and additional data structures for Dijkstra's algorithm.

Code

C++ Code

Java Code

Python Code

Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questionsarrow-up-right. Let’s make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


📍Visitor Count

Last updated