-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.h
165 lines (116 loc) · 5.15 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
#pragma once
#include<iostream>
using namespace std;
/*
|Verticies| |<--- adjacency list --->|
+---------+
| | +------+------+------+------+------+------+------+------+
| pointer |-------->| node | node | node | node | node | node | node | node |
| | +------+------+------+------+------+------+------+------+
+---------+
| | +------+------+------+------+------+------+------+------+
| pointer |-------->| node | node | node | node | node | node | node | node |
| | +------+------+------+------+------+------+------+------+
+---------+
| | +------+------+------+------+------+------+------+------+
| pointer |-------->| node | node | node | node | node | node | node | node |
| | +------+------+------+------+------+------+------+------+
+---------+
| | +------+------+------+------+------+------+------+------+
| pointer |-------->| node | node | node | node | node | node | node | node |
| | +------+------+------+------+------+------+------+------+
+---------+
*/
/*
This header file defines the graph class.
# Member variables:
- Node : a signle node in the adjacency list
- Color : enumirations of the colors used to detect a cycle
- vertices_count : total number of verticies in the graph
- verticies : a 1D array of a Node pointer
# Basic operations are:
- Constructor : allocates a graph in memory
- Destructor : deletes the graph from memory
- addEdge : adds an edge between two verticies
- printGraph : prints a formated graph
- DFSUtil : performs a Depth first search
- isCyclic : detects cycles in the graph
- deleteEdge : removes an edge between two verticies
- checkEdge : checks the source verticies and destination verticies of an edge is valid/within bound
- << : output operator overloading
*/
class Graph {
private:
/*
Node class
# Member variables :
- index :index of the vertex
- next : a pointer to a Node
# Basic operations are :
- constructors : allocates a Node in the memory
*/
class Node
{
public:
//takes index and initialization a node with index [i] and next [NULL];
Node(int data);
Node();
int index;
Node* next;
};
enum Color { WHITE, GRAY, BLACK };
int vertices_count;
Node** verticies;
bool DFSUtil(int vertices_count, int color[]);
/*----------------------------------------------------------------------
definiton: helper function, performs a recursive DFS on graph
fails:
condition:
-----------------------------------------------------------------------*/
public:
Graph(int vertices_count = 0);
/*----------------------------------------------------------------------
definiton: take <vertices_count> and construct a graph with <vertices_count> verticies
fails: no free contiguous memory
condition: <vertices_count> is more than one
-----------------------------------------------------------------------*/
~Graph();
/*----------------------------------------------------------------------
definiton: deallocates a graph from memory
fails:
condition:
-----------------------------------------------------------------------*/
void addEdge(int source_node_index, int destination_node_index);
/*----------------------------------------------------------------------
definiton: takes the src node and dest node, and addes a node with index[destination_node_index] to the adjacency list of vertix[source_node_index]
fails: no free contiguous memory for a new node
condition: <source_node_index> and <destination_node_index> are between 0 and (V - 1)
-----------------------------------------------------------------------*/
void printGraph();
/*----------------------------------------------------------------------
definiton: visualise the connections between a graph
fails:
condition:
-----------------------------------------------------------------------*/
bool isCyclic();
/*----------------------------------------------------------------------
definiton: assigns a <WHITE> color to all verticies in <color> array. calls <DFSUtil> for each <WHITE> node
fails: no free contiguous memory
condition:
return: true if cycle is detected, false otherwise
-----------------------------------------------------------------------*/
void deleteEdge(int source_node_index, int destination_node_index);
/*----------------------------------------------------------------------
definiton: removes node [destination_node_index] from the adjacency list of node [source_node_index]
fails:
condition: <source_node_index> and <destination_node_index> are between 0 and (V - 1)
-----------------------------------------------------------------------*/
bool checkEdge(int source_node_index, int destination_node_index);
/*----------------------------------------------------------------------
definiton: checks the range of the edge
fails:
condition:
returns: true if <source_node_index> and <destination_node_index> are between 0 and (V - 1); false otherwise
-----------------------------------------------------------------------*/
};
ostream& operator <<(ostream& out, Graph& g);