Hello I have implemented in C Dijkstra's algorithm to find the shortest path, but I need to return the n shortest paths, anyone have an idea how can I do.
My dijkstra function:
int * Dijkstra(graph **g, int totalVertex, int vStart) {
int i;
int *distance = (int*) malloc(totalVertex * sizeof (int));
int *last = (int*) malloc(totalVertex * sizeof (int));
int *visited = (int*) calloc(totalVertex, sizeof (int));
int maxDistance, m;
graph *vertex;
for (i = 0; i < totalVertex; i++) {
distance[i] = MAXINT;
last[i] = -1;
}
distance[vOrigem] = 0;
while (sum(visited, totalVertex) < totalVertex) {
maxDistance = MAXINT;
for (i = 0; i < totalVertex; i++) {
if ((distance[i] < maxDistance) && (visited[i] == 0)) {
maxDistance = distance[i];
m = i;
}
}
vertex = g[m];
while (vertex != NULL) {
if ((vertex->distance + distance[m]) < (distance[vertex-> destination])) {
distance[vertex->destination] = vertex->distance + distance[m];
last[vertex->destination] = m;
}
vertex = vertice->next;
}
visited[m] = 1;
}
free(distance);
free(visited);
return last;
}
I need to call eg 2 times this function and it returns, the two shortest paths in the graph.
Thank you.