void dijkstra(graph* g, int src) { int v, V = g->V; // getting the number of indices from the graph. int dist[V]; int i,max = 0; minHeap* mHeap = createMinHeap(V); for(v = 0; v < V; v++) { dist[v] = MAXINT; mHeap->array[v] = createHeapNode(v, dist[v]); mHeap->pos[v] = v; } mHeap->array[src] = createHeapNode(src, dist[src]); mHeap->pos[src] = src; dist[src] = 0; decreaseKey(mHeap, src, dist[src]); mHeap->size = V; while ((mHeap->size!=0)) { minHeapNode* mHeapNode = extractMin(mHeap); int u = mHeapNode->v; node* pNode = g->array[u].head; while (pNode != NULL) { int v = pNode->d; if ((isInMinHeap(mHeap, v) && dist[u] != MAXINT) && ((pNode->w + dist[u]) < dist[v])) { dist[v] = dist[u] + pNode->w; decreaseKey(mHeap, v, dist[v]); } pNode = pNode->next; } } //print the shortest path from distant vertex for(i = 0; i < V; i++) { if(max < dist[i]) max = dist[i]; } printf("most distant vertex is %d\n",max); }
int main() { int i; srand(time(NULL)); Heap * h = createHeap(100); for (i = 0; i < 100; ++i) { if (i > 20) { heapAdd(h, createHeapNode(FLT_MAX, i)); continue; } heapAdd(h, createHeapNode(rand() % 100, i)); } displayHeap(h); for (i = 0; i < 10; ++i) { HeapNode *tmp = heapExtractHead(h); fprintf(stdout, "\nAncien noeud : "); displayHeapNode(tmp); fprintf(stdout, "\n"); tmp->c = rand() * i % 100 + 100; fprintf(stdout, "Nouveau noeud : "); displayHeapNode(tmp); heapAdd(h, tmp); displayHeap(h); } HeapNode * tmp = heapExtract(h, 2); tmp->c = rand() % 100; heapAdd(h, tmp); displayHeap(h); freeHeap(h); return 0; }