You are on page 1of 4

Ex 14: Intercom System Implementation with Minimal Spanning Tree

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.

2. Create Edge Class:


Represent an edge between two departments with a weight (wiring distance).

3. Define find Function:


Input: parent - a dictionary representing the parent of each department, i - the department to
find.
Output: The root of the set to which i belongs.
Recursively find the root of the set using the parent dictionary.

4. Define union Function:


Input: parent - a dictionary representing the parent of each department, rank - a dictionary
representing the rank of each department, x and y - the departments to union.
Perform union operation by rank to merge the sets to which x and y belong.

5. Define kruskal_mst Function:


Input: departments - a list of all departments, edges - a list of edges representing wiring
distances.
Output: List of edges in the Minimal Spanning Tree (MST).
Sort edges in ascending order based on weights.
Initialize parent and rank dictionaries for each department.
Iterate through sorted edges:
For each edge, find the roots of the sets to which its departments belong.
If the roots are different, add the edge to the MST and perform union operation.
Return the MST edges.
6. Example Usage in __main__:
Create instances of Department and Edge for each department and edge.
Call kruskal_mst to find the MST.
Establish intercom connections based on the MST edges.
Print the intercom connections for each department.

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

def find(parent, i):


if parent[i] == i:
return i
return find(parent, parent[i])

def union(parent, rank, x, y):


root_x = find(parent, x)
root_y = find(parent, y)

if rank[root_x] < rank[root_y]:


parent[root_x] = root_y
elif rank[root_x] > rank[root_y]:
parent[root_y] = root_x
else:
parent[root_y] = root_x
rank[root_x] += 1

def kruskal_mst(departments, edges):


edges.sort(key=lambda x: x.weight)
parent = {department: department for department in departments}
rank = {department: 0 for department in departments}
mst_edges = []

for edge in edges:


root1 = find(parent, edge.department1)
root2 = find(parent, edge.department2)

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")

# Create edges with weights representing wiring distance


edge1 = Edge(department_A, department_B, 2)
edge2 = Edge(department_A, department_C, 1)
edge3 = Edge(department_B, department_C, 3)
edge4 = Edge(department_B, department_D, 4)
edge5 = Edge(department_C, department_D, 5)

# Create a list of all departments and edges


all_departments = [department_A, department_B, department_C, department_D]
all_edges = [edge1, edge2, edge3, edge4, edge5]

# Find Minimal Spanning Tree using Kruskal's algorithm


mst_edges = kruskal_mst(all_departments, all_edges)

# Implement intercom connections based on the MST edges


for edge in mst_edges:
edge.department1.connected_departments.append(edge.department2)
edge.department2.connected_departments.append(edge.department1)

# Print intercom connections


for department in all_departments:
connected_departments = ", ".join([connected.name for connected in
department.connected_departments])
print(f"{department.name} is connected to: {connected_departments}")

OUTPUT:
A is connected to: C, B
B is connected to: A, D
C is connected to: A
D is connected to: B

You might also like