Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4.  
  5.  
  6. #include "Dijkstra.h"
  7. #include "PQ.h"
  8.  
  9. void clearPredNode (PredNode **AdjList, Vertex v) {
  10.     // If we havent set a pred for that node yet
  11.     if (AdjList[v] == NULL) {
  12.         return;
  13.     }
  14.  
  15.     PredNode *curr = AdjList[v];
  16.     PredNode *temp = NULL;
  17.  
  18.     // If there is only 1 node in the linked list
  19.     if (curr->next == NULL) {
  20.         free (curr);
  21.     } else {
  22.         // Freeing the linked list
  23.         temp = curr->next;
  24.         while (temp != NULL) {
  25.             free (curr);
  26.             curr = temp;
  27.             temp = temp->next;
  28.         }
  29.     }
  30. }
  31.  
  32. void addPredNode (PredNode **AdjList, Vertex v, Vertex w) {
  33.     // v = adj->v, w = edge.key
  34.     PredNode *new = malloc (sizeof (AdjList));
  35.     new->v = w;
  36.     new->next = NULL;
  37.     PredNode *curr = AdjList[v];
  38.     // If this is the first node
  39.     if (curr == NULL) {
  40.         curr = new;
  41.     } else {
  42.         // Check if it is already in the list
  43.         if (curr->v == w) {
  44.             return;
  45.         }
  46.         while (curr->next != NULL) {
  47.             curr = curr->next;
  48.             if (curr->v == w) {
  49.                 return;
  50.             }
  51.         }
  52.         curr->next = new;
  53.     }
  54. }
  55.  
  56. ShortestPaths dijkstra (Graph g, Vertex src) {
  57.     int max = __INT_MAX__;
  58.    
  59.     // Initializing shortestpaths structure sp
  60.     ShortestPaths sp;
  61.     sp.numNodes = GraphNumVertices (g);
  62.     sp.src = src;
  63.     // Allocating memory for dist and pred
  64.     sp.dist = malloc (sp.numNodes * sizeof (sp.dist));
  65.     sp.pred = malloc (sp.numNodes * sizeof (PredNode *));
  66.  
  67.  
  68.     // Initializing values for pred and dist
  69.     for (int i = 0; i < sp.numNodes; i++) {
  70.         sp.dist[i] = max;
  71.         sp.pred[i] = NULL;
  72.     }
  73.  
  74.     // Creating empty queue
  75.     // Note that key is vertex and value is dist
  76.     PQ q = PQNew ();
  77.     // Setting dist[src]
  78.     sp.dist[sp.src] = 0;
  79.     // Initializing queue
  80.     for (int j = 0; j < sp.numNodes; j++) {
  81.         ItemPQ newEdge;
  82.         newEdge.key = j;
  83.         newEdge.value = sp.dist[j];
  84.         PQAdd(q, newEdge);
  85.     }
  86.  
  87.     // While the queue is not empty
  88.     while (PQIsEmpty(q) == false) {
  89.         // Dequeue edge
  90.         ItemPQ edge = PQDequeue (q);
  91.         AdjList adj = GraphOutIncident (g, edge.key);
  92.         // While there are still adjacent nodes
  93.         while (adj != NULL) {
  94.             // Distance holds the distance from src to w
  95.             // If we have not visited w
  96.                 // Relax along edge
  97.                 int distance = sp.dist[edge.key] + adj->weight;
  98.                 if (distance == sp.dist[adj->v]) {
  99.                     addPredNode (sp.pred, adj->v, edge.key);
  100.                 }            
  101.                 else if (distance < sp.dist[adj->v]) {
  102.                     // Updating dist array with our new shortest distance
  103.                     sp.dist[adj->v] = sp.dist[edge.key] + adj->weight;
  104.                     // Update our pred array with new pred after clearing node
  105.                     clearPredNode (sp.pred, adj->v);
  106.                     // Update pred array
  107.                     addPredNode (sp.pred, adj->v, edge.key);
  108.                     // Update the PQ
  109.                     ItemPQ updatePQ;
  110.                     updatePQ.key = adj->v;
  111.                     updatePQ.value = distance;
  112.                     PQUpdate (q, updatePQ);
  113.                 }
  114.             adj = adj->next;
  115.         }
  116.     }
  117.     PQFree (q);
  118.     return sp;
  119.  
  120. }
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127. void showShortestPaths(ShortestPaths sps) {
  128.     return;
  129.  
  130. }
  131. void freeShortestPaths(ShortestPaths sps) {
  132.     return;
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement