dijkstras algorithm

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • onkardesai
    New Member
    • Aug 2009
    • 2

    dijkstras algorithm

    Code:
    #include<stdio.h>
    
    int main()
    {
    	int a,b,w,v,e,i;
    	int g[20][20],visited[20],d[20],p[20];
    	printf("enter the number of vertices");
    	scanf("%d",&v);
    	printf("enter the number of edges");
    	scanf("%d",&e);
    	for(i=1;i<=v;i++)
    	{
    		for(int j=1;j<=v;j++)
    		{
    			g[i][j]=0;
    		}
    	}
    	for(i=1;i<=e;i++)
    	{
    		printf("enter edge info");
    		scanf("%d",&a);
    		scanf("%d",&b);
    		printf("enter weight of the edge");
    		scanf("%d",&w);
    		g[a][b]=g[b][a]=w;
    	}
    	void calldij()
    	  {
    		for(i=1;i<=v;i++)
    		{
    			p[i]=visited[i]=0;
    		}
    		for(i=1;i<=v;i++)
    		{
    			d[i]=32767;
    		}
    		dij();
    	  }
    
    	void dij()
    	{
    		int current,source,dest;
    		printf("enter source and destination vertex");
    		scanf("%d",&source);
    		scanf("%d",&dest);
    
    		current=source;
    		visited[current]=1;
    		d[current]=0;
    		while(current!=dest)
    		{
    			int dc=d[current];
    			for(i=1;i<=v;i++)
    			{
    				if((g[current][i]!=0) && (visited[i]!=1))
    				{
    				if((g[current][i]+dc)<(d[i]))
    				 {
    					d[i]=(g[current][i]+dc);
    					p[i]=current;
    				 }
    				}
    			}
    			int min=32767;
    			for(i=1;i<=v;i++)
    			{
    				if((visited[i]!=1) && (d[i]<min))
    				{
    					min=d[i];
    					current=i;
    				}
    			}
    			visited[current]=1;
    		}
    		printf("shortest distance= %d",d[dest]);
    	}
    	return 0;
    	
    }
    Error on 2nd line of the function dij() and calldij() (declaration syntax error)
    Please revert as soon as possible. URGENT!
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You can not declare functions inside functions in C/C++

    Comment

    • MrPickle
      New Member
      • Jul 2008
      • 100

      #3
      Also, dij() need to come before calldij() because calldij() is calling dij(), or you can pre-define it.

      Comment

      • alexis4
        New Member
        • Dec 2009
        • 113

        #4
        The structure of this program should be as following:

        Code:
        #include<stdio.h>
        
        void calldij (void);
        void dij (void);
        
        int main (void)
        {
        }
        
        void calldij (void)
        {
        }
        
        void dij (void)
        {
        }

        Comment

        • onkardesai
          New Member
          • Aug 2009
          • 2

          #5
          yes i understood my mistake ..dijakstras algorithm is working properly now ..but i have to perform simuation of routing protocol .dijakstras also is jst a part of link state routing ..can u plzz suggest me some site where i can understand the concept to print the tables (routing tables ) at each hop .URGENT

          Comment

          • alexis4
            New Member
            • Dec 2009
            • 113

            #6
            There is a forum for algorithms in this site. See at the top and right of this very page and you will see it. Post your question there!

            Comment

            Working...