For a school project I have to modify a Dijkstra short path code given by the professor and change the priority queue from a simple array to an heap. The idea is clear but I can't understand what I should include in the heap nodes because in the code I have there are 3 different arrays, one for the distance between two nodes, one with the predecessors of the nodes and one boolean that says if I have yet visited the node.
int *Dijkstra(int s, int n) //restituisce array dei predecessori
{
//S nodi gia' visitati, Q nodi ancora da visitare
int u, v, nVisited;
int *d, *p; //array, distanze e pred dei nodi
bool *Q; //coda di priorita' con array
Vertex *vnext;
d = (int*)malloc(n* sizeof(int));
p = (int*)malloc(n* sizeof(int));
Q = (bool*)malloc(n* sizeof(bool));
for (v = 0; v<n; v++)
{
d[v] = INT_MAX;
p[v] = -1;
Q[v] = true;
}
d[s] = 0;
p[s] = s;
nVisited = 0;
while (nVisited < n)
{
u = extractMin(Q, d, n);
nVisited++;
Q[u] = false; //non e' più da visitare
vnext = Adj[u];
while (vnext!= NULL)
{
v = vnext->id;
if ((d[v] > d[u] + vnext->dist))
{
d[v] = d[u] + vnext->dist;
p[v] = u;
}
vnext = vnext->next;
}
}
return p;
}
Where Vertex is:
typedef struct _Vertex
{
int id;
int dist;
struct _Vertex* next;
} Vertex;
Vertex** Adj;
The Adj array is initialized through a readfile that properly create it.
My idea of heap was this:
typedef struct node {
//What should I put here?
//I thought about int distance, predecessors;
} node ;
typedef struct minHeap {
int size ;
node *elem ;
} minHeap ;