-
Notifications
You must be signed in to change notification settings - Fork 5
/
DegreeCentrality.java
35 lines (30 loc) · 1.04 KB
/
DegreeCentrality.java
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
import java.util.HashSet;
import java.util.Set;
public class DegreeCentrality {
/**
* Finds the degree centrality of a given node
* If people is null or source cannot be found in the graph, throw and
* IllegalArgumentException
* @param source is the ID of the source node
*
*/
//this is fairly straight forward...
public static int getDegree(Set<Friend> people, String source) {
//check for exceptions
if (people == null) throw new IllegalArgumentException();
HashSet<Edge> edges = MSTKruskal.MST((HashSet<Friend>) people);
if (edges.size() == 0) throw new IllegalArgumentException();
//calculates the centrality by keeping a count
int count = 0;
for (Edge e: edges) {
if(e.A.id.equals(source)||e.B.id.equals(source)) count++;
}
return count;
}
public static void main(String[] args) {
FakebookReader f = new FakebookReader("5.json");
HashSet<Friend> friends = new HashSet<Friend>();
friends.addAll(f.Users.values());
System.out.println(getDegree(friends, "531553417"));
}
}