forked from Yehudit-Brickner/OOP2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.java
299 lines (253 loc) · 9.52 KB
/
Graph.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.w3c.dom.Node;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
public class Graph implements DirectedWeightedGraph {
// this graph create new graph with all the parameters.
int mc;
public HashMap<Integer, Vertex> Nodes;
public HashMap<Integer, HashMap<Integer, EdgeData>> Edges;
public HashMap<String, Edge> edg; // this hashmap save all the edges in my graph . < " src,dis" , edge> .
//my constructor create new JSONArray from the json file.
public Graph(String jsonName) throws ParseException {
Nodes = new HashMap<>();
Edges = new HashMap<>();
edg= new HashMap<>();
String filename = jsonName;
try {
JSONObject jsonObject = parseJSONFile(filename);
JSONArray vertex = jsonObject.getJSONArray("Nodes");
JSONArray edges = jsonObject.getJSONArray("Edges");
saveJson(vertex, edges);
} catch (IOException e) {
e.printStackTrace();
}
this.mc = 0;
}
public static JSONObject parseJSONFile(String filename) throws JSONException, IOException {
String content = new String(Files.readAllBytes(Paths.get(filename)));
return new JSONObject(content);
}
//must use the json file on and add the details in.
//this function get two JSSONArray and copy the details from them to our hashmaps.
public void saveJson(JSONArray nodes, JSONArray edges) {
for (int i = 0; i < nodes.length(); i++) {
Integer key = nodes.getJSONObject(i).getInt("id");
String a = nodes.getJSONObject(i).getString("pos");
Geo p = new Geo(a);
Vertex b = new Vertex(key, p, 0, 0, "");
addNode(b);
}
for (int i = 0; i < edges.length(); i++) {
Integer src = edges.getJSONObject(i).getInt("src");
Double w = edges.getJSONObject(i).getDouble("w");
Integer dest = edges.getJSONObject(i).getInt("dest");
connect(src, dest, w);
}
}
//this constructor copy the G' Nodes and Edges to our hashmaps
public Graph(Graph g) {
HashMap<Integer, Vertex> Nodes = g.Nodes;
HashMap<Integer, HashMap<Integer, EdgeData>> Edges = g.Edges;
HashMap<String, Edge> edg=g.edg;
mc=g.mc;
}
//default constructor
public Graph() {
this.Nodes = new HashMap<Integer, Vertex>();
this.Edges = new HashMap<Integer, HashMap<Integer, EdgeData>>();
this.edg = new HashMap<>();
this.mc=0;
}
@Override
//return the node by key
public NodeData getNode(int key) {
return this.Nodes.get(key);
}
@Override
//return the edge by src and dest
public EdgeData getEdge(int src, int dest) {
return this.Edges.get(src).get(dest);
}
@Override
//adding a new node to nodes hashmap and to edge hashmap
public void addNode(NodeData n) {
Integer key = n.getKey();
this.Nodes.put(key, (Vertex) n);
this.Edges.put(n.getKey(), new HashMap<>());
mc++;
}
@Override
//add a new edge to my edge hashmap , from src,dest and w.
public void connect(int src, int dest, double w) {
if(this.Nodes.get(src)==null||this.Nodes.get(dest)==null|| src==dest){
System.out.println("there was a problem making a edge");
}
Edge a = new Edge(src, dest, w);//create bew edge from the src ,dest and w
this.edg.put(src+","+dest,a);//add the new edge to the hashmap og edge, the kry is string.
this.Edges.get(src).put(dest, a);//add the new edge to my hashmap
this.Nodes.get(src).nodes.put(src,dest);//add the new edge to my nodes .
mc++;
}
@Override
//this function return iterator of NodeData.
public Iterator<NodeData> nodeIter() {
return new Iterator<NodeData>(){
Iterator<Vertex> iter=Nodes.values().iterator();
private int currentMc=mc;
NodeData last=null;
@Override
public boolean hasNext() {
if (currentMc != mc) {
throw new RuntimeException("you updated the graph while the iterater was running");
}
return iter.hasNext();
}
@Override
public NodeData next() {
if (currentMc != mc) {
throw new RuntimeException("you updated the graph while the iterater was running");
}
last=iter.next();
return last;
}
@Override
public void remove() {
if (currentMc != mc) {
throw new RuntimeException("you updated the graph while the iterater was running");
}
if(last!=null){
removeNode(last.getKey());
}
Iterator.super.remove();
}
};
}
@Override
// this function return the iterator of EdgeData , step by step as the last function
public Iterator<EdgeData> edgeIter() {
return new Iterator<EdgeData>() {
Iterator<Edge>iter=edg.values().iterator();
private int currentMc=mc;
EdgeData last=null;
@Override
public boolean hasNext() {
if (currentMc != mc) {
throw new RuntimeException("you updated the graph while the iterater was running");
}
return iter.hasNext();
}
@Override
public EdgeData next() {
if (currentMc != mc) {
throw new RuntimeException("you updated the graph while the iterater was running");
}
last = iter.next();
return last;
}
public void remove(){
if (currentMc != mc) {
throw new RuntimeException("you updated the graph while the iterater was running");
}
if(last!=null){
removeEdge(last.getSrc(), last.getDest());
currentMc=mc;
}
}
};
}
@Override
// this function return the iterator of EdgeData , step by step as the last function.
public Iterator<EdgeData> edgeIter(int node_id) {
return new Iterator<EdgeData>() {
Iterator<EdgeData> iter = Edges.get(node_id).values().iterator();
private int currentMc = mc;
EdgeData last = null;
@Override
public boolean hasNext() {
if (currentMc != mc) {
throw new RuntimeException("you updated the graph while the iterater was running");
}
return iter.hasNext();
}
@Override
public EdgeData next() {
if (currentMc != mc) {
throw new RuntimeException("you updated the graph while the iterater was running");
}
last = iter.next();
return last;
}
@Override
public void remove() {
if (currentMc != mc) {
throw new RuntimeException("you updated the graph while the iterater was running");
}
if (last !=null){
removeEdge(last.getSrc(), last.getDest());
currentMc = mc;
}
}
};
}
@Override
// this function remove node by node that i get in .
public NodeData removeNode(int key) {
ArrayList<String> keys = new ArrayList<String>();
for (String k: edg.keySet()) //running on the hashmap of edg
{
if(key==Integer.parseInt((k.split(","))[0])||key==Integer.parseInt((k.split(","))[1])) {
keys.add(k); //adding the value of key to the list
}
}
keys.forEach(k->this.edg.remove(k)); //running on the list and delete the nodes with tge same key.
Iterator<NodeData> it=nodeIter();
while (it.hasNext()){
Vertex v=(Vertex)it.next();
Iterator<EdgeData> iter=edgeIter(v.getKey());
while(iter.hasNext()){
Edge e=(Edge)iter.next();
if(e.getSrc()==key || e.getDest()==key){//checking if the edge have the same key as (x,x).
v.nodes.remove(e);
//remove also from the nodes' hashmap.
}
}
}
Vertex ans=this.Nodes.remove(key);
mc++;
return ans;
}
@Override
// remove edge from the hashmap by src and dest .
public EdgeData removeEdge(int src, int dest) {
EdgeData edg1 = this.Edges.get(src).get(dest);
this.Edges.get(src).remove(dest);
//this.edg.get(src+","+dest);
this.edg.remove(src+","+dest);
this.Nodes.get(src).getnodes().remove(src,dest);
mc++;
return edg1;
}
@Override
public int nodeSize() {
return Nodes.size();
}
@Override
public int edgeSize() {
return edg.size();//the size of edg hashmap is the same size of the hashmap of all edges.
}
@Override
public int getMC() {
return this.mc;
}
}