
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find out the sum of minimum cost within a graph among all vertices in Python
Suppose there is a weighted graph with n vertices and m edges. The edges have the weights in powers of 2. Any vertex can be reached from any vertex in the graph, and the cost of travel will be the addition of all the edge weights in the graph. We shall have to determine the sum of minimum cost between each pair of vertices.
So, if the input is like
and number of vertices (n) = 6; then the output will be 2696.
The sum of all the distances is 2696.
To solve this, we will follow these steps −
- Define a function par_finder() . This will take i, par
- if par[i] is same as -1, then
- return i
- res := par_finder(par[i], par)
- par[i] := res
- return res
- if par[i] is same as -1, then
- Define a function helper() . This will take i, par, w, G, n
- child := 1
- for each item in G[i], do
- if item[0] is same as par, then
- go for next iteration
- otherwise,
- child := child + helper(item[0], i, item[1], G, n)
- if par is not same as -1, then
- ans := ans + child * (n - child) *(1 * 2^w)
- return child
- if item[0] is same as par, then
- G := a new list containing n + 1 other lists
- edges := a new list
- for each item in roads, do
- u := item[0]
- v := item[1]
- w := item[2]
- insert (u-1, v-1, w) at the end of edges
- sort the list edges by the edge weights
- par := a new list of size n + 1 initialized with -1
- r_edge := a new list
- for each i in edges, do
- if par_finder(i[0], par) is same as par_finder(i[1], par), then
- go for next iteration
- otherwise,
- insert i at the end of r_edge
- insert pair (i[1],i[2]) at the end of G[i[0]]
- insert pair (i[0],i[2]) at the end of G[i[1]]
- par[par_finder(i[0], par)] := par_finder(i[1], par)
- if par_finder(i[0], par) is same as par_finder(i[1], par), then
- ans := 0
- helper(0, -1, 0, G, n)
- return ans
Example
Let us see the following implementation to get better understanding −
def par_finder(i, par) : if par[i] == -1 : return i res = par_finder(par[i], par) par[i] = res return res def helper(i, par, w, G, n) : global ans child = 1 for item in G[i] : if item[0] == par : continue else : child += helper(item[0],i,item[1], G, n) if par != -1 : ans += child * (n - child) * (1 << w) return child def solve(n, roads): global ans G = [[] for i in range(n + 1)] edges = [] for item in roads : u,v,w = map(int, item) edges.append((u-1, v-1, w)) edges = sorted(edges,key = lambda item : item[2]) par = [-1 for i in range(n + 1)] r_edge = [] for i in edges : if par_finder(i[0], par) == par_finder(i[1], par) : continue else : r_edge.append(i) G[i[0]].append((i[1],i[2])) G[i[1]].append((i[0],i[2])) par[par_finder(i[0], par)] = par_finder(i[1], par) ans = 0 helper(0, -1, 0, G, n) return ans print(solve(6, [(1,4,8), (2,4,4), (3,4,4), (3,4,2), (5,3,8), (6,3,2)]))
Input
6, [(1,4,8), (2,4,4), (3,4,4), (3,4,2), (5,3,8), (6,3,2)]
Output
2696
- Related Articles
- Program to Find Out the Minimum Cost Possible from Weighted Graph in Python
- Program to Find Out the Minimum Cost to Purchase All in Python
- Program to find out the path between two vertices in a graph that has the minimum penalty (Python)
- C++ Program to find out the super vertices in a graph
- Program to find out the minimum size of the largest clique in a graph (Python)
- Program to find minimum cost to connect all points in Python
- Program to find minimum number of vertices to reach all nodes using Python
- C++ Program to find out the sum of shortest cost paths for all given triplets
- Program to find out the minimum path to deliver all letters in Python
- C++ program to find out the maximum sum of a minimally connected graph
- Program to find out if a vertex in an undirected graph has a lesser cost path in Python
- Program to find minimum cost to cut a stick in Python
- Program to find out the minimum value from sum of node values of sub-trees in Python
- C++ program to find out the shortest cost path in a given graph for q queries
- Program to find minimum cost to merge stones in Python

Advertisements