11#include " ../Headers/0003_Graph/0005_HamiltonianPathAndCycle.h"
22
33using 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}
0 commit comments