Skip to content

Commit

Permalink
added disjoint set application problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudherabelly committed Oct 16, 2018
1 parent 7fd9150 commit af701a6
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
You are given number of nodes N, and number of edges M of a undirected connected graph.
After adding each edge, print the size of all the connected components (in increasing order).
Input:
First line contains two integers N and M ,number of nodes and number of edges.
Next M lines contains two integers each,
X and Y, denoting that there is an edge between X and Y.
Output:
For each edge, print the size of all the connected components (in increasing order) after adding that edge.
Constraints:
1<=N<=10^3
1<=M<=N-1
1<=X,Y<=N-1
sample input:
5 4
1 2
3 4
4 5
1 3
sample output:
1 1 1 2
1 2 2
2 3
5
*/
using System;
using System.Numerics;
class MyClass {
static void Main(string[] args) {
int[] nm = Array.ConvertAll(Console.ReadLine().Split(' '),Int32.Parse);
int n = nm[0];
int m = nm[1];
int[] arr = new int[n];
int[] size = new int[n];
Intialize(arr,size,n);

for(int i = 0; i < m; i++)
{
int[] ver= Array.ConvertAll(Console.ReadLine().Split(' '), Int32.Parse);
WeightedUnion(arr,size,ver[0],ver[1]);
DisplayConnected(size,n);
}
}

private static void DisplayConnected(int[] size, int n)
{
int[] temp=new int[n];
Array.Copy(size,temp,n);
Array.Sort(temp);
for (int j=0; j < n; j++)
{
if(temp[j]>0)Console.Write(temp[j]+" ");
}
Console.WriteLine();
}

private static void WeightedUnion(int[] arr, int[] size, int a, int b)
{
int root_a = root(a,arr);
int root_b = root(b,arr);
if (arr[root_a] == arr[root_b]) return;
else if (size[root_a] < size[root_b])
{
arr[root_a] = arr[root_b];
size[root_b] += size[root_a];
size[root_a]=0;
}
else
{
arr[root_b] = arr[root_a];
size[root_a] += size[root_b];
size[root_b]=0;
}
}

private static int root(int a,int[] arr)
{
int i = a - 1;
while (arr[i] != i)
{
i = arr[i];
}
return i;
}

private static void Intialize(int[] arr,int[] size,int n)
{
for(int i = 0; i < n; i++)
{
arr[i] = i;
size[i] = 1;
}
}
}
83 changes: 83 additions & 0 deletions Data Structures/disjointset/ExampleProblem_TeachersDilemma.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Monk is having a hard time teaching the 2nd standard students.
He wants to divide the students into small groups so that he can conduct
some fun-filled activities for them. But students also want their friends in the same group. So, if student
A is a friend of student B, and student B is a friend of student C, then the students
A,B and C must be in the same group, otherwise they will start crying.
After dividing the students, he will choose a leader from each group who will lead their respective groups.
Now he wants to know the number of ways he can choose the group leaders from all the groups.
Print this answer modulo 10^9+7.
Note: Two ways A and B will be considered different if at least 1 person is a leader in group A,
and is not a leader in group B, or vice-versa.
Input:
The first line consists of two integers N and M denoting the number of students and the number of relationships respectively.
The next M lines consists of two integers
u and v denoting that student u and student v are friends.
u and v can never be equal and relationships are not repeated.
output:
Print the answer modulo 10^9+7 in a single line.
*/
using System;
using System.Numerics;
class MyClass {
static void Main(string[] args)
{
int[] _nm= Array.ConvertAll(Console.ReadLine().Split(' '), Int32.Parse);
int n = _nm[0];
int m = _nm[1];

int[] students = new int[n];
long[] size = new long[n];
Initialize(students,size,n);
for(int i = 0; i < m; i++)
{
int[] ver= Array.ConvertAll(Console.ReadLine().Split(' '), Int32.Parse);
WeightedUnion(students,size,ver[0]-1,ver[1]-1);
}
long product = 1;
for(int i = 0; i < n; i++)
{
if (size[i] != 0) product *= size[i];
product %= (long)(Math.Pow(10, 9) + 7);
}
Console.WriteLine(product);
}
private static void WeightedUnion(int[] students, long[] size, int a, int b)
{
int root_a = root(a,students);
int root_b = root(b,students);
if (students[root_a] == students[root_b]) return;
else if (size[root_a] < size[root_b])
{
students[root_a] = students[root_b];
size[root_b] += size[root_a];
size[root_a]=0;
}
else
{
students[root_b] = students[root_a];
size[root_a] += size[root_b];
size[root_b] = 0;
}
}
private static int root(int v, int[] students)
{
int i = v;
while (students[i] != i)
{
i = students[i];
}
return i;
}
private static void Initialize(int[] students, long[] size, int n)
{
for(int i = 0; i < n; i++)
{
students[i] = i;
size[i] = 1;
}
}
}

0 comments on commit af701a6

Please sign in to comment.