I want to save all the paths taken from the start node, can someone help me think? the "visited" array just stores every node visited while searching the shortest path to another node, it does not contain the full path.
EDIT: Sorry for the mess, this was supposed to be solved 2 minutes ago for an assignment.
Please ask me to clarify if I have made myself unclear as I would still like to solve this.
void Graph::calcShortestPath(string start, string dest) {
distance = new int[nrOfCities];
visited = new bool[nrOfCities];
int chosen;
path = new vector < int > ;
pathe = new int[7];
for (int i = 0; i < 7; i++) {
pathe[i] = 0;
}
pathe[0] = getIndexOfCity(start);
path->push_back(getIndexOfCity(start));
int nextNode = 0;
int leastHours = 88888;
for (int i = 0; i < nrOfCities; i++) {
distance[i] = 0;
visited[i] = false;
}
for (int i = 0; i < nrOfCities; i++) {
distance[i] = adjacencyMatrix[getIndexOfCity(start)][i];
}
distance[getIndexOfCity(start)] = 0;
visited[getIndexOfCity(start)] = true;
for (int i = 0; i < nrOfCities; i++) {
leastHours = 88888;
for (int k = 0; k < nrOfCities; k++) {
if (leastHours >= distance[k] && visited[k] != true) {
leastHours = distance[k];
nextNode = k;
cout << cities[k];
}
}
visited[nextNode] = true;
//cout << nextNode << ", ";
for (int s = 0; s < nrOfCities; s++) {
if (visited[s] != true) {
if (leastHours + adjacencyMatrix[nextNode][s] < distance[s]) {
distance[s] = leastHours + adjacencyMatrix[nextNode][s];
//cout << adjacencyMatrix[nextNode][s];
}
}
}
}
cout << endl << "Shortest path from " << start << " to " << dest << " is " << distance[getIndexOfCity(dest)] << " hours long."<< endl;
cout << "The path was: ";
for (int i = 0; i < 7; i++) {
cout << cities[pathe[i]] << ", ";
}
cout << endl;
}
Aucun commentaire:
Enregistrer un commentaire