Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ExitCodes PrimAlgorithm(List** arrayOfLists, int vertices, int edges) {
- Edge* pQueue = (Edge*)calloc((size_t)vertices, sizeof(Edge));
- int* indexes = (int*)calloc((size_t)vertices, sizeof(int));
- int* arrVertices = (int*)calloc((size_t)vertices, sizeof(int));
- boolean* used = (boolean*)calloc((size_t)vertices, sizeof(boolean));
- if (!pQueue || !indexes || !arrVertices || !used) {
- return OUT_OF_MEMORY;
- }
- for (int i = 0; i < vertices; i++) {
- pQueue[i].v1 = vertices;
- pQueue[i].v2 = vertices;
- pQueue[i].priority = (i == 0 ? 0 : (ll)MAX_LENGTH + 1);
- indexes[i] = i;
- arrVertices[i] = i;
- used[i] = false;
- }
- int size = vertices - 1;
- for (int i = 0; i < vertices - 1; i++) {
- used[arrVertices[0]] = true;
- swap(&pQueue[0], &pQueue[size - 1], sizeof(Edge));
- swap(&indexes[arrVertices[0]], &indexes[arrVertices[size - 1]], sizeof(int));
- swap(&arrVertices[0], &arrVertices[size - 1], sizeof(int));
- size--;
- int index = 0;
- while (2 * index + 1 < size) {
- //todo
- }
- for (List* cur = arrayOfLists[arrVertices[size]]; cur; cur = cur -> next) {
- if (used[cur -> vertice]) {
- continue;
- }
- if (pQueue[indexes[cur -> vertice]].priority > cur -> length) {
- pQueue[indexes[cur -> vertice]].v1 = cur -> vertice;
- pQueue[indexes[cur -> vertice]].v2 = arrVertices[size];
- pQueue[indexes[cur -> vertice]].priority = cur -> length;
- int curIndex = indexes[cur -> vertice];
- while (curIndex > 0) {
- if (pQueue[curIndex].priority < pQueue[(curIndex - 1) / 2].priority) {
- swap(&pQueue[curIndex], &pQueue[(curIndex - 1) / 2], sizeof(Edge));
- swap(&indexes[arrVertices[curIndex]], &indexes[arrVertices[(curIndex - 1) / 2]], sizeof(int));
- swap(&arrVertices[curIndex], &arrVertices[(curIndex - 1) / 2], sizeof(int));
- curIndex /= 2;
- } else {
- break;
- }
- }
- }
- }
- }
- return SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement