Skip to content

Commit

Permalink
Merge pull request #2 from akshaydixi/graphObj
Browse files Browse the repository at this point in the history
In-memory Graph object support added
  • Loading branch information
akshaydixi committed Apr 8, 2015
2 parents 8f6d37c + 43c9157 commit ee7036b
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 8 deletions.
16 changes: 10 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11 -g -Wall")

find_package( Boost REQUIRED COMPONENTS program_options)
include_directories( ${Boost_INCLUDE_DIRS} )
add_library(snametize_lib src/snametize.cc src/util/util.cc src/writer/MetisWriter.cc src/writer/GmlWriter.cc src/writer/SnapWriter.cc src/writer/writer.cc)
add_library(snametize_lib src/snametize.cc src/util/util.cc src/writer/MetisWriter.cc src/writer/GmlWriter.cc src/writer/SnapWriter.cc src/writer/writer.cc src/graph/graph.cc src/graph/EdgeListGraph.cc)
add_executable(snametize src/main.cc)
target_link_libraries( snametize snametize_lib ${Boost_PROGRAM_OPTIONS_LIBRARY} )

Expand All @@ -19,8 +19,12 @@ if (test)
add_subdirectory(lib/gtest-1.7.0 src)
enable_testing()
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
add_executable(runUnitTests test/snametize_test.cc)
target_link_libraries(runUnitTests gtest gtest_main)
target_link_libraries(runUnitTests snametize_lib ${Boost_PROGRAM_OPTIONS_LIBRARY})
add_test(NAME snametizeUnitTests COMMAND runUnitTests)
endif()
add_executable(snametizeUnitTests test/snametize_test.cc)
add_executable(EdgeListGraphUnitTests test/EdgeListGraph_test.cc)
target_link_libraries(EdgeListGraphUnitTests gtest gtest_main)
target_link_libraries(snametizeUnitTests snametize_lib ${Boost_PROGRAM_OPTIONS_LIBRARY})
target_link_libraries(EdgeListGraphUnitTests snametize_lib ${Boost_PROGRAM_OPTIONS_LIBRARY})
target_link_libraries(snametizeUnitTests gtest gtest_main)
add_test(NAME snametizeTests COMMAND snametizeUnitTests)
add_test(NAME EdgeListGraphTests COMMAND EdgeListGraphUnitTests)
endif()
52 changes: 52 additions & 0 deletions src/graph/EdgeListGraph.cc
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));
}

38 changes: 38 additions & 0 deletions src/graph/EdgeListGraph.h
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_
14 changes: 14 additions & 0 deletions src/graph/edge.h
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_
10 changes: 10 additions & 0 deletions src/graph/graph.cc
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;
}
24 changes: 24 additions & 0 deletions src/graph/graph.h
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_
14 changes: 14 additions & 0 deletions src/graph/vertex.h
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_
8 changes: 6 additions & 2 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,20 @@ int main(int argc, char** argv) {
<< std::chrono::duration<double, std::milli>(t_mid-t_start).count()
<< "ms" << std::endl;

Writer* writer;
Writer* writer = nullptr;
if (currentOutputFormat == METIS) {
writer = new MetisWriter(output_file_path);
} else if (currentOutputFormat == GML) {
writer = new GmlWriter(output_file_path);
} else if (currentOutputFormat == SNAP) {
} else {

// We know it will be definitely SNAP because we have already
// eliminated possible errors while initializing currentOutputFormat.
writer = new SnapWriter(output_file_path);
}

writer->write(adjacency_list, vertices, edges);
delete writer;
std::clock_t end = std::clock();
auto t_end = std::chrono::high_resolution_clock::now();
std::cout << std::fixed << std::setprecision(3)
Expand Down
1 change: 1 addition & 0 deletions src/writer/writer.h
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_

Expand Down
51 changes: 51 additions & 0 deletions test/EdgeListGraph_test.cc
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());
}



0 comments on commit ee7036b

Please sign in to comment.