-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMst.java
1066 lines (886 loc) · 25.5 KB
/
Mst.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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.io.File;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
/*This class is resposible for all Fibonacci heap operations
* mainly Inserting elelment in the fibonacci heap,remove minimum element
*/
class FibonacciHeap {
private FibonacciHeapNode minNode;
private int nNodes;
public FibonacciHeap() //Fibonacci heap default constructor
{
}
public boolean isEmpty() //Returns null if heap is empty
{
return minNode==null;
}
public void clear() //Resets the heap
{
minNode=null;
nNodes=0;
}
//Decalration to keep the fibonacci heap property
private static final double oneOverLogPhi =
1.0 / Math.log((1.0 + Math.sqrt(5.0)) / 2.0);
//insert functions inserts the node in the heap in O(1) time.
public FibonacciHeapNode insert(FibonacciHeapNode node,double key)
{
node.key=key;
if(minNode!=null)
{
node.left = minNode;
node.right = minNode.right;
minNode.right = node;
node.right.left = node;
if (key < minNode.key) {
minNode = node;
}
}
else {
minNode = node;
}
nNodes++;
return node;
}
public FibonacciHeapNode min() //This function returns Minimum node in the heap.
{
return minNode;
}
public int size() //This function returns the current size of the haep
{
return nNodes;
}
/*This function is a helper function for consolidate function
* for linking of node y and node x in the heap
* */
protected void link(FibonacciHeapNode y, FibonacciHeapNode x)
{
// remove y from root list of heap
y.left.right = y.right;
y.right.left = y.left;
// make y a child of x
y.parent = x;
if (x.child == null) {
x.child = y;
y.right = y;
y.left = y;
} else {
y.left = x.child;
y.right = x.child.right;
x.child.right = y;
y.right.left = y;
}
// increase degree[x]
x.degree++;
// set mark[y] false
y.mark = false;
}
/**
* Removes the smallest element from the heap. This will cause the trees in
* the heap to be consolidated, if necessary.
* Running time is O(log n)
*
*This function returns node with the smallest key
*/
public FibonacciHeapNode removeMin()
{
FibonacciHeapNode z = minNode;
if (z != null) {
int numKids = z.degree;
FibonacciHeapNode x = z.child;
FibonacciHeapNode tempRight;
// for each child of z do...
while (numKids > 0) {
tempRight = x.right;
// remove x from child list
x.left.right = x.right;
x.right.left = x.left;
// add x to root list of heap
x.left = minNode;
x.right = minNode.right;
minNode.right = x;
x.right.left = x;
// set parent[x] to null
x.parent = null;
x = tempRight;
numKids--;
}
// remove z from root list of heap
z.left.right = z.right;
z.right.left = z.left;
if (z == z.right) {
minNode = null;
} else {
minNode = z.right;
consolidate();
}
// decrement size of heap
nNodes--;
}
return z;
}
protected void consolidate()
{
int arraySize =
((int) Math.floor(Math.log(nNodes) * oneOverLogPhi)) + 1;
ArrayList<FibonacciHeapNode> array =
new ArrayList<FibonacciHeapNode>(arraySize);
// Initialize degree array
for (int i = 0; i < arraySize; i++) {
array.add(null);
}
// Find the number of root nodes.
int numRoots = 0;
FibonacciHeapNode x = minNode;
if (x != null) {
numRoots++;
x = x.right;
while (x != minNode) {
numRoots++;
x = x.right;
}
}
// For each node in root list do...
while (numRoots > 0) {
// Access this node's degree..
int d = x.degree;
FibonacciHeapNode next = x.right;
// ..and see if there's another of the same degree.
for (;;) {
FibonacciHeapNode y = array.get(d);
if (y == null) {
// Nope.
break;
}
// There is, make one of the nodes a child of the other.
// Do this based on the key value.
if (x.key > y.key) {
FibonacciHeapNode temp = y;
y = x;
x = temp;
}
// FibonacciHeapNode<T> y disappears from root list.
link(y, x);
// We've handled this degree, go to next one.
array.set(d, null);
d++;
}
// Save this node for later when we might encounter another
// of the same degree.
array.set(d, x);
// Move forward through list.
x = next;
numRoots--;
}
// Set min to null (effectively losing the root list) and
// reconstruct the root list from the array entries in array[].
minNode = null;
for (int i = 0; i < arraySize; i++) {
FibonacciHeapNode y = array.get(i);
if (y == null) {
continue;
}
// We've got a live one, add it to root list.
if (minNode != null) {
// First remove node from root list.
y.left.right = y.right;
y.right.left = y.left;
// Now add to root list, again.
y.left = minNode;
y.right = minNode.right;
minNode.right = y;
y.right.left = y;
// Check if this is a new min.
if (y.key < minNode.key) {
minNode = y;
}
} else {
minNode = y;
}
}
}
/**
* Performs a cascading cut operation. This cuts y from its parent and then
* does the same for its parent, and so on up the tree.
*
*Running time is: O(log n);
*/
protected void cascadingCut(FibonacciHeapNode y)
{
FibonacciHeapNode z = y.parent;
// if there's a parent...
if (z != null) {
// if y is unmarked, set it marked
if (!y.mark) {
y.mark = true;
} else {
// it's marked, cut it from parent
cut(y, z);
// cut its parent as well
cascadingCut(z);
}
}
}
/**
* Decreases the key value for a heap node, given the new value to take on.
* The structure of the heap may be changed and will not be consolidated.
*
* Running time: O(1) amortized
*/
public void decreaseKey(FibonacciHeapNode x, double k)
{
if (k > x.key) {
return;
// throw new IllegalArgumentException(
// "decreaseKey() got larger key value");
}
x.key = k;
FibonacciHeapNode y = x.parent;
if ((y != null) && (x.key < y.key)) {
cut(x, y);
cascadingCut(y);
}
if (x.key < minNode.key) {
minNode = x;
}
}
/*
* The reverse of the link operation: removes x from the child list of y.
* This method assumes that min is non-null.
*
* Running time is: O(1)
*/
protected void cut(FibonacciHeapNode x, FibonacciHeapNode y)
{
// remove x from childlist of y and decrement degree[y]
x.left.right = x.right;
x.right.left = x.left;
y.degree--;
// reset y.child if necessary
if (y.child == x) {
y.child = x.right;
}
if (y.degree == 0) {
y.child = null;
}
// add x to root list of heap
x.left = minNode;
x.right = minNode.right;
minNode.right = x;
x.right.left = x;
// set parent[x] to nil
x.parent = null;
// set mark[x] to false
x.mark = false;
}
/*This overloaded function generates a minimum spanning tree from a given graph
* This function uses RemoveMin and Insert of the Fibonacci heap class for
* calculating MST.
* It returns weight of the minimum calculated tree for inputed Random Graph.
*
*
* */
public void prims_Fhip(Random_Graph graph)
{
Long start=System.currentTimeMillis();
int [] vertex_Array= new int[graph.no_of_vertices];
int [] cost= new int[graph.no_of_vertices];
for(int i=1;i<cost.length;i++)
{
cost[i]=0;
}
int[] final_cost= new int[graph.no_of_vertices];
FibonacciHeapNode [] refofHeap= new FibonacciHeapNode[graph.no_of_vertices];
ArrayList<Integer> visited= new ArrayList<>();
vertex_Array[0]=0;
FibonacciHeapNode node= new FibonacciHeapNode(0, 0);
refofHeap[0]= insert(node, 0);
for(int i=1;i<graph.no_of_vertices;i++)
{
node=new FibonacciHeapNode(i, 99999);
refofHeap[i]= insert(node, 99999);
}
FibonacciHeapNode x= removeMin();
visited.add(x.data);
final_cost[0]=(int)x.key;
for(Edge j=graph.adjecency_list[0].adjList;j!=null;j=j.next)
{
if(!visited.contains(j.vertex_number))
{
//if(cost[j.vertex_number]>j.cost)
{
// cost[j.vertex_number]=j.cost;
vertex_Array[j.vertex_number]=x.data;
}
decreaseKey(refofHeap[j.vertex_number], j.cost);
}
}
for(int i=1;i<graph.no_of_vertices;i++)
{
x= removeMin();
visited.add(x.data);
final_cost[i]=(int)x.key;
cost[i]=(int)x.key;
for(Edge j=graph.adjecency_list[x.data].adjList;j!=null;j=j.next)
{
if(!visited.contains(j.vertex_number))
{
//if(cost[j.vertex_number]>j.cost)
{
// cost[j.vertex_number]=j.cost;
vertex_Array[j.vertex_number]=x.data;
}
decreaseKey(refofHeap[j.vertex_number], j.cost);
}
}
}
int sum=0;
for(int i=0;i<cost.length;i++)
{
sum+=cost[i];
}
long stop= System.currentTimeMillis();
System.out.println("Final cost of minimum spanning tree is(Using Fheap):"+sum);
System.out.println("Time required for an Execution: "+(stop-start)+" Miliseconds");
}
/*This overloaded function generates a minimum spanning tree from a given graph
* This function uses RemoveMin and Insert of the Fibonacci heap class for
* calculating MST.
* It returns weight of the minimum calculated tree for inputed File graph.
*
*
* */
public void prims_Fhip(Mst graph)
{
//Decalaration of the local variables
Long start=System.currentTimeMillis();
int [] vertex_Array= new int[graph.no_of_vertices];
int [] cost= new int[graph.no_of_vertices];
for(int i=1;i<cost.length;i++)
{
cost[i]=0;
}
int[] final_cost= new int[graph.no_of_vertices];
FibonacciHeapNode [] refofHeap= new FibonacciHeapNode[graph.no_of_vertices];
ArrayList<Integer> visited= new ArrayList<>();
vertex_Array[0]=0;
FibonacciHeapNode node= new FibonacciHeapNode(0, 0);
refofHeap[0]= insert(node, 0); //Inserting 1st node in the heap
for(int i=1;i<graph.no_of_vertices;i++)
{
node=new FibonacciHeapNode(i, 99999);//Inserting remaining nodes in on the heap
refofHeap[i]= insert(node, 99999);
}
FibonacciHeapNode x= removeMin();//Removing minimum element from a tree
visited.add(x.data);
final_cost[0]=(int)x.key;
for(Edge j=graph.adjecency_list[0].adjList;j!=null;j=j.next)
{
if(!visited.contains(j.vertex_number))
{
{
vertex_Array[j.vertex_number]=x.data;
}
decreaseKey(refofHeap[j.vertex_number], j.cost);
}
}
for(int i=1;i<graph.no_of_vertices;i++)
{
x= removeMin();
System.out.println(""+ x.data+" "+vertex_Array[x.data]);
visited.add(x.data);
final_cost[i]=(int)x.key; //Adding weight of the minimum edge in the final cost
cost[i]=(int)x.key;
for(Edge j=graph.adjecency_list[x.data].adjList;j!=null;j=j.next)
{
if(!visited.contains(j.vertex_number))
{
{
vertex_Array[j.vertex_number]=x.data;
}
decreaseKey(refofHeap[j.vertex_number], j.cost);
}
}
}
int sum=0;
for(int i=0;i<cost.length;i++)
{
sum+=cost[i];
}
long stop= System.currentTimeMillis();
System.out.println("Final cost of minimum spanning tree is(Using Fheap):"+sum); //Printing final cost
System.out.println("Time required for an Execution: "+(stop-start)+" Miliseconds"); //Printing time required
}
}
/**
* Implements a node of the Fibonacci heap. It holds the information necessary
* for maintaining the structure of the heap. It also holds the reference to the
* key value (which is used to determine the heap structure).
*
*/
class FibonacciHeapNode
{
int data;
FibonacciHeapNode child;
FibonacciHeapNode left;
FibonacciHeapNode right;
FibonacciHeapNode parent;
boolean mark;
double key;
int degree;
public FibonacciHeapNode(int data,double key) {
right=this;
left=this;
this.data= data;
this.key= key;
}
public final double getKey()
{
return key;
}
public final int getData()
{
return data;
}
}
/*Implements the vertex of the graph
* this class holds necessary information for the vertex of the graph*/
class Vertex
{
int number;
Edge adjList;
boolean visited;
public Vertex(int number, Edge adjList, boolean visited) {
this.number = number;
this.adjList = adjList;
this.visited = visited;
}
public Vertex() {
this.number = 0;
this.adjList = null;
this.visited = false;
}
}
/*Implements Edges of the graph.It holds the information about the
* edges of the graph such as associate vertex,cost of the edge and it serves as a
* linked list by keeping link of the next edge
* */
class Edge
{
int vertex_number;
int cost;
Edge next;
public Edge(int vertex_number, int cost, Edge next) {
this.vertex_number = vertex_number;
this.cost = cost;
this.next = next;
}
public Edge() {
this.vertex_number = 0;
this.cost = 0;
this.next = null;
}
}
/*
* This class creates the random graph.It used vertex and edge class for the initializing of graph
* vertices and edges.
*/
class Random_Graph
{
int no_of_vertices;
int no_of_edges;
Vertex [] adjecency_list;
int density;
public Random_Graph(int vertices,int density)
{
int max_edges=(vertices*(vertices-1))/2;
float divide_density=density/100f;
double possible= Math.ceil((max_edges)*divide_density);
no_of_vertices=vertices;
no_of_edges= (int)possible;
// adjecency_list= new Vertex[no_of_vertices];
}
/*
* This function creates a random graph.and returns an instance of it*/
public void createGraph()
{
Random generator= new Random();
Random cost_generator= new Random();
int vertex1,vertex2,edge_cost;
int edge_count=0;
boolean b[][] = new boolean[no_of_vertices][no_of_vertices];
adjecency_list= new Vertex[no_of_vertices];
for(int i=0;i<no_of_vertices;i++)
{
adjecency_list[i]=new Vertex(i,null,false);
}
vertex1=generator.nextInt(no_of_vertices);
vertex2= generator.nextInt(no_of_vertices);
while(vertex1==vertex2)
{
vertex2= generator.nextInt(no_of_vertices);
}
edge_cost=cost_generator.nextInt(1000)+1;
if(!b[vertex1][vertex2] && !b[vertex2][vertex1])
{
adjecency_list[vertex1].adjList= new Edge(vertex2, edge_cost,adjecency_list[vertex1].adjList);
adjecency_list[vertex2].adjList= new Edge(vertex1,edge_cost,adjecency_list[vertex2].adjList);
b[vertex1][vertex2]=true;
b[vertex2][vertex1]=true;
edge_count++;
}
while( edge_count!=no_of_edges || !depthFisrtSearch(this))
{
vertex1=generator.nextInt(no_of_vertices);
vertex2= generator.nextInt(no_of_vertices);
while(vertex1==vertex2)
{
vertex2= generator.nextInt(no_of_vertices);
}
edge_cost=cost_generator.nextInt(1000)+1;
if(!b[vertex1][vertex2] && !b[vertex2][vertex1])
{
adjecency_list[vertex1].adjList= new Edge(vertex2, edge_cost,adjecency_list[vertex1].adjList);
adjecency_list[vertex2].adjList= new Edge(vertex1,edge_cost,adjecency_list[vertex2].adjList);
b[vertex1][vertex2]=true;
b[vertex2][vertex1]=true;
edge_count++;
}
}
System.out.println("Total Edges are:"+edge_count);
// print();
}
/*
* This function returns true if the graph is connected*/
public boolean depthFisrtSearch(Mst Graph_for_dfs)
{
Stack<Integer> stack= new Stack<Integer>();
int [] visited= new int [Graph_for_dfs.no_of_vertices];
for(int i=0;i<visited.length;i++)
{
visited[i]=-1;
}
stack.push(0);
while(!stack.isEmpty())
{
int u=stack.pop();
if(visited[u]==-1)
{
visited[u]=1;
}
for(Edge iterator= Graph_for_dfs.adjecency_list[u].adjList;iterator!=null;iterator=iterator.next)
{
if(visited[iterator.vertex_number]==-1)
{
stack.push(iterator.vertex_number);
}
}
}
int count=0;
for(int i=0;i<visited.length;i++)
{
if(visited[i]==1)
{
count++;
}
}
if(count==Graph_for_dfs.no_of_vertices)
{
//System.out.println("Graph is connected!!");
return true;
}
else
{
System.out.println("Graph is not conneceted by "+(Graph_for_dfs.no_of_vertices-count)+" Nodes");
return false;
}
}
/*/*
* This function returns true if the graph is connected
* Difference is this dfs works for random generated graph*/
public boolean depthFisrtSearch(Random_Graph Graph_for_dfs)
{
Stack<Integer> stack= new Stack<Integer>();
int [] visited= new int [Graph_for_dfs.no_of_vertices];
for(int i=0;i<visited.length;i++)
{
visited[i]=-1;
}
stack.push(0);
while(!stack.isEmpty())
{
int u=stack.pop();
if(visited[u]==-1)
{
visited[u]=1;
}
for(Edge iterator= Graph_for_dfs.adjecency_list[u].adjList;iterator!=null;iterator=iterator.next)
{
if(visited[iterator.vertex_number]==-1)
{
stack.push(iterator.vertex_number);
}
}
}
int count=0;
for(int i=0;i<visited.length;i++)
{
if(visited[i]==1)
{
count++;
}
}
if(count==Graph_for_dfs.no_of_vertices)
{
//System.out.println("Graph is connected!!");
return true;
}
else
{
// System.out.println("Graph is not conneceted by "+(Graph_for_dfs.no_of_vertices-count)+" Nodes");
return false;
}
}
/*
* prints the graph*/
public void print()
{
for(int i=0;i<adjecency_list.length;i++)
{
for(Edge next=adjecency_list[i].adjList;next!=null;next=next.next)
{
System.out.println(adjecency_list[i].number+" "+next.vertex_number+" "+next.cost);
}
}
}
}
/*This is the main class which reads the input and handles the flow of the program*/
public class Mst {
int no_of_vertices;
int no_of_edges;
Vertex [] adjecency_list;
public Mst(int no_of_vertices, int no_of_edges, Vertex[] adjecency_list) {
this.no_of_vertices = no_of_vertices;
this.no_of_edges = no_of_edges;
this.adjecency_list = adjecency_list;
}
/*Reads the input file provided and creates the graph*/
public void createGraph(String filename)
{
try {
Scanner Reader=null;
File file1= new File(filename);
Reader= new Scanner(file1);
this.no_of_vertices=Reader.nextInt();
this.no_of_edges=Reader.nextInt();
adjecency_list= new Vertex[no_of_vertices];
boolean b[][] = new boolean[no_of_vertices][no_of_vertices];
//Reading vertices numbers
for(int i=0;i<no_of_vertices;i++)
{
adjecency_list[i]=new Vertex(i,null,false);
}
while(Reader.hasNext())
{
int vertex1=Reader.nextInt();
int vertex2= Reader.nextInt();
int edge_cost=Reader.nextInt();
if(!b[vertex1][vertex2] && !b[vertex2][vertex1])
{
adjecency_list[vertex1].adjList= new Edge(vertex2, edge_cost, adjecency_list[vertex1].adjList);
adjecency_list[vertex2].adjList= new Edge(vertex1,edge_cost,adjecency_list[vertex2].adjList);
b[vertex1][vertex2]=true;
b[vertex2][vertex1]=true;
}
}
Reader.close();
//print();
} catch (Exception e) {
// TODO: handle exception
System.out.println("Exception occured!!!");
}
}
public Mst() {
this.no_of_vertices = 0;
this.no_of_edges = 0;
this.adjecency_list =null;
}
/*Prints the graph*/
public void print()
{
for(int i=0;i<adjecency_list.length;i++)
{
for(Edge next=adjecency_list[i].adjList;next!=null;next=next.next)
{
System.out.println(adjecency_list[i].number+" "+next.vertex_number+" "+next.cost);
}
}
}
public static void main(String[] args) {
if(args.length<0)
{
System.out.println("Provide input!!");
}
else
{
if(args.length==2)
{
String filename=args[1];
if(args[0].equals("-s"))
{
System.out.println("Simple scheme output");
System.out.println("********************************************");
Mst graph= new Mst();
graph.createGraph(filename);
new Prims(graph);
}
else
{
System.out.println("Fibonacci scheme output");
System.out.println("********************************************");
//System.out.println(filename);
Mst graph= new Mst();
graph.createGraph(filename);
FibonacciHeap fhip= new FibonacciHeap();
fhip.prims_Fhip(graph);
}
}
else if(args.length==3)
{
int n= Integer.parseInt(args[1]);
int d=Integer.parseInt(args[2]);
Random_Graph rg= new Random_Graph(n, d);
rg.createGraph();
new Prims(rg);
FibonacciHeap fhip= new FibonacciHeap();
fhip.prims_Fhip(rg);
}
}
}
}
/*This class is responsible for calculating minimum spanning tree from a given graph*/
class Prims
{
Mst graph;
Random_Graph graph1;
int i=0,j=0,k=0;
int min=99999999;
int []mincost;
int counter=0;
int ssss=0;
int final_edges[][];
ArrayList<Integer> visited;
//calculate MST for random graph
public Prims(Random_Graph graph)
{
long start= System.currentTimeMillis();
this.graph1=graph;
mincost= new int[graph.no_of_vertices];
visited= new ArrayList<Integer>();
i=0;
for(Edge iterator=graph.adjecency_list[0].adjList;iterator!=null;iterator=iterator.next)
{
if(iterator.cost<min)
{
min=iterator.cost;
i=iterator.vertex_number;
}
}
mincost[counter]=min;
graph.adjecency_list[0].visited=true;
graph.adjecency_list[i].visited=true;
counter++;
visited.add(graph.adjecency_list[0].number);
visited.add(graph.adjecency_list[i].number);
while(visited.size()<graph.no_of_vertices)
{
min=9999999;
for(int j=0;j<visited.size();j++)
{
for(Edge Iterator= graph.adjecency_list[visited.get(j)].adjList;Iterator!=null;Iterator=Iterator.next)
{
if(Iterator.cost<min && !visited.contains(Iterator.vertex_number))
{
min=Iterator.cost;
i=Iterator.vertex_number;
}
}
}
visited.add(i);
mincost[counter]=min;
counter++;
}
min=0;
for(k=0;k<mincost.length;k++)
{
min= mincost[k]+min;
}
System.out.println("Final cost of spanning tree is(using simple scheme): "+min);
long stop= System.currentTimeMillis();
System.out.println("Time for execution: "+(stop-start)+" Miliseconds");
}
//calculate MST for input file graph
public Prims(Mst graph)
{
final_edges=new int[graph.no_of_vertices][graph.no_of_vertices];
long start=System.currentTimeMillis();
this.graph=graph;
mincost= new int[graph.no_of_vertices];
visited= new ArrayList<Integer>();
i=0;
for(Edge iterator=graph.adjecency_list[0].adjList;iterator!=null;iterator=iterator.next)