diff --git a/src/MST_OpenMP b/src/MST_OpenMP new file mode 100755 index 0000000000000000000000000000000000000000..1cb0a282693bdf20d9286e778158fc9f0b8e5669 Binary files /dev/null and b/src/MST_OpenMP differ diff --git a/src/MST_OpenMP.c b/src/MST_OpenMP.c index f71ec19e094fe5c73956db1bc167eb9316a922f4..cfa88eae1c6fa592cf00233671a3b16703267644 100644 --- a/src/MST_OpenMP.c +++ b/src/MST_OpenMP.c @@ -46,7 +46,95 @@ void PrintGraph(Graph *graph) printf("%d -- %d = %d\n", graph->edge[i].src, graph->edge[i].dest, graph->edge[i].weight); } } +void PartisiSrc(Edge T[], int i, int j, int *k) +{ + Edge pivot = T[i - 1]; + int p = i; + int q = j; + while (p <= q) + { + while (T[p - 1].src < pivot.src) + { + p++; + } + while (T[q - 1].src > pivot.src) + { + q--; + } + if (p < q) + { + Edge temp = T[p - 1]; + T[p - 1] = T[q - 1]; + T[q - 1] = temp; + p++; + q--; + } + else if (p == q) + { + p++; + } + } + *k = q; +} + +// Mengurutkan array dengan menggunakan Quick Sort +void QuickSortSrc(Edge T[], int i, int j) +{ + int k; + if (i < j) + { + PartisiSrc(T, i, j, &k); + // PrintArray(T, i, k); + // PrintArray(T, k + 1, j); + QuickSortSrc(T, i, k); + QuickSortSrc(T, k + 1, j); + } +} +void PartisiWeight(Edge T[], int i, int j, int *k) +{ + Edge pivot = T[i - 1]; + int p = i; + int q = j; + while (p <= q) + { + while (T[p - 1].weight < pivot.weight) + { + p++; + } + while (T[q - 1].weight > pivot.weight) + { + q--; + } + if (p < q) + { + Edge temp = T[p - 1]; + T[p - 1] = T[q - 1]; + T[q - 1] = temp; + p++; + q--; + } + else if (p == q) + { + p++; + } + } + *k = q; +} + +// Mengurutkan array dengan menggunakan Quick Sort +void QuickSortWeight(Edge T[], int i, int j) +{ + int k; + if (i < j) + { + PartisiWeight(T, i, j, &k); + // PrintArray(T, i, k); + // PrintArray(T, k + 1, j); + QuickSortWeight(T, i, k); + QuickSortWeight(T, k + 1, j); + } +} // A function to implement bubble sort void bubbleSort(Edge arr[], int n, int type) { diff --git a/src/MST_OpenMPI b/src/MST_OpenMPI index 556099513aad8e81d4c4db15ab75e054b12b7459..eb441f46946f1c69a252c28dbd476ec82c871769 100755 Binary files a/src/MST_OpenMPI and b/src/MST_OpenMPI differ diff --git a/src/MST_OpenMPI.c b/src/MST_OpenMPI.c index 5ac42e2dd9ee1e58fe21f6417df8e757673249ca..41e0911fd4ef010202950997ab1710d30468a6b9 100644 --- a/src/MST_OpenMPI.c +++ b/src/MST_OpenMPI.c @@ -53,7 +53,7 @@ void PartisiSrc(Edge T[], int i, int j, int *k) { Edge temp = T[p - 1]; T[p - 1] = T[q - 1]; - T[q - 1] = Edge; + T[q - 1] = temp; p++; q--; } @@ -283,7 +283,8 @@ void KruskalMST(Graph *graph, Edge result[], int *e) // Step 1: Sort all the edges into correspondent sorted edges // qsort(graph->edge, graph->E, sizeof(graph->edge[0]), compare); - bubbleSort(graph->edge, graph->E, 1); + // bubbleSort(graph->edge, graph->E, 1); + QuickSortWeight(graph->edge, 0, graph->E); // Step 2: Allocate memory for creating V subsets Subset *subsets = CreateSubset(V); @@ -291,7 +292,6 @@ void KruskalMST(Graph *graph, Edge result[], int *e) { // Pick the smallest edge of the graph Edge next_edge = graph->edge[i++]; - MPI_Send(&x, 1, MPI_INT, 1, 0, MPI_COMM_WORLD); int x = find(subsets, next_edge.src); int y = find(subsets, next_edge.dest); if (x != y) @@ -301,7 +301,7 @@ void KruskalMST(Graph *graph, Edge result[], int *e) } } - bubbleSort(result, (*e), 2); + QuickSortSrc(graph->edge, 0, graph->E); return; // qsort(result, e, sizeof(result[0]), compare_src); } @@ -320,7 +320,7 @@ void printResult(Edge *result, int e) printf("%d\n", minimumCost); - for (int i = start; i < numPartition; i++) + for (int i = 0; i < e; i++) { printf("%d-%d\n", result[i].src, result[i].dest); } diff --git a/src/MST_Serial.c b/src/MST_Serial.c index 6f4a6437722c513d3cb808e7b96f1ef2c73d979e..4f41d8328ee34e26bacca5bf691bb18d08d34339 100644 --- a/src/MST_Serial.c +++ b/src/MST_Serial.c @@ -43,6 +43,96 @@ void PrintGraph(Graph *graph) printf("%d -- %d = %d\n", graph->edge[i].src, graph->edge[i].dest, graph->edge[i].weight); } } +void PartisiSrc(Edge T[], int i, int j, int *k) +{ + Edge pivot = T[i - 1]; + int p = i; + int q = j; + while (p <= q) + { + while (T[p - 1].src < pivot.src) + { + p++; + } + while (T[q - 1].src > pivot.src) + { + q--; + } + if (p < q) + { + Edge temp = T[p - 1]; + T[p - 1] = T[q - 1]; + T[q - 1] = temp; + p++; + q--; + } + else if (p == q) + { + p++; + } + } + *k = q; +} + +// Mengurutkan array dengan menggunakan Quick Sort +void QuickSortSrc(Edge T[], int i, int j) +{ + int k; + if (i < j) + { + PartisiSrc(T, i, j, &k); + // PrintArray(T, i, k); + // PrintArray(T, k + 1, j); + QuickSortSrc(T, i, k); + QuickSortSrc(T, k + 1, j); + } +} + +void PartisiWeight(Edge T[], int i, int j, int *k) +{ + Edge pivot = T[i - 1]; + int p = i; + int q = j; + while (p <= q) + { + while (T[p - 1].weight < pivot.weight) + { + p++; + } + while (T[q - 1].weight > pivot.weight) + { + q--; + } + if (p < q) + { + Edge temp = T[p - 1]; + T[p - 1] = T[q - 1]; + T[q - 1] = temp; + p++; + q--; + } + else if (p == q) + { + p++; + } + } + *k = q; +} + +// Mengurutkan array dengan menggunakan Quick Sort +void QuickSortWeight(Edge T[], int i, int j) +{ + int k; + if (i < j) + { + PartisiWeight(T, i, j, &k); + // PrintArray(T, i, k); + // PrintArray(T, k + 1, j); + QuickSortWeight(T, i, k); + QuickSortWeight(T, k + 1, j); + } +} + void bubbleSort(Edge arr[], int n, int type) { //Jika type ==1, maka diurutkan berdasarkan weight diff --git a/src/hello b/src/hello deleted file mode 100755 index 1de23036653bc3c34fc555ce1c6d1e0e66f45edb..0000000000000000000000000000000000000000 Binary files a/src/hello and /dev/null differ