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