-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from Apolo151/all_paths
All paths finished, waiting for review
- Loading branch information
Showing
4 changed files
with
117 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
5 | ||
Asyut - Cairo Train 250 Bus 450 | ||
Giza - Cairo Metro 30 Bus 60 Uber 230 | ||
Giza - Dahab Bus 500 Microbus 345 | ||
Dahab - BeniSuef Microbus 200 Bus 315 | ||
BeniSuef - Cairo Microbus 20 Bus 15 | ||
BeniSuef - Dahab Microbus 200 Bus 315 | ||
Dahab - Giza Bus 500 Microbus 345 | ||
Giza - Cairo Metro 30 Bus 60 Uber 230 | ||
Cairo - Asyut Train 250 Bus 450 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
#include <vector> | ||
#include <unordered_map> | ||
#include <string> | ||
#include <utility> | ||
#include <map> | ||
#include "../include/map.h" | ||
#include "../include/map_helpers.h" | ||
using namespace std; | ||
class AllPaths | ||
{ | ||
|
||
public: | ||
class Path | ||
{ | ||
public: | ||
int cost; | ||
vector <string> pathVector; | ||
Path(int cost, vector <string> pathVector); | ||
bool operator<(const Path& other) const; | ||
}; | ||
|
||
string source, destination; | ||
unordered_map<string, unordered_map<string, Route>> graph; | ||
vector <pair <int, vector <string>>> allPathsVector; | ||
AllPaths(unordered_map<string, unordered_map<string, Route>> &graph, string, string); | ||
void displayAllPaths(); | ||
void computeAllPaths(); | ||
void dfsAllPaths(string, map<string, bool>&, vector <string>, int); | ||
bool comparePaths(Path&, Path&); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include "../include/all_paths.h" | ||
#include "../include/map.h" | ||
#include "../include/map_helpers.h" | ||
#include <unordered_map> | ||
#include <map> | ||
#include <string> | ||
#include <vector> | ||
#include <algorithm> | ||
#include <iostream> | ||
|
||
AllPaths::AllPaths(unordered_map<string, unordered_map<string, Route>> &graph, string source, string destination) | ||
{ | ||
this->graph = graph; | ||
this->source = source; | ||
this->destination = destination; | ||
} | ||
AllPaths::Path::Path(int weight, vector <string> curPath) | ||
{ | ||
this->cost = weight; | ||
this->pathVector = curPath; | ||
} | ||
void AllPaths::computeAllPaths() | ||
{ | ||
map <string, bool> curVis; | ||
curVis[this->source] = true; | ||
dfsAllPaths(this->source, curVis, {}, 0); | ||
|
||
sort(this->allPathsVector.begin(), this->allPathsVector.end()); | ||
} | ||
bool AllPaths::Path::operator<(const Path& other) const | ||
{ | ||
return this->cost < other.cost; | ||
} | ||
void AllPaths::displayAllPaths() | ||
{ | ||
for(int i = 0; i < allPathsVector.size(); i++) | ||
{ | ||
cout << "Path # " << i + 1 << "\n"; | ||
cout << "The cost is: " << allPathsVector[i].first << '\n'; | ||
cout << "The route:\n"; | ||
for(auto &j : allPathsVector[i].second) | ||
cout << j; | ||
cout << '\n'; | ||
} | ||
} | ||
void AllPaths::dfsAllPaths(string node, map <string, bool> &curVis,vector <string> curPath, int weight) | ||
{ map<transportations, string> enumToStr = {{BUS, "Bus"}, | ||
{MICROBUS, "Microbus"}, | ||
{TRAIN, "Train"}, | ||
{METRO, "Metro"}, | ||
{UBER, "Uber"}}; | ||
|
||
if(node == this->destination) | ||
{ | ||
this->allPathsVector.push_back({weight, {curPath}}); | ||
return; | ||
} | ||
for(auto &it : this->graph[node]) // city, Route | ||
{ | ||
// moving on unordered map | ||
//it: city 2, Route | ||
for(auto &i : it.second.roads) | ||
{ | ||
if(curVis[it.first]) | ||
continue; | ||
string tmp = "From " + node + " to " + it.first + " by " + enumToStr[i.transport] + " with cost: " + to_string(i.cost) + " " + "\n"; | ||
curPath.push_back(tmp); | ||
curVis[it.first] = true; | ||
dfsAllPaths(it.first, curVis, curPath, weight + i.cost); | ||
curPath.pop_back(); | ||
curVis[it.first] = false; | ||
|
||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters