This repository has been archived by the owner on Oct 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphSearches.cpp
185 lines (157 loc) · 4.66 KB
/
GraphSearches.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
// Lab 15a, Write Functions For BFS and DFS
// Programmer: Minos Park
// Editor(s) used: Sublime Text 2
// Compiler(s) used: G++
#include <algorithm>
#include <fstream>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
struct Vertex
{
string name;
bool isVisited;
list<int> adjacentVertices;
};
queue<int> doBreadthFirstSearch(int iOriginVertex, vector<Vertex>& database)
{
queue<int> searchOrder;
queue<int> toDoList;
if (iOriginVertex < 0 || iOriginVertex >= database.size()) return searchOrder;
for (int i = 0; i < database.size(); i++)
database[i].isVisited = false;
database[iOriginVertex].isVisited = true;
searchOrder.push(iOriginVertex);
toDoList.push(iOriginVertex);
while (!toDoList.empty())
{
int t = toDoList.front();
toDoList.pop();
for (list<int>::iterator it = database[t].adjacentVertices.begin(); it != database[t].adjacentVertices.end(); it++)
{
if (database[*it].isVisited == false)
{
database[*it].isVisited = true;
searchOrder.push(*it);
toDoList.push(*it);
}
}
}
return searchOrder;
}
queue<int> doDepthFirstSearch(int iOriginVertex, vector<Vertex>& database)
{
queue<int> searchOrder;
stack<int> toDoList;
for (int i = 0; i < database.size(); i++) database[i].isVisited = false;
if (iOriginVertex < 0 || iOriginVertex >= database.size()) return searchOrder;
toDoList.push(iOriginVertex);
while (!toDoList.empty())
{
int t = toDoList.top();
toDoList.pop();
if (database[t].isVisited == false)
{
database[t].isVisited = true;
searchOrder.push(t);
for (list<int>::reverse_iterator r_it = database[t].adjacentVertices.rbegin(); r_it != database[t].adjacentVertices.rend(); ++r_it)
{
if (database[*r_it].isVisited == false)
toDoList.push(*r_it);
}
}
}
return searchOrder;
}
int main()
{
cout << "Lab 15a, Write Functions For BFS and DFS\n";
cout << "Programmer: Minos Park\n";
cout << "Editor(s) used: Sublime Text 2\n";
cout << "Compiler(s) used: G++\n";
cout << "File: " << __FILE__ << endl;
cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
ifstream fin;
fin.open("cities.txt");
if (!fin.good()) throw "I/O error";
// process the input file
vector<Vertex> database;
while (fin.good()) // EOF loop
{
string fromCity, toCity;
// read one edge
getline(fin, fromCity);
fromCity.erase(remove_if(fromCity.begin(), fromCity.end(), ::isspace), fromCity.end());
getline(fin, toCity);
toCity.erase(remove_if(toCity.begin(), toCity.end(), ::isspace), toCity.end());
fin.ignore(1000, 10); // skip the line with distance
fin.ignore(1000, 10); // skip the separator
// add vertices for new cities included in the edge
int iToVertex = -1, iFromVertex = -1, i;
for (i = 0; i < database.size(); i++) // seek "to" city
if (database[i].name == fromCity)
break;
if (i == database.size()) // not in database yet
{
// store the vertex if it is new
Vertex fromVertex;
fromVertex.name = fromCity;
database.push_back(fromVertex);
}
iFromVertex = i;
for (i = 0; i < database.size(); i++) // seek "from" city
if (database[i].name == toCity)
break;
if (i == database.size()) // not in vector yet
{
// store the vertex if it is new
Vertex toVertex;
toVertex.name = toCity;
database.push_back(toVertex);
}
iToVertex = i;
// store bi-directional edges
database[iFromVertex].adjacentVertices.push_back(iToVertex);
database[iToVertex].adjacentVertices.push_back(iFromVertex);
}
fin.close();
cout << "Input file processed\n\n";
while (true)
{
// get the start city for the search
string startCity;
cout << "\nEnter the start city [blank to exit]: ";
getline(cin, startCity);
if (startCity.length() == 0) break;
// find the start city
int i;
for (i = 0; i < database.size(); i++)
if (database[i].name == startCity)
break;
// -- skip the next code blocks if no matching city is found
if (i < database.size())
{
// BFS result
cout << "BFS";
queue<int> q = doBreadthFirstSearch(i, database);
while(!q.empty())
{
cout << '-'<< database[q.front()].name;
q.pop();
}
cout << endl;
cout << "DFS";
q = doDepthFirstSearch(i, database);
while(!q.empty())
{
cout << '-'<< database[q.front()].name;
q.pop();
}
cout << endl;;
}
}
}