|
| 1 | +#include <Python.h> |
| 2 | +#include <unordered_map> |
| 3 | +#include <queue> |
| 4 | +#include <string> |
| 5 | +#include <unordered_set> |
| 6 | +#include "AdjacencyList.hpp" |
| 7 | +#include "AdjacencyMatrix.hpp" |
| 8 | + |
| 9 | + |
| 10 | +static PyObject* breadth_first_search_adjacency_list(PyObject* self, PyObject* args, PyObject* kwargs) { |
| 11 | + PyObject* graph_obj; |
| 12 | + const char* source_name; |
| 13 | + PyObject* operation; |
| 14 | + PyObject* varargs = nullptr; |
| 15 | + PyObject* kwargs_dict = nullptr; |
| 16 | + |
| 17 | + static const char* kwlist[] = {"graph", "source_node", "operation", "args", "kwargs", nullptr}; |
| 18 | + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!sO|OO", const_cast<char**>(kwlist), |
| 19 | + &AdjacencyListGraphType, &graph_obj, |
| 20 | + &source_name, &operation, |
| 21 | + &varargs, &kwargs_dict)) { |
| 22 | + return nullptr; |
| 23 | + } |
| 24 | + |
| 25 | + AdjacencyListGraph* cpp_graph = reinterpret_cast<AdjacencyListGraph*>(graph_obj); |
| 26 | + |
| 27 | + auto it = cpp_graph->node_map.find(source_name); |
| 28 | + AdjacencyListGraphNode* start_node = it->second; |
| 29 | + |
| 30 | + std::unordered_set<std::string> visited; |
| 31 | + std::queue<AdjacencyListGraphNode*> q; |
| 32 | + |
| 33 | + q.push(start_node); |
| 34 | + visited.insert(start_node->name); |
| 35 | + |
| 36 | + while (!q.empty()) { |
| 37 | + AdjacencyListGraphNode* node = q.front(); |
| 38 | + q.pop(); |
| 39 | + |
| 40 | + for (const auto& [adj_name, adj_obj] : node->adjacent) { |
| 41 | + if (visited.count(adj_name)) continue; |
| 42 | + if (!PyObject_IsInstance(adj_obj, (PyObject*)&AdjacencyListGraphNodeType)) continue; |
| 43 | + |
| 44 | + AdjacencyListGraphNode* adj_node = reinterpret_cast<AdjacencyListGraphNode*>(adj_obj); |
| 45 | + |
| 46 | + PyObject* base_args = PyTuple_Pack(2, |
| 47 | + reinterpret_cast<PyObject*>(node), |
| 48 | + reinterpret_cast<PyObject*>(adj_node)); |
| 49 | + if (!base_args) |
| 50 | + return nullptr; |
| 51 | + |
| 52 | + PyObject* final_args; |
| 53 | + if (varargs && PyTuple_Check(varargs)) { |
| 54 | + final_args = PySequence_Concat(base_args, varargs); |
| 55 | + Py_DECREF(base_args); |
| 56 | + if (!final_args) |
| 57 | + return nullptr; |
| 58 | + } else { |
| 59 | + final_args = base_args; |
| 60 | + } |
| 61 | + |
| 62 | + PyObject* result = PyObject_Call(operation, final_args, kwargs_dict); |
| 63 | + Py_DECREF(final_args); |
| 64 | + |
| 65 | + if (!result) |
| 66 | + return nullptr; |
| 67 | + |
| 68 | + Py_DECREF(result); |
| 69 | + |
| 70 | + visited.insert(adj_name); |
| 71 | + q.push(adj_node); |
| 72 | + } |
| 73 | + } |
| 74 | + if (PyErr_Occurred()) { |
| 75 | + return nullptr; |
| 76 | + } |
| 77 | + |
| 78 | + Py_RETURN_NONE; |
| 79 | +} |
| 80 | + |
| 81 | +static PyObject* breadth_first_search_adjacency_matrix(PyObject* self, PyObject* args, PyObject* kwargs) { |
| 82 | + PyObject* graph_obj; |
| 83 | + const char* source_name; |
| 84 | + PyObject* operation; |
| 85 | + PyObject* varargs = nullptr; |
| 86 | + PyObject* kwargs_dict = nullptr; |
| 87 | + |
| 88 | + static const char* kwlist[] = {"graph", "source_node", "operation", "args", "kwargs", nullptr}; |
| 89 | + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!sO|OO", const_cast<char**>(kwlist), |
| 90 | + &AdjacencyMatrixGraphType, &graph_obj, |
| 91 | + &source_name, &operation, |
| 92 | + &varargs, &kwargs_dict)) { |
| 93 | + return nullptr; |
| 94 | + } |
| 95 | + |
| 96 | + AdjacencyMatrixGraph* cpp_graph = reinterpret_cast<AdjacencyMatrixGraph*>(graph_obj); |
| 97 | + |
| 98 | + auto it = cpp_graph->node_map.find(source_name); |
| 99 | + if (it == cpp_graph->node_map.end()) { |
| 100 | + PyErr_SetString(PyExc_KeyError, "Source node not found in graph"); |
| 101 | + return nullptr; |
| 102 | + } |
| 103 | + AdjacencyMatrixGraphNode* start_node = it->second; |
| 104 | + |
| 105 | + std::unordered_set<std::string> visited; |
| 106 | + std::queue<AdjacencyMatrixGraphNode*> q; |
| 107 | + |
| 108 | + q.push(start_node); |
| 109 | + visited.insert(source_name); |
| 110 | + |
| 111 | + while (!q.empty()) { |
| 112 | + AdjacencyMatrixGraphNode* node = q.front(); |
| 113 | + q.pop(); |
| 114 | + |
| 115 | + std::string node_name = reinterpret_cast<GraphNode*>(node)->name; |
| 116 | + auto& neighbors = cpp_graph->matrix[node_name]; |
| 117 | + |
| 118 | + for (const auto& [adj_name, connected] : neighbors) { |
| 119 | + if (!connected || visited.count(adj_name)) continue; |
| 120 | + |
| 121 | + auto adj_it = cpp_graph->node_map.find(adj_name); |
| 122 | + if (adj_it == cpp_graph->node_map.end()) continue; |
| 123 | + |
| 124 | + AdjacencyMatrixGraphNode* adj_node = adj_it->second; |
| 125 | + |
| 126 | + PyObject* base_args = PyTuple_Pack(2, |
| 127 | + reinterpret_cast<PyObject*>(node), |
| 128 | + reinterpret_cast<PyObject*>(adj_node)); |
| 129 | + if (!base_args) return nullptr; |
| 130 | + |
| 131 | + PyObject* final_args; |
| 132 | + if (varargs && PyTuple_Check(varargs)) { |
| 133 | + final_args = PySequence_Concat(base_args, varargs); |
| 134 | + Py_DECREF(base_args); |
| 135 | + if (!final_args) return nullptr; |
| 136 | + } else { |
| 137 | + final_args = base_args; |
| 138 | + } |
| 139 | + |
| 140 | + PyObject* result = PyObject_Call(operation, final_args, kwargs_dict); |
| 141 | + Py_DECREF(final_args); |
| 142 | + if (!result) return nullptr; |
| 143 | + Py_DECREF(result); |
| 144 | + |
| 145 | + visited.insert(adj_name); |
| 146 | + q.push(adj_node); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + if (PyErr_Occurred()) { |
| 151 | + return nullptr; |
| 152 | + } |
| 153 | + |
| 154 | + Py_RETURN_NONE; |
| 155 | +} |
0 commit comments