| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Backtracking
Hi,
I am trying to make a program to obtain all shortest paths in graphs with Dijkstra´s. I already get the shortest paths and I put it in a vector path[]={{},{0},{0},{1,2},{1,2},{3,4}} now, I want to extract this output 5 <- 3 <- 1 <- 0 5 <- 3 <- 2 <- 0 5 <- 4 <- 1 <- 0 5 <- 4 <- 2 <- 0 Where 5 means the position on vector path, where {3,4} means that the positions 3 and 4 are adjacency with 5. can you help me to obtain the expected output? this is my code: Code:
#include <iostream.h>
#include <stdlib.h>
#include <vector>
vector<int> path[6];
/*
void printnode(int node){
if(path[node].size()!=0){
cout << path[node][0];
if (path[node][0]!=0){
cout << " <-- ";
} else {
cout << endl;
}
printnode(path[node][0]);
}
}
*/
void printnode(int node){
if(path[node].size()!=0){
for(int i=0; i < path[node].size(); i++){
cout << path[node][i];
if(i+1 < path[node].size()){
cout << " or ";
}
}
if (path[node][0]!=0){
cout << " <-- ";
} else {
cout << endl;
}
printnode(path[node][0]);
}
}
int main()
{
//path[0].push_back(0);
path[1].push_back(0);
path[2].push_back(0);
path[3].push_back(1);
path[3].push_back(2);
path[4].push_back(1);
path[4].push_back(2);
path[5].push_back(3);
path[5].push_back(4);
printnode(5);
system("PAUSE");
return 0;
}
best regards |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Backtracking |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|