| | |
need help with dijkstra program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 77
Reputation:
Solved Threads: 0
I am doing a program to print out the shortest path between locations. I implemented it using a 2D array for the adjacency matrix.
However, when I run it and input the start and end locations, nothing else happens.
However, when I run it and input the start and end locations, nothing else happens.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <limits.h> #include <assert.h> #include <cstdlib> using namespace std; #define INF UINT_MAX //define INF as infinity value #define locationCount 9 //define number of locations = 9 #define stack_limit 10000 //stack limit typedef enum location {changi, punggol, sembawang, woodland, bukitbatok, clementi, outram, kallang, bedok, notExist}; const location first_location = changi; const location last_location = bedok; char* name[] = { "changi", "punggol", "sembawang", "woodland", "bukit batok", "clementi", "outram", "kallang", "bedok" }; //Adjacency Matrix unsigned int weightedGraph[locationCount][locationCount] = { // ch pu sem wl bb cle out kll bed {INF, 19 , INF, INF, INF, INF, INF, INF, 10 },//changi {19 , INF, 17 , INF, INF, INF, INF, 17 , INF },//punggol {INF, 17 , INF, 5 , INF, INF, INF, 22 , INF },//sembawang {INF, INF, 5 , INF, 14 , INF, INF, 24 , INF },//woodland {INF, INF, INF, 14 , INF, 5 , INF, 25 , INF },//bukit batok {INF, INF, INF, INF, 5 , INF, 10 , 17 , INF },//clementi {INF, INF, INF, INF, INF, 10 , INF, 7 , 14 },//outram {INF, 17 , 22, 24, 25 , 17 , 7 , INF, 10 },//kallang {10 , INF, INF, INF, INF, INF, 14, 10, INF } //bedok }; unsigned int overEst[locationCount]; bool tight[locationCount]; location predecessor[locationCount]; location minNonTight() { location i, j; for(i = first_location; i <= last_location; i+1) { if(!tight[i]) break; } assert(j <= last_location); j = i; for(i+1; i<= last_location; i+1) { if(!tight[i] && overEst[i] < overEst[j]) j = i; } return j; } bool successor(int i, int j) { return (weightedGraph[i][j] != INF && i != j); } void dijkstraAlg(location s) { location i, j; overEst[s] = 0; for(i = first_location; i <= last_location; i+1) { if(i != s) overEst[i] = INF; tight[i] = false; predecessor[i] = notExist; } for(int x = 0; x < locationCount; x++) { j = minNonTight(); tight[j] = true; if(overEst[j] = INF) continue; for(i = first_location; i <= last_location; i+1) { if(successor(i,j) && !tight[i] && overEst[j] + weightedGraph[i][j] < overEst[j]) { overEst[i] = overEst[j] + weightedGraph[i][j]; predecessor[i] = j; } } } } /*stack for Djikstra shortest path */ location stack[stack_limit]; unsigned int stackTop; void push(location l) { assert(stackTop < stack_limit); stack[stackTop] = l; stackTop++; } location pop() { stackTop--; return stack[stackTop]; } bool emptyStack() { return(stackTop == 0); } void print_shortest_path(location origin, location destination) { location v; assert(origin != notExist && destination != notExist); dijkstraAlg(origin); cout << "The shortest path from " << name[origin] << " to " << name[destination] << " :" <<endl; for (v = destination; v != origin; v = predecessor[v]) if (v == notExist) { cout << "Path does not exist" << endl; return; } else push(v); push(origin); while (!emptyStack()) cout << name[pop()] << endl; } //MAIN int main() { int start, end; cout << "changi, punggol, sembawang, woodland, bukitbatok, clementi, outram, kallang, bedok" << endl; cout << "Enter a starting location: " << endl; cin >> start; cout << "Enter an ending location: " << endl; cin >> end; print_shortest_path((location)start, (location)end); system("pause"); }
•
•
Join Date: Jan 2008
Posts: 77
Reputation:
Solved Threads: 0
by the way this is my main when I run the program for testing
C++ Syntax (Toggle Plain Text)
int main() { print_shortest_path(changi, bedok); system("pause"); }
If this is a duplicate forgive me. I posted but it didn't show up!
This is a mistake! It's in two places in your code!
i+1 is not an expression! Use either i++ or i+= 1
Also set 0's as your diagonal when source=destination!
This is a mistake! It's in two places in your code!
C++ Syntax (Toggle Plain Text)
// for(i = first_location; i <= last_location; i+1) for(i = first_location; i <= last_location; i++)
C++ Syntax (Toggle Plain Text)
for(i+1; i<= last_location; i+1) ^ ^
Last edited by wildgoose; Aug 12th, 2009 at 11:41 am.
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle. 2) Problem 2[b]solved by : jonsca
Enum math isn't available so you have to do a work around.
You have to use integers for the loop, but cast it into the enum!
You have to use integers for the loop, but cast it into the enum!
C++ Syntax (Toggle Plain Text)
for( int i = first_location; i <= last_location; i++) { location eLoc = static_cast <location> (i); or location eLoc = (location) i; }
![]() |
Similar Threads
- Playing .Wav/MIDI files in a Visual Basic Program (Visual Basic 4 / 5 / 6)
- Finding alternate paths using Dijkstra algorithm (C++)
- dijkstra algorithm (Pascal and Delphi)
- Program Compiles, and excecutes but crashes when run. (C++)
- View source code while running program (C#)
- Dijkstra Help (Java)
- Dijkstra's Algorithm help (C++)
- Cool little Program to disable startup programs (Windows NT / 2000 / XP)
- Program is shutting down right after program is executed (C++)
- 3d Program (Game Development)
Other Threads in the C++ Forum
- Previous Thread: seed and random number generator
- Next Thread: include problem with md5sum.h, pls help
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






