Skip to content

Commit 601924d

Browse files
authored
Merge pull request #36 from Debashis08/feature-graph-implementation
Feature graph implementation
2 parents 3ba353b + 70be789 commit 601924d

9 files changed

+465
-1
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+
}

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+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#include "../Headers/0003_Graph/0010_DirectedAcyclicGraphShortestPath.h"
2+
#include<climits>
3+
#include<algorithm>
4+
using namespace std;
5+
6+
namespace DirectedAcyclicGraphShortestPath
7+
{
8+
Node::Node(int data)
9+
{
10+
this->data = data;
11+
this->color = WHITE;
12+
this->distance = INT_MAX;
13+
this->parent = nullptr;
14+
}
15+
16+
Edge::Edge(Node* nodeU, Node* nodeV, int weight)
17+
{
18+
this->nodeU = nodeU;
19+
this->nodeV = nodeV;
20+
this->weight = weight;
21+
}
22+
23+
// Graph Private Member Methods
24+
Node* Graph::MakeOrFindNode(int data)
25+
{
26+
Node* node = nullptr;
27+
if (this->_nodeMap.find(data) == this->_nodeMap.end())
28+
{
29+
node = new Node(data);
30+
this->_nodeMap[data] = node;
31+
}
32+
else
33+
{
34+
node = this->_nodeMap[data];
35+
}
36+
return node;
37+
}
38+
39+
void Graph::DepthFirstSearch(Node* nodeU)
40+
{
41+
nodeU->color = GRAY;
42+
for (auto& nodeV : this->_adjlist[nodeU])
43+
{
44+
if (nodeV->color == WHITE)
45+
{
46+
this->DepthFirstSearch(nodeV);
47+
}
48+
}
49+
nodeU->color = BLACK;
50+
this->_topologicalSortedNodeList.push_front(nodeU);
51+
}
52+
53+
void Graph::TopologicalSort()
54+
{
55+
for (auto& iterator : this->_nodeMap)
56+
{
57+
if (iterator.second->color == WHITE)
58+
{
59+
this->DepthFirstSearch(iterator.second);
60+
}
61+
}
62+
}
63+
64+
void Graph::InitializeSingleSource(Node* sourceNode)
65+
{
66+
for (auto& iterator : this->_nodeMap)
67+
{
68+
iterator.second->distance = INT_MAX;
69+
iterator.second->parent = nullptr;
70+
}
71+
sourceNode->distance = 0;
72+
}
73+
74+
void Graph::Relax(Edge* edge)
75+
{
76+
if (edge->nodeU->distance != INT_MAX && (edge->nodeV->distance > (edge->nodeU->distance + edge->weight)))
77+
{
78+
edge->nodeV->distance = edge->nodeU->distance + edge->weight;
79+
edge->nodeV->parent = edge->nodeU;
80+
}
81+
}
82+
83+
void Graph::GetShortestPath(Node* node, vector<int>& path)
84+
{
85+
path.push_back(node->data);
86+
if (node->parent != nullptr)
87+
{
88+
this->GetShortestPath(node->parent, path);
89+
}
90+
}
91+
92+
// Graph Public Member Methods
93+
void Graph::PushDirectedEdge(int dataU, int dataV, int weight)
94+
{
95+
Node* nodeU = this->MakeOrFindNode(dataU);
96+
Node* nodeV = this->MakeOrFindNode(dataV);
97+
98+
this->_adjlist[nodeU].push_back(nodeV);
99+
this->_edgeMap[nodeU].push_back(new Edge(nodeU, nodeV, weight));
100+
}
101+
102+
void Graph::FindDAGShortestPath(int data)
103+
{
104+
this->TopologicalSort();
105+
Node* source = this->_nodeMap[data];
106+
this->InitializeSingleSource(source);
107+
for (auto& node : this->_topologicalSortedNodeList)
108+
{
109+
for (auto& edge : this->_edgeMap[node])
110+
{
111+
this->Relax(edge);
112+
}
113+
}
114+
}
115+
116+
vector<int> Graph::GetDAGShortestPath(int data)
117+
{
118+
vector<int> path = {};
119+
Node* node = this->_nodeMap[data];
120+
this->GetShortestPath(node, path);
121+
reverse(path.begin(), path.end());
122+
return path;
123+
}
124+
}

SourceCodes/0003_Graph/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ set(0003GRAPH_SOURCES
88
0006_EulerianPathAndCircuit.cc
99
0007_MinimumSpanningTreeKruskalAlgorithm.cc
1010
0008_MinimumSpanningTreePrimAlgorithm.cc
11+
0009_SingleSourceShortestPathBellmanFord.cc
12+
0010_DirectedAcyclicGraphShortestPath.cc
1113

1214
)
1315

0 commit comments

Comments
 (0)