Professional Documents
Culture Documents
Ex 14
Ex 14
By applying the minimal spanning tree technique, you can implement an intercom
system to connect all the departments in your college with the least amount of wiring.
ALGORITHM: Kruskal's algorithm
1. Create Department Class:
Represent a department with a name and a list of connected departments.
PROGRAM:
class Department:
def __init__(self, name):
self.name = name
self.connected_departments = []
class Edge:
def __init__(self, department1, department2, weight):
self.department1 = department1
self.department2 = department2
self.weight = weight
if root1 != root2:
mst_edges.append(edge)
union(parent, rank, root1, root2)
return mst_edges
# Example Usage:
if __name__ == "__main__":
# Create Department objects for each department
department_A = Department("A")
department_B = Department("B")
department_C = Department("C")
department_D = Department("D")
OUTPUT:
A is connected to: C, B
B is connected to: A, D
C is connected to: A
D is connected to: B