-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphCentrality.java
More file actions
147 lines (117 loc) · 4.34 KB
/
GraphCentrality.java
File metadata and controls
147 lines (117 loc) · 4.34 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.Collectors;
import twitter4j.*;
/**
* Created by Francis on 30/05/2016.
*/
public class GraphCentrality {
public GraphAlgorithm ga = new GraphAlgorithm();
public ArrayList<GetComputerScientist.ComputerScientist> cs = new GetComputerScientist().getComputerScientists();
public Map<Integer, GetComputerScientist.ComputerScientist> csTwitter = new HashMap<>();
public boolean twit = false;
public class NodeScore{
public Node node;
public String name;
public String handle;
public Float score;
public NodeScore(Node n, Float s){
Random myRandomizer = new Random();
csTwitter.computeIfAbsent(n.getID(), a -> cs.remove(myRandomizer.nextInt(cs.size())));
node = n;
score = s;
name = csTwitter.get(n.getID()).name;
handle = csTwitter.get(n.getID()).twitterHandle;
if(twit) {
try {
Twitter twitter = new TwitterFactory().getInstance();
User u = twitter.showUser((long) n.getID());
name = u.getName();
handle = u.getScreenName();
} catch (TwitterException te) {
te.printStackTrace();
}
}
}
}
private static class SortedPair implements Comparable<SortedPair> {
private final float distance;
private final Node node;
public SortedPair(float distance, Node node) {
this.distance = distance;
this.node = node;
}
@Override
public int compareTo(SortedPair other) {
return -Float.compare(distance, other.distance);
}
public Node getNode() {
return node;
}
public float getDistance() {
return distance;
}
}
public ArrayList<NodeScore> degreeCentral(NodeMap nodes) {
Queue<Node> Q = new PriorityQueue<>(nodes.size(), new Comparator<Node>() {
@Override
public int compare(Node n1, Node n2){
return -(n1.totalEdges() - n2.totalEdges());
}
});
Q.addAll(nodes.getMap().values());
ArrayList<NodeScore> result = new ArrayList<>(5);
for(int i = 0; i < 5; i++){
Node n = Q.poll();
result.add(i, new NodeScore(n, (float)n.totalEdges()));
}
return result;
}
public ArrayList<NodeScore> closenessCentral(NodeMap nodes) {
Queue<SortedPair> q = nodes
.getMap()
.values()
.stream()
.map(n -> new SortedPair(ga.bfsSum(n), n))
.collect(Collectors.toCollection(PriorityQueue::new));
ArrayList<NodeScore> result = new ArrayList<>(5);
for(int i = 0; i < 5; i++) {
SortedPair p = q.poll();
result.add(i, new NodeScore(p.getNode(), p.getDistance()));
}
return result;
}
public ArrayList<NodeScore> betweennessCentral(NodeMap nodes) {
Map<Node, GraphAlgorithm.MuteFloat> betweens = new HashMap<>();
nodes
.getMap()
.values()
.stream()
.forEach(n -> ga.brandesAlg(n, betweens));
Queue<SortedPair> q = betweens
.keySet()
.stream()
.map(n -> new SortedPair(betweens.getOrDefault(n, new GraphAlgorithm.MuteFloat(n)).value, n))
.collect(Collectors.toCollection(PriorityQueue::new));
ArrayList<NodeScore> result = new ArrayList<>(5);
for(int i = 0; i < 5; i++){
SortedPair n = q.poll();
result.add(new NodeScore(n.node, n.distance));
}
return result;
}
public ArrayList<NodeScore> katzCentral(NodeMap nodes) {
Queue<SortedPair> q = nodes
.getMap()
.values()
.stream()
.map(n -> new SortedPair(ga.bfsKatz(n), n))
.collect(Collectors.toCollection(PriorityQueue::new));
ArrayList<NodeScore> result = new ArrayList<>(5);
for(int i = 0; i < 5; i++) {
SortedPair p = q.poll();
result.add(i, new NodeScore(p.getNode(), p.getDistance()));
}
return result;
}
}