Skip to content

Commit 2afa34c

Browse files
committed
core: namespace added for ham cycle
1 parent 5f95bc2 commit 2afa34c

File tree

3 files changed

+117
-112
lines changed

3 files changed

+117
-112
lines changed

Headers/0003_Graph/0005_HamiltonianPathAndCycle.h

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,36 @@
55
#include<vector>
66
using namespace std;
77

8-
class HamiltonianNode
8+
namespace HamiltonianPathAndCycle
99
{
10-
public:
11-
int data;
12-
bool isVisited;
13-
HamiltonianNode(int value);
14-
};
10+
class Node
11+
{
12+
public:
13+
int data;
14+
bool isVisited;
15+
Node(int value);
16+
};
1517

16-
class HamiltonianGraph
17-
{
18-
private:
19-
bool _isHamiltonianCyclePresent;
20-
bool _isHamiltonianPathPresent;
21-
int _visitedNodeCount;
22-
HamiltonianNode* _startingNode;
23-
map<HamiltonianNode*, unordered_set<HamiltonianNode*>> _adjlist;
24-
map<int, HamiltonianNode*> _nodeMap;
25-
vector<int> _hamiltonianPath;
26-
HamiltonianNode* MakeOrFindNode(int value);
27-
bool IsSafe(HamiltonianNode* nodeU, HamiltonianNode* nodeV);
28-
bool HamiltonianCycleAndPathUtil(HamiltonianNode* node);
18+
class Graph
19+
{
20+
private:
21+
bool _isHamiltonianCyclePresent;
22+
bool _isHamiltonianPathPresent;
23+
int _visitedNodeCount;
24+
Node* _startingNode;
25+
map<Node*, unordered_set<Node*>> _adjlist;
26+
map<int, Node*> _nodeMap;
27+
vector<int> _hamiltonianPath;
28+
Node* MakeOrFindNode(int value);
29+
bool IsSafe(Node* nodeU, Node* nodeV);
30+
bool HamiltonianCycleAndPathUtil(Node* node);
2931

30-
public:
31-
void PushUndirectedEdge(int valueU, int valueV);
32-
void PushSingleNode(int valueU);
33-
void FindHamiltonianCycleAndPath();
34-
bool IsHamiltonianCyclePresent();
35-
bool IsHamiltonianPathPresent();
36-
vector<int> GetHamiltonianPath();
37-
};
32+
public:
33+
void PushUndirectedEdge(int valueU, int valueV);
34+
void PushSingleNode(int valueU);
35+
void FindHamiltonianCycleAndPath();
36+
bool IsHamiltonianCyclePresent();
37+
bool IsHamiltonianPathPresent();
38+
vector<int> GetHamiltonianPath();
39+
};
40+
}
Lines changed: 85 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,114 @@
11
#include "../Headers/0003_Graph/0005_HamiltonianPathAndCycle.h"
22

33
using namespace std;
4-
5-
HamiltonianNode::HamiltonianNode(int value)
6-
{
7-
this->data = value;
8-
this->isVisited = false;
9-
}
10-
11-
// HamiltonianGraph Private Member Methods
12-
HamiltonianNode* HamiltonianGraph::MakeOrFindNode(int value)
4+
namespace HamiltonianPathAndCycle
135
{
14-
HamiltonianNode* node = nullptr;
15-
if (this->_nodeMap.find(value) == this->_nodeMap.end())
6+
Node::Node(int value)
167
{
17-
node = new HamiltonianNode(value);
18-
this->_nodeMap[value] = node;
8+
this->data = value;
9+
this->isVisited = false;
1910
}
20-
else
21-
{
22-
node = this->_nodeMap[value];
23-
}
24-
return node;
25-
}
2611

27-
bool HamiltonianGraph::IsSafe(HamiltonianNode* nodeU, HamiltonianNode* nodeV)
28-
{
29-
if (this->_adjlist[nodeU].find(nodeV) == this->_adjlist[nodeU].end())
12+
// Graph Private Member Methods
13+
Node* Graph::MakeOrFindNode(int value)
3014
{
31-
return false;
32-
}
33-
if (nodeV->isVisited == true)
34-
{
35-
return false;
15+
Node* node = nullptr;
16+
if (this->_nodeMap.find(value) == this->_nodeMap.end())
17+
{
18+
node = new Node(value);
19+
this->_nodeMap[value] = node;
20+
}
21+
else
22+
{
23+
node = this->_nodeMap[value];
24+
}
25+
return node;
3626
}
37-
return true;
38-
}
3927

40-
bool HamiltonianGraph::HamiltonianCycleAndPathUtil(HamiltonianNode* nodeU)
41-
{
42-
if (this->_visitedNodeCount == this->_nodeMap.size())
28+
bool Graph::IsSafe(Node* nodeU, Node* nodeV)
4329
{
44-
if (this->_adjlist[nodeU].find(this->_startingNode) != this->_adjlist[nodeU].end())
30+
if (this->_adjlist[nodeU].find(nodeV) == this->_adjlist[nodeU].end())
4531
{
46-
this->_isHamiltonianCyclePresent = true;
47-
this->_isHamiltonianPathPresent = true;
48-
return true;
32+
return false;
4933
}
50-
this->_isHamiltonianPathPresent = true;
51-
return false;
34+
if (nodeV->isVisited == true)
35+
{
36+
return false;
37+
}
38+
return true;
5239
}
53-
for (auto& nodeV : this->_adjlist[nodeU])
40+
41+
bool Graph::HamiltonianCycleAndPathUtil(Node* nodeU)
5442
{
55-
if (this->IsSafe(nodeU, nodeV))
43+
if (this->_visitedNodeCount == this->_nodeMap.size())
5644
{
57-
this->_hamiltonianPath.push_back(nodeV->data);
58-
nodeV->isVisited = true;
59-
this->_visitedNodeCount++;
60-
if (this->HamiltonianCycleAndPathUtil(nodeV))
45+
if (this->_adjlist[nodeU].find(this->_startingNode) != this->_adjlist[nodeU].end())
6146
{
47+
this->_isHamiltonianCyclePresent = true;
48+
this->_isHamiltonianPathPresent = true;
6249
return true;
6350
}
64-
this->_visitedNodeCount--;
65-
nodeV->isVisited = false;
66-
this->_hamiltonianPath.pop_back();
51+
this->_isHamiltonianPathPresent = true;
52+
return false;
53+
}
54+
for (auto& nodeV : this->_adjlist[nodeU])
55+
{
56+
if (this->IsSafe(nodeU, nodeV))
57+
{
58+
this->_hamiltonianPath.push_back(nodeV->data);
59+
nodeV->isVisited = true;
60+
this->_visitedNodeCount++;
61+
if (this->HamiltonianCycleAndPathUtil(nodeV))
62+
{
63+
return true;
64+
}
65+
this->_visitedNodeCount--;
66+
nodeV->isVisited = false;
67+
this->_hamiltonianPath.pop_back();
68+
}
6769
}
70+
return false;
6871
}
69-
return false;
70-
}
7172

72-
// HamiltonianGraph Public Member Methods
73-
void HamiltonianGraph::PushUndirectedEdge(int valueU, int valueV)
74-
{
75-
HamiltonianNode* nodeU = this->MakeOrFindNode(valueU);
76-
HamiltonianNode* nodeV = this->MakeOrFindNode(valueV);
73+
// Graph Public Member Methods
74+
void Graph::PushUndirectedEdge(int valueU, int valueV)
75+
{
76+
Node* nodeU = this->MakeOrFindNode(valueU);
77+
Node* nodeV = this->MakeOrFindNode(valueV);
7778

78-
this->_adjlist[nodeU].insert(nodeV);
79-
this->_adjlist[nodeV].insert(nodeU);
80-
}
79+
this->_adjlist[nodeU].insert(nodeV);
80+
this->_adjlist[nodeV].insert(nodeU);
81+
}
8182

82-
void HamiltonianGraph::PushSingleNode(int valueU)
83-
{
84-
HamiltonianNode* nodeU = this->MakeOrFindNode(valueU);
85-
}
83+
void Graph::PushSingleNode(int valueU)
84+
{
85+
Node* nodeU = this->MakeOrFindNode(valueU);
86+
}
8687

87-
void HamiltonianGraph::FindHamiltonianCycleAndPath()
88-
{
89-
this->_isHamiltonianCyclePresent = false;
90-
this->_isHamiltonianPathPresent = false;
91-
this->_hamiltonianPath = {};
92-
this->_startingNode = this->_nodeMap[0];
93-
this->_hamiltonianPath.push_back(this->_startingNode->data);
94-
this->_startingNode->isVisited = true;
95-
this->_visitedNodeCount = 1;
96-
this->HamiltonianCycleAndPathUtil(this->_startingNode);
97-
}
88+
void Graph::FindHamiltonianCycleAndPath()
89+
{
90+
this->_isHamiltonianCyclePresent = false;
91+
this->_isHamiltonianPathPresent = false;
92+
this->_hamiltonianPath = {};
93+
this->_startingNode = this->_nodeMap[0];
94+
this->_hamiltonianPath.push_back(this->_startingNode->data);
95+
this->_startingNode->isVisited = true;
96+
this->_visitedNodeCount = 1;
97+
this->HamiltonianCycleAndPathUtil(this->_startingNode);
98+
}
9899

99-
bool HamiltonianGraph::IsHamiltonianCyclePresent()
100-
{
101-
return this->_isHamiltonianCyclePresent;
102-
}
100+
bool Graph::IsHamiltonianCyclePresent()
101+
{
102+
return this->_isHamiltonianCyclePresent;
103+
}
103104

104-
bool HamiltonianGraph::IsHamiltonianPathPresent()
105-
{
106-
return this->_isHamiltonianPathPresent;
107-
}
105+
bool Graph::IsHamiltonianPathPresent()
106+
{
107+
return this->_isHamiltonianPathPresent;
108+
}
108109

109-
vector<int> HamiltonianGraph::GetHamiltonianPath()
110-
{
111-
return this->_hamiltonianPath;
110+
vector<int> Graph::GetHamiltonianPath()
111+
{
112+
return this->_hamiltonianPath;
113+
}
112114
}

Tests/0003_Graph/0005_HamiltonianPathAndCycleTest.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
#include "../Headers/0003_Graph/0005_HamiltonianPathAndCycle.h"
33
#include "../0000_CommonUtilities/UnitTestHelper.h"
44

5-
namespace HamiltonianPathAndCycleTest
5+
namespace HamiltonianPathAndCycle
66
{
77
UnitTestHelper unitTestHelper;
88

99
TEST(HamiltonianCycleAndPathTest, ShowResult)
1010
{
11-
HamiltonianGraph graph;
11+
Graph graph;
1212

1313
graph.PushUndirectedEdge(0, 1);
1414
graph.PushUndirectedEdge(0, 3);

0 commit comments

Comments
 (0)