Last active
August 22, 2016 01:52
-
-
Save alexnum/5bbe1637c4363301a8a165fcb88f98bd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Artigo ATAL 2016.1 | |
import java.util.*; | |
import java.lang.*; | |
import java.io.*; | |
class Graph | |
{ | |
class Edge implements Comparable<Edge> | |
{ | |
int src, dest, weight; | |
public int compareTo(Edge compareEdge) | |
{ | |
return this.weight-compareEdge.weight; | |
} | |
}; | |
class subset | |
{ | |
int parent, rank; | |
}; | |
int V, E; | |
Edge edge[]; // Array com todos os vértices | |
Graph(int v, int e) | |
{ | |
V = v; | |
E = e; | |
edge = new Edge[E]; | |
for (int i=0; i<e; ++i) | |
edge[i] = new Edge(); | |
} | |
int find(subset subsets[], int i) | |
{ | |
if (subsets[i].parent != i) | |
subsets[i].parent = find(subsets, subsets[i].parent); | |
return subsets[i].parent; | |
} | |
void Union(subset subsets[], int x, int y) | |
{ | |
int xroot = find(subsets, x); | |
int yroot = find(subsets, y); | |
if (subsets[xroot].rank < subsets[yroot].rank) | |
subsets[xroot].parent = yroot; | |
else if (subsets[xroot].rank > subsets[yroot].rank) | |
subsets[yroot].parent = xroot; | |
else | |
{ | |
subsets[yroot].parent = xroot; | |
subsets[xroot].rank++; | |
} | |
} | |
void KruskalMST() | |
{ | |
Edge result[] = new Edge[V]; // Tnis will store the resultant MST | |
int e = 0; // An index variable, used for result[] | |
int i = 0; // An index variable, used for sorted edges | |
for (i=0; i<V; ++i) | |
result[i] = new Edge(); | |
Arrays.sort(edge); | |
subset subsets[] = new subset[V]; | |
for(i=0; i<V; ++i) | |
subsets[i]=new subset(); | |
for (int v = 0; v < V; ++v) | |
{ | |
subsets[v].parent = v; | |
subsets[v].rank = 0; | |
} | |
i = 0; // Index used to pick next edge | |
while (e < V - 1) | |
{ | |
Edge next_edge = new Edge(); | |
next_edge = edge[i++]; | |
int x = find(subsets, next_edge.src); | |
int y = find(subsets, next_edge.dest); | |
if (x != y) | |
{ | |
result[e++] = next_edge; | |
Union(subsets, x, y); | |
} | |
} | |
System.out.println("Following are the edges in the constructed MST"); | |
for (i = 0; i < e; ++i) | |
System.out.println(result[i].src+" -- "+result[i].dest+" == "+ | |
result[i].weight); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment