-
Notifications
You must be signed in to change notification settings - Fork 0
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 #39 from Debashis08/release
Release
- Loading branch information
Showing
17 changed files
with
909 additions
and
4 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
Headers/0003_Graph/0009_SingleSourceShortestPathBellmanFord.h
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,44 @@ | ||
#pragma once | ||
|
||
#include<map> | ||
#include<vector> | ||
using namespace std; | ||
|
||
namespace SingleSourceShortestPathBellmanFord | ||
{ | ||
class Node | ||
{ | ||
public: | ||
int data; | ||
int distance; | ||
Node* parent; | ||
Node(int data); | ||
}; | ||
|
||
class Edge | ||
{ | ||
public: | ||
Node* nodeU; | ||
Node* nodeV; | ||
int weight; | ||
Edge(Node* nodeU, Node* nodeV, int weight); | ||
}; | ||
|
||
class Graph | ||
{ | ||
private: | ||
map<Node*, vector<Node*>> _adjlist; | ||
map<int, Node*> _nodeMap; | ||
vector<Edge*> _edgeList; | ||
Node* MakeOrFindNode(int data); | ||
void InitializeSingleSource(Node* sourceNode); | ||
void Relax(Edge* edge); | ||
void GetShortestPath(Node* node, vector<int>& path); | ||
|
||
|
||
public: | ||
void PushDirectedEdge(int valueU, int valueV, int weight); | ||
bool FindSingleSourceShortestPathBellmanFord(int data); | ||
vector<int> GetShortestPathBellmanFord(int data); | ||
}; | ||
} |
51 changes: 51 additions & 0 deletions
51
Headers/0003_Graph/0010_DirectedAcyclicGraphShortestPath.h
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,51 @@ | ||
#pragma once | ||
|
||
#include<map> | ||
#include<vector> | ||
#include<list> | ||
using namespace std; | ||
|
||
namespace DirectedAcyclicGraphShortestPath | ||
{ | ||
enum color {WHITE, GRAY, BLACK}; | ||
|
||
class Node | ||
{ | ||
public: | ||
int data; | ||
int color; | ||
int distance; | ||
Node* parent; | ||
Node(int data); | ||
}; | ||
|
||
class Edge | ||
{ | ||
public: | ||
Node* nodeU; | ||
Node* nodeV; | ||
int weight; | ||
Edge(Node* nodeU, Node* nodeV, int weight); | ||
}; | ||
|
||
class Graph | ||
{ | ||
private: | ||
map<Node*, vector<Node*>> _adjlist; | ||
map<int, Node*> _nodeMap; | ||
map<Node*, vector<Edge*>> _edgeMap; | ||
list<Node*> _topologicalSortedNodeList; | ||
Node* MakeOrFindNode(int data); | ||
void DepthFirstSearch(Node* node); | ||
void TopologicalSort(); | ||
void InitializeSingleSource(Node* sourceNode); | ||
void Relax(Edge* edge); | ||
void GetShortestPath(Node* node, vector<int>& path); | ||
|
||
|
||
public: | ||
void PushDirectedEdge(int valueU, int valueV, int weight); | ||
void FindDAGShortestPath(int data); | ||
vector<int> GetDAGShortestPath(int data); | ||
}; | ||
} |
55 changes: 55 additions & 0 deletions
55
Headers/0003_Graph/0011_SingleSourceShortestPathDijkstra.h
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,55 @@ | ||
#pragma once | ||
|
||
#include<map> | ||
#include<vector> | ||
#include<set> | ||
using namespace std; | ||
|
||
namespace SingleSourceShortestPathDijkstra | ||
{ | ||
class Node | ||
{ | ||
public: | ||
int data; | ||
int distance; | ||
Node* parent; | ||
Node(int data); | ||
}; | ||
|
||
class Edge | ||
{ | ||
public: | ||
Node* nodeU; | ||
Node* nodeV; | ||
int weight; | ||
Edge(Node* nodeU, Node* nodeV, int weight); | ||
}; | ||
|
||
class CompareNodeDistance | ||
{ | ||
public: | ||
bool operator()(const Node* nodeU, const Node* nodeV) const | ||
{ | ||
return nodeU->distance < nodeV->distance; | ||
} | ||
}; | ||
|
||
class Graph | ||
{ | ||
private: | ||
map<Node*, vector<Node*>> _adjlist; | ||
map<int, Node*> _nodeMap; | ||
map<Node*, vector<Edge*>> _edgeMap; | ||
multiset<Node*, CompareNodeDistance> _operationalSet; | ||
Node* MakeOrFindNode(int data); | ||
void InitializeSingleSource(Node* sourceNode); | ||
void Relax(Edge* edge); | ||
void Dijkstra(Node* source); | ||
void GetShortestPath(Node* node, vector<int>& path); | ||
|
||
public: | ||
void PushDirectedEdge(int valueU, int valueV, int weight); | ||
void FindShortestPathDijkstra(int data); | ||
vector<int> GetDijkstraShortestPath(int data); | ||
}; | ||
} |
43 changes: 43 additions & 0 deletions
43
Headers/0003_Graph/0012_DifferenceConstraintsShortestPaths.h
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,43 @@ | ||
#pragma once | ||
|
||
#include<map> | ||
#include<string> | ||
#include<vector> | ||
using namespace std; | ||
|
||
namespace DifferenceConstraintsShortestPaths | ||
{ | ||
class Node | ||
{ | ||
public: | ||
string data; | ||
int distance; | ||
Node(string data); | ||
}; | ||
|
||
class Edge | ||
{ | ||
public: | ||
Node* nodeU; | ||
Node* nodeV; | ||
int weight; | ||
Edge(Node* nodeU, Node* nodeV, int weight); | ||
}; | ||
|
||
class Graph | ||
{ | ||
private: | ||
map<Node*, vector<Node*>> _adjlist; | ||
map<string, Node*> _nodeMap; | ||
vector<Edge*> _edgeList; | ||
Node* MakeOrFindNode(string data); | ||
void PushDirectedEdge(string valueU, string valueV, int weight); | ||
void InitializeSingleSource(Node* sourceNode); | ||
void Relax(Edge* edge); | ||
|
||
public: | ||
void PushAllDirectedEdges(vector<vector<int>> vectorA, vector<string> vectorX, vector<int> vectorB); | ||
bool FindDifferenceConstraintsSolutionBellmanFord(); | ||
vector<pair<string, int>> GetDifferenceConstrtaintsSolution(); | ||
}; | ||
} |
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
108 changes: 108 additions & 0 deletions
108
SourceCodes/0003_Graph/0009_SingleSourceShortestPathBellmanFord.cc
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,108 @@ | ||
#include "../Headers/0003_Graph/0009_SingleSourceShortestPathBellmanFord.h" | ||
#include<climits> | ||
#include<algorithm> | ||
using namespace std; | ||
|
||
namespace SingleSourceShortestPathBellmanFord | ||
{ | ||
Node::Node(int data) | ||
{ | ||
this->data = data; | ||
this->distance = INT_MAX; | ||
this->parent = nullptr; | ||
} | ||
|
||
Edge::Edge(Node* nodeU, Node* nodeV, int weight) | ||
{ | ||
this->nodeU = nodeU; | ||
this->nodeV = nodeV; | ||
this->weight = weight; | ||
} | ||
|
||
// Graph Private Member Methods | ||
Node* Graph::MakeOrFindNode(int data) | ||
{ | ||
Node* node = nullptr; | ||
if (this->_nodeMap.find(data) == this->_nodeMap.end()) | ||
{ | ||
node = new Node(data); | ||
this->_nodeMap[data] = node; | ||
} | ||
else | ||
{ | ||
node = this->_nodeMap[data]; | ||
} | ||
return node; | ||
} | ||
|
||
void Graph :: InitializeSingleSource(Node* sourceNode) | ||
{ | ||
for (auto& iterator : this->_nodeMap) | ||
{ | ||
iterator.second->distance = INT_MAX; | ||
iterator.second->parent = nullptr; | ||
} | ||
sourceNode->distance = 0; | ||
} | ||
|
||
void Graph::Relax(Edge* edge) | ||
{ | ||
if (edge->nodeU->distance != INT_MAX && (edge->nodeV->distance > (edge->nodeU->distance + edge->weight))) | ||
{ | ||
edge->nodeV->distance = edge->nodeU->distance + edge->weight; | ||
edge->nodeV->parent = edge->nodeU; | ||
} | ||
} | ||
|
||
void Graph::GetShortestPath(Node* node, vector<int>& path) | ||
{ | ||
path.push_back(node->data); | ||
if (node->parent != nullptr) | ||
{ | ||
this->GetShortestPath(node->parent, path); | ||
} | ||
} | ||
|
||
// Graph Public Member Methods | ||
void Graph::PushDirectedEdge(int dataU, int dataV, int weight) | ||
{ | ||
Node* nodeU = this->MakeOrFindNode(dataU); | ||
Node* nodeV = this->MakeOrFindNode(dataV); | ||
|
||
this->_adjlist[nodeU].push_back(nodeV); | ||
this->_edgeList.push_back(new Edge(nodeU, nodeV, weight)); | ||
} | ||
|
||
bool Graph::FindSingleSourceShortestPathBellmanFord(int data) | ||
{ | ||
Node* source = this->_nodeMap[data]; | ||
|
||
this->InitializeSingleSource(source); | ||
|
||
for (int i = 0; i < this->_nodeMap.size() - 1; i++) | ||
{ | ||
for (auto& edge : this->_edgeList) | ||
{ | ||
this->Relax(edge); | ||
} | ||
} | ||
|
||
for (auto& edge : this->_edgeList) | ||
{ | ||
if (edge->nodeV->distance > (edge->nodeU->distance + edge->weight)) | ||
{ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
vector<int> Graph::GetShortestPathBellmanFord(int data) | ||
{ | ||
vector<int> path = {}; | ||
Node* node = this->_nodeMap[data]; | ||
this->GetShortestPath(node, path); | ||
reverse(path.begin(), path.end()); | ||
return path; | ||
} | ||
} |
Oops, something went wrong.