Knight's Tour Generator
Tourneys and the Fast Generation and Obfuscation of Closed Knight's Tours
Graph.h
Go to the documentation of this file.
1 
4 // MIT License
5 //
6 // Copyright (c) 2019 Ian Parberry
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 
26 #include "Includes.h"
27 #include "Random.h"
28 
29 #ifndef __Graph__
30 #define __Graph__
31 
32 class CVertex; //forward declaration
33 
40 
41 class CEdge{
42  protected:
43  CVertex* m_pVertex0 = nullptr;
44  CVertex* m_pVertex1 = nullptr;
45 
46  UINT m_nIndex = 0;
47  bool m_bMarked = false;
48 
49  public:
50  CEdge(CVertex* p0, CVertex* p1, UINT index);
51 
53 
54  UINT GetIndex();
55 
56  void Mark(bool b=true);
57  bool Marked();
58 
59  void GetVertexIndices(UINT& i0, UINT& i1);
60 }; //CEdge
61 
63 
69 
70 class CVertex{
71  private:
72  std::vector<CEdge*> m_vAdjacencyList;
73 
74  UINT m_nIndex = 0;
75  bool m_bMarked = false;
76 
77  public:
78  CVertex();
79 
80  void InsertAdjacency(CEdge* pEdge);
81 
82  void SetIndex(const UINT n);
83  UINT GetIndex();
84 
85  void Mark(bool b=true);
86  bool Marked();
87 
88  std::vector<CEdge*>* GetAdjacencyList();
89  size_t GetDegree();
90 }; //CVertex
91 
93 
101 
102 class CGraph{
103  protected:
104  std::vector<CEdge*> m_vEdgeList;
106 
107  CVertex* m_pVertexList = nullptr;
109 
110  std::queue<CVertex*> m_qBFSQueue;
111 
113 
114  public:
115  CGraph(const UINT n);
116  ~CGraph();
117 
118  void InsertEdge(const UINT i, const UINT j);
119  UINT BFSF(std::vector<UINT>& result);
120 
121  void PrintGraph();
122 }; //CGraph
123 
124 #endif
CVertex * GetNextVertex(CVertex *p)
Get vertex at other end of edge.
Definition: Graph.cpp:47
size_t GetDegree()
Get degree.
Definition: Graph.cpp:135
UINT GetIndex()
Get index;.
Definition: Graph.cpp:107
UINT m_nNumEdges
Number of edges.
Definition: Graph.h:105
CRandom m_cRandom
Random number generator.
Definition: Graph.h:112
UINT m_nIndex
Index into vertex list.
Definition: Graph.h:74
void Mark(bool b=true)
Mark or unmark.
Definition: Graph.cpp:114
Useful includes.
void PrintGraph()
Print the graph to a text file.
Definition: Graph.cpp:233
std::queue< CVertex * > m_qBFSQueue
Queue for breadth-first search.
Definition: Graph.h:110
bool m_bMarked
Mark flag.
Definition: Graph.h:47
std::vector< CEdge * > m_vEdgeList
Edge list.
Definition: Graph.h:104
unsigned int UINT
Abbreviation for unsigned integer.
Definition: Defines.h:84
void InsertAdjacency(CEdge *pEdge)
Add an edge to the edge list.
Definition: Graph.cpp:92
void SetIndex(const UINT n)
Set index.
Definition: Graph.cpp:100
void InsertEdge(const UINT i, const UINT j)
Insert an edge.
Definition: Graph.cpp:172
Undirected multi-graph.
Definition: Graph.h:102
CVertex * m_pVertex0
Vertex at one end of the edge,.
Definition: Graph.h:43
void Mark(bool b=true)
Mark or unmark.
Definition: Graph.cpp:63
UINT BFSF(std::vector< UINT > &result)
Breadth-first spanning forest.
Definition: Graph.cpp:189
Graph vertex.
Definition: Graph.h:70
std::vector< CEdge * > m_vAdjacencyList
Adjacency list.
Definition: Graph.h:72
bool Marked()
Get mark.
Definition: Graph.cpp:70
UINT GetIndex()
Get index;.
Definition: Graph.cpp:56
CVertex * m_pVertex1
Vertex at the other end of the edge.
Definition: Graph.h:44
Pseudorandom number generator (PRNG for short).
Definition: Random.h:38
CEdge(CVertex *p0, CVertex *p1, UINT index)
Constructor.
Definition: Graph.cpp:38
CGraph(const UINT n)
Constructor.
Definition: Graph.cpp:145
Header for the pseudo-random number generator CRandom.
CVertex()
Constructor.
Definition: Graph.cpp:86
UINT m_nNumVerts
Number of vertices.
Definition: Graph.h:108
bool Marked()
Get mark.
Definition: Graph.cpp:121
~CGraph()
Destructor.
Definition: Graph.cpp:158
std::vector< CEdge * > * GetAdjacencyList()
Get adjacency list.
Definition: Graph.cpp:128
bool m_bMarked
Mark flag.
Definition: Graph.h:75
Graph edge.
Definition: Graph.h:41
UINT m_nIndex
Index into edge list.
Definition: Graph.h:46
CVertex * m_pVertexList
Vertex list.
Definition: Graph.h:107
void GetVertexIndices(UINT &i0, UINT &i1)
Get vertex indices.
Definition: Graph.cpp:78