

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find out the minimum cost path between the lowest value vertex to the highest value vertex (Python)
Suppose we are given an undirected, weighted graph and are asked to find out the path with the minimum possible travel cost from a particular node to another particular node. The travel cost is calculated as the following: suppose there is a path between vertex A to vertex C as A-> B-> C. The cost of travel from A to B is 10 and the cost of travel from B to C is 20. The cost of travel from A to C will be (cost of traveling from A to B) + (difference of traveling cost from B to C and the cumulative cost of traveling to node B). So that will translate to 10 + (20 - 10) = 20. We shall have to find out the minimum possible traveling values within the first node to the last node (the node with the lowest value to the node with the highest value) in a given graph.
So, if the input is like
then the output will be 15.
There exist two paths between vertices 1 and 4. The optimal path is 1->2->4, the cost of the path is 10 + (15 - 10) = 15. Otherwise, the path will cost 20.
To solve this, we will follow these steps −
- adjList := a new map containing empty lists
- for each item in edges, do
- u := item[0]
- v := item[1]
- w := item[2]
- insert pair (w,v) at the end of adjList[u]
- insert pair (w,u) at the end of adjList[v]
- q := a new heap
- v_list := a new set
- insert (0,1) at the end of q
- flag := True
- while q is not empty, do
- c := pop smallest item from q
- if c[1] is present in v_list, then
- go for next iteration
- add(c[1]) to v_list
- if c[1] is same as n, then
- flag := False
- return c[0]
- for each u in adjList[c[1]], do
- if u[1] is not present in v_list, then
- out := maximum of (u[0], c[0] ,u[1])
- push out into heap q
- if u[1] is not present in v_list, then
- if flag is True then
- return -1
Example
Let us see the following implementation to get better understanding −
from collections import defaultdict import heapq def solve(n, edges): adjList = defaultdict(list) for item in edges: u, v, w = map(int, item) adjList[u].append((w,v)) adjList[v].append((w,u)) q = [] v_list = set() q.append((0,1)) flag = True while q: c = heapq.heappop(q) if c[1] in v_list: continue v_list.add(c[1]) if c[1] == n: flag = False return c[0] for u in adjList[c[1]]: if u[1] not in v_list: out = (max(u[0],c[0]),u[1]) heapq.heappush(q,out) if flag: return -1 print(solve(4, [(1, 2, 10), (2, 3, 5), (2, 4, 15), (1, 4, 20)]))
Input
4, [(1, 2, 10), (2, 3, 5), (2, 4, 15), (1, 4, 20)]
Output
15
- Related Questions & Answers
- Program to find out if a vertex in an undirected graph has a lesser cost path in Python
- Program to calculate vertex-to-vertex reachablity matrix in Python
- Java Program to get the lowest and highest value in TreeSet
- Program to Find Out the Minimum Cost to Purchase All in Python
- C++ Program to Find the Vertex Connectivity of a Graph
- Program to find out the minimum path to deliver all letters in Python
- Program to Find Out the Minimum Cost Possible from Weighted Graph in Python
- Program to find out the path between two vertices in a graph that has the minimum penalty (Python)
- Finding the path from one vertex to rest using BFS in C++
- MySQL order by from highest to lowest value?
- C++ Program to find out the minimum difference value in n integer pairs
- C++ program to find the vertex, focus and directrix of a parabola
- Java Program to find the vertex, focus and directrix of a parabola
- Program to find out the shortest path to reach the goal in Python
- C++ Program to Implement a Heuristic to Find the Vertex Cover of a Graph