-
Notifications
You must be signed in to change notification settings - Fork 17
added bfs and operator<< methods #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
5f993b5
1458275
9b312d8
3a7a731
fdcef6a
8722650
c9dc4bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #include <iostream> | ||
| #include "../include/GraphMatrix.hpp" | ||
|
|
||
| class Vertex : public Appledore::GraphVertex { | ||
| public: | ||
| int data; | ||
| Vertex(int d): data(d) {}; | ||
| }; | ||
|
|
||
| int main() { | ||
|
|
||
| // In order to use bfs, EdgeType must support the operator+ and must have | ||
| // infinity. | ||
| Appledore::GraphMatrix<Vertex, double, Appledore::UndirectedG> undirected; | ||
|
|
||
| Vertex v1(4); | ||
| Vertex v2(10); | ||
| Vertex v3(12); | ||
| Vertex v4(2); | ||
|
|
||
| undirected.addVertex(v1); | ||
| undirected.addVertex(v2); | ||
| undirected.addVertex(v3); | ||
| undirected.addVertex(v4); | ||
|
|
||
| undirected.addEdge(v1,v2,1); | ||
| undirected.addEdge(v2,v3,1); | ||
| undirected.addEdge(v1,v4,1); | ||
|
|
||
| std::vector<double> dist = undirected.bfs(2, 10); | ||
|
|
||
| for (int item : dist) { | ||
| std::cout << item << " "; | ||
| } | ||
| std::cout << "\n"; | ||
| return 0; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,10 @@ | |
| #include <stack> | ||
| #include <algorithm> | ||
| #include <set> | ||
| #include <queue> | ||
| #include <limits> | ||
| #include "MatrixRep.hpp" | ||
|
|
||
| namespace Appledore | ||
| { | ||
|
|
||
|
|
@@ -518,6 +521,31 @@ namespace Appledore | |
| } | ||
| } | ||
|
|
||
| std::vector<EdgeType> bfs(size_t start, size_t maxDepth) { | ||
| EdgeType inf = std::numeric_limits<EdgeType>::infinity(); | ||
| std::vector<EdgeType> dist(numVertices, inf); | ||
| dist[start] = 0; | ||
|
|
||
| std::queue<VertexType> queue; | ||
| queue.push(indexToVertex[start]); | ||
| size_t depth = 0; | ||
|
|
||
| while (!queue.empty() && depth <= maxDepth) { | ||
| VertexType current = queue.front(); | ||
| queue.pop(); | ||
| depth = dist[vertexToIndex[current]]; | ||
|
|
||
| for (VertexType neighbor : getNeighbors(current)) { | ||
| EdgeType weight = getEdge(current, neighbor); | ||
| if (dist[vertexToIndex[neighbor]] == inf) { | ||
| dist[vertexToIndex[neighbor]] = dist[vertexToIndex[current]] + weight; | ||
| queue.push(neighbor); | ||
| } | ||
| } | ||
| } | ||
| return dist; | ||
| }; | ||
|
|
||
| // --------------------------------------------------------- | ||
| // NEW METHOD: removeVertex() | ||
| // --------------------------------------------------------- | ||
|
|
@@ -623,6 +651,23 @@ namespace Appledore | |
| } | ||
| } | ||
|
|
||
| friend std::ostream& operator<<(std::ostream& src, const GraphMatrix& other) { | ||
|
||
| src << "GraphMatrix Object:\n"; | ||
| src << "\tVertex List:\n"; | ||
| std::vector<VertexType> vertices = other.getVertices(); | ||
| for (VertexType vertex : vertices) { | ||
| src << "\t\t" << vertex << std::endl; | ||
| } | ||
|
|
||
| src << "\tEdge List:\n"; | ||
| for (VertexType incident : vertices) { | ||
| for (VertexType terminal : other.getNeighbors(incident)) { | ||
| src << "\t\t " << incident << "-->" << terminal << std::endl; | ||
| } | ||
| } | ||
| return src; | ||
| } | ||
|
|
||
| private: | ||
| std::map<VertexType, size_t> vertexToIndex; | ||
| std::vector<VertexType> indexToVertex; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the
startvariable be of typeVertexType? because user is passing in a vertex to start bfs from?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, you're rights. I can make that change