Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions examples/bfsExample.cpp
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;
}
45 changes: 45 additions & 0 deletions include/GraphMatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
#include <stack>
#include <algorithm>
#include <set>
#include <queue>
#include <limits>
#include "MatrixRep.hpp"

namespace Appledore
{

Expand Down Expand Up @@ -518,6 +521,31 @@ namespace Appledore
}
}

std::vector<EdgeType> bfs(size_t start, size_t maxDepth) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the start variable be of type VertexType? because user is passing in a vertex to start bfs from?

Suggested change
std::vector<EdgeType> bfs(size_t start, size_t maxDepth) {
std::vector<EdgeType> bfs(VertexType start, size_t maxDepth) {

Copy link
Author

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

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()
// ---------------------------------------------------------
Expand Down Expand Up @@ -623,6 +651,23 @@ namespace Appledore
}
}

friend std::ostream& operator<<(std::ostream& src, const GraphMatrix& other) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there a << operator in the class?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly for convenience. It allows for printing basic information about the graph using

std::cout << graph

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats a good approach.

But actually its not needed, the << operator overload is reserved for future to read graphs from a file, also user can print the graphs according to how they want, printing graphs on the console is not the concern of the library. 👍🏼

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;
Expand Down
5 changes: 5 additions & 0 deletions include/MatrixRep.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ namespace Appledore
{
return id == other.id;
}

friend std::ostream& operator<<(std::ostream& src, const GraphVertex& other) {
src << "Vertex " << other.id;
return src;
}
};
size_t Appledore::GraphVertex::nextId = 1;
class GraphEdge
Expand Down