-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphpath.cpp
190 lines (163 loc) · 5.52 KB
/
graphpath.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
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
#include <iostream>
#include <utility>
#include <algorithm>
#include <time.h>
#include <array>
#include <list>
// Determine if there is a path from one specified node to another
// Each node is made up of a pair of numbers that are the edges to other nodes
using std::cout;
using std::endl;
const int ARRAY_SIZE=5;
const int EDGE_RANGE=100;
typedef std::pair<int,int> intPair;
typedef std::list< intPair > adjacencyList;
// pair output
std::ostream& operator<<(std::ostream &os, const intPair &p)
{
os << "<" << p.first << "," << p.second << ">";
return os;
}
// adjacency list output
std::ostream& operator<<(std::ostream &os, const adjacencyList &aList)
{
os << "[ ";
if (!aList.empty())
for (intPair p : aList)
{
os << p << " ";
}
os << "]";
return os;
}
// adjacency array output
std::ostream& operator<<( std::ostream &os,
const std::array< adjacencyList, EDGE_RANGE > &adjacent)
{
for (int i=0; i<EDGE_RANGE; ++i)
{
os << i << ": " << adjacent[i] << endl;
}
return os;
}
// creates an input array of values
void
initRandomPairArray(
std::array< intPair, ARRAY_SIZE > &nodeArray // IN: array to be set
)
{
// Create random array of pairs
for (int i = 0; i< ARRAY_SIZE; ++i)
{
int first = rand() % EDGE_RANGE;
int second = rand() % EDGE_RANGE;
while (first == second) // lets make sure the first and second values are different
second = rand() % EDGE_RANGE;
nodeArray[i].first = first;
nodeArray[i].second = second;
}
}
void
createEdgeList( std::array< intPair, ARRAY_SIZE > &nodeArray, // IN: array of nodes
std::array< adjacencyList, EDGE_RANGE > &adjacent ) // OUT: resulting list
{
// Create an array which maps a value to a node or nodes in a list of
// pair <value, node-number>
// (using array and possibly wasting space but its a fast way to create this list, alternative is a map)
for (int i = 0; i< ARRAY_SIZE; ++i)
{
adjacent[nodeArray[i].first].emplace_back(i, nodeArray[i].second);
adjacent[nodeArray[i].second].emplace_back(i, nodeArray[i].first);
}
}
bool
findPath( std::array< adjacencyList, EDGE_RANGE> &adjacent, // IN adjacency array
std::array< intPair, ARRAY_SIZE > &nodeArray, // IN node array
int startNodeNumber, // IN where to start
int endNodeNumber) // IN where to end
{
// start a list of edges to scan
std::list<int> toScan;
toScan.emplace_back(nodeArray[startNodeNumber].first);
toScan.emplace_back(nodeArray[startNodeNumber].second);
// use this array a way to avoid cycles
std::array<bool,ARRAY_SIZE> seen;
seen.fill(false);
seen[startNodeNumber]=true;
// walk through the graph of nodes and edges
// for this edge, look at its list of adjacent nodes
auto scan = toScan.begin();
while (scan != toScan.end())
{
int edge = *scan;
adjacencyList ¤t = adjacent[edge];
// check the pair to see if we have found the endNode and if not
// add the next edge into the list to scan if we haven't visited it before
for (auto p : current)
{
// we can get to this node
int nodeNumber = p.first;
if (nodeNumber == endNodeNumber)
{
return true;
}
if (!seen[nodeNumber]) // avoid cycles
{
// push both its values on the toScan list
toScan.emplace_back(nodeArray[nodeNumber].first);
toScan.emplace_back(nodeArray[nodeNumber].second);
// don't visit this node again
seen[nodeNumber] = true;
}
}
++scan;
}
return false;
}
// NOTE quick driver for this problem; I wouldn't consider this a great unit test
int
main(int argc, const char * argv[])
{
// .TODO. add cmdline parsing
// arrays we will use for this problem
std::array <intPair, ARRAY_SIZE> nodeArray;
std::array< adjacencyList, EDGE_RANGE > adjacent;
// init rand
srand (time(NULL));
// remember, this is a driver, not a good unit test!
// repeat this a number of times
for (int j; j<20; ++j)
{
// initialize the values
initRandomPairArray( nodeArray );
// create edge/adjacency list (an array of lists of pairs)
createEdgeList( nodeArray, adjacent );
// choose the start, end nodes
int startNode = rand() % ARRAY_SIZE;
int endNode = rand() % ARRAY_SIZE;
while (startNode==endNode)
{
endNode = rand() % ARRAY_SIZE;
}
// output what we have
auto print = [](const intPair p) {cout << " " << p;};
cout << "Array: [ ";
for_each (nodeArray.begin(), nodeArray.end(), print);
cout << " ]" << endl;
cout << "finding path from " << startNode << " to " << endNode << endl;
cout << adjacent << endl;
// find the path
bool found = findPath( adjacent, nodeArray, startNode, endNode);
if (found)
cout << "** Path is found **" << endl << endl;
else
cout << "Path is not found" << endl << endl;
// reset adjacent
for (int i=0; i<EDGE_RANGE;++i)
{
adjacent[i].clear();
}
// cout << "***" << adjacent << "***" << endl;
}
return 1;
}