Skip to content
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
8 changes: 8 additions & 0 deletions include/graaflib/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ class graph {
*/
[[nodiscard]] vertices_t get_neighbors(vertex_id_t vertex_id) const;

/**
* Get a list of predecessor vertices (vertices pointing to this vertex)
*
* @param vertex_id The ID of the vertex
* @return vertices_t - A list of predecessor vertices
*/
[[nodiscard]] vertices_t get_predecessors(vertex_id_t vertex_id) const;

/**
* Add a vertex to the graph
*
Expand Down
19 changes: 19 additions & 0 deletions include/graaflib/graph.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,25 @@ graph<VERTEX_T, EDGE_T, GRAPH_TYPE_V>::get_neighbors(
return adjacency_list_.at(vertex_id);
}

template <typename VERTEX_T, typename EDGE_T, graph_type GRAPH_TYPE_V>
typename graph<VERTEX_T, EDGE_T, GRAPH_TYPE_V>::vertices_t
graph<VERTEX_T, EDGE_T, GRAPH_TYPE_V>::get_predecessors(
vertex_id_t vertex_id) const {

using enum graph_type;
if constexpr (GRAPH_TYPE_V == UNDIRECTED) {
return get_neighbors(vertex_id);
}
vertices_t predecessors{};
for (const auto& [source_id, neighbors] : adjacency_list_) {
if (neighbors.contains(vertex_id)) {
predecessors.insert(source_id);
}
}

return predecessors;
}

template <typename VERTEX_T, typename EDGE_T, graph_type GRAPH_TYPE_V>
vertex_id_t graph<VERTEX_T, EDGE_T, GRAPH_TYPE_V>::add_vertex(auto&& vertex) {
while (has_vertex(vertex_id_supplier_)) {
Expand Down