- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- 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 if an Edge is a Part of a Minimum Spanning Tree in Python

Suppose we have a 2D matrix named 'edges', that represents an undirected graph. Every item in the matrix 'edges' represents an edge and is of the form (u, v, w). This means nodes u and v are connected and the edge has the weight w. We also have integers a and b, that represent an edge (a,b). We have to find out if the edge (a, b) is part of a minimum spanning tree.

**Note** − the graph has to be connected and the edge (a, b) exists in the graph.

So, if the input is like edges =

[[0, 2, 100], [1, 2, 200], [1, 3, 100], [2, 3, 300]], a = 0 b = 2,

then the output will be True.

To solve this, we will follow these steps −

Define a function findPath() . This will take edges, a, b

if a is same as b, then

return True

if edges is empty, then

return False

for each x in edges, do

if x[2] is same as -1, then

continue the iteration

new_a := -1

if x[0] is same as a, then

new_a := x[1]

otherwise when x[1] is same as a, then

new_a := x[0]

if new_a is not same as -1, then

delete x from edges

if findPath(edges, new_a, b) is non-zero, then

return True

insert x at the end of edges

return False

Now from the main function, do the following −

weight := edge weights of edge x from input array ‘edges’, if

((x[0] is same as a and x[1] is same as b) or(x[1] is same as a and x[0] is same as b))

edges := [edge x from input array edges if x[2] <weight]

return not findPath(edges, a, b)

## Example

Let us see the following implementation to get a better understanding −

class Solution: def findPath(self, edges, a, b): if a == b: return True if not edges: return False for x in edges: if x[2] == -1: continue new_a = -1 if x[0] == a: new_a = x[1] elif x[1] == a: new_a = x[0] if new_a != -1: edges.remove(x) if self.findPath(edges, new_a, b): return True edges.append(x) return False def solve(self, edges, a, b): weight = next(x for x in edges if (x[0] == a and x[1] == b) or (x[1] == a and x[0] == b))[ 2 ] edges = [x for x in edges if x[2] < weight] return not self.findPath(edges, a, b) ob = Solution() print(ob.solve([ [0, 2, 100], [1, 2, 200], [1, 3, 100], [2, 3, 300] ], 0, 2))

## Input

[ [0, 2, 100], [1, 2, 200], [1, 3, 100], [2, 3, 300] ], 0, 2

## Output

True

- Related Articles
- Program to find out if a BST is present in a given binary tree in Python
- Kruskal’s Minimum Spanning Tree Algorithm
- Prim’s Minimum Spanning Tree Algorithm
- Minimum Spanning Tree in Data Structures
- Minimum spanning tree (MST) in Javascript
- Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph in C++
- Program to find out if a linked list is present in a given binary tree in Python
- Kruskal’s (Minimum Spanning Tree) MST Algorithm
- Prim’s (Minimum Spanning Tree) MST Algorithm
- Program to Find Out the Minimum Parsing Tree in C++
- Kruskal’s Minimum Spanning Tree using STL in C++
- Program to Find Out the Special Nodes in a Tree in Python
- Program to find out the lowest common ancestor of a binary tree using Python
- Program to find out the minimum size of the largest clique in a graph (Python)
- Program to find out distance between two nodes in a binary tree in Python