Skip to content

Commit

Permalink
Merge pull request #30 from Apolo151/all_paths
Browse files Browse the repository at this point in the history
All paths finished, waiting for review
  • Loading branch information
Apolo151 authored May 5, 2024
2 parents 1444e4f + 0fcbbb1 commit d8fefc9
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 5 deletions.
8 changes: 4 additions & 4 deletions data/data.txt
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
31 changes: 31 additions & 0 deletions include/all_paths.h
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&);
};
77 changes: 77 additions & 0 deletions src/all_paths.cpp
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;

}

}

}
6 changes: 5 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
#include "../include/map.h"


#include "../include/all_paths.h"
// Set Global Variables
string DATA_PATH = "../data/data.txt";
string DATA_PATH = "D:\\University\\2nd Year\\DS\\Project\\guide_me\\data\\data.txt";
string ADMIN_NAME = "admin";
string ADMIN_PASSWORD = "adminAdmin";
using namespace std;
Expand Down Expand Up @@ -78,6 +79,9 @@ int main(int argc, char **argv) {
user->deleteRoad(Road(name1, name2, RoadProperties(50, BUS)));

dataManager.printAdjList();
AllPaths allPaths(Map::adjList, "Cairo", "Dahab");
allPaths.computeAllPaths();
allPaths.displayAllPaths();
// user->deleteRoad()
// shutdown
dataManager.saveData(DATA_PATH);
Expand Down

0 comments on commit d8fefc9

Please sign in to comment.