-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
223 lines (177 loc) · 5.29 KB
/
Graph.cpp
File metadata and controls
223 lines (177 loc) · 5.29 KB
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
220
221
222
223
/*
Programmer: John Zavisa
Description: This program uses maps and graphs to build graph, add node, add edge, BFS, DFS, and Shortest Path
*/
#include <iostream>
#include <stack>
#include <queue>
#include <set>
#include <iterator>
#include <iostream>
#include <list>
#include <vector>
#include "Graph.h"
using namespace std;
Graph::Graph()
{
}
//Definition for addVertex function
void Graph::addVertex(const string &node)
{
pair<string, map<string, int>> vertex; //Create pair for storing node name
vertex.first = node; //Set first in pair to node input
graph.insert(vertex);
}
//Definition for add edge function
void Graph::addEdge(std::string sourceName, std::string targetName, int weight) throw (GraphException)
{
//if graph does not have source node, throw an exception
if (graph.find(sourceName) == graph.end() || graph.find(targetName) == graph.end())
{
throw GraphException("Edge does not exist. One of the endpoints is invalid.");
}
//Create pair for storing edge
pair<string, int> edge;
//set first in pair to target node, and 2nd in pair to weight
edge.first = targetName;
edge.second = weight;
graph[sourceName].insert(edge);
}
//Depth First Search
void Graph::DFS(const std::string& source)
{
set<string> visited; //Create a set of visited nodes
visited.insert(source); //Insert starting node
privateDFS(source, visited); //Perform Depth first search based on starting node
}
//Depth first search function for making the calculation
void Graph::privateDFS(const std::string& source, set<string>& visited)
{
visited.insert(source);
for (auto i : graph[source])
{
if (visited.find(i.first) == visited.end())
{
cout << "(" << source << "," << i.first << ") ";
privateDFS(i.first, visited);
}
}
}
//Breadth First Search
void Graph::BFS(const std::string& source)
{
queue<string> needToTraverse; //queue for storing un traversed nodes
needToTraverse.push(source);
set<string> visited; //set for storing visited nodes
visited.insert(source);
/* commented out because crashed ?????
if (graph.find(source) == graph.end())
{
throw GraphException("Source not in graph");
}*/
while (!needToTraverse.empty()) //while needToTraverse is not empty, traverse through and edit set/queue
{
for (auto it = graph[needToTraverse.front()].begin(); it != graph[needToTraverse.front()].end(); ++it)
{
if (visited.find(it->first) == visited.end())
{
visited.insert(it->first);
needToTraverse.push(it->first);
cout << "(" << needToTraverse.front() << ", " << it->first << ") ";
}
}
//cout << needToTraverse.front() << endl;
needToTraverse.pop();
}
//removes source to check if bfs is empty
visited.erase(source);
if (visited.empty())
{
throw GraphException("Empty BFS");
}
}
//Display graph
void Graph::printGraph()
{
cout << "*********************Graph*********************" << endl;
cout << "Vertices: " << endl;;
//Loop and display vertices
for (auto it = graph.begin(); it != graph.end(); ++it)
{
cout << it->first << " ";
}
cout << endl;
cout << endl;
cout << "Edges: " << endl;
//Loop and display edges
for (auto it = graph.begin(); it != graph.end(); ++it)
{
for (auto it2 = graph[it->first].begin(); it2 != graph[it->first].end(); ++it2)
{
cout << " (" << it->first << ", ";
cout << it2->first << ", ";
cout << it2->second << ")";
}
cout << endl;
}
cout << "______________________________________________" << endl;
}
//This method will print Vertices & Edges
std::ostream& operator<<(std::ostream& ostream, const Graph& graph)
{
ostream << "*********************Graph*********************" << endl;
ostream << "Vertices:" << endl;
//Print Vertices
for (std::map< std::string, bool >::const_iterator it = graph.nodes.begin(); it != graph.nodes.end(); it++)
{
ostream << it->first << "\t";
}
// Print Edges
ostream << endl << "Edges:" << endl;
map< string, map<string, int> >::const_iterator it = graph.printGraph1.begin();
//for-loop to print with printGraph
for (; it != graph.printGraph1.end(); it++)
{
map<string, int>::const_iterator it2 = it->second.begin();
for (; it2 != it->second.end(); it2++)
{
ostream << "(" << it->first << ", " << it2->first << ", " << it2->second << ")\t"; // print of the format (a, b, 6)
}
ostream << endl;
}
ostream << "*********************Graph*********************" << endl;
return ostream;
}
void Graph::shortestPath(std::string source, int count)
{
for (auto it = graph.begin(); it != graph.end(); ++it)
{
for (auto it2 = graph[it->first].begin(); it2 != graph[it->first].end(); ++it2)
{
bool flag = false;
int tempDist = it2->second;
if (count > 0)
{
int currDist = it2->second;
if ( currDist < tempDist)
{
cout << " (" << it->first << ", ";
cout << it2->first << ", ";
cout << it2->second << ")";
}
else
{
cout << " (" << it->first << ", ";
cout << it2->first << ", ";
cout << it2->second << ")";
}
}
count++;
/*cout << " (" << it->first << ", ";
cout << it2->first << ", ";
cout << it2->second << ")";*/
}
cout << endl;
}
cout << endl;
}