forked from Silver-Taurus/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.h
219 lines (191 loc) · 7.16 KB
/
graph.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#ifndef _GRAPH_H
#define _GRAPH_H
#include <vector>
#include <queue>
namespace algo {
template <class dataType> // Type of data vertex will hold
class Graph {
int numOfVertices; // number of vertices.
struct Vertex; // forward declaration of vertex structure
struct Node { // linkedlist for mapping edges in the graph
Vertex * vertexPtr; // points to the vertex to which the edge is adjecent
Node * next; // points to the next edge belonging to same vertex
};
enum visitedState{ // Enum representing visited state of vertex
WHITE, // not yet visited
GRAY, // being visited
BLACK // visited
};
struct Vertex {
visitedState state; // state of vertex, visited/being visited/done
dataType data; // the template data
Node * list; // Pointer to all edges (linkedlist)
};
std::vector<Vertex> vertices; // vector of all vertices.
//private methods
Node * getNode( Vertex * ); // allocate and initialize a newnode for the adj list.
void insertAtEnd( Node * & , Vertex * ); // insert at the end of adjacency list of vertex.
void deleteAllAfter( Node * ); // delete the adjacency list of the vertex.
void depth_first_traversal_util(Vertex * ); // Private utility function for DFS
public:
Graph() = default; // Default constructor
Graph(std::vector<dataType> &); // Constructor which takes vector of vertex data
void setEdge(dataType, dataType); // For setting a edge of graph
void display() const; // Print current config of the graph.
void breadth_first_search(dataType); // Breadth first traversal of the graph
void depth_first_search(dataType); // Depth first traversal of the graph
~Graph();
}; //end of class Graph
template <typename dataType>
typename Graph<dataType>::Node *
algo::Graph<dataType>::getNode(Vertex * v) // allocate and initialize a newnode for the adj list.
{
Node * newNode = new Node;
newNode->vertexPtr = v;
newNode->next = nullptr;
return newNode;
}
template <typename dataType>
void Graph<dataType>::insertAtEnd( Node * & node, Vertex * v) // insert at the end of adjacency list of vertex.
{
Node *newNode = getNode(v);
if ( node == nullptr ) {
node = newNode;
} else {
Node * temp = node;
while( temp->next != nullptr ) {
temp = temp->next;
}
temp->next = newNode;
}
}
template <typename dataType>
void Graph<dataType>::deleteAllAfter( Node * node ) // delete the adjacency list of the vertex.
{
Node * nextNode;
while( node != nullptr ) {
nextNode = node->next;
delete(node);
node = nextNode;
}
}
template <typename dataType>
Graph<dataType>::Graph(std::vector<dataType> & values) // Non default constructor, takes a vector of vertices data
: numOfVertices(values.size()),
vertices(numOfVertices)
{
for ( int i = 0; i < numOfVertices; ++i ) {
vertices[i].data = values[i];
vertices[i].list = nullptr;
vertices[i].state = WHITE;
}
}
template <typename dataType>
void Graph<dataType>::setEdge(dataType data1, dataType data2) // Setting individual edge of the graph.
{
for (int i = 0; i < numOfVertices; ++i) {
if (vertices[i].data == data1) {
for ( int j = 0; j < numOfVertices; ++j) {
if (vertices[j].data == data2) {
insertAtEnd(vertices[i].list, &vertices[j]);
break;
}
}
break;
}
}
}
template <typename dataType>
void Graph<dataType>::display() const // Prints the current config of the graph
{
Node * node;
for ( int i = 0; i < numOfVertices; ++i ) {
std::cout << "Vertex:" << vertices[i].data << " ";
std::cout << "Connections: ";
node = vertices[i].list;
while( node != nullptr ) {
std::cout << node->vertexPtr->data << " ";
node = node->next;
}
std::cout << std::endl;
}
}
template <typename dataType>
void Graph<dataType>::breadth_first_search(dataType startElem) // Breadth first traversal of the graph
{
//mark all vertices as not visited, i.e. state = WHITE
for ( int i = 0; i < numOfVertices; ++i ) {
vertices[i].state = WHITE;
}
// search for the vertex containing start element
Vertex * startVertex = nullptr;
for ( int i = 0; i < numOfVertices; ++i ) {
if ( vertices[i].data == startElem ) {
startVertex = &vertices[i];
break;
}
}
//Return if start vertex not found
if ( startVertex == nullptr ) {
return;
}
//Create a queue for traversing breadth wise.
std::queue<Vertex *> vertexQueue;
//mark the first vertex as being processed
startVertex->state = GRAY;
//push the first vertex
vertexQueue.push(startVertex);
Vertex * currVertex = nullptr;
while( !vertexQueue.empty() ) {
currVertex = vertexQueue.front();
vertexQueue.pop();
currVertex->state = BLACK;
std::cout << currVertex->data << " ";
Node * adjVertex = currVertex->list;
while( adjVertex != nullptr ) {
if ( adjVertex->vertexPtr->state == WHITE ) {
adjVertex->vertexPtr->state = GRAY;
vertexQueue.push(adjVertex->vertexPtr);
}
adjVertex = adjVertex->next;
}
}
std::cout << std::endl;
}
template <typename dataType>
void Graph<dataType>::depth_first_traversal_util(Vertex * v) // Depth first search private utility function
{
v->state = GRAY;
std::cout << v->data << " ";
Node * node = v->list;
while( node != nullptr ) {
if (node->vertexPtr->state == WHITE) {
depth_first_traversal_util(node->vertexPtr);
}
node = node->next;
}
v->state = BLACK;
}
template <typename dataType>
void Graph<dataType>::depth_first_search(dataType startElem) // Public function for depth first traversal
{
for( int i = 0; i < numOfVertices; ++i ) {
vertices[i].state = WHITE;
}
for ( int i = 0; i < numOfVertices; ++i) {
if (vertices[i].data == startElem) {
depth_first_traversal_util(&vertices[i]);
break;
}
}
std::cout << std::endl;
}
template <typename dataType>
Graph<dataType>::~Graph()
{
for( int i = 0; i < numOfVertices; ++i ) {
deleteAllAfter(vertices[i].list);
}
}
} //end of namespace algo
#endif