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
No related merge requests found
......@@ -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.
## 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:
./MST_OpenMP.out < "../test/test_case_01"
./MST_OpenMP.out
./src/MST_OpenMP < "./test/test_1"
```
## Author
* Michael Hans / 13518056
* 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)
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
Graph *ReadArguments()
{
......@@ -148,10 +180,15 @@ void KruskalMST(Graph *graph, Edge result[], int *e)
int i = 0;
// 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
Subset *subsets = CreateSubset(V);
<<<<<<< HEAD
=======
>>>>>>> fd8239d3b13d39b3a279991ba4a69031b1378216
while (((*e) < V - 1) && (i < graph->E))
{
// Pick the smallest edge of the graph
......@@ -159,7 +196,6 @@ void KruskalMST(Graph *graph, Edge result[], int *e)
int x = find(subsets, next_edge.src);
int y = find(subsets, next_edge.dest);
if (x != y)
{
result[(*e)++] = next_edge;
......@@ -167,19 +203,17 @@ void KruskalMST(Graph *graph, Edge result[], int *e)
}
}
// Print all minimum spanning tree edges
qsort(result, (*e), sizeof(result[0]), compare_src);
bubbleSort(result, (*e), 2);
return;
// qsort(result, e, sizeof(result[0]), compare_src);
}
void printResult(Edge *result, int e)
{
// printf("%d\n", e);
int minimumCost = 0;
#pragma omp parallel for
for (int i = 0; i < e; i++)
{
#pragma omp critical
minimumCost += result[i].weight;
}
printf("%d\n", minimumCost);
......@@ -189,16 +223,18 @@ void printResult(Edge *result, int e)
}
}
// Driver program to test above functions
int main()
{
Graph *graph = ReadArguments();
Edge result[graph->V];
int e = 0;
// PrintGraph(graph);
int t = clock();
KruskalMST(graph, result, &e);
t = clock() - t;
printf("Waktu Eksekusi: %f ms\n", ((double)t) / CLOCKS_PER_SEC * 1000);
printResult(result, e);
printf("Waktu Eksekusi: %f ms\n", ((double)t) / CLOCKS_PER_SEC * 1000);
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