import java.util.*;
import sk.upjs.paz.graph.*;
public class Kruskal {
public static void kostra(Graph g) {
int p = 0;
Edge najkratsia;
int menim;
int mensie;
Map<Vertex, Integer> mapa = g.createVertexMap(0);
for (Vertex v : mapa.keySet()) {
mapa.put(v, p);
p++;
}
Set<Edge> s = g.getEdges();
// for (Edge a : s) {
// System.out.println(a + " " + a.getWeight());
// }
List<Edge> zoznam = new ArrayList<>();
List<Edge> kostra = new ArrayList<>();
zoznam.addAll(s);
// Collections.sort(zoznam);
najkratsia = zoznam.get(0);
while (kostra.size() != g.getVertices().size() - 1) {
for (Edge hrana : zoznam) {
if (hrana.getWeight() < najkratsia.getWeight()) {
najkratsia = hrana;
}
}
if (mapa.get(najkratsia.getSource()) < mapa.get(najkratsia
.getTarget())) {
mensie = mapa.get(najkratsia.getSource());
menim = mapa.get(najkratsia.getTarget());
} else {
menim = mapa.get(najkratsia.getSource());
mensie = mapa.get(najkratsia.getTarget());
}
if (menim != mensie) {
for (Vertex v : mapa.keySet()) {
if (mapa.get(v) == menim) {
mapa.put(v, mensie);
}
}
kostra.add(najkratsia);
}
zoznam.remove(najkratsia);
najkratsia = zoznam.get(0);
}
System.out.println(kostra.toString());
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Graph g = Graph.createFromFile("graf.txt");
kostra(g);
}
}