top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to implement a C / C++ program for Dijkstra's shortest path algorithm.

+2 votes
779 views

using adjacency matrix representation of graph?

posted Dec 24, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
struct heap{
int vono;
int dis;
};
void makeMinHeap(struct heap[],int,int);
struct heap deleteMinNode(struct heap[]);
void dijkstra(struct heap[],int v);
int noofnode;
int w[4][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
int d[4];
int location[10];
int visited[10];
int main()
{
    struct heap h[10];
    int i;
    w[0][1]=1;
    w[1][2]=2;
    w[2][3]=3;
    w[1][3]=4;
    w[0][3]=8;
    w[0][2]=5;
    visited[0]=0;
    visited[1]=0;
    visited[2]=0;
    visited[3]=0;
    for(i=0;i<10;i++)
        location[i]=-1;

    dijkstra(h,0);
    for(i=0;i<4;i++)
        printf("%d\n",d[i]);
    return 0;
}
void dijkstra(struct heap h[],int v)
{
    struct heap u;
    int i;
    makeMinHeap(h,0,v);
    visited[v]=1;
    d[v]=0;
    while(isEmptyMinHeap())
    {
    u=deleteMinNode(h);
    for(i=0;i<4;i++)
    {
        if(!visited[i]&&w[u.vono][i])
        {
            d[i]=u.dis+w[u.vono][i];
            makeMinHeap(h,d[i],i);
            visited[i]=1;

        }
        else{
          if(w[u.vono][i]&&u.dis+w[u.vono][i]<d[i])
         {
             d[i]=u.dis+w[u.vono][i];
             h[location[i]].dis=u.dis+w[u.vono][i];
        }
    }
    }
    }
}
int isEmptyMinHeap()
{
    return noofnode;
}
void makeMinHeap(struct heap h[],int dis,int i)
{
    int parent;
    struct heap temp;
    h[i].dis=dis;
    h[i].vono=i;
    location[i]=i;
    while(i!=0)
    {
        parent=(i-1)/2;
        if(h[parent].dis>h[i].dis)
        {
            temp=h[parent];
            h[parent]=h[i];
            h[i]=temp;
            location[h[i].vono]=i;
            location[h[parent].vono]=parent;
        }
        else
            break;

        i=parent;

    }
    noofnode++;
}
struct heap deleteMinNode(struct heap h[])
{
    int i,left;
    struct heap temp;
    struct heap value=h[0];
    h[0]=h[noofnode-1];
    location[h[0].vono]=0;
    noofnode--;
    i=0;
    while(i<noofnode)
    {
        left=2*i+1;

        if(h[left].dis>h[left+1].dis&&left<noofnode-1)
        {
            left++;
        }
        if(h[left].dis<h[i].dis&&left<noofnode)
        {
            temp=h[left];
            h[left]=h[i];
            h[i]=temp;
            location[h[i].vono]=i;
            location[h[left].vono]=left;
           }
        else
        break;
        i=left;
    }
  return value;
}
answer Dec 24, 2015 by Rajan Paswan
Similar Questions
+3 votes

How to find shortest path in a multistage graph using dynamic programming, C/C++ code would be helpful?

+2 votes

Assume priority queue in Dijkstra’s algorithm is implemented using a sorted link list and graph G (V, E) is represented using adjacency matrix.

What is the time complexity of Dijkstra’s algorithm (Assume graph is connected)?

+3 votes

Given a 2d array, u need to sort the 2 diagonals independently.
And the elements in diagonal should remain in diagonal only.

INPUT =>
24 2 3 4 7

6 13 8 5 10

11 12 9 14 15

16 21 18 25 20

17 22 23 24 1

OUTPUT =>
1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

21 22 23 24 25

+5 votes
      40
      /\
     20 60
     /\  \
   10 30  80
      /   /\
     25  70 90
           \
           75

longest path 
25 30 20 40 60 80 70 75 
Answer
    25 ----> 75
...