diff --git a/README.md b/README.md index 963e49800d7973459fd241c87b2c35461ebaff70..1b7302618541cf7dcc89f28a4827552d724cc018 100644 --- a/README.md +++ b/README.md @@ -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 + diff --git a/src/MST_OpenMP b/src/MST_OpenMP index bd3592b945561c7078d02e3a191cd7487125fa65..31321e2fd1b102ccd7c68b1d6467c0d5478aa3f0 100755 Binary files a/src/MST_OpenMP and b/src/MST_OpenMP differ diff --git a/src/MST_OpenMP.out b/src/MST_OpenMP.out new file mode 100755 index 0000000000000000000000000000000000000000..81f11debdaf328022fa25db0eb3d50a202cea1f0 Binary files /dev/null and b/src/MST_OpenMP.out differ diff --git a/src/MST_Serial b/src/MST_Serial index bea1adf661965ac3d204c4f6295699fcced39a09..3927436b13eb5c70e4182cc7a394f42e3bebbc5a 100755 Binary files a/src/MST_Serial and b/src/MST_Serial differ diff --git a/src/MST_Serial.c b/src/MST_Serial.c index dc07c958b842875892a378e95db339fd511f2563..78971ddaf750a8a6556e6d4a3ec91f77bbfa82c6 100644 --- a/src/MST_Serial.c +++ b/src/MST_Serial.c @@ -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 diff --git a/test/test_case_01.txt b/test/test_1 similarity index 100% rename from test/test_case_01.txt rename to test/test_1 diff --git a/test/test_case_02.txt b/test/test_2 similarity index 100% rename from test/test_case_02.txt rename to test/test_2