Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.27 KB | None | 0 0
  1. ExitCodes PrimAlgorithm(List** arrayOfLists, int vertices, int edges) {
  2.     Edge* pQueue = (Edge*)calloc((size_t)vertices, sizeof(Edge));
  3.     int* indexes = (int*)calloc((size_t)vertices, sizeof(int));
  4.     int* arrVertices = (int*)calloc((size_t)vertices, sizeof(int));
  5.     boolean* used = (boolean*)calloc((size_t)vertices, sizeof(boolean));
  6.     if (!pQueue || !indexes || !arrVertices || !used) {
  7.         return OUT_OF_MEMORY;
  8.     }
  9.  
  10.     for (int i = 0; i < vertices; i++) {
  11.         pQueue[i].v1 = vertices;
  12.         pQueue[i].v2 = vertices;
  13.         pQueue[i].priority = (i == 0 ? 0 : (ll)MAX_LENGTH + 1);
  14.         indexes[i] = i;
  15.         arrVertices[i] = i;
  16.         used[i] = false;
  17.     }
  18.  
  19.     int size = vertices - 1;
  20.     for (int i = 0; i < vertices - 1; i++) {
  21.         used[arrVertices[0]] = true;
  22.         swap(&pQueue[0], &pQueue[size - 1], sizeof(Edge));
  23.         swap(&indexes[arrVertices[0]], &indexes[arrVertices[size - 1]], sizeof(int));
  24.         swap(&arrVertices[0], &arrVertices[size - 1], sizeof(int));
  25.         size--;
  26.  
  27.         int index = 0;
  28.         while (2 * index + 1 < size) {
  29.             //todo
  30.         }
  31.  
  32.         for (List* cur = arrayOfLists[arrVertices[size]]; cur; cur = cur -> next) {
  33.             if (used[cur -> vertice]) {
  34.                 continue;
  35.             }
  36.  
  37.             if (pQueue[indexes[cur -> vertice]].priority > cur -> length) {
  38.                 pQueue[indexes[cur -> vertice]].v1 = cur -> vertice;
  39.                 pQueue[indexes[cur -> vertice]].v2 = arrVertices[size];
  40.                 pQueue[indexes[cur -> vertice]].priority = cur -> length;
  41.  
  42.                 int curIndex = indexes[cur -> vertice];
  43.                 while (curIndex > 0) {
  44.                     if (pQueue[curIndex].priority < pQueue[(curIndex - 1) / 2].priority) {
  45.                         swap(&pQueue[curIndex], &pQueue[(curIndex - 1) / 2], sizeof(Edge));
  46.                         swap(&indexes[arrVertices[curIndex]], &indexes[arrVertices[(curIndex - 1) / 2]], sizeof(int));
  47.                         swap(&arrVertices[curIndex], &arrVertices[(curIndex - 1) / 2], sizeof(int));
  48.  
  49.                         curIndex /= 2;
  50.                     } else {
  51.                         break;
  52.                     }
  53.                 }
  54.             }
  55.         }
  56.     }
  57.  
  58.     return SUCCESS;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement