-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from akshaydixi/graphObj
In-memory Graph object support added
- Loading branch information
Showing
10 changed files
with
220 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2015 Akshay Dixit | ||
|
||
#include "EdgeListGraph.h" | ||
|
||
EdgeListGraph::EdgeListGraph(bool directedness, bool self_loops) | ||
: Graph(directedness) { | ||
this->self_loops = self_loops; | ||
this->edges = 0; | ||
} | ||
|
||
void EdgeListGraph::addEdge(uint64_t v1, uint64_t v2) { | ||
|
||
// Don't process self loop edges, if we don't plan on suppporting them | ||
if (v2 == v1 && !this->self_loops) return; | ||
|
||
|
||
if(this->vertexSet.find(v1) != this->vertexSet.end() && | ||
this->adjacencyList.find(v1) != this->adjacencyList.end()) { | ||
// Check if you've already added this edge before. If so, do not | ||
// update the number of edges. | ||
if (this->adjacencyList[v1].find(v2) != this->adjacencyList[v1].end()) { | ||
this->adjacencyList[v1].insert(v2); | ||
this->edges++; | ||
} | ||
} else { | ||
this->adjacencyList[v1] = std::unordered_set<uint64_t>({v2}); | ||
this->vertexSet.insert({v1,v2}); | ||
this->edges++; | ||
} | ||
} | ||
|
||
void EdgeListGraph::getEdgeList(uint64_t vertex, | ||
std::vector<uint64_t>* edgeList) { | ||
std::copy(this->adjacencyList[vertex].begin(), | ||
this->adjacencyList[vertex].end(), | ||
std::back_inserter(*edgeList)); | ||
} | ||
|
||
uint64_t EdgeListGraph::getVertexCount() { | ||
return this->vertexSet.size(); | ||
} | ||
|
||
uint64_t EdgeListGraph::getEdgeCount() { | ||
return this->edges; | ||
} | ||
|
||
void EdgeListGraph::getVertices(std::vector<uint64_t>* vertices) { | ||
std::copy(this->vertexSet.begin(), | ||
this->vertexSet.end(), | ||
std::back_inserter(*vertices)); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2015 Akshay Dixit | ||
|
||
#ifndef GRAPH_EDGELISTGRAPH_H_ | ||
#define GRAPH_EDGELISTGRAPH_H_ | ||
|
||
#include "graph.h" | ||
#include <unordered_set> | ||
#include <unordered_map> | ||
|
||
class EdgeListGraph : public Graph { | ||
private: | ||
// Whether this graph supports self loops or not. This is because | ||
// METIS doesn't support self loops, so with this we aim to not | ||
// read and store the self loop edges. | ||
bool self_loops; | ||
|
||
// The number of edges in the graph | ||
uint64_t edges; | ||
|
||
// The main structure of the graph is stored in the adjacencyList. | ||
std::unordered_map<uint64_t, std::unordered_set<uint64_t> > adjacencyList; | ||
|
||
// We're using an unordered_set here, because it gives O(1) access time on | ||
// an average case. We could have also used a regular map, or maintained | ||
// a list of seen vertices, but those would have taken O(log N) due to | ||
// binary search time for lookup. | ||
std::unordered_set <uint64_t> vertexSet; | ||
public: | ||
EdgeListGraph(bool directedness, bool self_loops); | ||
uint64_t getVertexCount(); | ||
uint64_t getEdgeCount(); | ||
void addEdge(uint64_t, uint64_t); | ||
void getEdgeList(uint64_t vertex, std::vector<uint64_t>*); | ||
void getVertices(std::vector<uint64_t>*); | ||
|
||
}; | ||
|
||
#endif // GRAPH_EDGELISTGRAPH_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2015 Akshay Dixit | ||
|
||
#ifndef GRAPH_EDGE_H_ | ||
#define GRAPH_EDGE_H_ | ||
|
||
class Edge { | ||
public: | ||
Edge(); | ||
virtual inline Edge() = 0; | ||
}; | ||
|
||
Edge::~Edge() {} | ||
|
||
#endif // EDGE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright 2015 Akshay Dixit | ||
|
||
#include "graph.h" | ||
|
||
// We assume that the graph is undirected by default | ||
Graph::Graph() : Graph::Graph(false) {} | ||
|
||
Graph::Graph(bool directedness) { | ||
_directedness = directedness; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2015 Akshay Dixit | ||
|
||
#ifndef GRAPH_GRAPH_H_ | ||
#define GRAPH_GRAPH_H_ | ||
|
||
#include <cstdint> | ||
#include <vector> | ||
|
||
class Graph { | ||
private: | ||
bool _directedness; | ||
|
||
public: | ||
Graph(); | ||
explicit Graph(bool); | ||
virtual inline ~Graph() = 0; | ||
virtual uint64_t getVertexCount() = 0; | ||
virtual uint64_t getEdgeCount() = 0; | ||
bool isDirected() { return _directedness; } | ||
}; | ||
|
||
Graph::~Graph() {} | ||
|
||
#endif // GRAPH_GRAPH_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2015 Akshay Dixit | ||
|
||
#ifndef GRAPH_VERTEX_H_ | ||
#define GRAPH_VERTEX_H_ | ||
|
||
class Vertex { | ||
public: | ||
Vertex(); | ||
virtual inline ~Vertex() = 0; | ||
}; | ||
|
||
Vertex::~Vertex() {} | ||
|
||
#endif // GRAPH_VERTEX_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// Copyright 2015 Akshay Dixit | ||
|
||
#ifndef WRITER_WRITER_H_ | ||
#define WRITER_WRITER_H_ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2015 Akshay Dixit | ||
|
||
#include "../src/graph/EdgeListGraph.h" | ||
#include "gtest/gtest.h" | ||
#include <iostream> | ||
|
||
class EdgeListGraphTest : public ::testing::Test { | ||
protected: | ||
EdgeListGraphTest() { | ||
} | ||
|
||
virtual ~EdgeListGraphTest() { | ||
} | ||
|
||
virtual void SetUp() { | ||
this->mySmallGraph = new EdgeListGraph(false, false); | ||
this->mySmallGraph->addEdge(1, 2); | ||
this->mySmallGraph->addEdge(2, 3); | ||
this->mySmallGraph->addEdge(3, 4); | ||
this->mySmallGraph->addEdge(4, 5); | ||
this->mySmallGraph->addEdge(1, 1); | ||
} | ||
|
||
virtual void TearDown() { | ||
delete this->mySmallGraph; | ||
} | ||
|
||
public: | ||
EdgeListGraph* testGraph; | ||
EdgeListGraph* mySmallGraph; | ||
}; | ||
|
||
TEST_F(EdgeListGraphTest, CheckAddEdge) { | ||
testGraph = new EdgeListGraph(false, false); | ||
testGraph->addEdge(1,1); | ||
EXPECT_EQ(testGraph->getVertexCount(), 0); | ||
testGraph->addEdge(1,2); | ||
EXPECT_EQ(1, testGraph->getEdgeCount()); | ||
delete testGraph; | ||
} | ||
|
||
TEST_F(EdgeListGraphTest, CheckEdgeCount) { | ||
EXPECT_EQ(4, mySmallGraph->getEdgeCount()); | ||
} | ||
|
||
TEST_F(EdgeListGraphTest, CheckVertexCount) { | ||
EXPECT_EQ(5, mySmallGraph->getVertexCount()); | ||
} | ||
|
||
|
||
|