Código de Java - Algoritmo de Kruskal

Imágen de perfil
Val: 712
Bronce
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Algoritmo de Kruskalgráfica de visualizaciones


Java

Publicado el 12 de Octubre del 2020 por Daniel (194 códigos)
6.963 visualizaciones desde el 12 de Octubre del 2020
El algoritmo de Kruskal es un algoritmo de la teoría de grafos para encontrar un árbol recubridor mínimo en un grafo conexo y ponderado.

Es decir, busca un subconjunto de aristas que, formando un árbol, incluyen todos los vértices y donde el valor de la suma de todas las aristas del árbol es el mínimo.

Si el grafo no es conexo, entonces busca un bosque expandido mínimo (un árbol expandido mínimo para cada componente conexa).

1.0

Publicado el 12 de Octubre del 2020gráfica de visualizaciones de la versión: 1.0
6.964 visualizaciones desde el 12 de Octubre del 2020
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

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
import java.io.*;
 
class Kruskal
{
	public static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
 
	public  int [][] G;
	public int [][] t;
	public boolean [][] in;
	public boolean [][] temp;
 
	static int n;
	static int mincost = 0;
	public int k, l, num_ed=0;
 
	public Kruskal(int n) throws IOException
	{
		this.G = new int[n+1][n+1];
		this.in = new boolean[n+1][n+1];
		this.t = new int[n+1][3];
 
		System.out.print("\nDame los costos de las aristas:\n");
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
			{
				in[i][j] = in[j][i] = false;
				if((i!=j)&&(i<j))
				{
					System.out.print(i+" al "+j+": ");
					G[j][i] = G[i][j] = Integer.parseInt(br.readLine());
					if(G[i][j] == 0 )
						G[j][i] = G[i][j] = 7001;
				}
				if(i==j)
					G[i][j]=7001;
			}
	}
 
	public void Kruskals()
	{
		for (int i = 1; i<=n; i++)
		{
			getMinKL();
			if(k==l)
				break;
			System.out.print(l + "-" +k);
			if(formscycle(i-1))
			{
				System.out.println(" --> Ya estan en el mismo conjunto");
				i--;
				continue;
			}
			else
				System.out.println();
 
			mincost = mincost + G[k][l];
			num_ed = (isPresent(i, k))?num_ed:num_ed+1;
			num_ed = (isPresent(i, l))?num_ed:num_ed+1;
 
			t[i][1] = l;
			t[i][2] = k;
			if(num_ed >= n)
			{
				if(allconnect(i))
					return;
			}
 
		}
		System.out.println("\nNo hay solucion");
	}
 
	public boolean allconnect(int i)
	{
		for(int c=2;c<=n;c++)
		{
			temp = new boolean[n+1][n+1];
			for(int a=1;a<=n;a++)
				for(int b=1;b<=n;b++)
					temp[a][b] = temp[b][a] = false;
 
			if(can_reach(1, c, i) == false)
				return false;
		}
		return true;
	}
 
	public boolean formscycle(int i)
	{
		if(isPresent(i, k) && isPresent(i, l))
		{
			temp = new boolean[n+1][n+1];
			for(int a=1;a<=n;a++)
				for(int b=1;b<=n;b++)
					temp[a][b] = temp[b][a] = false;
 
			if(can_reach(k, l, i) )
				return true;
		}
		return false;
	}
 
	public boolean can_reach(int k, int l, int i)
	{
		temp[k][l] = temp[l][k] = true;
		for(int o=1; o<=i; o++)
		{
			if(((k == t[o][1]) && (l == t[o][2])) || ((l == t[o][1]) && (k == t[o][2])))
				return true;
			if((k == t[o][1]) && !(temp[t[o][2]][l]) )
			{
				if(can_reach(t[o][2], l, i) == true)
					return true;
			}
			else if((k == t[o][2]) && !(temp[t[o][1]][l]))
			{
				if(can_reach(t[o][1], l, i) == true)
					return true;
			}
		}
		return false;
	}
 
	public boolean isPresent(int i, int val)
	{
		for(int o=1; o<=i; o++)
			if((val == t[o][1]) || ((val == t[o][2]) ))
				return true;
		return false;
	}
 
	public void getMinKL()
	{
		int k1 = 1, l1 = 1;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
			{
				if((i!=j)&&(i<j))
				{
					if((G[i][j] < G[k1][l1]) && G[i][j] !=0 && in[j][i]==false)
					{
						k1 = i;
						l1 = j;
					}
				}
			}
		if(G[k1][l1] !=0 )
		{
			k =k1; l=l1;
			in[k][l] = in[l][k] = true;
		}
	}
 
	public static void main (String[] args) throws IOException
	{
		System.out.println("\t\t\t\t Algoritmo de Kruskal");
		System.out.print("\nDe el numero de vertices: ");
		n = Integer.parseInt(br.readLine());
 
 
		Kruskal kr=new Kruskal(n);
 
		System.out.println("\n\nSolucion : \n");
		kr.Kruskals();
		System.out.println("\n\n\nEl costo es de: "+ mincost);
	}
 
}



Comentarios sobre la versión: 1.0 (0)


No hay comentarios
 

Comentar la versión: 1.0

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s6590