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

added kahn algorithm #121

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ take a look at the [docs](https://bobluppes.github.io/graaf/docs/algorithms/intr
- Tarjan's Strongly Connected Components
6. [**Topological Sorting Algorithms**](https://bobluppes.github.io/graaf/docs/category/topological-sorting):
- Topological sorting DFS-based
- Kahn's Algorithm
7. [**Traversal Algorithms**](https://bobluppes.github.io/graaf/docs/category/traversal-algorithms):
- Breadth-First Search (BFS)
- Depth-First Search (DFS)
Expand Down
51 changes: 51 additions & 0 deletions include/graaflib/algorithm/topological_sorting/kahn.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// graaflib/algorithm/topological_sorting/kahn.cpp
#include "kahn.h"

namespace graaflib::algorithm::topological_sorting {

template <typename V, typename E>
std::optional<std::vector<vertex_id_t>> kahns_topological_sort(
const graph<V, E, graph_type::DIRECTED>& graph
) {
std::vector<vertex_id_t> topological_order;
std::vector<vertex_id_t> in_degree(graph.num_vertices(), 0);

//calculate in-degrees for all vertices
for (const auto& edge : graph.edges()) {
in_degree[edge.target()]++;
}

//queue to store vertices with in-degree 0
std::queue<vertex_id_t> zero_in_degree_vertices;

//initialize the queue with vertices having in-degree 0
for (vertex_id_t vertex = 0; vertex < graph.num_vertices(); ++vertex) {
if (in_degree[vertex] == 0) {
zero_in_degree_vertices.push(vertex);
}
}

while (!zero_in_degree_vertices.empty()) {
vertex_id_t current_vertex = zero_in_degree_vertices.front();
zero_in_degree_vertices.pop();

topological_order.push_back(current_vertex);

//remove the current vertex and its outgoing edges
for (const auto& edge : graph.edges_from(current_vertex)) {
vertex_id_t neighbor = edge.target();
if (--in_degree[neighbor] == 0) {
zero_in_degree_vertices.push(neighbor);
}
}
}

//check for the presence of a cycle
if (topological_order.size() != graph.num_vertices()) {
return std::nullopt; //graph contains a cycle
}

return topological_order;
}

}
19 changes: 19 additions & 0 deletions include/graaflib/algorithm/topological_sorting/kahn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// graaflib/algorithm/topological_sorting/kahn.h
#ifndef GRAAF_KAHN_H
#define GRAAF_KAHN_H

#include <vector>
#include <optional>
#include <graaf/include/graaflib/graph.h> // Include graph header
#include "graaf/properties/vertex_properties.h" // Include vertex properties header

namespace graaflib::algorithm::topological_sorting {

template <typename V, typename E>
std::optional<std::vector<vertex_id_t>> kahns_topological_sort(
const graph<V, E, graph_type::DIRECTED>& graph
);

}

#endif
Loading