how to find short paths

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • latalui
    New Member
    • Sep 2008
    • 10

    how to find short paths

    i am a beginner, guys plizzzzzzzz help me. i am given a assignment to find short paths either using dijkstra algorithm or using binary search algorithm in linux OS.
    check out what is wrong the code i tried
    /program to find shortest path
    Code:
    #include<stdio.h>
    #include<cstdlib>
    #include<cstring>
    #include<fstream>
    #include<iostream.h>
    #define member 1 // use to indicate current node
    #define nonmember 0
    #define infinity 999 // intial distance
    struct arc
    {
    	int wt;
    };
    struct arc g[10][10];
    int path[10]; 
    //end of global defintn
    void jointwt(struct arc g[][10], int n1, int n2 ,int wt)
    // function to assign the weight on arcs
    {
    	g[n1][n2].wt = wt; // assign the weight to arc  n1 -n2
    }
    // end of function joint
     cost(struct arc c[][10], int n)
    // function to get the weights
    {
    	int i,j,wt;
    	for(i=0;i<n;i++)
    	for(j=0;j<n;j++)
    	{
    		printf("\n weight of arc %d %d", i,j);
    		scanf("%d",&wt);
    		jointwt(c,i,j,wt);
    	}
    	printf("\n");
    }
    // end of function
     mat(struct arc a[][10], int n)
    // function to print the weight in matrx
    {
    	int i,j;
    	printf("\n\t");
    	for(i=0;i<n;i++)
    	{
    		 for(j=0;j<n;j++)
    		 {
     			 printf("%d",a[i][j].wt);
    		 }
    		 printf("\n\t");
    	}
    } //end of mat
     shortpath(int wt, int s, int t, int proced, int n)
    struct arc wt[][10];
    int s,t,proced[10];
    int n;
    {
    	int dist[10], perm[10];
    	int curr, i,k,dc,h=0;
    	int smalldist, newdist;
    	for(i=0;i<n;i++)
    	{
    		perm[i] = nonmember;
    		dist[i] =infinity;
    	}
    	 	perm[s] = member;
                    dist[s] = 0;
    		curr = s;
    	while(curr != t)
    	{
    		smalldist = infinity;
    		dc = dist[curr];
    		 for(i=0;i<n;i++)
    			if(perm[i] == nonmember)
    			{
    				newdist = dc + wt[curr][i].wt;
    				if(newdist<smalldist)
    				{
    					dist[i] = newdist;
    					proced[i] = curr;
    				}
    				if(dist[i]<smalldist)
    				{
    					smalldist = dist[i];
    					k=i;
    				}
    			}
    		 curr = k;
    		perm[curr] = member;
    		path[h++] = curr;
    	}
    	printf("\n\t path is = %d", s+1);
    	 for(i=0;i<h;i++)
    	printf("-> %d",path[i]+1);
    	return(dist[t]);
    } // end of short path
    /*int cost(struct arc , int );
    int mat(struct arc , int );
    void jointwt(struct arc , int , int  ,int );
    int shortpath(int , int , int , int , int );*/
    int main()
    {
    	struct arc c[10][10];
    	int n,s,t,proced[10];
    	int x;
     	printf("\n\t\t How many nodes :");
    	scanf("%d",&n);
    	printf("\n\t\t Enter the adjacency matrix of weight");
    	printf("\n\t\t Enter 999 if no edge exits :");
    	cost(c,n);
    	printf("\n\t\t Weight matrix :");
    	mat(c,n);
    	printf("\n\t\t Enter source and distnation  :");
    	scanf("%d %d",&s, &t);
    	printf("\n\t\t shortest path from %d to  %d is:\n",s,t);
    	x = shortpath(c, s - 1, t - 1, proced, n);
    } //efmain
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Does the code compile and run correctly then? Better tell us a specific problem rather than ask us to look for errors in your code.

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      Usually shortest distance will be computed with help of Graphs.
      You should start from there.

      raghu

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Perhaps you could list the specific compiler errors you do not understand.

        Comment

        • latalui
          New Member
          • Sep 2008
          • 10

          #5
          Originally posted by r035198x
          Does the code compile and run correctly then? Better tell us a specific problem rather than ask us to look for errors in your code.
          errors in codes are
          ISO C++ forbids declaration of `cost' with no type
          shortestpath.c: 38: error: ISO C++ forbids declaration of `mat' with no type
          shortestpath.c: 51: error: expected constructor, destructor, or type conversion before "struct"
          shortestpath.c: 51: error: expected `,' or `;' before "struct"
          shortestpath.c: 54: error: expected unqualified-id before '{' token
          shortestpath.c: 54: error: expected `,' or `;' before '{' token
          shortestpath.c: In function `int main()':
          shortestpath.c: 113: error: `shortpath' undeclared (first use this function)
          shortestpath.c: 113: error: (Each undeclared identifier is reported only once for each function it appears in.)

          Comment

          • latalui
            New Member
            • Sep 2008
            • 10

            #6
            Originally posted by Banfa
            Perhaps you could list the specific compiler errors you do not understand.
            errors are:-
            forbids declaration of `cost' with no type
            shortestpath.c: 38: error: ISO C++ forbids declaration of `mat' with no type
            shortestpath.c: 51: error: expected constructor, destructor, or type conversion before "struct"
            shortestpath.c: 51: error: expected `,' or `;' before "struct"
            shortestpath.c: 54: error: expected unqualified-id before '{' token
            shortestpath.c: 54: error: expected `,' or `;' before '{' token
            shortestpath.c: In function `int main()':
            shortestpath.c: 113: error: `shortpath' undeclared (first use this function)
            shortestpath.c: 113: error: (Each undeclared identifier is reported only once for each function it appears in.)

            Comment

            • arnaudk
              Contributor
              • Sep 2007
              • 425

              #7
              Originally posted by gpraghuram
              Usually shortest distance will be computed with help of Graphs.
              You should start from there.

              raghu
              That depends what is meant by "shortest distance". If the OP means "the least distance connecting N points with straight lines" then you use graph theory (i.e. calculate the minimum spanning tree or work out the Steiner tree problem if you can add intermediate points). If it's just the shortest distance between two points along some cost function then you don't need graph theory, the problem can be solved numerically using Dijkstra's algorithm.

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by latalui
                errors are:-
                forbids declaration of `cost' with no type
                shortestpath.c: 38: error: ISO C++ forbids declaration of `mat' with no type
                shortestpath.c: 51: error: expected constructor, destructor, or type conversion before "struct"
                shortestpath.c: 51: error: expected `,' or `;' before "struct"
                shortestpath.c: 54: error: expected unqualified-id before '{' token
                shortestpath.c: 54: error: expected `,' or `;' before '{' token
                shortestpath.c: In function `int main()':
                shortestpath.c: 113: error: `shortpath' undeclared (first use this function)
                shortestpath.c: 113: error: (Each undeclared identifier is reported only once for each function it appears in.)
                Now take each error in turn and examine the line of code given and the line of code before that error. For instance

                shortestpath.c: 38: error: ISO C++ forbids declaration of `mat' with no type

                Line 38 is the definition of you function mat, do you see anything wrong with how you have defined mat?

                Comment

                Working...