Skip to content

Commit

Permalink
added kahn algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhandeep10 committed Oct 1, 2023
1 parent 0ebb28d commit 4a2ce7e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
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

0 comments on commit 4a2ce7e

Please sign in to comment.