# Graph problem

Printable View

• 12-20-2009
bananaHUNT
Graph problem
There's an array of structs, containing the different routes between each points. For example:
http://img693.imageshack.us/img693/9682/96931372.png

Now what my program should do is, when a user inputs his starting position and the desired ending position, all possible ways are shown. For example if he starts at F and wants to go to E, then the following is shown:
Code:

```Route:   F -> I   I -> E Total distance: 80 Route:   F -> B   B -> E Total distance: 95 Route:   F -> C   C -> H   H -> G   G -> B   B -> E Total distance: ##```
My code so far, it doesn't work but I'm kind of stuck.
Could someone go over it for me?

Code:

```struct Route {     string  from;     string  to;     int      distance; }; const int Total_routes = 10; Route routes [ Total_routes ] =     { { "F", "I", 20 }, { "F", "B", 45 }, { "H", "C", 25 },       { "B", "E", 50 }, { "I", "E", 60 }, { "F", "C", 60 },       { "H", "G", 50 }, { "B", "G", 30 },       { "H", "D", 45 }, { "A", "E", 40 } } ; void show_route_taken ( int max_stations , Route routes[] ) { } void swap_route ( Route routes[] , int n ) {     string tempstring = routes[n].from;     routes[n].from = routes[n].to;     routes[n].to = tempstring; } bool been_there ( string point , Route temp_routes[] ) {     int n ( 0 );     for ( int t ( 0 ) ; t < Total_routes ; t++ )     {         if ( point.compare(temp_routes[t].from) == 0 || point.compare(temp_routes[t].to) == 0 )         {             n++;         }     }     if ( n > 0 )     {         return true;     }     else     {         return false;     } } void find_route(string point_start,string point_finish,string temp_point_start,string temp_point_finish,Route routes[],Route temp_routes[],int points_visited) {     if (point_finish.compare(temp_point_finish)==0)     {         cout << endl << temp_point_finish << "  - route found" << endl;         //show_route_taken();     }     else     {         int b(0);         string i[Total_routes];         for ( int a ( 0 ) ; a < Total_routes ; a++ )         {             if ( ((routes[a].from.compare(temp_point_finish) == 0) && (routes[a].to.compare(temp_point_start) != 0) ) || ((routes[a].to.compare(temp_point_finish) == 0) && (routes[a].from.compare(temp_point_start) != 0) ))             {                 if ( routes[a].to.compare(temp_point_finish) == 0 )                 {                     swap_route ( routes , a );                 }                 i[b] = routes[a].to;                 b++;             }         }         for ( int c ( 0 ) ; c < b ; c++ )         {             if ( !been_there (i[c],temp_routes) )             {                 temp_routes[points_visited].from = temp_point_finish;                 temp_routes[points_visited].to = i[c];                 find_route(point_start,point_finish,temp_point_finish,i[c],routes,temp_routes,points_visited);             }         }     } } void travel ( Route routes[] , Route temp_routes[] ) {     int points_visited ( 0 );     string startstr, finishstr, temp_point_finish;     cout << "Input starting point: ";     getline(cin,startstr);     cout << "Input finishing point: ";     getline(cin,finishstr);     for ( int g ( 0 ) ; g < Total_routes ; g++ )     {         if ( routes[g].from.compare(startstr) == 0 || routes[g].to.compare(startstr) == 0 )         {             if ( routes[g].to.compare(startstr) == 0 )             {                 swap_route ( routes , g );             }             temp_point_finish = routes[g].to;             find_route (startstr,finishstr,startstr,temp_point_finish,routes,temp_routes,points_visited);         }         for ( int x ( 0 ) ; x < Total_routes ; x++ )         {             temp_routes[x].from = " ";             temp_routes[x].to = " ";         }         points_visited = 0;     } } int main () {     Route temp_routes[Total_routes];     travel (routes,temp_routes);     return 0 ; }```