Sorry for my english..
Im trying to make a program that show the shortest route of this nodes using BFS algorithm..
i tried to print the prev array which shows the shortest route but somehow it doesnt appear on console when running..
how to fix it?
btw, is there any much simple algorithm than these one?
thanks.
> #include <stdio.h>
> #include <queue>
> #include <conio.h>
> using namespace std;
>
> int nodes, edges, src, dest;
> int graph[100][100], color[100],distance[100],prev[100];
> const int WHITE = 0;
> const int GRAY = 1;
> const int BLACK = 2;
>
> int main() {
> printf("Nodes, edges, source, destination? ");
> scanf("%d %d %d %d", &nodes, &edges, &src, &dest);
> for (int i = 1; i <= edges; i++) {
> printf("Edge %d: ", i);
> int x, y;
> scanf("%d %d", &x, &y);
> graph[x][y] = 1;
> }
>
> //run BFS
> queue<int> q; //create a queue
> q.push(src); //1. put root node on the queue
> do {
> int u = q.front(); //2. pull a node from the beginning of the queue
> q.pop();
> printf("%d ", u); //print the node
> for (int i = 1; i <= nodes; i++) { //4. get all the adjacent nodes
> if ((graph[u][i] == 1) //if an edge exists between these two nodes,
> && (color[i] == WHITE)) { //and this adjacent node is still WHITE,
> q.push(i); //4. push this node into the queue
> color[i] = GRAY; prev[i] = u; //color this adjacent node with GRAY
> }
> }
> color[u] = BLACK; //color the current node black to mark it as dequeued
> } while (!q.empty()); //5. if the queue is empty, then all the nodes have been visited
> for(int i=10;i<=0; i--)
> {if (prev[i] == 0)
> { }
> else
> printf("%d -> ", prev[i]); }
> getch();
> return 0;
> }
>