Skip to content

Feature graph implementation #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Headers/0003_Graph/0005_HamiltonianPathAndCycle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include<map>
#include<unordered_set>
#include<vector>
using namespace std;

class HamiltonianNode
{
public:
int data;
bool isVisited;
HamiltonianNode(int value);
};

class HamiltonianGraph
{
private:
bool _isHamiltonianCyclePresent;
bool _isHamiltonianPathPresent;
int _visitedNodeCount;
HamiltonianNode* _startingNode;
map<HamiltonianNode*, unordered_set<HamiltonianNode*>> _adjlist;
map<int, HamiltonianNode*> _nodeMap;
vector<int> _hamiltonianPath;
HamiltonianNode* MakeOrFindNode(int value);
bool IsSafe(HamiltonianNode* nodeU, HamiltonianNode* nodeV);
bool HamiltonianCycleAndPathUtil(HamiltonianNode* node);

public:
void PushUndirectedEdge(int valueU, int valueV);
void PushSingleNode(int valueU);
void FindHamiltonianCycleAndPath();
bool IsHamiltonianCyclePresent();
bool IsHamiltonianPathPresent();
vector<int> GetHamiltonianPath();
};
112 changes: 112 additions & 0 deletions SourceCodes/0003_Graph/0005_HamiltonianPathAndCycle.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include "../Headers/0003_Graph/0005_HamiltonianPathAndCycle.h"

using namespace std;

HamiltonianNode::HamiltonianNode(int value)
{
this->data = value;
this->isVisited = false;
}

// HamiltonianGraph Private Member Methods
HamiltonianNode* HamiltonianGraph::MakeOrFindNode(int value)
{
HamiltonianNode* node = nullptr;
if (this->_nodeMap.find(value) == this->_nodeMap.end())
{
node = new HamiltonianNode(value);
this->_nodeMap[value] = node;
}
else
{
node = this->_nodeMap[value];
}
return node;
}

bool HamiltonianGraph::IsSafe(HamiltonianNode* nodeU, HamiltonianNode* nodeV)
{
if (this->_adjlist[nodeU].find(nodeV) == this->_adjlist[nodeU].end())
{
return false;
}
if (nodeV->isVisited == true)
{
return false;
}
return true;
}

bool HamiltonianGraph::HamiltonianCycleAndPathUtil(HamiltonianNode* nodeU)
{
if (this->_visitedNodeCount == this->_nodeMap.size())
{
if (this->_adjlist[nodeU].find(this->_startingNode) != this->_adjlist[nodeU].end())
{
this->_isHamiltonianCyclePresent = true;
this->_isHamiltonianPathPresent = true;
return true;
}
this->_isHamiltonianPathPresent = true;
return false;
}
for (auto& nodeV : this->_adjlist[nodeU])
{
if (this->IsSafe(nodeU, nodeV))
{
this->_hamiltonianPath.push_back(nodeV->data);
nodeV->isVisited = true;
this->_visitedNodeCount++;
if (this->HamiltonianCycleAndPathUtil(nodeV))
{
return true;
}
this->_visitedNodeCount--;
nodeV->isVisited = false;
this->_hamiltonianPath.pop_back();
}
}
return false;
}

// HamiltonianGraph Public Member Methods
void HamiltonianGraph::PushUndirectedEdge(int valueU, int valueV)
{
HamiltonianNode* nodeU = this->MakeOrFindNode(valueU);
HamiltonianNode* nodeV = this->MakeOrFindNode(valueV);

this->_adjlist[nodeU].insert(nodeV);
this->_adjlist[nodeV].insert(nodeU);
}

void HamiltonianGraph::PushSingleNode(int valueU)
{
HamiltonianNode* nodeU = this->MakeOrFindNode(valueU);
}

void HamiltonianGraph::FindHamiltonianCycleAndPath()
{
this->_isHamiltonianCyclePresent = false;
this->_isHamiltonianPathPresent = false;
this->_hamiltonianPath = {};
this->_startingNode = this->_nodeMap[0];
this->_hamiltonianPath.push_back(this->_startingNode->data);
this->_startingNode->isVisited = true;
this->_visitedNodeCount = 1;
this->HamiltonianCycleAndPathUtil(this->_startingNode);
}

bool HamiltonianGraph::IsHamiltonianCyclePresent()
{
return this->_isHamiltonianCyclePresent;
}

bool HamiltonianGraph::IsHamiltonianPathPresent()
{
return this->_isHamiltonianPathPresent;
}

vector<int> HamiltonianGraph::GetHamiltonianPath()
{
return this->_hamiltonianPath;
}
1 change: 1 addition & 0 deletions SourceCodes/0003_Graph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(0003GRAPH_SOURCES
0002_DepthFirstSearch.cc
0003_TopologicalSort.cc
0004_StronglyConnectedComponents.cc
0005_HamiltonianPathAndCycle.cc
)

# Create a library target
Expand Down
37 changes: 33 additions & 4 deletions Tests/0000_CommonUtilities/UnitTestHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class UnitTestHelper
{
public:
template<typename T>
string VerifyVectorResult(vector<T> vector)
string SerializeVectorToString(vector<T> vector)
{
string result = "";
for (auto& iterator : vector)
Expand All @@ -22,7 +22,7 @@ class UnitTestHelper
}

template<typename T>
string VerifyVectorResult(vector<pair<T,T>> vector)
string SerializeVectorToString(vector<pair<T,T>> vector)
{
string result = "";
for (auto& iterator : vector)
Expand All @@ -38,7 +38,7 @@ class UnitTestHelper
}

template<typename T>
string VerifyVectorResult(vector<pair<T, pair<T, T>>> vector)
string SerializeVectorToString(vector<pair<T, pair<T, T>>> vector)
{
string result = "";
for (auto& iterator : vector)
Expand All @@ -54,7 +54,7 @@ class UnitTestHelper
}

template<typename T>
string VerifyVectorResult(vector<vector<T>> vector)
string SerializeVectorToString(vector<vector<T>> vector)
{
string result = "";
for (auto& iterator : vector)
Expand Down Expand Up @@ -107,4 +107,33 @@ class UnitTestHelper

return data;
}

template<typename T>
bool NormalizeCyclesAnCompare(vector<T> data1, vector<T> data2)
{
if (data1.size() != data2.size())
{
return false;
}

// Normalized rotation of cycle 1
vector<T> normalizedCycle1(data1);
auto minIterator1 = min_element(normalizedCycle1.begin(), normalizedCycle1.end());
rotate(normalizedCycle1.begin(), minIterator1, normalizedCycle1.end());

// Normalized rotation of cycle 2
vector<T> normalizedCycle2(data2);
auto minIterator2 = min_element(normalizedCycle2.begin(), normalizedCycle2.end());
rotate(normalizedCycle2.begin(), minIterator2, normalizedCycle2.end());

// Check clock wise
if (normalizedCycle1 == normalizedCycle2)
{
return true;
}

// Check counter clock wise
reverse(normalizedCycle2.begin() + 1, normalizedCycle2.end());
return (normalizedCycle1 == normalizedCycle2);
}
};
12 changes: 6 additions & 6 deletions Tests/0002_Tree/0001_BinarySearchTreeTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace BinarySearchTreeTest
bst.InsertNode(60);


string actualResult = unitTestHelper.VerifyVectorResult(bst.GetRecursiveInorderTravesalResult());
string actualResult = unitTestHelper.SerializeVectorToString(bst.GetRecursiveInorderTravesalResult());
string expectedResult = "30 50 60";

EXPECT_EQ(actualResult, expectedResult);
Expand All @@ -28,7 +28,7 @@ namespace BinarySearchTreeTest
bst.InsertNode(30);
bst.InsertNode(60);

string actualResult = unitTestHelper.VerifyVectorResult(bst.GetRecursivePreorderTravesalResult());
string actualResult = unitTestHelper.SerializeVectorToString(bst.GetRecursivePreorderTravesalResult());
string expectedResult = "50 30 60";

EXPECT_EQ(actualResult, expectedResult);
Expand All @@ -41,7 +41,7 @@ namespace BinarySearchTreeTest
bst.InsertNode(30);
bst.InsertNode(60);

string actualResult = unitTestHelper.VerifyVectorResult(bst.GetRecursivePostorderTravesalResult());
string actualResult = unitTestHelper.SerializeVectorToString(bst.GetRecursivePostorderTravesalResult());
string expectedResult = "30 60 50";

EXPECT_EQ(actualResult, expectedResult);
Expand All @@ -55,7 +55,7 @@ namespace BinarySearchTreeTest
bst.InsertNode(60);


string actualResult = unitTestHelper.VerifyVectorResult(bst.GetMorrisInorderTraversalResult());
string actualResult = unitTestHelper.SerializeVectorToString(bst.GetMorrisInorderTraversalResult());
string expectedResult = "30 50 60";

EXPECT_EQ(actualResult, expectedResult);
Expand All @@ -69,7 +69,7 @@ namespace BinarySearchTreeTest
bst.InsertNode(60);


string actualResult = unitTestHelper.VerifyVectorResult(bst.GetMorrisPreorderTraversalResult());
string actualResult = unitTestHelper.SerializeVectorToString(bst.GetMorrisPreorderTraversalResult());
string expectedResult = "50 30 60";

EXPECT_EQ(actualResult, expectedResult);
Expand All @@ -83,7 +83,7 @@ namespace BinarySearchTreeTest
bst.InsertNode(60);


string actualResult = unitTestHelper.VerifyVectorResult(bst.GetMorrisPostorderTraversalResult());
string actualResult = unitTestHelper.SerializeVectorToString(bst.GetMorrisPostorderTraversalResult());
string expectedResult = "30 60 50";

EXPECT_EQ(actualResult, expectedResult);
Expand Down
4 changes: 2 additions & 2 deletions Tests/0003_Graph/0001_BreadthFirstSearchTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace BreadthFirstSearchTest

graph.BFS(1);

string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowBFSResult());
string actualResult = unitTestHelper.SerializeVectorToString(graph.ShowBFSResult());
string expectedResult = "1(0) 2(1) 3(1) 4(2) 5(2) 6(2) 7(3) 8(3)";
EXPECT_EQ(actualResult, expectedResult);
}
Expand All @@ -38,7 +38,7 @@ namespace BreadthFirstSearchTest

graph.BFS(1);

string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowBFSResult());
string actualResult = unitTestHelper.SerializeVectorToString(graph.ShowBFSResult());
string expectedResult = "1(0) 2(1)";
EXPECT_EQ(actualResult, expectedResult);
}
Expand Down
18 changes: 9 additions & 9 deletions Tests/0003_Graph/0002_DepthFirstSearchTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace DepthFirstSearchTest

graph.DFS();

string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowDFSResult());
string actualResult = unitTestHelper.SerializeVectorToString(graph.ShowDFSResult());
string expectedResult = "1(1,8) 2(2,7) 3(9,12) 4(4,5) 5(3,6) 6(10,11)";
EXPECT_EQ(actualResult, expectedResult);
}
Expand All @@ -35,7 +35,7 @@ namespace DepthFirstSearchTest

graph.DFS();

string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowDFSResult());
string actualResult = unitTestHelper.SerializeVectorToString(graph.ShowDFSResult());
string expectedResult = "1(1,2)";
EXPECT_EQ(actualResult, expectedResult);
}
Expand All @@ -49,7 +49,7 @@ namespace DepthFirstSearchTest

graph.DFS();

string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowDFSResult());
string actualResult = unitTestHelper.SerializeVectorToString(graph.ShowDFSResult());
string expectedResult = "1(1,4) 2(2,3) 3(5,8) 4(6,7)";
EXPECT_EQ(actualResult, expectedResult);
}
Expand All @@ -64,7 +64,7 @@ namespace DepthFirstSearchTest

graph.DFS();

string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowDFSResult());
string actualResult = unitTestHelper.SerializeVectorToString(graph.ShowDFSResult());
string expectedResult = "1(1,6) 2(2,5) 3(3,4)";
EXPECT_EQ(actualResult, expectedResult);
}
Expand Down Expand Up @@ -92,7 +92,7 @@ namespace DepthFirstSearchTest

graph.DFS();

string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowDFSResult());
string actualResult = unitTestHelper.SerializeVectorToString(graph.ShowDFSResult());
string expectedResult = "1(1,32) 2(2,29) 3(30,31) 4(3,28) 5(4,27) 6(5,26) 7(6,25) 8(7,24) 9(8,23) 10(9,22) 11(10,21) 12(11,20) 13(12,19) 14(13,18) 15(14,17) 16(15,16)";
EXPECT_EQ(actualResult, expectedResult);
}
Expand All @@ -108,7 +108,7 @@ namespace DepthFirstSearchTest

graph.DFS();

string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowDFSResult());
string actualResult = unitTestHelper.SerializeVectorToString(graph.ShowDFSResult());
string expectedResult = "1(1,2) 2(3,4) 3(5,6)";
EXPECT_EQ(actualResult, expectedResult);
}
Expand All @@ -125,7 +125,7 @@ namespace DepthFirstSearchTest

graph.DFS();

string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowDFSResult());
string actualResult = unitTestHelper.SerializeVectorToString(graph.ShowDFSResult());
string expectedResult = "1(1,8) 2(2,7) 3(3,4) 4(5,6)";
EXPECT_EQ(actualResult, expectedResult);
}
Expand All @@ -150,7 +150,7 @@ namespace DepthFirstSearchTest

graph.DFS();

string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowDFSResult());
string actualResult = unitTestHelper.SerializeVectorToString(graph.ShowDFSResult());
string expectedResult = "1(1,8) 2(2,7) 3(3,6) 4(4,5)";
EXPECT_EQ(actualResult, expectedResult);
}
Expand All @@ -169,7 +169,7 @@ namespace DepthFirstSearchTest

graph.DFS();

string actualResult = unitTestHelper.VerifyVectorResult(graph.ShowDFSResult());
string actualResult = unitTestHelper.SerializeVectorToString(graph.ShowDFSResult());
string expectedResult = "1(1,6) 2(2,5) 3(3,4)";
EXPECT_EQ(actualResult, expectedResult);
}
Expand Down
Loading