Skip to content

Commit 10b80c0

Browse files
authored
Merge branch 'main' into node-ids
2 parents 366d1e7 + d076a35 commit 10b80c0

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

pydatastructs/graphs/_backend/cpp/Algorithms.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,6 @@ static PyObject* minimum_spanning_tree_prim_adjacency_list(PyObject* self, PyObj
319319
return reinterpret_cast<PyObject*>(mst);
320320
}
321321

322-
323322
static PyObject* shortest_paths_dijkstra_adjacency_list(PyObject* self, PyObject* args, PyObject* kwargs) {
324323
PyObject* graph_obj;
325324
const char* source_name;
@@ -374,6 +373,7 @@ static PyObject* shortest_paths_dijkstra_adjacency_list(PyObject* self, PyObject
374373
dist[v_id] = new_dist;
375374
pred[v_id] = u_id;
376375
pq.push({new_dist, v_id});
376+
377377
}
378378
}
379379
}
@@ -410,6 +410,7 @@ static PyObject* shortest_paths_dijkstra_adjacency_list(PyObject* self, PyObject
410410
}
411411

412412
if (PyDict_SetItemString(pred_dict, name.c_str(), py_pred) < 0) {
413+
413414
Py_DECREF(py_pred);
414415
Py_DECREF(dist_dict);
415416
Py_DECREF(pred_dict);

pydatastructs/graphs/_backend/cpp/algorithms.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ static PyMethodDef AlgorithmsMethods[] = {
77
{"bfs_adjacency_list", (PyCFunction)breadth_first_search_adjacency_list, METH_VARARGS | METH_KEYWORDS, "Run BFS on adjacency list with callback"},
88
{"bfs_adjacency_matrix", (PyCFunction)breadth_first_search_adjacency_matrix, METH_VARARGS | METH_KEYWORDS, "Run BFS on adjacency matrix with callback"},
99
{"minimum_spanning_tree_prim_adjacency_list", (PyCFunction)minimum_spanning_tree_prim_adjacency_list, METH_VARARGS | METH_KEYWORDS, "Run Prim's algorithm on adjacency list"},
10+
{"shortest_paths_dijkstra_adjacency_list", (PyCFunction)shortest_paths_dijkstra_adjacency_list, METH_VARARGS | METH_KEYWORDS, "Dijkstra's algorithm for adjacency list graphs"},
1011
{NULL, NULL, 0, NULL}
1112
};
1213

pydatastructs/graphs/algorithms.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -804,15 +804,19 @@ def shortest_paths(graph: Graph, algorithm: str,
804804
.. [1] https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm
805805
.. [2] https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
806806
"""
807-
raise_if_backend_is_not_python(
808-
shortest_paths, kwargs.get('backend', Backend.PYTHON))
809-
import pydatastructs.graphs.algorithms as algorithms
810-
func = "_" + algorithm + "_" + graph._impl
811-
if not hasattr(algorithms, func):
812-
raise NotImplementedError(
813-
"Currently %s algorithm isn't implemented for "
814-
"finding shortest paths in graphs."%(algorithm))
815-
return getattr(algorithms, func)(graph, source, target)
807+
backend = kwargs.get('backend', Backend.PYTHON)
808+
if (backend == Backend.PYTHON):
809+
import pydatastructs.graphs.algorithms as algorithms
810+
func = "_" + algorithm + "_" + graph._impl
811+
if not hasattr(algorithms, func):
812+
raise NotImplementedError(
813+
"Currently %s algorithm isn't implemented for "
814+
"finding shortest paths in graphs."%(algorithm))
815+
return getattr(algorithms, func)(graph, source, target)
816+
else:
817+
from pydatastructs.graphs._backend.cpp._algorithms import shortest_paths_dijkstra_adjacency_list
818+
if graph._impl == "adjacency_list" and algorithm == 'dijkstra':
819+
return shortest_paths_dijkstra_adjacency_list(graph, source, target)
816820

817821
def _bellman_ford_adjacency_list(graph: Graph, source: str, target: str) -> tuple:
818822
distances, predecessor, visited, cnts = {}, {}, {}, {}

pydatastructs/graphs/tests/test_algorithms.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,25 @@ def _test_shortest_paths_positive_edges(ds, algorithm):
367367
graph.add_edge('D', 'SLC', -10)
368368
assert raises(ValueError, lambda: shortest_paths(graph, 'bellman_ford', 'SLC'))
369369

370+
if (ds == 'List' and algorithm == 'dijkstra'):
371+
vertices2 = [AdjacencyListGraphNode('S', 0, backend = Backend.CPP), AdjacencyListGraphNode('C', 0, backend = Backend.CPP),
372+
AdjacencyListGraphNode('SLC', 0, backend = Backend.CPP), AdjacencyListGraphNode('SF', 0, backend = Backend.CPP),
373+
AdjacencyListGraphNode('D', 0, backend = Backend.CPP)]
374+
graph2 = Graph(*vertices2, backend = Backend.CPP)
375+
graph2.add_edge('S', 'SLC', 2)
376+
graph2.add_edge('C', 'S', 4)
377+
graph2.add_edge('C', 'D', 2)
378+
graph2.add_edge('SLC', 'C', 2)
379+
graph2.add_edge('SLC', 'D', 3)
380+
graph2.add_edge('SF', 'SLC', 2)
381+
graph2.add_edge('SF', 'S', 2)
382+
graph2.add_edge('D', 'SF', 3)
383+
(dist2, pred2) = shortest_paths(graph2, algorithm, 'SLC', backend = Backend.CPP)
384+
assert dist2 == {'S': 6, 'C': 2, 'SLC': 0, 'SF': 6, 'D': 3}
385+
assert pred2 == {'S': 'C', 'C': 'SLC', 'SLC': None, 'SF': 'D', 'D': 'SLC'}
386+
387+
388+
370389
def _test_shortest_paths_negative_edges(ds, algorithm):
371390
import pydatastructs.utils.misc_util as utils
372391
GraphNode = getattr(utils, "Adjacency" + ds + "GraphNode")

0 commit comments

Comments
 (0)