-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMGraph.java
More file actions
74 lines (61 loc) · 1.27 KB
/
MGraph.java
File metadata and controls
74 lines (61 loc) · 1.27 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
public class MGraph<K extends Comparable<K>> implements Graph<K> {
public Set<K> nodes; // Do not change this
public Map<K, Set<K>> adj; // Do not change this
public MGraph() {
nodes = new BSTSet<K>();
adj = new BSTMap<K, Set<K>>();
}
@Override
public boolean addNode(K i) {
boolean res = nodes.insert(i);
if (res)
adj.insert(i, new BSTSet<K>());
return res;
}
@Override
public boolean isNode(K i) {
boolean res = adj.retrieve(i).first;
return (res == true);
}
@Override
public boolean addEdge(K i, K j) {
if (!isNode(i) || !isNode(j))
return false;
boolean res = isEdge(i, j);
if (!res) {
adj.retrieve(i).second.insert(j);
adj.retrieve(j).second.insert(i);
}
return (res == false);
}
@Override
public boolean isEdge(K i, K j) {
boolean res = (isNode(i) && isNode(j));
if (res) {
res = adj.retrieve(i).second.exists(j);
}
return (res == true);
}
@Override
public Set<K> neighb(K i) {
boolean res = isNode(i);
if (res) {
return adj.retrieve(i).second;
} else {
return null;
}
}
@Override
public int deg(K i) {
boolean res = isNode(i);
if (res == true) {
return adj.retrieve(i).second.size();
} else {
return -1;
}
}
@Override
public Iterator<K> nodesIt() {
return nodes.minIt();
}
}