Skip to content
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

Graph Algorithms (SSSP and KCores) #164

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ check_style:
benchmark: all
./benchmarks/run_benchmarks.py

example: all
./examples/run_examples.py

docs:
doxygen ./docs/Doxyfile
2 changes: 2 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ AC_CONFIG_FILES([Makefile
examples/graphs/Makefile
examples/graphs/page_rank/Makefile
examples/graphs/wcc/Makefile
examples/graphs/k_cores/Makefile
examples/graphs/sssp/Makefile
benchmarks/Makefile])


Expand Down
2 changes: 1 addition & 1 deletion examples/graphs/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
include $(top_srcdir)/common.mk

AUTOMAKE_OPTIONS = foreign
SUBDIRS = page_rank wcc
SUBDIRS = page_rank wcc sssp k_cores

12 changes: 12 additions & 0 deletions examples/graphs/data/graph_4
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
4
6
2
1
2
0
3
0
1
3
1
0
16 changes: 16 additions & 0 deletions examples/graphs/data/graph_5_dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
4
5
2
1
1
3
2
2
2
5
3
3
0
1
2
2
55 changes: 55 additions & 0 deletions examples/graphs/k_cores/Input.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <examples/graphs/k_cores/Input.h>
#include <fstream>

namespace graphs {

/** Graphs must be in the format:
number of vertices
number of edges
number of edges per vertex
edge for vertex 0
...
edge for vertex n - 1
**/
std::vector<Vertex> readGraph(const std::string& fname) {
std::ifstream infile(fname.c_str());
std::vector<Vertex> vertices;

if (!infile.is_open()) {
throw std::runtime_error("Error opening file: " + fname);
}

int num_vertices = -1;
int num_edges = -1;
infile >> num_vertices;
infile >> num_edges;

vertices.reserve(num_vertices);

for (int j = 0; j < num_vertices; ++j) {
int num_edges_per_vertex = -1;
infile >> num_edges_per_vertex;

Vertex v(j); // new vertex

// read neighbors for new vertex
for (int i = 0; i < num_edges_per_vertex; ++i) {
int neigh_id;
infile >> neigh_id;
v.addNeighbor(neigh_id);
}
vertices.push_back(v);
}

for (Vertex v : vertices) {
std::cout << "Vertex ID: " << v.getId() << std::endl;
std::cout << "Neighbors:" << std::endl;
for (int i : v.getNeighbors()) {
std::cout << i << std::endl;
}
}

return vertices;
}

} // namespace graphs
14 changes: 14 additions & 0 deletions examples/graphs/k_cores/Input.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef EXAMPLES_GRAPHS_K_CORES_INPUT_H_
#define EXAMPLES_GRAPHS_K_CORES_INPUT_H_

#include <vector>
#include <string>
#include "Vertex.h"

namespace graphs {

std::vector<Vertex> readGraph(const std::string& fname);

} // graphs

#endif // EXAMPLES_GRAPHS_K_CORES_INPUT_H_
59 changes: 59 additions & 0 deletions examples/graphs/k_cores/KCores.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef K_CORES_H
#define K_CORES_H

#include <vector>
#include "Vertex.h"

#include "cache_manager/CacheManager.h"
#include "iterator/CirrusIterable.h"

namespace graphs {

/**
* For all of the neighbors n, deletes id from n's list of neighbors
* @param cm CacheManager used to access Vertex objects
* @param id the vertex that should be deleted from neighbors' neighbors
* @param neighbors set of vertices that id should be deleted from
*/

void deleteKCoreNeighbor(cirrus::CacheManager<Vertex>& cm,
int id, std::set<int> neighbors) {
for (int n : neighbors) {
Vertex v = cm.get(n);
v.deleteTempNeighbor(id);
cm.put(n, v);
}
}
/**
* Returns the k-core of the inputted graph. Graphs must be undirected.
* @param cm CacheManager used to access Vertex objects
* @param num_vertices Number of vertices in the graph
* @param k The maximum degree of a vertex in the ending graph
*/

void k_cores(cirrus::CacheManager<Vertex>& cm, unsigned int num_vertices) {
int k = 1;
std::set<int> processed;
while (processed.size() < num_vertices) {
//cirrus::CirrusIterable<Vertex> iter(&cm, 40, 0, num_vertices - 1);
bool changed = true;
while (changed) {
changed = false;
for (unsigned int i = 0; i < num_vertices; i ++) {
Vertex v = cm.get(i);
if (v.getSeen() != 1 && v.getTempNeighborsSize() < k) {
changed = true;
v.setK(k-1);
processed.insert(v.getId());
v.setSeen(1);
deleteKCoreNeighbor(cm, v.getId(), v.getNeighbors());
cm.put(i, v);
}
}
}
k++;
}
}
} // namespace graphs

#endif
19 changes: 19 additions & 0 deletions examples/graphs/k_cores/KCores.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdlib.h>
#include <limits>
#include <functional>
#include <queue>

#include "Vertex.h"

#include "cache_manager/CacheManager.h"
#include "iterator/CirrusIterable.h"


namespace graphs {

void deleteKCoreNeighbor(int id, std::set<int> neighbors);

void k_cores(cirrus::CacheManager<Vertex>& cm, unsigned int num_vertices);


} // namespace graphs
26 changes: 26 additions & 0 deletions examples/graphs/k_cores/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
include $(top_srcdir)/common.mk

AUTOMAKE_OPTIONS = foreign

bin_PROGRAMS = kc

LIBS = -lclient \
-lauthentication -lutils -lcommon -levictionpolicies \
$(LIBRDMACM) $(LIBIBVERBS)

LINCLUDES = -L$(top_srcdir)/src/utils/ \
-L$(top_srcdir)/src/client/ \
-L$(top_srcdir)/src/authentication \
-L$(top_srcdir)/src/common \
-L$(top_srcdir)/src/cache_manager

CPPFLAGS = -ggdb -O3 -I$(top_srcdir) -I$(top_srcdir)/examples/sparsehash/src/ \
-isystem $(top_srcdir)/third_party/libcuckoo/ \
-I$(top_srcdir)/src \
-I$(top_srcdir)/third_party/flatbuffers/include
SOURCES = KCores.cpp Input.cpp Vertex.cpp

kc_SOURCES = kc.cpp $(SOURCES)
kc_LDFLAGS = -pthread
kc_LDADD = $(LINCLUDES) $(LIBS)

120 changes: 120 additions & 0 deletions examples/graphs/k_cores/Vertex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#include <examples/graphs/k_cores/Vertex.h>
#include <arpa/inet.h>

namespace graphs {

Vertex::Vertex(int id) :
id(id), k(1), seen(false) {}

Vertex::Vertex(int id, const std::vector<int>& neighbors) :
id(id), k(1), seen(false)
{
setNeighbors(neighbors);
}

void Vertex::setNeighbors(const std::vector<int>& v) {
for (const auto& i : v) {
addNeighbor(i);
}
}

void Vertex::addNeighbor(int id) {
neighbors.insert(id);
tempNeighbors.insert(id);
}

std::set<int> Vertex::getNeighbors() const {
return neighbors;
}

int Vertex::getNeighborsSize() const {
return neighbors.size();
}

bool Vertex::hasNeighbor(int id) const {
return neighbors.find(id) != neighbors.end();
}

Vertex Vertex::deserializer(const void* data, unsigned int size) {
Vertex v;

const uint32_t* ptr = reinterpret_cast<const uint32_t*>(data);
v.setId(ntohl(*ptr++));
v.setK(ntohl(*ptr++));
v.setSeen(ntohl(*ptr++));

uint32_t n = ntohl(*ptr++);

uint32_t expected_size = sizeof(uint32_t) * (2 + n);

if (size != expected_size) {
/** throw std::runtime_error("Incorrect size: "
+ std::to_string(size) +
+ " expected: " + std::to_string(expected_size));
*/
}


for (uint32_t i = 0; i < n; ++i) {
v.addNeighbor(ntohl(*ptr++));
}

n = ntohl(*ptr++);
for (uint32_t i = 0; i < n; i++) {
v.addTempNeighbor(ntohl(*ptr++));
}
return v;
}

int Vertex::getId() const {
return id;
}

void Vertex::setId(int i) {
id = i;
}

void Vertex::addTempNeighbor(int id) {
tempNeighbors.insert(id);
}

void Vertex::deleteTempNeighbor(int id) {
std::set<int>::iterator it = tempNeighbors.find(id);
tempNeighbors.erase(it, tempNeighbors.end());
for (auto it = tempNeighbors.begin(); it != tempNeighbors.end();)
if (*it % 2 == 1)
it = tempNeighbors.erase(it);
else
++it;
}

std::set<int> Vertex::getTempNeighbors() const {
return tempNeighbors;
}

int Vertex::getTempNeighborsSize() const {
return tempNeighbors.size();
}

int Vertex::getK() const {
return k;
}

void Vertex::setK(int val) {
k = val;
}

int Vertex::getSeen() const {
return seen;
}

void Vertex::setSeen(int val) {
seen = val;
}

void Vertex::print() const {
std::cout << "Print vertex id: " << id <<
" k: " << k << std::endl;
}

}
Loading