Skip to content
Snippets Groups Projects
Commit 039567ee authored by michaelhans's avatar michaelhans
Browse files

Merge branch 'master' of https://gitlab.informatika.org/michaelhans/if3230_mp_mst into master

parents 7031d2e0 1e747acf
Branches
Tags
No related merge requests found
...@@ -6,13 +6,19 @@ Minimum Spanning Tree merupakan suatu tree yang memiliki bobot minimum yang meng ...@@ -6,13 +6,19 @@ Minimum Spanning Tree merupakan suatu tree yang memiliki bobot minimum yang meng
Untuk meningkatkan eksekusi pembangunan tree, maka kami memanfaatkan OpenMP dan OpenMPI sebagai library pemrograman paralel untuk kasus dengan jumlah node > 1000. Untuk meningkatkan eksekusi pembangunan tree, maka kami memanfaatkan OpenMP dan OpenMPI sebagai library pemrograman paralel untuk kasus dengan jumlah node > 1000.
## How to Run ## How to Run
Pertama pastikan adnda berada pada bagian home (bukan pada folder src dan folder test) <br><br>
```
Cara compile program OpenMP:
gcc -o MST_OpenMP MST_OpenMP.c
```
<br>
```
Cara menjalankan program OpenMP: Cara menjalankan program OpenMP:
./MST_OpenMP.out < "../test/test_case_01" ./src/MST_OpenMP < "./test/test_1"
./MST_OpenMP.out ```
## Author ## Author
* Michael Hans / 13518056 * Michael Hans / 13518056
* Kevin Austin Stefano / 13518104 * Kevin Austin Stefano / 13518104
Cara compile program OpenMP:
gcc -o MST_OpenMP.out MST_OpenMP.c
No preview for this file type
File added
No preview for this file type
...@@ -43,7 +43,39 @@ void PrintGraph(Graph *graph) ...@@ -43,7 +43,39 @@ void PrintGraph(Graph *graph)
printf("%d -- %d = %d\n", graph->edge[i].src, graph->edge[i].dest, graph->edge[i].weight); printf("%d -- %d = %d\n", graph->edge[i].src, graph->edge[i].dest, graph->edge[i].weight);
} }
} }
void bubbleSort(Edge arr[], int n, int type)
{
//Jika type ==1, maka diurutkan berdasarkan weight
//Jika type ==2, maka diurutkan berdasarkan src
int i, j;
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (type == 1)
{
if (arr[j].weight > arr[j + 1].weight)
{
Edge temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
else
{
if (arr[j].src > arr[j + 1].src)
{
Edge temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
else if (arr[j].src == arr[j + 1].src && arr[j].dest > arr[j + 1].dest)
{
Edge temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// Create graph based from the adjacency matrix in file // Create graph based from the adjacency matrix in file
Graph *ReadArguments() Graph *ReadArguments()
{ {
...@@ -148,10 +180,15 @@ void KruskalMST(Graph *graph, Edge result[], int *e) ...@@ -148,10 +180,15 @@ void KruskalMST(Graph *graph, Edge result[], int *e)
int i = 0; int i = 0;
// Step 1: Sort all the edges into correspondent sorted edges // Step 1: Sort all the edges into correspondent sorted edges
qsort(graph->edge, graph->E, sizeof(graph->edge[0]), compare); // qsort(graph->edge, graph->E, sizeof(graph->edge[0]), compare);
bubbleSort(graph->edge, graph->E, 1);
// Step 2: Allocate memory for creating V subsets // Step 2: Allocate memory for creating V subsets
Subset *subsets = CreateSubset(V); Subset *subsets = CreateSubset(V);
<<<<<<< HEAD
=======
>>>>>>> fd8239d3b13d39b3a279991ba4a69031b1378216
while (((*e) < V - 1) && (i < graph->E)) while (((*e) < V - 1) && (i < graph->E))
{ {
// Pick the smallest edge of the graph // Pick the smallest edge of the graph
...@@ -159,7 +196,6 @@ void KruskalMST(Graph *graph, Edge result[], int *e) ...@@ -159,7 +196,6 @@ void KruskalMST(Graph *graph, Edge result[], int *e)
int x = find(subsets, next_edge.src); int x = find(subsets, next_edge.src);
int y = find(subsets, next_edge.dest); int y = find(subsets, next_edge.dest);
if (x != y) if (x != y)
{ {
result[(*e)++] = next_edge; result[(*e)++] = next_edge;
...@@ -167,19 +203,17 @@ void KruskalMST(Graph *graph, Edge result[], int *e) ...@@ -167,19 +203,17 @@ void KruskalMST(Graph *graph, Edge result[], int *e)
} }
} }
// Print all minimum spanning tree edges bubbleSort(result, (*e), 2);
qsort(result, (*e), sizeof(result[0]), compare_src);
return; return;
// qsort(result, e, sizeof(result[0]), compare_src);
} }
void printResult(Edge *result, int e) void printResult(Edge *result, int e)
{ {
// printf("%d\n", e); // printf("%d\n", e);
int minimumCost = 0; int minimumCost = 0;
#pragma omp parallel for
for (int i = 0; i < e; i++) for (int i = 0; i < e; i++)
{ {
#pragma omp critical
minimumCost += result[i].weight; minimumCost += result[i].weight;
} }
printf("%d\n", minimumCost); printf("%d\n", minimumCost);
...@@ -189,16 +223,18 @@ void printResult(Edge *result, int e) ...@@ -189,16 +223,18 @@ void printResult(Edge *result, int e)
} }
} }
// Driver program to test above functions // Driver program to test above functions
int main() int main()
{ {
Graph *graph = ReadArguments(); Graph *graph = ReadArguments();
Edge result[graph->V]; Edge result[graph->V];
int e = 0; int e = 0;
// PrintGraph(graph);
int t = clock(); int t = clock();
KruskalMST(graph, result, &e); KruskalMST(graph, result, &e);
t = clock() - t; t = clock() - t;
printf("Waktu Eksekusi: %f ms\n", ((double)t) / CLOCKS_PER_SEC * 1000);
printResult(result, e); printResult(result, e);
printf("Waktu Eksekusi: %f ms\n", ((double)t) / CLOCKS_PER_SEC * 1000);
return 0; return 0;
} }
\ No newline at end of file
File moved
File moved
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment