Skip to content
Snippets Groups Projects
Commit 1410179f authored by 13518104 Kevin Austin Stefano's avatar 13518104 Kevin Austin Stefano
Browse files

add QS to MPI, MP, Serial

parent bef36a93
No related merge requests found
File added
......@@ -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)
{
......
No preview for this file type
......@@ -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);
}
......
......@@ -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
......
File deleted
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