-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParte3.java
More file actions
163 lines (128 loc) · 5.29 KB
/
Copy pathParte3.java
File metadata and controls
163 lines (128 loc) · 5.29 KB
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
/*Autores:
* Lucia Castillo 202214949
* Andres Molano 202215460
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class Parte3 {
static class Edge implements Comparable<Edge> {
int source, destination, weight;
public Edge(int source, int destination, int weight) {
this.source = source;
this.destination = destination;
this.weight = weight;
}
// Compara las aristas por su peso (costo)
public int compareTo(Edge compareEdge) {
return this.weight - compareEdge.weight;
}
}
static class Subset {
int parent, rank;
}
// Clase que implementa el algoritmo de Kruskal
static class KruskalAlgorithm {
int vertices;
List<Edge> edges;
public KruskalAlgorithm(int vertices) {
this.vertices = vertices;
edges = new ArrayList<>();
}
public void addEdge(int source, int destination, int weight) {
edges.add(new Edge(source, destination, weight));
}
public int find(Subset[] subsets, int u) {
if (subsets[u].parent != u)
subsets[u].parent = find(subsets, subsets[u].parent);
return subsets[u].parent;
}
public void union(Subset[] subsets, int u, int v) {
int rootU = find(subsets, u);
int rootV = find(subsets, v);
// Une los árboles
if (subsets[rootU].rank < subsets[rootV].rank) {
subsets[rootU].parent = rootV;
} else if (subsets[rootU].rank > subsets[rootV].rank) {
subsets[rootV].parent = rootU;
} else {
subsets[rootV].parent = rootU;
subsets[rootU].rank++;
}
}
// Algoritmo de Kruskal para encontrar el MST
public void kruskalMST() {
List<Edge> result = new ArrayList<>();
int e = 0;
int i = 0;
Collections.sort(edges);
// Crea V subconjuntos (uno para cada nodo)
Subset[] subsets = new Subset[vertices];
for (int v = 0; v < vertices; v++) {
subsets[v] = new Subset();
subsets[v].parent = v;
subsets[v].rank = 0;
}
while (e < vertices - 1) {
Edge nextEdge = edges.get(i++);
int x = find(subsets, nextEdge.source);
int y = find(subsets, nextEdge.destination);
// Si no forman un ciclo, incluye la arista en el resultado
if (x != y) {
result.add(nextEdge);
e++;
union(subsets, x, y);
}
}
System.out.println("Las vías que deben convertirse en dobles son:");
for (Edge edge : result) {
System.out.println(edge.source + " -- " + edge.destination + " == " + edge.weight);
}
}
}
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(System.in);
// Pedir el nombre del archivo desde la entrada
System.out.print("Introduce el nombre del archivo (ejemplo: grafo.txt): ");
String nombreArchivo = scanner.nextLine();
// Leer el archivo de texto que contiene el grafo
File file = new File(nombreArchivo);
Scanner fileScanner = new Scanner(file);
// Inicializar variables para calcular el número de vértices
int maxVertex = -1;
List<int[]> edgesFromFile = new ArrayList<>();
// Leer las aristas del archivo
while (fileScanner.hasNext()) {
int source = fileScanner.nextInt();
int destination = fileScanner.nextInt();
int weight = fileScanner.nextInt();
// Guardar aristas temporalmente
edgesFromFile.add(new int[] { source, destination, weight });
// Encontrar el vértice más grande
maxVertex = Math.max(maxVertex, Math.max(source, destination));
}
fileScanner.close();
// El número total de vértices es el máximo vértice + 1 (si el índice comienza
// desde 0)
int vertices = maxVertex + 1;
// Crear el objeto para ejecutar el algoritmo de Kruskal
KruskalAlgorithm graph = new KruskalAlgorithm(vertices);
// Añadir todas las aristas leídas al grafo
for (int[] edge : edgesFromFile) {
graph.addEdge(edge[0], edge[1], edge[2]);
}
// Medir el tiempo de ejecución de Kruskal
long startTime = System.nanoTime(); // Iniciar el cronómetro
// Ejecutar el algoritmo de Kruskal para encontrar el MST
graph.kruskalMST();
long endTime = System.nanoTime(); // Detener el cronómetro
// Calcular el tiempo transcurrido en nanosegundos
long duration = endTime - startTime;
System.out.println("Tiempo de ejecución: " + duration + " nanosegundos.");
} catch (FileNotFoundException e) {
System.out.println("Archivo no encontrado.");
e.printStackTrace();
}
}
}