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

Assignment Session 13

Uploaded by

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

Assignment Session 13

Uploaded by

nelsonchandra84
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Dynamic Disjoint Set Data Structure for large range values

Nelson Chandra / 20010008

Code :
# Dynamic Disjoint Set Data Structure
# Union-Find
# Dynamic Disjoint Set Data Structure
class DynamicDisjointSetDS:

# Constructor
def __init__(self, n):

# Total number of Vertex in the Graph


self.N = n

# We will add the vertex to the edge


# only when it is asked to i.e. maintain
# a dynamic DS.
self.parent = {}
self.degree = {}

# Get Parent of vertex V


def getParent(self, vertex):

# If the vertex is already inserted


# in the graph
if vertex in self.parent:

if self.parent[vertex] != vertex:
self.parent[vertex] = \
self.getParent(self.parent[vertex])

return self.parent[vertex]

# if the vertex is operated


# for the first time
else:

# insert the vertex and assign


# its parent to itself
self.parent[vertex] = vertex

# Degree of the vertex


self.degree[vertex] = 1

return vertex

# Union by Rank
def Union(self, vertexA, vertexB):

# Parent of Vertex A
x = self.getParent(vertexA)

# Parent of Vertex B
y = self.getParent(vertexB)

# if both have same parent


# Do Nothing
if x == y:
return

# Merging the component


# Assigning the parent of smaller Component
# as the parent of the bigger Component.
if self.degree[x] > self.degree[y]:
self.parent[y] = x
self.degree[x] = (self.degree[x] +
self.degree[y])

else:
self.parent[x] = y
self.degree[y] = (self.degree[y] +
self.degree[x])

# Count total Component in the Graph


def GetTotalComponent(self):

# To count the total Component formed


# from the inserted vertex in the Graph
total = set()

# Iterate through the parent


for itr in self.parent:
# Add the parent of each Vertex
# to the set
total.add(self.getParent(itr))

# Total Component = Total Vertexes -


# Number of Vertex in the parent +
# Number of Component formed from
# the Vertexes inserted in the Graph
return self.N - len(self.parent) + len(total)

# Solve
def Solve(N):
# Declaring the Dynamic Disjoint Set DS
dsu = DynamicDisjointSetDS(N)

# Traversing through the Edges


for i in range(0, 3):

# If the Vertices in the Edges


# have same parent do nothing
if (dsu.getParent(Edges[i][0]) ==
dsu.getParent(Edges[i][1])):
continue

# else Do Union of both the Components.


else:
dsu.Union(Edges[i][0], Edges[i][1])

# Get total Components


print(dsu.GetTotalComponent())

# Driver Code
if __name__ == "__main__":
# Total Number of Vertexes
N = 5
Edges = [[1, 2], [2, 3], [4, 3]]

# Solve
Solve(N)

Output : 2

Explanation :

Disjoint Set data structure is used to track a set of elements that are partitioned into
discontinuous (non-overlapping) subsets. This data structure is basically helpful in situations
where we cannot simply use arrays to create intermittent sets due to large inputs of the order of
10^9. To illustrate this point, consider the following problem. We need to find the total number of
connected components on the Graph when the Total Sum of Vertices can be up to 10^9.
The idea to solve this problem is, we will maintain two hash tables. One for parents and the
other for degrees. Parent[V] will give the parent of the component which the Vertex V is part of
and Degree will give the number of vertices in that component. Initially, both Parent and Degree
will be empty. We will keep inserting vertices into the maps sequentially.See the code and the
explanation simultaneously for a better understanding.
Below are the methods used in the code to solve the above problem:
1. getParent(V) → Given the parent of the vertex V, this method recursively finds the
parent of the vertex V (see code), meanwhile we assign all the vertices in that
component to have the same parent. In a disjoint data structure all the vertices in the
same component have the same parent.
2. union() → When building an edge with two vertices of different components, we use the
Union() function to link them together. The parent of the component generated by uniting
the two components will be the parent of the component with the most vertices prior to
the union. The new component's degree is updated correspondingly.
3. getTotalComponent() → Vertex in the same component will have the same parent.

Reference: https://www.geeksforgeeks.org/disjoint-set-data-structures/

You might also like