472,784 Members | 995 Online

# dijkstras algorithm

Expand|Select|Wrap|Line Numbers
1. #include<stdio.h>
2.
3. int main()
4. {
5.     int a,b,w,v,e,i;
6.     int g[20][20],visited[20],d[20],p[20];
7.     printf("enter the number of vertices");
8.     scanf("%d",&v);
9.     printf("enter the number of edges");
10.     scanf("%d",&e);
11.     for(i=1;i<=v;i++)
12.     {
13.         for(int j=1;j<=v;j++)
14.         {
15.             g[i][j]=0;
16.         }
17.     }
18.     for(i=1;i<=e;i++)
19.     {
20.         printf("enter edge info");
21.         scanf("%d",&a);
22.         scanf("%d",&b);
23.         printf("enter weight of the edge");
24.         scanf("%d",&w);
25.         g[a][b]=g[b][a]=w;
26.     }
27.     void calldij()
28.       {
29.         for(i=1;i<=v;i++)
30.         {
31.             p[i]=visited[i]=0;
32.         }
33.         for(i=1;i<=v;i++)
34.         {
35.             d[i]=32767;
36.         }
37.         dij();
38.       }
39.
40.     void dij()
41.     {
42.         int current,source,dest;
43.         printf("enter source and destination vertex");
44.         scanf("%d",&source);
45.         scanf("%d",&dest);
46.
47.         current=source;
48.         visited[current]=1;
49.         d[current]=0;
50.         while(current!=dest)
51.         {
52.             int dc=d[current];
53.             for(i=1;i<=v;i++)
54.             {
55.                 if((g[current][i]!=0) && (visited[i]!=1))
56.                 {
57.                 if((g[current][i]+dc)<(d[i]))
58.                  {
59.                     d[i]=(g[current][i]+dc);
60.                     p[i]=current;
61.                  }
62.                 }
63.             }
64.             int min=32767;
65.             for(i=1;i<=v;i++)
66.             {
67.                 if((visited[i]!=1) && (d[i]<min))
68.                 {
69.                     min=d[i];
70.                     current=i;
71.                 }
72.             }
73.             visited[current]=1;
74.         }
75.         printf("shortest distance= %d",d[dest]);
76.     }
77.     return 0;
78.
79. }
Error on 2nd line of the function dij() and calldij() (declaration syntax error)
Please revert as soon as possible. URGENT!
Dec 20 '09 #1
5 2293
Banfa
9,065 Expert Mod 8TB
You can not declare functions inside functions in C/C++
Dec 20 '09 #2
MrPickle
100 100+
Also, dij() need to come before calldij() because calldij() is calling dij(), or you can pre-define it.
Dec 20 '09 #3
alexis4
113 100+
The structure of this program should be as following:

Expand|Select|Wrap|Line Numbers
1. #include<stdio.h>
2.
3. void calldij (void);
4. void dij (void);
5.
6. int main (void)
7. {
8. }
9.
10. void calldij (void)
11. {
12. }
13.
14. void dij (void)
15. {
16. }
17.
Dec 20 '09 #4
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
Dec 24 '09 #5
alexis4
113 100+
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!
Dec 25 '09 #6