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

Implement C++ Backend for Breadth-First Search #675

Open
wants to merge 5 commits into
base: main
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
4 changes: 3 additions & 1 deletion docs/source/pydatastructs/graphs/algorithms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ Algorithms

.. autofunction:: pydatastructs.topological_sort_parallel

.. autofunction:: pydatastructs.find_bridges
.. autofunction:: pydatastructs.find_bridges

.. autofunction:: pydatastructs.bfs
12 changes: 9 additions & 3 deletions pydatastructs/graphs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
__all__ = []
__all__ = [
'bfs'
]

from . import graph
from . import (
graph,
_extensions
)
from .graph import (
Graph
)
Expand All @@ -22,7 +27,8 @@
topological_sort,
topological_sort_parallel,
max_flow,
find_bridges
find_bridges,
bfs
)

__all__.extend(algorithms.__all__)
21 changes: 21 additions & 0 deletions pydatastructs/graphs/_backend/cpp/algorithms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <Python.h>
#include "bfs.hpp"

static PyMethodDef bfs_PyMethodDef[] = {
{"bfs", (PyCFunction)bfs, METH_VARARGS | METH_KEYWORDS, "Breadth-First Search"},
{NULL, NULL, 0, NULL}
};

static struct PyModuleDef bfs_module = {
PyModuleDef_HEAD_INIT,
"_bfs",
"BFS algorithms module",
-1,
bfs_PyMethodDef
};

PyMODINIT_FUNC PyInit__bfs(void) {
PyObject *module = PyModule_Create(&bfs_module);
if (module == NULL) return NULL;
return module;
}
64 changes: 64 additions & 0 deletions pydatastructs/graphs/_backend/cpp/bfs.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#ifndef BFS_HPP
#define BFS_HPP

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <queue>
#include <unordered_map>

struct Graph {
PyObject* adj_list;
};

static PyObject* bfs_impl(PyObject* graph, PyObject* start_vertex, PyObject* visited = NULL) {
if (!PyDict_Check(graph)) {
PyErr_SetString(PyExc_TypeError, "Graph must be a dictionary");
return NULL;
}

std::queue<PyObject*> q;
PyObject* visited_dict = visited ? visited : PyDict_New();

q.push(start_vertex);
PyDict_SetItem(visited_dict, start_vertex, Py_True);

PyObject* result = PyList_New(0);

while (!q.empty()) {
PyObject* vertex = q.front();
q.pop();

PyList_Append(result, vertex);

PyObject* neighbors = PyDict_GetItem(graph, vertex);
if (neighbors && PyList_Check(neighbors)) {
Py_ssize_t size = PyList_Size(neighbors);
for (Py_ssize_t i = 0; i < size; i++) {
PyObject* neighbor = PyList_GetItem(neighbors, i);
if (!PyDict_Contains(visited_dict, neighbor)) {
q.push(neighbor);
PyDict_SetItem(visited_dict, neighbor, Py_True);
}
}
}
}

if (!visited) Py_DECREF(visited_dict);
return result;
}

static PyObject* bfs(PyObject* self, PyObject* args, PyObject* kwds) {
PyObject *graph = NULL, *start_vertex = NULL;
static char *kwlist[] = {"graph", "start_vertex", NULL};

if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist, &graph, &start_vertex)) {
return NULL;
}

PyObject* result = bfs_impl(graph, start_vertex);
if (result == NULL) return NULL;
Py_INCREF(result);
return result;
}

#endif
17 changes: 17 additions & 0 deletions pydatastructs/graphs/_extensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from setuptools import Extension
import sysconfig

project = 'pydatastructs'

module = 'graphs'

backend = "_backend"

cpp = 'cpp'

bfs = '.'.join([project, module, backend, cpp, '_bfs'])
bfs_sources = ['/'.join([project, module, backend, cpp, 'algorithms.cpp'])]

extensions = [
Extension(bfs, sources=bfs_sources)
]
19 changes: 18 additions & 1 deletion pydatastructs/graphs/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pydatastructs.graphs.graph import Graph
from pydatastructs.linear_data_structures.algorithms import merge_sort_parallel
from pydatastructs import PriorityQueue
from pydatastructs.graphs._backend.cpp._bfs import bfs as _bfs_cpp

__all__ = [
'breadth_first_search',
Expand All @@ -24,7 +25,8 @@
'topological_sort',
'topological_sort_parallel',
'max_flow',
'find_bridges'
'find_bridges',
'bfs'
]

Stack = Queue = deque
Expand Down Expand Up @@ -1368,3 +1370,18 @@ def dfs(u):
bridges.append((b, a))
bridges.sort()
return bridges

def bfs(graph, start_vertex, backend=Backend.PYTHON):
if backend == Backend.CPP:
return _bfs_cpp(graph, start_vertex)
from collections import deque
visited = set()
q = deque([start_vertex])
result = []
while q:
vertex = q.popleft()
if vertex not in visited:
visited.add(vertex)
result.append(vertex)
q.extend(graph.get(vertex, []))
return result
19 changes: 18 additions & 1 deletion pydatastructs/graphs/tests/test_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
breadth_first_search_parallel, minimum_spanning_tree,
minimum_spanning_tree_parallel, strongly_connected_components,
depth_first_search, shortest_paths,all_pair_shortest_paths, topological_sort,
topological_sort_parallel, max_flow, find_bridges)
topological_sort_parallel, max_flow, find_bridges, bfs, Backend)
from pydatastructs.utils.raises_util import raises

def test_breadth_first_search():
Expand Down Expand Up @@ -504,3 +504,20 @@ def _test_find_bridges(ds):

_test_find_bridges("List")
_test_find_bridges("Matrix")

def test_bfs():
graph = {
0: [1, 2],
1: [0, 3],
2: [0],
3: [1]
}
start_vertex = 0
expected = [0, 1, 2, 3]

result_python = bfs(graph, start_vertex, backend=Backend.PYTHON)
assert result_python == expected

result_cpp = bfs(graph, start_vertex, backend=Backend.CPP)
result_cpp_list = [x for x in result_cpp]
assert result_cpp_list == expected
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pydatastructs import linear_data_structures
from pydatastructs import miscellaneous_data_structures
from pydatastructs import trees
from pydatastructs import graphs

with open("README.md", "r") as fh:
long_description = fh.read()
Expand All @@ -13,6 +14,7 @@
extensions.extend(linear_data_structures._extensions.extensions)
extensions.extend(miscellaneous_data_structures._extensions.extensions)
extensions.extend(trees._extensions.extensions)
extensions.extend(graphs._extensions.extensions)

setuptools.setup(
name="cz-pydatastructs",
Expand Down
Loading