forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKruskal.java
150 lines (124 loc) · 4.12 KB
/
Kruskal.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
package kruskal;
public class Kruskal {
public static void main(String[] args) {
/* Let the given graph is :
(1)____1___(2)
/ \ / \
3 4 4 6
/ \ / \
/ \ / \
(0)___5___(5)____5___(3)
\ | /
\ | /
\ | /
\ 2 /
6 | 8
\ | /
\ | /
\ | /
\ | /
(4)
*/
Vertice zero = new Vertice(0);
Vertice one = new Vertice(1);
Vertice two = new Vertice(2);
Vertice three = new Vertice(3);
Vertice four = new Vertice(4);
Vertice five = new Vertice(5);
zero.link(one);
zero.link(five);
zero.link(four);
one.link(zero);
one.link(five);
one.link(two);
two.link(five);
two.link(three);
two.link(one);
three.link(two);
three.link(five);
three.link(four);
four.link(zero);
four.link(five);
four.link(three);
five.link(zero);
five.link(one);
five.link(two);
five.link(three);
five.link(four);
Arista a = new Arista(1, 2, 1);
Arista b = new Arista(2, 3, 6);
Arista c = new Arista(3, 4, 8);
Arista d = new Arista(4, 0, 6);
Arista e = new Arista(0, 1, 3);
Arista f = new Arista(1,5,4);
Arista g = new Arista(2,5,4);
Arista h = new Arista(5,3, 5);
Arista i = new Arista(5,4,2);
Arista j = new Arista(5,0,5);
VerticesSet vertSet = new VerticesSet();
vertSet.addVertice(five);
vertSet.addVertice(four);
vertSet.addVertice(three);
vertSet.addVertice(two);
vertSet.addVertice(one);
vertSet.addVertice(zero);
AristContainer artContainer = new AristContainer();
artContainer.add(a);
artContainer.add(b);
artContainer.add(c);
artContainer.add(d);
artContainer.add(e);
artContainer.add(f);
artContainer.add(g);
artContainer.add(h);
artContainer.add(i);
artContainer.add(j);
Grafo graph = new Grafo(6);
graph.addAristsContainer(artContainer);
graph.addVerticesSet(vertSet);
//System.out.println(g.esConexo());
if (graph.isConexo()) {
showNumVertices(graph);
AristContainer solution = kruskal(graph);
salidaKruskal(solution);
checkOutputForLoops(new LoopDetector(solution).hasLoops());
} else {
System.out.println("El grafo pasado en la entrada no es conexo.");
}
}
private static AristContainer kruskal(Grafo g) {
int n = g.getNumVertices();
AristContainer T = new AristContainer();
//La inicialización ya está echa sola al crear Grafo
Arista[] sortedAristas = g.getAristas().getSortedAristContainer();
int uconj, vconj;
for (Arista arista : sortedAristas) {
uconj = g.getPartitionEstructure().seek(arista.getU());
vconj = g.getPartitionEstructure().seek(arista.getV());
if (uconj != vconj) {
g.getPartitionEstructure().merge(uconj, vconj);
T.addArista(arista);
}
if (T.size() >= n) {
break;
}
}
return T;
}
private static void showNumVertices(Grafo g) {
System.out.println("Num vértices:" + g.getNumVertices());
}
private static void salidaKruskal(AristContainer solucion) {
solucion.getSortedAristContainer();
for (Arista arista : solucion) {
System.out.println("Arista: " + arista.getU() + ", " + arista.getV() + "; weight: " + arista.getPeso());
}
}
private static void checkOutputForLoops(boolean tieneBucles) {
if (tieneBucles) {
System.out.println("Has loops");
} else {
System.out.println("Do not has loops");
}
}
}