0% found this document useful (0 votes)
30 views

Tutorial 5

The document describes an algorithm for finding the minimum spanning tree (MST) of a graph using Kruskal's algorithm. It defines a DisjointSet class to track connected components in the graph. The runKruskalAlgorithm function takes a graph's edges and number of nodes as input. It sorts the edges by weight and iteratively adds edges to the MST if their endpoints are in different connected components, merging the components, until n-1 edges are added.

Uploaded by

Tony Stark
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Tutorial 5

The document describes an algorithm for finding the minimum spanning tree (MST) of a graph using Kruskal's algorithm. It defines a DisjointSet class to track connected components in the graph. The runKruskalAlgorithm function takes a graph's edges and number of nodes as input. It sorts the edges by weight and iteratively adds edges to the MST if their endpoints are in different connected components, merging the components, until n-1 edges are added.

Uploaded by

Tony Stark
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Tutorial 5

Solution:- {IN PYTHON}


# A class to represent a disjoint set
class DisjointSet:
    parent = {}
 
    # perform MakeSet operation
    def makeSet(self, n):
        # create `n` disjoint sets (one for each vertex)
        for i in range(n):
            self.parent[i] = i
 
    # Find the root of the set in which element `k` belongs
    def find(self, k):
        # if `k` is root
        if self.parent[k] == k:
            return k
 
        # recur for the parent until we find the root
        return self.find(self.parent[k])
 
    # Perform Union of two subsets
    def union(self, a, b):
        # find the root of the sets in which elements `x` and `y` belongs
        x = self.find(a)
        y = self.find(b)
 
        self.parent[x] = y
 
 
# Function to construct MST using Kruskal’s algorithm
def runKruskalAlgorithm(edges, n):
 
    # stores the edges present in MST
    MST = []
 
    # Initialize `DisjointSet` class.
    # Create a singleton set for each element of the universe.
    ds = DisjointSet()
    ds.makeSet(n)
 
    index = 0
 
    # sort edges by increasing weight
    edges.sort(key=lambda x: x[2])
 
    # MST contains exactly `V-1` edges
    while len(MST) != n - 1:
 
        # consider the next edge with minimum weight from the graph
        (src, dest, weight) = edges[index]
        index = index + 1
 
        # find the root of the sets to which two endpoints
        # vertices of the next edge belongs
        x = ds.find(src)
        y = ds.find(dest)
 
        # if both endpoints have different parents, they belong to
        # different connected components and can be included in MST
        if x != y:
            MST.append((src, dest, weight))
            ds.union(x, y)
 
    return MST
 
 
if __name__ == '__main__':
 
    # (u, v, w) triplet represent undirected edge from
    # vertex `u` to vertex `v` having weight `w`
    edges = [
        (0, 1, 7), (1, 2, 8), (0, 3, 5), (1, 3, 9), (1, 4, 7), (2, 4, 5),
        (3, 4, 15), (3, 5, 6), (4, 5, 8), (4, 6, 9), (5, 6, 11)
    ]
 
    # total number of nodes in the graph (labelled from 0 to 6)
    n = 7
 
    # construct graph
    e = runKruskalAlgorithm(edges, n)
 
    print(e)

You might also like