-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.cpp
111 lines (72 loc) · 2.19 KB
/
graph.cpp
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
#include "graph.h"
graph::graph() {
std::cout << "Enter the number of vertices : ";
std::cin >> VertexNum;
edgeListLen = new int[VertexNum];
Vertices = new int[VertexNum];
Visited = new bool[VertexNum];
EdgeList = new int*[VertexNum];
std::cout << "Enter the Vertices :" << std::endl;
for (int i = 0; i < VertexNum; i++) {
std::cin >> Vertices[i];
Visited[i] = false;
}
for (int i = 0; i < VertexNum; i++) {
std::cout << "Enter the number of adjacent vertices of vertex " << Vertices[i] << " : ";
std::cin >> edgeListLen[i];
EdgeList[i] = new int[edgeListLen[i]];
std::cout << "Enter the adjacent vertices of vertex " << Vertices[i] << " : \n";
for (int j = 0; j < edgeListLen[i]; j++) {
std::cin >> EdgeList[i][j];
}
}
for (int subarr = 0, arr = 0; arr < VertexNum; arr++) {
std::cout << "\n" << Vertices[arr] << " => ";
for (subarr = 0; subarr < edgeListLen[arr]; subarr++) {
std::cout << EdgeList[arr][subarr] << " ";
}
std::cout << "\n";
}
}
/*
bool graph::AddEdge(int* vertex1, int* vertex2) {
}
*/
int graph::searchPos(int VrtValue) {
bool vertexSearchFlag = false;
try {
for (int arrVal = 0; arrVal < VertexNum; arrVal++) {
if (Vertices[arrVal] == VrtValue) {
return arrVal;
}
}
if (vertexSearchFlag == false) {
throw 101;
}
}
catch (int errValue) {
std::cerr << errValue << "Value Not Found." << std::endl;
return -3333;
}
}
void graph::DFT(int startFromNode) {
int NodePos = searchPos(startFromNode);
std::cout << " " << Vertices[NodePos];
Visited[NodePos] = true;
if (NodePos != -3333) {
for (int range = 0; range < edgeListLen[NodePos]; range++) {
// To find if the node has not been visited yet.
// Range travels over the the length of array that EdgeList[NudePos] points to.
// searchPos queries the position of the vertex in the vertices list
// This queried value is then passed in the Visited array to check if it is already visited or not.
// if not then it calls depth first search on it
// otherwise it continues to the next iteration
if (Visited[searchPos(EdgeList[NodePos][range])] == false) {
DFT(EdgeList[NodePos][range]);
}
else {
continue;
}
}
}
}