对于学校项目,我必须修改教授给出的Dijkstra短路径代码,并将优先级队列从简单数组更改为堆。 这个想法很清楚但我无法理解我应该在堆节点中包含什么,因为在代码中我有3个不同的数组,一个用于两个节点之间的距离,一个用于节点的前驱,一个用于布尔如果我还没有访问过节点那就说
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;
}
Vertex的位置:
typedef struct _Vertex
{
int id;
int dist;
struct _Vertex* next;
} Vertex;
Vertex** Adj;
Adj数组通过正确创建它的readfile进行初始化。
我对堆的想法是:
typedef struct node {
//What should I put here?
//I thought about int distance, predecessors;
} node ;
typedef struct minHeap {
int size ;
node *elem ;
} minHeap ;