-
Notifications
You must be signed in to change notification settings - Fork 0
/
GreedySummary.java
745 lines (680 loc) · 21.4 KB
/
GreedySummary.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
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Set;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
class NodePair{
long u, v;
Double scost;
NodePair(long u, long v){
if(u < v){
this.u = u;
this.v = v;
}
else{
this.u = v;
this.v = u;
}
this.scost = 0.0;
}
NodePair(long u, long v, double scost){
if(u < v){
this.u = u;
this.v = v;
}
else{
this.u = v;
this.v = u;
}
this.scost = scost;
}
void setSCost(double scost){
this.scost = scost;
}
double getSCost(){
return this.scost;
}
@Override
public String toString(){
return "("+u+","+v+") - "+scost;
}
}
class PairSCost{
Map<Long, HashMap<Long, Double>> map;
PairSCost(){
map = new HashMap<Long, HashMap<Long, Double>>();
}
void put(Long u, Long v, Double cost){
if(u > v){
Long temp = u;
u = v;
v = temp;
}
if(!map.containsKey(u)){
map.put(u, new HashMap<Long, Double>());
}
map.get(u).put(v, cost);
}
Double get(Long u, Long v){
if(u > v){
Long temp = u;
u = v;
v = temp;
}
if(map.containsKey(u)){
Map<Long, Double> t = map.get(u);
if(t.containsKey(v)){
return t.get(v);
}
}
return -1.0;
}
void set(Long u, Long v, double scost){
if(u > v){
Long temp = u;
u = v;
v = temp;
}
if(map.containsKey(u)){
Map<Long, Double> t = map.get(u);
if(t.containsKey(v)){
t.put(v, scost);
}
}
}
void delete(Long u, Long v){
if(u > v){
Long temp = u;
u = v;
v = temp;
}
if(map.containsKey(u)){
Map<Long, Double> t = map.get(u);
if(t.containsKey(v)){
t.clear();
}
}
}
boolean isAvailable(Long u, Long v){
if(u > v){
Long temp = u;
u = v;
v = temp;
}
if(map.containsKey(u)){
Map<Long, Double> t = map.get(u);
if(t.containsKey(v)){
return true;
}
}
return false;
}
void print(){
for (Map.Entry<Long, HashMap<Long, Double>> u : map.entrySet()) {
for(Map.Entry<Long, Double> v : u.getValue().entrySet()){
System.out.println(u.getKey()+","+v.getKey()+" - "+v.getValue());
}
}
}
}
public class GreedySummary {
GraphDatabaseService G;
PriorityQueue<NodePair> maxHeap;
PairSCost scost;
Set<Long> Vg, Vs;
Map<Long, HashSet<Long>> Eg, Es;
Map<Long, HashSet<Long>> superNodeElements;
Map<Long, HashSet<Long>> ES, pC, nC;
GreedySummary(GraphDatabaseService graph)
{
this.Vg = new HashSet<Long>();
this.Vs = new HashSet<Long>();
this.Eg = new HashMap<Long, HashSet<Long>>();
this.Es = new HashMap<Long, HashSet<Long>>();
this.superNodeElements = new HashMap<Long, HashSet<Long>>();
this.scost = new PairSCost();
this.maxHeap = new PriorityQueue<NodePair>(new Comparator<NodePair>() {
@Override
public int compare(NodePair arg0, NodePair arg1) {
// TODO Auto-generated method stub
return arg1.scost.compareTo(arg0.scost);
}
});
//ExecutionResult exec = (ExecutionResult) G.execute("match (n)-[r]->(m) return count(r)");
this.G = graph;
//initializing nodes and edges
for(Node n: G.getAllNodes()){
Vg.add(n.getId());
Vs.add(n.getId());
Eg.put(n.getId(), new HashSet<Long>());
Es.put(n.getId(), new HashSet<Long>());
superNodeElements.put(n.getId(), new HashSet<Long>());
superNodeElements.get(n.getId()).add(n.getId());
for(Relationship r: n.getRelationships()){
Eg.get(n.getId()).add(r.getOtherNode(n).getId());
Es.get(n.getId()).add(r.getOtherNode(n).getId());
}
}
this.pC = new HashMap<Long, HashSet<Long>>();
this.nC = new HashMap<Long, HashSet<Long>>();
this.ES = new HashMap<Long, HashSet<Long>>();
}
double calc_cost(long u, Map<Long, HashSet<Long>> Et, HashSet<Long> Vt) throws Exception
{
double cost = 0.0;
double pi, actual_edges;
//for(Long x : Et.get(u)){
//if(superNodeElements.get(u).contains(1L) && superNodeElements.get(u).contains(2L))
// System.out.println("neighbors of "+u+" "+get_neighbors(u, Vt));
for(Long x: get_neighbors(u, Vt)){
if(u == x){
int n = superNodeElements.get(u).size();
pi = ((n*(n-1))/2) + n;
//pi = n*n;
actual_edges = 0;
List<Long> temp = new ArrayList<Long>(superNodeElements.get(u));
for(int i=0; i<temp.size()-1; i++){
for(int j=i+1; j<temp.size(); j++){
//if(superNodeElements.get(u).contains(1L) && superNodeElements.get(u).contains(2L))
// System.out.println("su "+temp.get(i)+" sx"+temp.get(j));
if(Eg.get(temp.get(i)).contains(temp.get(j)))
actual_edges++;
}
}
}
else{
pi = superNodeElements.get(u).size() * superNodeElements.get(x).size();
actual_edges = 0;
for(Long su: superNodeElements.get(u)){
for(Long sx: superNodeElements.get(x)){
//if(superNodeElements.get(u).contains(1L) && superNodeElements.get(u).contains(2L))
// System.out.println("su "+su+" sx"+sx);
if(Eg.get(su).contains(sx))
actual_edges++;
}
}
}
cost += Math.min(pi - actual_edges + 1, actual_edges);
//if(superNodeElements.get(u).contains(1L) && superNodeElements.get(u).contains(2L))
// System.out.println("x"+x+" "+cost);
}
//if(Et.get(u).contains(u)) // self -edge
// cost++;
return cost;
}
double calc_scost(long u, long v, Map<Long, HashSet<Long>> Et, HashSet<Long> Vt) throws Exception
{
double cu, cv, cw ;
cu = calc_cost(u, Et, Vt);
cv = calc_cost(v, Et, Vt);
//Node w = G.createNode();
long w = -1l; // dummy node
superNodeElements.put(w, new HashSet<Long>());
superNodeElements.get(w).addAll(superNodeElements.get(u));
superNodeElements.get(w).addAll(superNodeElements.get(v));
//update super nodes
Vt.add(w);
Vt.remove(u);
Vt.remove(v);
//update super edges
Et.put(w, new HashSet<Long>());
HashSet<Long> neigh = new HashSet<Long>();
//System.out.println("u"+u+" "+Et.get(u)+" v"+v+" "+Et.get(v));
if(Et.containsKey(u) && !Et.get(u).isEmpty())
neigh.addAll(Et.get(u));
if(Et.containsKey(v) && !Et.get(v).isEmpty())
neigh.addAll(Et.get(v));
neigh.remove(u); neigh.remove(v);
for( Long n : neigh)
{
double actual_edges = 0.0;
double pi = superNodeElements.get(w).size() * superNodeElements.get(n).size();
for(Long su: superNodeElements.get(w)){
for(Long sv: superNodeElements.get(n)){
//System.out.println("su "+su+" sx"+sx);
if(Eg.get(su).contains(sv))
actual_edges++;
}
}
if(actual_edges > ((pi + 1)/2)){
Et.get(w).add(n);
}
}
//System.out.println(w+" edges "+Et.get(w));
if(Et.containsKey(w)){
for(Long nw: Et.get(w)){
Et.get(nw).add(w);
}
}
if(Et.containsKey(u)){
for(Long nu: Et.get(u)){
if(nu != u) // avoid removing self edge
Et.get(nu).remove(u);
}
}
if(Et.containsKey(v)){
for(Long nv: Et.get(v)){
if(nv != v) // avoid removing self edge
Et.get(nv).remove(v);
}
}
/*
Et.put(w, new HashSet<Long>());
Et.get(w).addAll(Et.get(u));
Et.get(w).addAll(Et.get(v)); //doubt - add all or retain all?
Et.get(w).remove(u); // remove edge between u and v + self edge of u or v
Et.get(w).remove(v);
for(Long nw: Et.get(w))
Et.get(nw).add(w);
for(Long nu: Et.get(u))
if(nu != u) // to avoid removing self edge
Et.get(nu).remove(u);
//System.out.println(Et);
//System.out.println(u+" "+v);
for(Long nv: Et.get(v))
if(nv != v) // to avoid removing self edge
Et.get(nv).remove(v);
*/
//self edge - if edge between u and v
if((Et.containsKey(u) && Et.get(u).contains(v)) || (Et.containsKey(v) && Et.get(v).contains(u)))
Et.get(w).add(w);
cw = calc_cost(w, Et, Vt); // calculating cost of the merged node
//System.out.println("u "+u+" "+cu+" v "+v+" "+cv+" w "+cw+" "+Et.get(w));
if(Et.get(w).contains(w))
Et.get(w).remove(w);
if(Et.containsKey(v))
for(Long nv: Et.get(v))
Et.get(nv).add(v);
if(Et.containsKey(u))
for(Long nu: Et.get(u))
Et.get(nu).add(u);
if(Et.containsKey(w))
for(Long nw: Et.get(w))
Et.get(nw).remove(w);
Et.remove(w);
Vt.remove(w);
Vt.add(u);
Vt.add(v);
superNodeElements.remove(w);
//w.delete();
return (cu + cv - cw)/(cu + cv);
}
void initialize() throws Exception
{
//int i=0;
for(Node n: G.getAllNodes()){
//if(i>50)
// break;
//i++;
Set<Long> visited = new HashSet<Long>();
visited.add(n.getId());
for(Relationship r: n.getRelationships()){
for(Relationship twoHopR: r.getOtherNode(n).getRelationships()){
Node twoHopN = twoHopR.getOtherNode(r.getOtherNode(n));
if(!visited.contains(twoHopN.getId()) && !scost.isAvailable(n.getId(), twoHopN.getId())){
Double redn_cost = calc_scost(n.getId(), twoHopN.getId(), edgesDeepCopy(Es), new HashSet<Long>(Vs));
if(redn_cost > 0.0){
maxHeap.add(new NodePair(n.getId(), twoHopN.getId(), redn_cost));
scost.put(n.getId(), twoHopN.getId(), redn_cost);
}
visited.add(twoHopN.getId());
}
}
}
}
}
HashMap<Long, HashSet<Long>> edgesDeepCopy(Map<Long, HashSet<Long>> hm){
HashMap<Long, HashSet<Long>> temp = new HashMap<Long, HashSet<Long>>();
for(long key: hm.keySet()){
temp.put(key, new HashSet<Long>(hm.get(key)));
}
return temp;
}
HashSet<Long> get_neighbors(long w, HashSet<Long> Vt) throws Exception
{
HashSet<Long> Nw = new HashSet<Long>();
for(long v : Vt){
//if(v == w)
// continue;
for(long sub_w: superNodeElements.get(w)){
for(long sub_v: superNodeElements.get(v)){
if(Eg.get(sub_w).contains(sub_v)){
Nw.add(v);
break;
}
}
}
}
return Nw;
}
void merge() throws Exception
{
int old_size = Vs.size();
while(!maxHeap.isEmpty()){
//System.out.println("merge "+maxHeap.size());
//System.out.println("before merge: "+maxHeap);
NodePair uv = maxHeap.poll();
System.out.println(uv.toString());
//Node u = G.getNodeById(uv.u);
//Node v = G.getNodeById(uv.v);
Node w = G.createNode();
superNodeElements.put(w.getId(), new HashSet<Long>());
superNodeElements.get(w.getId()).addAll(superNodeElements.get(uv.u));
superNodeElements.get(w.getId()).addAll(superNodeElements.get(uv.v));
System.out.println("Super nodes: "+Vs);
//update super nodes
Vs.add(w.getId());
Vs.remove(uv.u);
Vs.remove(uv.v);
//update super edges
Es.put(w.getId(), new HashSet<Long>());
HashSet<Long> neigh = get_neighbors(w.getId(), new HashSet<Long>(Vs));//new HashSet<Long>();
/*if(Es.containsKey(uv.u) && !Es.get(uv.u).isEmpty())
neigh.addAll(Es.get(uv.u));
if(Es.containsKey(uv.v) && !Es.get(uv.v).isEmpty())
neigh.addAll(Es.get(uv.v));
neigh.remove(uv.u); neigh.remove(uv.v);*/
for( Long n : neigh)
{
double actual_edges = 0.0;
double pi = superNodeElements.get(w.getId()).size() * superNodeElements.get(n).size();
for(Long su: superNodeElements.get(w.getId())){
for(Long sv: superNodeElements.get(n)){
//System.out.println("su "+su+" sx"+sx);
if(Eg.get(su).contains(sv))
actual_edges++;
}
}
if(actual_edges > ((pi + 1)/2)){
Es.get(w.getId()).add(n);
}
}
//System.out.println(w.getId()+" edges "+Es.get(w.getId()));
for(Long nw: Es.get(w.getId())){
Es.get(nw).add(w.getId());
}
if(Es.containsKey(uv.u)){
for(Long nu: Es.get(uv.u)){
if(nu != uv.u) // avoid removing self edge
Es.get(nu).remove(uv.u);
}
}
if(Es.containsKey(uv.v)){
for(Long nv: Es.get(uv.v)){
if(nv != uv.v) // avoid removing self edge
Es.get(nv).remove(uv.v);
}
}
List<NodePair> addList = new ArrayList<NodePair>();
List<NodePair> removeList = new ArrayList<NodePair>();
//System.out.println(Es);
for(long x : Vs){
if(x == w.getId())
continue;
if(Es.containsKey(uv.u))
{
for(long oneHop : get_neighbors(uv.u, new HashSet<Long>(Vs))){ //Es.get(uv.u)){
if(get_neighbors(oneHop, new HashSet<Long>(Vs)).contains(x)){ //if(Es.get(oneHop).contains(x)){
for(NodePair e: maxHeap){
if((e.u == x || e.v == x) && (e.u == uv.u || e.v == uv.u)){
removeList.add(e);
if(!scost.isAvailable(w.getId(), x)){
double redn_cost = calc_scost(w.getId(), x, edgesDeepCopy(Es), new HashSet<Long>(Vs));
if(redn_cost >= 0.0){
addList.add(new NodePair(w.getId(), x, redn_cost));
scost.put(w.getId(), x, redn_cost);
}
}
}
if((e.u == oneHop || e.v == oneHop) && (e.u == uv.u || e.v == uv.u)){
removeList.add(e);
if(!scost.isAvailable(w.getId(), oneHop)){
double redn_cost = calc_scost(w.getId(), oneHop, edgesDeepCopy(Es), new HashSet<Long>(Vs));
if(redn_cost >= 0.0){
addList.add(new NodePair(w.getId(), oneHop, redn_cost));
scost.put(w.getId(), oneHop, redn_cost);
}
}
}
}
}
}
}
if(Es.containsKey(uv.v))
{
for(long oneHop : get_neighbors(uv.v, new HashSet<Long>(Vs))){//Es.get(uv.v)){
if(get_neighbors(oneHop, new HashSet<Long>(Vs)).contains(x)){ //if(Es.get(oneHop).contains(x)){
for(NodePair e: maxHeap){
if ((e.u == x || e.v == x) && (e.u == uv.v || e.v == uv.v)){
removeList.add(e);
if(!scost.isAvailable(w.getId(), x))
{
double redn_cost = calc_scost(w.getId(), x, edgesDeepCopy(Es), new HashSet<Long>(Vs));
if(redn_cost >= 0.0){
addList.add(new NodePair(w.getId(), x, redn_cost));
scost.put(w.getId(), x, redn_cost);
}
}
}
if((e.u == oneHop || e.v == oneHop) && (e.u == uv.v || e.v == uv.v)){
removeList.add(e);
if(!scost.isAvailable(w.getId(), oneHop)){
double redn_cost = calc_scost(w.getId(), oneHop, edgesDeepCopy(Es), new HashSet<Long>(Vs));
if(redn_cost >= 0.0){
addList.add(new NodePair(w.getId(), oneHop, redn_cost));
scost.put(w.getId(), oneHop, redn_cost);
}
}
}
}
}
}
}
}
/*
for(long oneHop : get_neighbors(uv.u, new HashSet<Long>(Vs))){ //Es.get(uv.u)){
//System.out.println("oneHop "+oneHop);
//System.out.println(Es.get(oneHop));
//System.out.println(Es);
for(long twoHop : get_neighbors(oneHop, new HashSet<Long>(Vs))){ //Es.get(oneHop)){
//System.out.println("twoHop"+twoHop);
if(!Vs.contains(twoHop) || twoHop == w.getId())
continue;
for(NodePair e: maxHeap){
if((e.u == twoHop || e.v == twoHop) && (e.u == uv.u || e.v == uv.u)){
removeList.add(e);
double redn_cost = calc_scost(w.getId(), twoHop, edgesDeepCopy(Es), new HashSet<Long>(Vs));
if(redn_cost > 0.0){
addList.add(new NodePair(w.getId(), twoHop, redn_cost));
scost.put(w.getId(), twoHop, redn_cost);
}
}
//System.out.println("NodePair"+e.toString());
//System.out.println("Es "+Es);
}
//System.out.println(twoHop);
//System.out.println(Es.get(oneHop));
//System.out.println(Es);
}
}
for(Long oneHop : get_neighbors(uv.v, new HashSet<Long>(Vs))){ //Es.get(uv.v)){
for(Long twoHop : get_neighbors(oneHop, new HashSet<Long>(Vs))){ // Es.get(oneHop)){
if(!Vs.contains(twoHop) || twoHop == w.getId())
continue;
for(NodePair e: maxHeap){
if ((e.u == twoHop || e.v == twoHop) && (e.u == uv.v || e.v == uv.v)){
removeList.add(e);
double redn_cost = calc_scost(w.getId(), twoHop, edgesDeepCopy(Es), new HashSet<Long>(Vs));
if(redn_cost > 0.0){
addList.add(new NodePair(w.getId(), twoHop, redn_cost));
scost.put(w.getId(), twoHop, redn_cost);
}
}
}
}
}
*/
maxHeap.removeAll(removeList);
maxHeap.addAll(addList);
addList = new ArrayList<NodePair>();
removeList = new ArrayList<NodePair>();
for(NodePair e: maxHeap){
HashSet<Long> Nw = get_neighbors(w.getId(), new HashSet<Long>(Vs));
//if(Es.get(w.getId()).contains(e.u) || Es.get(w.getId()).contains(e.v)){
if(Nw.contains(e.u) || Nw.contains(e.v)){
removeList.add(e);
double redn_cost = calc_scost(e.u, e.v, edgesDeepCopy(Es), new HashSet<Long>(Vs));
if( redn_cost >= 0.0 ){
addList.add(new NodePair(e.u, e.v, redn_cost));
scost.set(e.u, e.v, redn_cost);
}
}
}
//self edge
if( (Es.containsKey(uv.u) && Es.get(uv.u).contains(uv.v)) || (Es.containsKey(uv.v) && Es.get(uv.v).contains(uv.u)))
Es.get(w.getId()).add(w.getId());
//if(!removeList.isEmpty())
maxHeap.removeAll(removeList);
//if(!addList.isEmpty())
maxHeap.addAll(addList);
Es.remove(uv.u);
Es.remove(uv.v);
//System.out.println("after merge: "+maxHeap);
//System.out.println("Super nodes: "+Vs);
//System.out.println("Super Edges: "+Es);
//System.out.println(superNodeElements);
if(old_size<Vs.size())
break;
else
old_size = Vs.size();
//System.out.println("");
}
System.out.println("Super nodes: "+Vs);
System.out.println("Super Edges: "+Es);
System.out.println(superNodeElements);
}
void output(){
List<Long> list_Vs = new ArrayList<Long>(Vs);
for(int i=0; i<list_Vs.size()-1; i++){
for(int j=i+1; j<list_Vs.size(); j++){
long u = list_Vs.get(i);
long v = list_Vs.get(j);
double actual_edges = 0.0;
double pi = superNodeElements.get(u).size() * superNodeElements.get(v).size();
for(Long su: superNodeElements.get(u)){
for(Long sv: superNodeElements.get(v)){
//System.out.println("su "+su+" sx"+sx);
if(Eg.get(su).contains(sv))
actual_edges++;
}
}
if(actual_edges > ((pi + 1)/2)){
if(ES.containsKey(u))
ES.get(u).add(v);
else{
ES.put(u, new HashSet<Long>());
ES.get(u).add(v);
}
if(ES.containsKey(v))
ES.get(v).add(u);
else{
ES.put(v, new HashSet<Long>());
ES.get(v).add(u);
}
for(Long su: superNodeElements.get(u)){
for(Long sv: superNodeElements.get(v)){
//System.out.println("su "+su+" sx"+sx);
if(!Eg.get(su).contains(sv)){
if(su < sv){
if(nC.containsKey(su))
nC.get(su).add(sv);
else{
nC.put(su, new HashSet<Long>());
nC.get(su).add(sv);
}
}
else{
if(nC.containsKey(sv))
nC.get(sv).add(su);
else{
nC.put(sv, new HashSet<Long>());
nC.get(sv).add(su);
}
}
}
}
}
}
else{
for(Long su: superNodeElements.get(u)){
for(Long sv: superNodeElements.get(v)){
//System.out.println("su "+su+" sx"+sx);
if(Eg.get(su).contains(sv)){
if(su < sv){
if(pC.containsKey(su))
pC.get(su).add(sv);
else{
pC.put(su, new HashSet<Long>());
pC.get(su).add(sv);
}
}
else{
if(pC.containsKey(sv))
pC.get(sv).add(su);
else{
pC.put(sv, new HashSet<Long>());
pC.get(sv).add(su);
}
}
}
}
}
}
}
}
System.out.println("positive constraints: "+pC);
System.out.println("negative constraints: "+nC);
System.out.println("ES: "+ES);
}
public static void main(String args[]) throws Exception{
//File storeDir = new File("C:\\Users\\Giridhar\\Documents\\Neo4j\\movies.db");
//File storeDir = new File("C:\\Users\\Giridhar\\Documents\\Neo4j\\tempGS1.graphdb");
File storeDir = new File("C:\\Users\\Giridhar\\Documents\\Neo4j\\completeGraph3000.graphdb");
//System.out.println(""+sourceDir.getAbsolutePath()+"\n"+storeDir.getAbsolutePath());
//copyFiles(sourceDir, storeDir);
GraphDatabaseService tempGs = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(storeDir)
.setConfig(GraphDatabaseSettings.pagecache_memory, "1g")
.setConfig(GraphDatabaseSettings.allow_store_upgrade, "true")
.newGraphDatabase();
Transaction tx = tempGs.beginTx();
GreedySummary gs = new GreedySummary(tempGs);
gs.initialize();
System.out.println("No. of nodes before merge: "+gs.Vg.size());
System.out.println("Nodes: "+gs.Vg);
//gs.scost.print();
//System.out.println(gs.maxHeap.peek().toString());
//System.out.println(gs.Eg.toString());
long startTime = System.currentTimeMillis();
System.out.println("start time: "+startTime);
gs.merge();
gs.output();
long endTime = System.currentTimeMillis();
System.out.println("end time: "+endTime);
System.out.println("Running Time (in ms): "+(endTime - startTime));
System.out.println("No. of nodes after merge: "+gs.Vs.size());
System.out.println("done");
//System.out.println(gs.Eg);
tx.failure();
tempGs.shutdown();
}
}