-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNode.java
71 lines (56 loc) · 1.59 KB
/
Node.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
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.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Node {
public enum Status {
COMPLETE,
INPROGRESS,
UNSEEN,
}
public int id;
public int index;
public int lowlink;
public Status status;
public Search search;
public ArrayList<Search> blocked;
public Node(int id, int index, int lowlink) {
this.id = id;
this.index = index;
this.lowlink = lowlink;
this.status = Status.UNSEEN;
this.blocked = new ArrayList<>();
this.search = null;
}
public void updateLowLink(int update) {
this.lowlink = Math.min(this.lowlink, update);
}
// Transfere o nó para uma nova busca, atualizando o índice, lowlink e a busca
public synchronized int tranfer(int deltaIndex, Search newSearch) {
this.index += deltaIndex;
this.lowlink += deltaIndex;
this.search = newSearch;
return this.index;
}
// Retorna um HashMap com todos os nós do grafo
public static HashMap<Integer, Node> getNodeMap(Set<Integer> nodes) {
HashMap<Integer, Node> nodeMap = new HashMap<>();
for(int nodeId : nodes)
nodeMap.put(nodeId, new Node(nodeId, -1, -1));
return nodeMap;
}
// Retorna o próximo nó não visitado
public static Node getNotInSCC(Map<Integer, Node> nodes, Integer nextNode) {
long maxId = nodes.keySet().stream().max(Integer::compare).get();
if(nextNode > maxId)
return null;
Node node = nodes.get(nextNode);
while(nextNode <= maxId) {
if(node != null && node.status == Status.UNSEEN)
return node;
nextNode++;
node = nodes.get(nextNode);
}
return null;
}
}