473,394 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

how to find short paths

10
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
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<cstdlib>
  3. #include<cstring>
  4. #include<fstream>
  5. #include<iostream.h>
  6. #define member 1 // use to indicate current node
  7. #define nonmember 0
  8. #define infinity 999 // intial distance
  9. struct arc
  10. {
  11.     int wt;
  12. };
  13. struct arc g[10][10];
  14. int path[10]; 
  15. //end of global defintn
  16. void jointwt(struct arc g[][10], int n1, int n2 ,int wt)
  17. // function to assign the weight on arcs
  18. {
  19.     g[n1][n2].wt = wt; // assign the weight to arc  n1 -n2
  20. }
  21. // end of function joint
  22.  cost(struct arc c[][10], int n)
  23. // function to get the weights
  24. {
  25.     int i,j,wt;
  26.     for(i=0;i<n;i++)
  27.     for(j=0;j<n;j++)
  28.     {
  29.         printf("\n weight of arc %d %d", i,j);
  30.         scanf("%d",&wt);
  31.         jointwt(c,i,j,wt);
  32.     }
  33.     printf("\n");
  34. }
  35. // end of function
  36.  mat(struct arc a[][10], int n)
  37. // function to print the weight in matrx
  38. {
  39.     int i,j;
  40.     printf("\n\t");
  41.     for(i=0;i<n;i++)
  42.     {
  43.          for(j=0;j<n;j++)
  44.          {
  45.               printf("%d",a[i][j].wt);
  46.          }
  47.          printf("\n\t");
  48.     }
  49. } //end of mat
  50.  shortpath(int wt, int s, int t, int proced, int n)
  51. struct arc wt[][10];
  52. int s,t,proced[10];
  53. int n;
  54. {
  55.     int dist[10], perm[10];
  56.     int curr, i,k,dc,h=0;
  57.     int smalldist, newdist;
  58.     for(i=0;i<n;i++)
  59.     {
  60.         perm[i] = nonmember;
  61.         dist[i] =infinity;
  62.     }
  63.          perm[s] = member;
  64.                 dist[s] = 0;
  65.         curr = s;
  66.     while(curr != t)
  67.     {
  68.         smalldist = infinity;
  69.         dc = dist[curr];
  70.          for(i=0;i<n;i++)
  71.             if(perm[i] == nonmember)
  72.             {
  73.                 newdist = dc + wt[curr][i].wt;
  74.                 if(newdist<smalldist)
  75.                 {
  76.                     dist[i] = newdist;
  77.                     proced[i] = curr;
  78.                 }
  79.                 if(dist[i]<smalldist)
  80.                 {
  81.                     smalldist = dist[i];
  82.                     k=i;
  83.                 }
  84.             }
  85.          curr = k;
  86.         perm[curr] = member;
  87.         path[h++] = curr;
  88.     }
  89.     printf("\n\t path is = %d", s+1);
  90.      for(i=0;i<h;i++)
  91.     printf("-> %d",path[i]+1);
  92.     return(dist[t]);
  93. } // end of short path
  94. /*int cost(struct arc , int );
  95. int mat(struct arc , int );
  96. void jointwt(struct arc , int , int  ,int );
  97. int shortpath(int , int , int , int , int );*/
  98. int main()
  99. {
  100.     struct arc c[10][10];
  101.     int n,s,t,proced[10];
  102.     int x;
  103.      printf("\n\t\t How many nodes :");
  104.     scanf("%d",&n);
  105.     printf("\n\t\t Enter the adjacency matrix of weight");
  106.     printf("\n\t\t Enter 999 if no edge exits :");
  107.     cost(c,n);
  108.     printf("\n\t\t Weight matrix :");
  109.     mat(c,n);
  110.     printf("\n\t\t Enter source and distnation  :");
  111.     scanf("%d %d",&s, &t);
  112.     printf("\n\t\t shortest path from %d to  %d is:\n",s,t);
  113.     x = shortpath(c, s - 1, t - 1, proced, n);
  114. } //efmain
Oct 29 '08 #1
7 3118
r035198x
13,262 8TB
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.
Oct 29 '08 #2
gpraghuram
1,275 Expert 1GB
Usually shortest distance will be computed with help of Graphs.
You should start from there.

raghu
Oct 29 '08 #3
Banfa
9,065 Expert Mod 8TB
Perhaps you could list the specific compiler errors you do not understand.
Oct 29 '08 #4
latalui
10
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.)
Nov 6 '08 #5
latalui
10
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.)
Nov 6 '08 #6
arnaudk
424 256MB
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.
Nov 6 '08 #7
Banfa
9,065 Expert Mod 8TB
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?
Nov 6 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Greg Yasko | last post by:
Hi. Does anyone know if there's an equivalent of Perl's file::find module in Python? It traverses a directory. I've googled extensively and checked this newsgroup and can't find anything like it...
0
by: Ethel Aardvark | last post by:
I am running a 9.0.1 database on a W2K server and have come across some strange behaviour with a SQL query. I have a query which runs in a PL/SQL cursor which has several PL/SQL variables used to...
11
by: PÃ¥l Eilertsen | last post by:
Hi, I have recently installed Visual Studio .Net 2003 and am trying to compile and run a simple windows form app (used the VS wizard). When trying to run I get an error message telling me:...
0
by: citizenkahn | last post by:
I am trying to parse a build log for errors. I figure I can do this one of three ways: - find the absolute platonic form of an error and search for that item - create definitions of what patterns...
4
by: Ben | last post by:
Hi We have a list of file paths and short file names in our database. We need to loop through them all, picking up the long file name from the file itself and check that the long file name...
34
by: priyanka | last post by:
Hi, I was wondering if we could parse or do something in the executable( whose source language was C). How can I use some scripting language like perl/python to find out the information about...
14
by: CapCity | last post by:
If I have a strig that represents a path, how can I programmatically get the "short" DOS name for it? For example, if I have "C:\Program Files\" I want "C:\Progra~1\". Thanks
3
by: Alan Cohen | last post by:
This seems like a really, really dumb question, but I can't seem to find the simple answer that it seems to deserve. My C#/ASP.net 2.0 app needs to email a URL link back to the site from a web...
3
by: Alexander Vasilevsky | last post by:
Here, there is a challenge: to find the files must be on a local computer, have the same names. Get drives and folders to go through without problems. But what and how to store files while a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.