-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWGraph_DS.java
199 lines (171 loc) · 4.25 KB
/
WGraph_DS.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package ex1.src;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Objects;
public class WGraph_DS implements weighted_graph , Serializable {
private static final long serialVersionUID = 1L;
private HashMap<Integer, node_info> graph;
private HashMap<Integer, HashMap<node_info, Double>> EList;
private int E;
private int T;
public WGraph_DS() {
this.graph = new HashMap<>();
this.EList = new HashMap<>();
this.E = 0;
this.T = 0;
}
@Override
public void addNode(int id) {
if (!this.graph.containsKey(id)) {
this.graph.put(id, new Node(id));
this.EList.put(id,new HashMap<>());
T++;
}
}
@Override
public node_info removeNode(int key) {
if (this.graph.containsKey(key)==true) {
for (node_info n : graph.values()) {
removeEdge(n.getKey(), key);
}
return graph.remove(key);
}
return null;
}
@Override
public void connect(int node1, int node2, double w)
{
if (this.graph.containsKey(node1)==true) {
if (node1 != node2) {
if (hasEdge(node1, node2)==false) {
EList.get(node1).put(getNode(node2), w);
EList.get(node2).put(getNode(node1), w);
T++;
E++;
}
else
{
EList.get(node1).replace(getNode(node2), w);
EList.get(node2).replace(getNode(node1), w);
T++;
}
}
}
}
@Override
public void removeEdge(int node1, int node2)
{
if (hasEdge(node1, node2)==true) {
EList.get(node1).remove(getNode(node2));
EList.get(node2).remove(getNode(node1));
E--;
T++;
}
}
@Override
public node_info getNode(int key) {
return this.graph.get(key);
}
@Override
public boolean hasEdge(int node1, int node2) {
if(EList.get(node1).containsKey(getNode(node2))==true)
{
return true;
}
else
{
return false;
}
}
@Override
public double getEdge(int node1, int node2) {
if (hasEdge(node1,node2))
{
return EList.get(node1).get(getNode(node2));
}
else
{
return -1;
}
}
@Override
public Collection<node_info> getV() {
return this.graph.values();
}
@Override
public Collection<node_info> getV(int node_id) {
return this.EList.get(node_id).keySet();
}
@Override
public int nodeSize() {
return graph.size();
}
@Override
public int edgeSize() {
return this.E;
}
@Override
public int getMC() {
return this.T;
}
@Override
public boolean equals(Object o) {
if (this == o)
{
return true;
}
if (o == null)
{
return false;
}
if(getClass() != o.getClass())
{
return false;
}
WGraph_DS ngraph = (WGraph_DS) o;
if(graph.size() == ngraph.graph.size() && E == ngraph.E)
{
return true;
}
else
{
return false;
}
}
@Override
public int hashCode() {
return Objects.hash(E, T, graph, EList);
}
private class Node implements node_info, Serializable {
private static final long serialVersionUID = 1L;
private String nod_inf;
private int key;
private double tag;
public Node(int k) {
this.nod_inf = "";
this.key = k;
this.tag = 0;
}
@Override
public int getKey() {
return this.key;
}
@Override
public String getInfo() {
return this.nod_inf;
}
@Override
public void setInfo(String s) {
this.nod_inf = s;
}
@Override
public void setTag(double t) {
this.tag = t;
}
@Override
public double getTag() {
return this.tag;
}
}
}