void dijkstras(int count,int matrix[count][count]) { int i,j,d[count],value[count],n=count; for(i=0;i<count;i++) { value[i]=0;d[i]=INT_MAX; } d[0]=0; int min=minheap(count,d,value); while(min>=0) { value[min]=1; for(i=0;i<count;i++) { if(matrix[min][i]>=0 && d[i]>d[min]+matrix[min][i]) //Checks for adjesent nodes which satisify d[i]>d[min]+matrix[min][i] { d[i]=d[min]+matrix[min][i]; } } min=minheap(count,d,value); n--; } printf("\n\nThe Shortest Path is: { "); //Print the distance for (i=0;i<count;i++) { if(d[i]==INT_MAX) printf("(s%d,%s)",i,"Unreachable"); else printf("(s%d,%d)",i,d[i]); if(i==count-1) printf(" }\n\n"); else printf(", "); } }
int pop_min(int *s, int *m) { swap(s, 0, --(*m)); minheap(s, 0, *m); return s[*m]; }
void make_minheap(int *s, int m) { int i; for (i = (m-1) / 2; i >= 0; i--) { minheap(s, i, m); } }
void minheap(int *s, int i, int m) { int left = 2 * i + 1; int right = 2 * i + 2; int smallest = i; /* pay attention to the comparison is between the values of a[s[i]] but not s[i] itself. */ if (left < m && a[s[left]] < a[s[smallest]]) smallest = left; if (right < m && a[s[right]] < a[s[smallest]]) smallest = right; if (smallest != i) { swap(s, i, smallest); minheap(s, smallest, m); } }