-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocialNetworkConnectivity.java
More file actions
71 lines (60 loc) · 1.9 KB
/
SocialNetworkConnectivity.java
File metadata and controls
71 lines (60 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.io.IOException;
import java.util.Date;
import java.util.Scanner;
/**
* Created by Syed on 09-04-2018.
*/
public class SocialNetworkConnectivity extends WeightedQuickUnion
{
private int numberOfConnectedComponents=id.length;
public SocialNetworkConnectivity(int n)
{
super(n);
}
@Override
public void union(int p, int q) {
if(!(connected(p,q)))
{
--numberOfConnectedComponents;
}
super.union(p, q);
}
public int getNumberOfConnectedComponents() {
return numberOfConnectedComponents;
}
public void setNumberOfConnectedComponents(int numberOfConnectedComponents) {
this.numberOfConnectedComponents = numberOfConnectedComponents;
}
public void display()
{
for(int iid:id)
{
System.out.print(iid+" ");
}
System.out.print("\n");
}
public static void main (String a[])throws InterruptedException, IOException {
Scanner scanner=new Scanner(System.in);
System.out.println("Enter the number of users in the social network: ");
int N = scanner.nextInt();
SocialNetworkConnectivity socialNetwork = new SocialNetworkConnectivity(N);
System.out.println("Enter number of union operations to be performed: ");
int M=scanner.nextInt();
int p,q;
long timestamp;
for(int i=0;i<M;i++)
{
System.out.println("Enter elements to be connected: ");
p=scanner.nextInt();
q=scanner.nextInt();
timestamp=new Date().getTime();
socialNetwork.union(p,q);
socialNetwork.display();
if(socialNetwork.getNumberOfConnectedComponents()==1)
{
System.out.println("All the users in the social network at connected at" +
": "+timestamp);
}
}
}
}