Skip to content

Commit 124fe76

Browse files
LauraLaureusjainaman224
authored andcommitted
Kruskal_OOP in Java (jainaman224#252)
* Kruskal_OOP in Java * Remove unnecessary files Only data types, and Test files as well as Main kruskal are now the project
1 parent 63b5a54 commit 124fe76

File tree

10 files changed

+647
-0
lines changed

10 files changed

+647
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package kruskal;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.Comparator;
6+
7+
public class AristContainer extends ArrayList<Arista> {
8+
9+
10+
public void addArista(Arista nueva) {
11+
12+
if (!this.contains(nueva)) {
13+
this.add(nueva);
14+
}
15+
}
16+
17+
@Override
18+
public Arista[] toArray() {
19+
Arista[] arista = new Arista[this.size()];
20+
for (int i = 0; i < this.size(); i++) {
21+
arista[i] = this.get(i);
22+
}
23+
return arista;
24+
}
25+
26+
public Arista[] getSortedAristContainer() {
27+
Comparator<Arista> comparador = new Comparator<Arista>() {
28+
29+
@Override
30+
public int compare(Arista o1, Arista o2) {
31+
return new Integer(o1.getPeso()).compareTo(o2.getPeso());
32+
}
33+
};
34+
35+
/*
36+
Esta implementación es un adaptativa, mergesort iterativo estable
37+
que requiere muchas menos comparaciones que n lg (n)
38+
cuando la matriz de entrada está parcialmente ordenada,
39+
al tiempo que ofrece el rendimiento de un mergesort tradicionales
40+
cuando la matriz de entrada se ordena al azar.
41+
Si la matriz de entrada está casi ordenada,
42+
la aplicación requiere aproximadamente n comparaciones.
43+
Los requisitos de almacenamiento temporales varían de una pequeña
44+
constante para matrices de entrada casi ordenados a n / 2
45+
referencias de objetos para matrices de entrada ordenados aleatoriamente.
46+
http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort%28java.util.List%29
47+
*/
48+
Collections.sort(this, comparador);
49+
return this.toArray();
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package kruskal;
2+
3+
public class Arista implements Comparable<Arista> {
4+
5+
private final Integer u;
6+
private final Integer v;
7+
8+
public int getU() {
9+
return u;
10+
}
11+
12+
public int getV() {
13+
return v;
14+
}
15+
private final int weight;
16+
17+
public Arista(int a, int b, int peso) {
18+
this.u = a;
19+
this.v = b;
20+
this.weight = peso;
21+
}
22+
23+
public int getPeso() {
24+
return this.weight;
25+
}
26+
27+
@Override
28+
public int compareTo(Arista o) {
29+
if (this.weight == o.weight) {
30+
return 0;
31+
}
32+
return this.weight - o.weight;
33+
}
34+
35+
@Override
36+
public boolean equals(Object arista) {
37+
if (!(arista instanceof Arista)) {
38+
return false;
39+
}
40+
Arista o = (Arista) arista;
41+
if (this.getU() == o.getU() && this.getV() == o.getV()) {
42+
return true;
43+
} else if (this.getU() == o.getV() && this.getV() == o.getU()) {
44+
return true;
45+
}
46+
return false;
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package kruskal;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileReader;
6+
import java.io.IOException;
7+
8+
public class CVSCargador {
9+
10+
private BufferedReader lector = null;
11+
12+
public CVSCargador(String ruta) {
13+
try {
14+
this.lector = new BufferedReader(new FileReader(ruta));
15+
} catch (FileNotFoundException ex) {
16+
System.out.println("File not found");
17+
}
18+
}
19+
20+
public Grafo carga() {
21+
22+
String line = readLine();
23+
String[] splitLine;
24+
Arista arista;
25+
Vertice a, b;
26+
AristContainer contAristas = new AristContainer();
27+
VerticesSet vertices = new VerticesSet();
28+
29+
while (line != null) {
30+
//si comienza por # es un comentario y nos lo saltamos
31+
if ((line.length() > 0) && (line.charAt(0) != '#')) {
32+
splitLine = line.split(",");
33+
a = translateLineToVertice(splitLine, 0);
34+
b = translateLineToVertice(splitLine, 1);
35+
36+
if (a != null && b != null) {
37+
if (vertices.contains(a)) {
38+
a = vertices.getVertice(a.getId());
39+
}
40+
if (vertices.contains(b)) {
41+
b = vertices.getVertice(b.getId());
42+
}
43+
a.link(b);
44+
b.link(a);
45+
}
46+
vertices.addVertice(a);
47+
vertices.addVertice(b);
48+
49+
arista = genArista(splitLine);
50+
if (arista != null) {
51+
contAristas.addArista(arista);
52+
}
53+
}
54+
55+
line = readLine();
56+
}
57+
58+
Grafo resultado = new Grafo(vertices.size());
59+
resultado.addAristsContainer(contAristas);
60+
resultado.addVerticesSet(vertices);
61+
62+
return resultado;
63+
}
64+
65+
private String readLine() {
66+
String line;
67+
try {
68+
line = lector.readLine();
69+
} catch (IOException ex) {
70+
line = null;
71+
}
72+
73+
return line;
74+
}
75+
76+
private Integer[] traduceLinea(String[] lineaPartida) {
77+
78+
//si es Inf entonces no existe arista pero existen los vértices
79+
for (int i = 0; i < lineaPartida.length; i++) {
80+
if (lineaPartida[i].equalsIgnoreCase("Inf")) {
81+
lineaPartida[i] = "-1";
82+
}
83+
}
84+
85+
Integer[] resultado = {
86+
Integer.parseInt(lineaPartida[0]),
87+
Integer.parseInt(lineaPartida[1]),
88+
Integer.parseInt(lineaPartida[2])
89+
};
90+
91+
if (resultado[0] < 0) {
92+
resultado[0] = null;
93+
} else if (resultado[1] < 0) {
94+
resultado[1] = null;
95+
} else if (resultado[2] < 0) {
96+
resultado[2] = null;
97+
}
98+
99+
return resultado;
100+
}
101+
102+
private Vertice translateLineToVertice(String[] lineaPartida, int i) {
103+
104+
if (Character.isDigit(lineaPartida[i].charAt(0))) {
105+
return new Vertice(Integer.parseInt(lineaPartida[i]));
106+
}
107+
return null;
108+
109+
}
110+
111+
private Arista genArista(String[] lineaPartida) {
112+
Integer[] lineaTraducida = traduceLinea(lineaPartida);
113+
if (lineaTraducida[0] != null
114+
&& lineaTraducida[1] != null
115+
&& lineaTraducida[2] != null) {
116+
return new Arista(
117+
lineaTraducida[0],
118+
lineaTraducida[1],
119+
lineaTraducida[2]
120+
);
121+
}
122+
return null;
123+
}
124+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package kruskal;
2+
3+
import java.util.HashMap;
4+
import java.util.HashSet;
5+
6+
class ConnectionStructure {
7+
8+
private final HashSet<Integer> conectados;
9+
private final HashMap<Integer, Vertice> conjunto;
10+
11+
public ConnectionStructure(HashMap<Integer, Vertice> conjunto) {
12+
conectados = new HashSet<>();
13+
this.conjunto = conjunto;
14+
}
15+
16+
public void conecta(Integer v) {
17+
conectados.add(v);
18+
}
19+
20+
public boolean isCompletelyConnected() {
21+
exploraGrafo(this.conjunto.entrySet().iterator().next().getValue());
22+
return conectados.size() == conjunto.size();
23+
}
24+
25+
private void exploraGrafo(Vertice v) {
26+
if (!estaVerticeConectado(v.getId())) {
27+
conecta(v.getId());
28+
for (Vertice value : v.getDifferentWays().values()) {
29+
exploraGrafo(value);
30+
}
31+
}
32+
}
33+
34+
public boolean estaVerticeConectado(Integer v) {
35+
return conectados.contains(v);
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package kruskal;
2+
3+
public class Grafo {
4+
5+
private final int numVertices;
6+
private AristContainer aristas;
7+
private final PartitionStructure partitionStructure;
8+
private VerticesSet vertices;
9+
10+
public Grafo(int numVertices) {
11+
this.numVertices = numVertices;
12+
partitionStructure = new PartitionStructure(numVertices);
13+
}
14+
15+
public int getNumVertices() {
16+
return numVertices;
17+
}
18+
19+
public PartitionStructure getPartitionEstructure() {
20+
return partitionStructure;
21+
}
22+
23+
public void addAristsContainer(AristContainer contenedorAristas) {
24+
this.aristas = contenedorAristas;
25+
}
26+
27+
public AristContainer getAristas() {
28+
return aristas;
29+
}
30+
31+
public void addVerticesSet(VerticesSet vertices) {
32+
this.vertices = vertices;
33+
}
34+
35+
public boolean isConexo() {
36+
return vertices.esConexo();
37+
}
38+
}

0 commit comments

Comments
 (0)