Skip to content

Commit 1b3a3d7

Browse files
committed
test: DAG shortest path test added
1 parent 38cec21 commit 1b3a3d7

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

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
{

SourceCodes/0003_Graph/0009_SingleSourceShortestPathBellmanFord.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace SingleSourceShortestPathBellmanFord
4646

4747
void Graph::Relax(Edge* edge)
4848
{
49-
if (edge->nodeV->distance > (edge->nodeU->distance + edge->weight))
49+
if (edge->nodeU->distance != INT_MAX && (edge->nodeV->distance > (edge->nodeU->distance + edge->weight)))
5050
{
5151
edge->nodeV->distance = edge->nodeU->distance + edge->weight;
5252
edge->nodeV->parent = edge->nodeU;

SourceCodes/0003_Graph/0010_DirectedAcyclicGraphShortestPath.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@ namespace DirectedAcyclicGraphShortestPath
3838
void Graph::DepthFirstSearch(Node* nodeU)
3939
{
4040
nodeU->color = GRAY;
41-
for (auto nodeV : this->_adjlist[nodeU])
41+
for (auto& nodeV : this->_adjlist[nodeU])
4242
{
4343
if (nodeV->color == WHITE)
4444
{
45-
nodeV->parent = nodeU;
4645
this->DepthFirstSearch(nodeV);
4746
}
4847
}
@@ -73,7 +72,7 @@ namespace DirectedAcyclicGraphShortestPath
7372

7473
void Graph::Relax(Edge* edge)
7574
{
76-
if (edge->nodeV->distance > (edge->nodeU->distance + edge->weight))
75+
if (edge->nodeU->distance != INT_MAX && (edge->nodeV->distance > (edge->nodeU->distance + edge->weight)))
7776
{
7877
edge->nodeV->distance = edge->nodeU->distance + edge->weight;
7978
edge->nodeV->parent = edge->nodeU;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include<gtest/gtest.h>
2+
#include "../Headers/0003_Graph/0010_DirectedAcyclicGraphShortestPath.h"
3+
#include "../0000_CommonUtilities/UnitTestHelper.h"
4+
5+
namespace DirectedAcyclicGraphShortestPath
6+
{
7+
UnitTestHelper unitTestHelper;
8+
9+
// Test for Simple Graph
10+
TEST(DAGTest, SimpleGraph)
11+
{
12+
Graph graph;
13+
14+
graph.PushDirectedEdge(0, 1, 5);
15+
graph.PushDirectedEdge(0, 2, 3);
16+
graph.PushDirectedEdge(1, 2, 2);
17+
graph.PushDirectedEdge(1, 3, 6);
18+
graph.PushDirectedEdge(2, 3, 7);
19+
graph.PushDirectedEdge(2, 4, 4);
20+
graph.PushDirectedEdge(2, 5, 2);
21+
graph.PushDirectedEdge(3, 4, -1);
22+
graph.PushDirectedEdge(3, 5, 1);
23+
graph.PushDirectedEdge(4, 5, -2);
24+
25+
graph.FindDAGShortestPath(1);
26+
string expectedPath = "1 3 4 5";
27+
ASSERT_EQ(unitTestHelper.SerializeVectorToString(graph.GetDAGShortestPath(5)), expectedPath);
28+
}
29+
}

0 commit comments

Comments
 (0)