Skip to content

Commit 38cec21

Browse files
committed
feature: DAG shortest path initial changes
1 parent d68a35b commit 38cec21

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed
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: 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+
using namespace std;
4+
5+
namespace DirectedAcyclicGraphShortestPath
6+
{
7+
Node::Node(int data)
8+
{
9+
this->data = data;
10+
this->color = WHITE;
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::DepthFirstSearch(Node* nodeU)
39+
{
40+
nodeU->color = GRAY;
41+
for (auto nodeV : this->_adjlist[nodeU])
42+
{
43+
if (nodeV->color == WHITE)
44+
{
45+
nodeV->parent = nodeU;
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->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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ set(0003GRAPH_SOURCES
99
0007_MinimumSpanningTreeKruskalAlgorithm.cc
1010
0008_MinimumSpanningTreePrimAlgorithm.cc
1111
0009_SingleSourceShortestPathBellmanFord.cc
12+
0010_DirectedAcyclicGraphShortestPath.cc
1213

1314
)
1415

Tests/0003_Graph/0010_DirectedAcyclicGraphShortestPathTest.cc

Whitespace-only changes.

Tests/0003_Graph/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ add_executable(
2121
0007_MinimumSpanningTreeKruskalAlgorithmTest.cc
2222
0008_MinimumSpanningTreePrimAlgorithmTest.cc
2323
0009_SingleSourceShortestPathBellmanFordTest.cc
24+
0010_DirectedAcyclicGraphShortestPathTest.cc
2425
)
2526

2627
target_link_libraries(

0 commit comments

Comments
 (0)