-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
559 lines (444 loc) · 14.3 KB
/
Copy pathGraph.java
File metadata and controls
559 lines (444 loc) · 14.3 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
package hw2;
import java.util.Random;
/*
You must NOT change the signatures of classes/methods in this skeleton file.
You are required to implement the methods of this skeleton file according to the requirements.
You are allowed to add classes, methods, and members as required.
*/
/**
* This class represents a graph that efficiently maintains the heaviest neighborhood over edge addition and
* vertex deletion.
*
*/
public class Graph {
hash_table hash;
static max_heap binary_max_heap;
int NumNodes;
int NumEdges;
/**
* Initializes the graph on a given set of nodes. The created graph is empty, i.e. it has no edges.
* You may assume that the ids of distinct nodes are distinct.
*
* @param nodes - an array of node objects
*/
// complexity=O(N)
public Graph(Node [] nodes){
hash = new hash_table(nodes.length);
binary_max_heap=new max_heap(nodes.length+1);
for(int i=0; i<nodes.length; i++) {
if(nodes[i]!=null) {
hash.insert(nodes[i]);
NumNodes++;
nodes[i].index_in_heap=i+1;
binary_max_heap.arr[i+1]=nodes[i];
}
}
for(int i=1; i<(nodes.length+1)/2; i++) {
if(nodes[0]!=null) {
binary_max_heap.heapify_down(binary_max_heap.arr[(nodes.length+1)/2-i]);
}
}
}
/**
* This method returns the node in the graph with the maximum neighborhood weight.
* Note: nodes that have been removed from the graph using deleteNode are no longer in the graph.
* @return a Node object representing the correct node. If there is no node in the graph, returns 'null'.
*/
// complexity=O(1)
public Node maxNeighborhoodWeight(){
if(getNumNodes()==0) {
return null;
}
return binary_max_heap.max();
}
/**
* given a node id of a node in the graph, this method returns the neighborhood weight of that node.
*
* @param node_id - an id of a node.
* @return the neighborhood weight of the node of id 'node_id' if such a node exists in the graph.
* Otherwise, the function returns -1.
*/
// complexity=O(1)
public int getNeighborhoodWeight(int node_id){
if(!hash.is_in_table(node_id)) {
return -1;
}
Node node=hash.find_in_table(node_id);
return node.neighborhood_sum;
}
/**
* This function adds an edge between the two nodes whose ids are specified.
* If one of these nodes is not in the graph, the function does nothing.
* The two nodes must be distinct; otherwise, the function does nothing.
* You may assume that if the two nodes are in the graph, there exists no edge between them prior to the call.
*
* @param node1_id - the id of the first node.
* @param node2_id - the id of the second node.
* @return returns 'true' if the function added an edge, otherwise returns 'false'.
*/
// complexity=O(logn)
public boolean addEdge(int node1_id, int node2_id){
if(hash.is_in_table(node1_id) && hash.is_in_table(node2_id) ) {
Node node1=hash.find_in_table(node1_id);
Node node2=hash.find_in_table(node2_id);
cell cell1=new cell(node1);
cell cell2=new cell(node2);
cell1.parallel_edge=cell2;
cell2.parallel_edge=cell1;
node1.add_edge_to_node(cell2);
node2.add_edge_to_node(cell1);
NumEdges++;
return true;
}
return false;
}
/**
* Given the id of a node in the graph, deletes the node of that id from the graph, if it exists.
*
* @param node_id - the id of the node to delete.
* @return returns 'true' if the function deleted a node, otherwise returns 'false'
*/
// complexity=O((d(v)+1)*logn), d(v) - v's rank
public boolean deleteNode(int node_id){
if(hash.is_in_table(node_id)) {
Node node=hash.find_in_table(node_id);
NumEdges-=node.neighbors.size;
cell c_edge=node.neighbors.head;
for(int i=0; i<node.neighbors.size; i++) {
c_edge.node.delete_edge_from_node(c_edge.parallel_edge);
c_edge=c_edge.next;
}
binary_max_heap.delete(node);
hash.delete(node);
NumNodes--;
return true;
}
return false;
}
// returns the number of nodes currently in the graph
// complexity=O(1)
public int getNumNodes() {
return NumNodes;
}
// returns the number of edges currently in the graph
// complexity=O(1)
public int getNumEdges() {
return NumEdges;
}
/**
* This class represents a node in the graph.
*/
public static class Node{
int index_in_heap;
public int weight;
public int id;
public LinkedList neighbors;
public int neighborhood_sum = weight;
/**
* Creates a new node object, given its id and its weight.
* @param id - the id of the node.
* @param weight - the weight of the node.
*/
// complexity=O(1)
public Node(int id, int weight){
this.id = id;
this.weight = weight;
this.neighbors=new LinkedList();
this.neighborhood_sum=this.weight;
}
/**
* Returns the id of the node.
* @return the id of the node.
*/
// complexity=O(1)
public int getId(){
return this.id;
}
/**
* Returns the weight of the node.
* @return the weight of the node.
*/
// complexity=O(1)
public int getWeight(){
return this.weight;
}
// add an edge to node's neighbors list, and update it's neighborhood's weight accordingly
// complexity=O(logn)
void add_edge_to_node(cell c) {
neighbors.insert_cell(c);
binary_max_heap.increase_key(this, c.node.weight);
}
// delete an edge from node, and update it's neighborhood's weight accordingly
// complexity=O(logn)
void delete_edge_from_node(cell c) {
neighbors.delete_cell(c);
binary_max_heap.decrease_key(this,c.node.weight);
}
}
/**
* This class represents a node's neighbors in the graph.
*/
// cell for the LinkedList
static class cell {
cell next;
cell prev;
cell parallel_edge;
Node node;
//constructor for cell
// complexity=O(1)
public cell(Node node) {
this.node=node;
}
}
// a doubly linked list
public static class LinkedList{
cell head;
cell last;
public int size = 0;
// insert a cell to the LinkedList
// complexity=O(1)
void insert_cell(cell c) {
if(this.size==0) {
head=c;
last=c;
}
else {
last.next=c;
c.prev=last;
last=c;
}
size++;
}
// delete a cell in the LinkedList
// complexity=O(1)
void delete_cell(cell c) {
if(size==1) {
head=null;
last=null;
}
else if(last==c) {
last=c.prev;
c.prev.next=null;
}
else if(head==c) {
head=head.next;
head.prev=null;
}
else {
c.prev.next=c.next;
c.next.prev=c.prev;
}
c.next=null;
c.prev=null;
size--;
}
// check if a node is in the list, given his id
// complexity=O(d), d=list.size
public boolean is_in(int id) {
cell c=head;
while (c!=null){
if(c.node.getId()==id) {
return true;
}
c=c.next;
}
return false;
}
// pre - is_in(id)==true
// find node in the list
// complexity=O(d), d=list.size
public Node find(int id) {
cell c=head;
while (c.node.getId()!=id){
c=c.next;
}
return c.node;
}
// adding a Node to the LinkedList at the end
// complexity=O(1)
public void insert_node(Node node) {
cell c= new cell(node);
if(this.size==0) {
head=c;
last=c;
}
else {
last.next=c;
c.prev=last;
last=c;
}
size++;
}
// pre - is_in(node)==true
// deleting Node from the LinkedList
// complexity=O(d)
public void delete_node(Node node) {
cell c=head;
while(c.node!=node) {
c=c.next;
}
if(c==head) {
head=c.next;
if(head!=null) {
head.prev=null;
}
}
else if(c==last) {
last=c.prev;
if(last!=null) {
last.next=null;
}
}
else {
if(c.prev!=null) {
c.prev.next=c.next;
}
if(c.next!=null) {
c.next.prev=c.prev;
}
}
c.next=null;
c.prev=null;
size--;
}
}
//hash_table with chaining
class hash_table{
LinkedList[] table;
int a;
int b;
int p=10^9+9;
//hash_table constructor
// complexity=O(1)
public hash_table(int n) {
table = new LinkedList[n];
Random rand=new Random();
a=rand.nextInt(p-1)+1;
b=rand.nextInt(p);
}
// insert node to the hash_table
// complexity=O(1)
public void insert(Node node) {
int place=((a*node.getId()+b)%p)%table.length;
if(table[place]==null) {
table[place]= new LinkedList();
}
table[place].insert_node(node);
}
// pre - node is in the hash table
// delete node from the hash table
// complexity=O(1)
public void delete(Node node) {
int place=((a*node.getId()+b)%p)%table.length;
table[place].delete_node(node);
}
// check if node is in the hash table, given his id
// complexity=O(1)
public boolean is_in_table(int id) {
int place=((a*id+b)%p)%table.length;
if(table[place]!=null) {
return table[place].is_in(id);
}
return false;
}
// pre - is_in(id)==true
// return node, given his id
// complexity=O(1)
public Node find_in_table(int id) {
int place=((a*id+b)%p)%table.length;
return table[place].find(id);
}
}
// max_binary_heap represented by an array
class max_heap {
int last_item=0;
Node[] arr;
//constructor for max_heap
// complexity=O(1)
public max_heap(int n){
arr=new Node[n];
this.last_item=n-1;
}
//insert a Node to the heap
// complexity=O(logn)
public void insert(Node node) {
arr[last_item+1]=node;
heapify_up(node);
last_item++;
}
// returns the node with the maximal neighborhood_sum
// complexity=O(1)
public Node max() {
return arr[1];
}
// moving node up until his father's neighborhood_sum is bigger than his
// complexity=O(logn)
public void heapify_up(Node node) {
while(node.index_in_heap!=1 && node.neighborhood_sum>arr[node.index_in_heap/2].neighborhood_sum) {
int father_index_in_heap=node.index_in_heap/2;
Node father=arr[father_index_in_heap];
arr[node.index_in_heap]=father;
arr[father_index_in_heap]=node;
father.index_in_heap=node.index_in_heap;
node.index_in_heap=node.index_in_heap/2;
}
}
//moving node down until he has no sons with bigger neighborhood_sum than his
// complexity=O(logn)
public void heapify_down(Node node) {
while(node.index_in_heap*2+1<=last_item &&
(node.neighborhood_sum<arr[node.index_in_heap*2].neighborhood_sum
|| node.neighborhood_sum<arr[node.index_in_heap*2+1].neighborhood_sum)) {
Node son=maximal_son(node);
int son_index_in_heap=son.index_in_heap;
arr[node.index_in_heap]=son;
arr[son.index_in_heap]=node;
son.index_in_heap=node.index_in_heap;
node.index_in_heap=son_index_in_heap;
}
if(node.index_in_heap*2==last_item && node.neighborhood_sum<arr[node.index_in_heap*2].neighborhood_sum) {
Node son=maximal_son(node);
int son_index_in_heap=son.index_in_heap;
arr[node.index_in_heap]=son;
arr[son.index_in_heap]=node;
son.index_in_heap=node.index_in_heap;
node.index_in_heap=son_index_in_heap;
}
}
// pre - node.index_in_heap*2>=arr.length
// check which son has the bigger neighborhood_sum
// complexity=O(1)
public Node maximal_son(Node node) {
if(node.index_in_heap*2==last_item) { // node has only one son
return arr[node.index_in_heap*2];
}
if(arr[node.index_in_heap*2].neighborhood_sum > arr[node.index_in_heap*2+1].neighborhood_sum) {
return arr[node.index_in_heap*2];
}
return arr[node.index_in_heap*2+1];
}
// delete node from the heap
// complexity=O(logn)
public void delete(Node node) {
arr[node.index_in_heap]=arr[last_item];
arr[node.index_in_heap].index_in_heap=node.index_in_heap;
arr[last_item]=null;
last_item--;
if(last_item+1!=node.index_in_heap) {
heapify_down(arr[node.index_in_heap]);
}
}
//increase the max_neighbors field of a node
// complexity=O(logn)
public void increase_key(Node node, int delta) {
node.neighborhood_sum+=delta;
heapify_up(node);
}
//decrease the max_neighbors field of a node
// complexity=O(logn)
public void decrease_key(Node node, int delta) {
node.neighborhood_sum-=delta;
heapify_down(node);
}
}
}