LAMPIRAN
LISTING PROGRAM
KodeůlgoritmaBor vka:
using System;usingSystem.Collections.Generic; usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
namespaceJarakTerdekat {
classBoruvka {
public List<int>resultPath;
public double totalJarak;
publicBoruvka() {
resultPath = new List<int>(); totalJarak = 0;
} class Graph {
publicint V, E;
public List<Edge> edge = new List<Edge>(); };
class subset {
publicint parent; publicint rank; };
voidboruvkaMST(Graph graph) {
int V = graph.V; int E = graph.E;
List<Edge> edge = graph.edge;
List<subset> subsets = new List<subset>();
List<int> cheapest = new List<int>();
for (int v = 0; v < V; ++v) {
subsets.Add(new subset()); subsets[v].parent = v; subsets[v].rank = 0; cheapest.Add(-1); }
intnumTrees = V; doubleMSTweight = 0;
while (numTrees> 1) {
for (int i = 0; i < E; i++) {
int set1 = find(subsets, edge[i].src); int set2 = find(subsets, edge[i].dest);
if (set1 == set2) continue;
else
{
if (checkIfCurrentEdgeIsCloserToPreviousCheapest(set1, i, edge, cheapest))
cheapest[set1] = i;
if (checkIfCurrentEdgeIsCloserToPreviousCheapest(set2, i, edge, cheapest))
cheapest[set2] = i; }
int set1 = find(subsets, edge[cheapest[i]].src); int set2 = find(subsets, edge[cheapest[i]].dest);
if (set1 == set2) continue;
MSTweight += edge[cheapest[i]].weight; resultPath.Add(edge[cheapest[i]].index);
totalJarak = MSTweight; return;
}
privateboolcheckIfCurrentEdgeIsCloserToPreviousCheapest(intedgeIn dex, int i, List<Edge> edge, List<int> cheapest)
{
varcond = false;
if (cheapest[edgeIndex] == -1) cond = true;
if (cheapest[edgeIndex] != -1) {
if (edge[cheapest[edgeIndex]].weight > edge[i].weight) cond = true;
graph.edge = new List<Edge>(); return graph;
}
int find(List<subset> subsets, int i) {
if (subsets[i].parent != i) subsets[i].parent =
find(subsets, subsets[i].parent);
return subsets[i].parent; }
void Union(List<subset> subsets, int x, int y) {
intxroot = find(subsets, x); intyroot = 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++;
} }
public List<int> start(List<Edge> edges, intnumOfVertices, intnumOfEdges)
Graph graph = createGraph(numOfVertices, numOfEdges);
graph.edge = edges;
boruvkaMST(graph);
returnresultPath; }
public void displayEdges(List<Edge> edges) {
int i = 1;
foreach (var edge in edges) {
Console.WriteLine(i + ") edge " + edge.src + "-" + edge.dest + " : " + edge.weight);
i++;
} } } }
KodeAlgoritmaPrim:
using System;
usingSystem.Collections.Generic; usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
namespaceJarakTerdekat {
class Prim {
public List<int>resultEdgeCollection;
public List<Edge> edges;
publicint V;
public double totalJarak;
public Prim() {
resultEdgeCollection = new List<int>(); totalJarak = 0;
}
publicintminKey(List<double> key, List<bool>mstSet) {
double min = double.PositiveInfinity; intmin_index = 0;
for (int v = 0; v < V; v++)
min = key[v]; min_index = v; }
returnmin_index; }
public void printMST(List<int> parent, int n, List<List<double>> graph)
{
Console.WriteLine("Edge Weight\n"); for (int i = 1; i < V; i++)
{
Console.WriteLine(String.Format("{0} - {1} {2} \n", parent[i], i, graph[i][parent[i]]));
totalJarak += graph[i][parent[i]]; addEdgeIndexToResult(parent[i], i); }
}
public void addEdgeIndexToResult(intsrc, intdest) {
foreach (var edge in edges) {
if (((edge.src == src) && (edge.dest == dest)) || ((edge.src == dest) && (edge.dest == src)))
{
resultEdgeCollection.Add(edge.index); }
} }
public void primMST(List<List<double>> graph) {
key[i] = double.PositiveInfinity; mstSet[i] = false;
}
key[0] = 0; parent[0] = -1;
for (int count = 0; count < V - 1; count++) {
int u = minKey(key, mstSet);
mstSet[u] = true;
parent.Add(0);
if (((graph[u][v] > 0) ? true : false) &&mstSet[v] == false && graph[u][v] < key[v])
{ parent[v] = u;
key[v] = graph[u][v]; } } }
printMST(parent, V, graph); }
public List<int> main(List<Edge> edges, List<List<double>>graphCollection, int V) {
this.edges = edges; this.V = V;
primMST(graphCollection);
returnresultEdgeCollection; }