Skip to content

Commit 60ce192

Browse files
authored
Merge pull request #39 from Debashis08/release
Release
2 parents c621526 + b554a3e commit 60ce192

17 files changed

+909
-4
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#pragma once
2+
3+
#include<map>
4+
#include<vector>
5+
using namespace std;
6+
7+
namespace SingleSourceShortestPathBellmanFord
8+
{
9+
class Node
10+
{
11+
public:
12+
int data;
13+
int distance;
14+
Node* parent;
15+
Node(int data);
16+
};
17+
18+
class Edge
19+
{
20+
public:
21+
Node* nodeU;
22+
Node* nodeV;
23+
int weight;
24+
Edge(Node* nodeU, Node* nodeV, int weight);
25+
};
26+
27+
class Graph
28+
{
29+
private:
30+
map<Node*, vector<Node*>> _adjlist;
31+
map<int, Node*> _nodeMap;
32+
vector<Edge*> _edgeList;
33+
Node* MakeOrFindNode(int data);
34+
void InitializeSingleSource(Node* sourceNode);
35+
void Relax(Edge* edge);
36+
void GetShortestPath(Node* node, vector<int>& path);
37+
38+
39+
public:
40+
void PushDirectedEdge(int valueU, int valueV, int weight);
41+
bool FindSingleSourceShortestPathBellmanFord(int data);
42+
vector<int> GetShortestPathBellmanFord(int data);
43+
};
44+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#pragma once
2+
3+
#include<map>
4+
#include<vector>
5+
#include<list>
6+
using namespace std;
7+
8+
namespace DirectedAcyclicGraphShortestPath
9+
{
10+
enum color {WHITE, GRAY, BLACK};
11+
12+
class Node
13+
{
14+
public:
15+
int data;
16+
int color;
17+
int distance;
18+
Node* parent;
19+
Node(int data);
20+
};
21+
22+
class Edge
23+
{
24+
public:
25+
Node* nodeU;
26+
Node* nodeV;
27+
int weight;
28+
Edge(Node* nodeU, Node* nodeV, int weight);
29+
};
30+
31+
class Graph
32+
{
33+
private:
34+
map<Node*, vector<Node*>> _adjlist;
35+
map<int, Node*> _nodeMap;
36+
map<Node*, vector<Edge*>> _edgeMap;
37+
list<Node*> _topologicalSortedNodeList;
38+
Node* MakeOrFindNode(int data);
39+
void DepthFirstSearch(Node* node);
40+
void TopologicalSort();
41+
void InitializeSingleSource(Node* sourceNode);
42+
void Relax(Edge* edge);
43+
void GetShortestPath(Node* node, vector<int>& path);
44+
45+
46+
public:
47+
void PushDirectedEdge(int valueU, int valueV, int weight);
48+
void FindDAGShortestPath(int data);
49+
vector<int> GetDAGShortestPath(int data);
50+
};
51+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#pragma once
2+
3+
#include<map>
4+
#include<vector>
5+
#include<set>
6+
using namespace std;
7+
8+
namespace SingleSourceShortestPathDijkstra
9+
{
10+
class Node
11+
{
12+
public:
13+
int data;
14+
int distance;
15+
Node* parent;
16+
Node(int data);
17+
};
18+
19+
class Edge
20+
{
21+
public:
22+
Node* nodeU;
23+
Node* nodeV;
24+
int weight;
25+
Edge(Node* nodeU, Node* nodeV, int weight);
26+
};
27+
28+
class CompareNodeDistance
29+
{
30+
public:
31+
bool operator()(const Node* nodeU, const Node* nodeV) const
32+
{
33+
return nodeU->distance < nodeV->distance;
34+
}
35+
};
36+
37+
class Graph
38+
{
39+
private:
40+
map<Node*, vector<Node*>> _adjlist;
41+
map<int, Node*> _nodeMap;
42+
map<Node*, vector<Edge*>> _edgeMap;
43+
multiset<Node*, CompareNodeDistance> _operationalSet;
44+
Node* MakeOrFindNode(int data);
45+
void InitializeSingleSource(Node* sourceNode);
46+
void Relax(Edge* edge);
47+
void Dijkstra(Node* source);
48+
void GetShortestPath(Node* node, vector<int>& path);
49+
50+
public:
51+
void PushDirectedEdge(int valueU, int valueV, int weight);
52+
void FindShortestPathDijkstra(int data);
53+
vector<int> GetDijkstraShortestPath(int data);
54+
};
55+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
3+
#include<map>
4+
#include<string>
5+
#include<vector>
6+
using namespace std;
7+
8+
namespace DifferenceConstraintsShortestPaths
9+
{
10+
class Node
11+
{
12+
public:
13+
string data;
14+
int distance;
15+
Node(string data);
16+
};
17+
18+
class Edge
19+
{
20+
public:
21+
Node* nodeU;
22+
Node* nodeV;
23+
int weight;
24+
Edge(Node* nodeU, Node* nodeV, int weight);
25+
};
26+
27+
class Graph
28+
{
29+
private:
30+
map<Node*, vector<Node*>> _adjlist;
31+
map<string, Node*> _nodeMap;
32+
vector<Edge*> _edgeList;
33+
Node* MakeOrFindNode(string data);
34+
void PushDirectedEdge(string valueU, string valueV, int weight);
35+
void InitializeSingleSource(Node* sourceNode);
36+
void Relax(Edge* edge);
37+
38+
public:
39+
void PushAllDirectedEdges(vector<vector<int>> vectorA, vector<string> vectorX, vector<int> vectorB);
40+
bool FindDifferenceConstraintsSolutionBellmanFord();
41+
vector<pair<string, int>> GetDifferenceConstrtaintsSolution();
42+
};
43+
}

SourceCodes/0003_Graph/0003_TopologicalSort.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace TopologicalSort
3636
this->time++;
3737
nodeU->discoveryTime = this->time;
3838
nodeU->color = GRAY;
39-
for (auto nodeV : this->_adjlist[nodeU])
39+
for (auto& nodeV : this->_adjlist[nodeU])
4040
{
4141
if (nodeV->color == WHITE)
4242
{
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include "../Headers/0003_Graph/0009_SingleSourceShortestPathBellmanFord.h"
2+
#include<climits>
3+
#include<algorithm>
4+
using namespace std;
5+
6+
namespace SingleSourceShortestPathBellmanFord
7+
{
8+
Node::Node(int data)
9+
{
10+
this->data = data;
11+
this->distance = INT_MAX;
12+
this->parent = nullptr;
13+
}
14+
15+
Edge::Edge(Node* nodeU, Node* nodeV, int weight)
16+
{
17+
this->nodeU = nodeU;
18+
this->nodeV = nodeV;
19+
this->weight = weight;
20+
}
21+
22+
// Graph Private Member Methods
23+
Node* Graph::MakeOrFindNode(int data)
24+
{
25+
Node* node = nullptr;
26+
if (this->_nodeMap.find(data) == this->_nodeMap.end())
27+
{
28+
node = new Node(data);
29+
this->_nodeMap[data] = node;
30+
}
31+
else
32+
{
33+
node = this->_nodeMap[data];
34+
}
35+
return node;
36+
}
37+
38+
void Graph :: InitializeSingleSource(Node* sourceNode)
39+
{
40+
for (auto& iterator : this->_nodeMap)
41+
{
42+
iterator.second->distance = INT_MAX;
43+
iterator.second->parent = nullptr;
44+
}
45+
sourceNode->distance = 0;
46+
}
47+
48+
void Graph::Relax(Edge* edge)
49+
{
50+
if (edge->nodeU->distance != INT_MAX && (edge->nodeV->distance > (edge->nodeU->distance + edge->weight)))
51+
{
52+
edge->nodeV->distance = edge->nodeU->distance + edge->weight;
53+
edge->nodeV->parent = edge->nodeU;
54+
}
55+
}
56+
57+
void Graph::GetShortestPath(Node* node, vector<int>& path)
58+
{
59+
path.push_back(node->data);
60+
if (node->parent != nullptr)
61+
{
62+
this->GetShortestPath(node->parent, path);
63+
}
64+
}
65+
66+
// Graph Public Member Methods
67+
void Graph::PushDirectedEdge(int dataU, int dataV, int weight)
68+
{
69+
Node* nodeU = this->MakeOrFindNode(dataU);
70+
Node* nodeV = this->MakeOrFindNode(dataV);
71+
72+
this->_adjlist[nodeU].push_back(nodeV);
73+
this->_edgeList.push_back(new Edge(nodeU, nodeV, weight));
74+
}
75+
76+
bool Graph::FindSingleSourceShortestPathBellmanFord(int data)
77+
{
78+
Node* source = this->_nodeMap[data];
79+
80+
this->InitializeSingleSource(source);
81+
82+
for (int i = 0; i < this->_nodeMap.size() - 1; i++)
83+
{
84+
for (auto& edge : this->_edgeList)
85+
{
86+
this->Relax(edge);
87+
}
88+
}
89+
90+
for (auto& edge : this->_edgeList)
91+
{
92+
if (edge->nodeV->distance > (edge->nodeU->distance + edge->weight))
93+
{
94+
return false;
95+
}
96+
}
97+
return true;
98+
}
99+
100+
vector<int> Graph::GetShortestPathBellmanFord(int data)
101+
{
102+
vector<int> path = {};
103+
Node* node = this->_nodeMap[data];
104+
this->GetShortestPath(node, path);
105+
reverse(path.begin(), path.end());
106+
return path;
107+
}
108+
}

0 commit comments

Comments
 (0)