Skip to content

Commit

Permalink
Add skeletons for bfs, dfs & topo_sort algos
Browse files Browse the repository at this point in the history
These will be used for training.
  • Loading branch information
pratzl committed Sep 2, 2024
1 parent 0290eda commit 3f0ac1b
Show file tree
Hide file tree
Showing 8 changed files with 492 additions and 1 deletion.
115 changes: 115 additions & 0 deletions include/graph/algorithm/breadth_first_search.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* @file breadth_first_search.hpp
*
* @brief Single-Source & multi-source breadth-first search.
*
* @copyright Copyright (c) 2024
*
* SPDX-License-Identifier: BSL-1.0
*
* @authors
*/

#include "graph/graph.hpp"
#include "graph/views/incidence.hpp"

#include <vector>
#include <queue>
#include <ranges>
#include <limits>
#include <fmt/format.h>

#ifndef GRAPH_BREADTH_FIRST_SEARCH_HPP
# define GRAPH_BREADTH_FIRST_SEARCH_HPP

namespace graph {

using vertex_id_type = int;
using Graph = std::vector<std::vector<vertex_id_type>>;
using Predecessors = std::vector<vertex_id_type>;
using Source = vertex_id_type;
using Sources = std::vector<Source>;
using Distance = int;
using Distances = std::vector<Distance>;

class dfs_visitor_base {
// Types
public:
// Visitor Functions
public:
// vertex visitor functions
void on_initialize_vertex(const vertex_id_type& uid) {}
void on_discover_vertex(const vertex_id_type& uid) {}
void on_examine_vertex(const vertex_id_type& uid) {}
void on_finish_vertex(const vertex_id_type& uid) {}

// edge visitor functions
void on_examine_edge(const vertex_id_type& uid) {}
};

/**
* @ingroup graph_algorithms
* @brief Returns the largest value used to represent an infinite distance.
*
* @return The largest possible distance value.
*/
inline Distance bfs_infinite_distance() { return std::numeric_limits<Distance>::max(); }

/**
* @ingroup graph_algorithms
* @brief Returns a distance value of zero.
*
* @return A value of zero distance.
*/
inline Distance bfs_zero() { return 0; }

/**
* @ingroup graph_algorithms
* @brief Intializes the distance values to the infinite value.
*
* @param distances The range of distance values to initialize.
*/
inline void init_bfs(Distances& distances) {
for (size_t i = 0; i < distances.size(); ++i) {
distances[i] = bfs_infinite_distance();
}
}

/**
* @ingroup graph_algorithms
* @brief Intializes the distance and predecessor values for bfs.
*
* @param distances The range of distance values to initialize. Each value will be assigned the infinite distance.
* @param predecessors The range of predecessors to initialize. Each value will be assigned the vertex id.
*/
inline void init_bfs(Distances& distances, Predecessors& predecessors) {
init_bfs(distances);
for (size_t i = 0; i < predecessors.size(); ++i) {
predecessors[i] = i;
}
}

/**
* @brief Single-source breadth-first search.
*
* @param g The graph.
* @param source The source vertex.
* @param predecessors The predecessor vertex for g[uid].
*/

// C++
inline void
breadth_first_search(const Graph& g, const Source& source, Distances& distances, Predecessors& predecessors) {}

// C
inline void breadth_first_search(const Graph* g,
const Source source,
const size_t distances_len,
Distances* distances,
const size_t predecessors_len,
Predecessors* predecessors) {}


} // namespace graph

#endif // GRAPH_BREADTH_FIRST_SEARCH_HPP
115 changes: 115 additions & 0 deletions include/graph/algorithm/depth_first_search.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* @file depth_first_search.hpp
*
* @brief Single-Source & multi-source depth-first search.
*
* @copyright Copyright (c) 2024
*
* SPDX-License-Identifier: BSL-1.0
*
* @authors
*/

#include "graph/graph.hpp"
#include "graph/views/incidence.hpp"

#include <vector>
#include <queue>
#include <ranges>
#include <limits>
#include <fmt/format.h>

#ifndef GRAPH_DFS_ALGORITHM_HPP
# define GRAPH_DFS_ALGORITHM_HPP

namespace graph {

using vertex_id_type = int;
using Graph = std::vector<std::vector<vertex_id_type>>;
using Predecessors = std::vector<vertex_id_type>;
using Source = vertex_id_type;
using Sources = std::vector<Source>;
using Distance = int;
using Distances = std::vector<Distance>;

class dfs_visitor_base {
// Types
public:
// Visitor Functions
public:
// vertex visitor functions
void on_initialize_vertex(const vertex_id_type& uid) {}
void on_discover_vertex(const vertex_id_type& uid) {}
void on_examine_vertex(const vertex_id_type& uid) {}
void on_finish_vertex(const vertex_id_type& uid) {}

// edge visitor functions
void on_examine_edge(const vertex_id_type& uid) {}
};

/**
* @ingroup graph_algorithms
* @brief Returns the largest value used to represent an infinite distance.
*
* @return The largest possible distance value.
*/
inline Distance dfs_infinite_distance() { return std::numeric_limits<Distance>::max(); }

/**
* @ingroup graph_algorithms
* @brief Returns a distance value of zero.
*
* @return A value of zero distance.
*/
inline Distance dfs_zero() { return 0; }

/**
* @ingroup graph_algorithms
* @brief Intializes the distance values to the infinite value.
*
* @param distances The range of distance values to initialize.
*/
inline void init_dfs(Distances& distances) {
for (size_t i = 0; i < distances.size(); ++i) {
distances[i] = dfs_infinite_distance();
}
}

/**
* @ingroup graph_algorithms
* @brief Intializes the distance and predecessor values for dfs.
*
* @param distances The range of distance values to initialize. Each value will be assigned the infinite distance.
* @param predecessors The range of predecessors to initialize. Each value will be assigned the vertex id.
*/
inline void init_dfs(Distances& distances, Predecessors& predecessors) {
init_dfs(distances);
for (size_t i = 0; i < predecessors.size(); ++i) {
predecessors[i] = i;
}
}

/**
* @brief Single-source depth-first search.
*
* @param g The graph.
* @param source The source vertex.
* @param predecessors The predecessor vertex for g[uid].
*/

// C++
inline void
depth_first_search(const Graph& g, const Source& source, Distances& distances, Predecessors& predecessors) {}

// C
inline void depth_first_search(const Graph* g,
const Source source,
const size_t distances_len,
Distances* distances,
const size_t predecessors_len,
Predecessors* predecessors) {}


} // namespace graph

#endif // GRAPH_DFS_ALGORITHM_HPP
2 changes: 1 addition & 1 deletion include/graph/algorithm/dijkstra_shortest_paths.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @file dijkstra_shortest_paths.hpp
*
* @brief Single-Source Shortest paths and shortest distances algorithms using Dijkstra's algorithm.
* @brief Single-Source & multi-source shortest paths & shortest distances algorithms using Dijkstra's algorithm.
*
* @copyright Copyright (c) 2024
*
Expand Down
114 changes: 114 additions & 0 deletions include/graph/algorithm/topological_sort.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* @file topological_sort.hpp
*
* @brief Single-Source & multi-source topological sort search.
*
* @copyright Copyright (c) 2024
*
* SPDX-License-Identifier: BSL-1.0
*
* @authors
*/

#include "graph/graph.hpp"
#include "graph/views/incidence.hpp"

#include <vector>
#include <queue>
#include <ranges>
#include <limits>
#include <fmt/format.h>

#ifndef GRAPH_TOPO_SORT_ALGO_HPP
# define GRAPH_TOPO_SORT_ALGO_HPP

namespace graph {

using vertex_id_type = int;
using Graph = std::vector<std::vector<vertex_id_type>>;
using Predecessors = std::vector<vertex_id_type>;
using Source = vertex_id_type;
using Sources = std::vector<Source>;
using Distance = int;
using Distances = std::vector<Distance>;

class topological_sort_visitor_base {
// Types
public:
// Visitor Functions
public:
// vertex visitor functions
void on_initialize_vertex(const vertex_id_type& uid) {}
void on_discover_vertex(const vertex_id_type& uid) {}
void on_examine_vertex(const vertex_id_type& uid) {}
void on_finish_vertex(const vertex_id_type& uid) {}

// edge visitor functions
void on_examine_edge(const vertex_id_type& uid) {}
};

/**
* @ingroup graph_algorithms
* @brief Returns the largest value used to represent an infinite distance.
*
* @return The largest possible distance value.
*/
inline Distance topological_sort_infinite_distance() { return std::numeric_limits<Distance>::max(); }

/**
* @ingroup graph_algorithms
* @brief Returns a distance value of zero.
*
* @return A value of zero distance.
*/
inline Distance topological_sort_zero() { return 0; }

/**
* @ingroup graph_algorithms
* @brief Intializes the distance values to the infinite value.
*
* @param distances The range of distance values to initialize.
*/
inline void init_topological_sort(Distances& distances) {
for (size_t i = 0; i < distances.size(); ++i) {
distances[i] = topological_sort_infinite_distance();
}
}

/**
* @ingroup graph_algorithms
* @brief Intializes the distance and predecessor values for topological sort.
*
* @param distances The range of distance values to initialize. Each value will be assigned the infinite distance.
* @param predecessors The range of predecessors to initialize. Each value will be assigned the vertex id.
*/
inline void init_topological_sort(Distances& distances, Predecessors& predecessors) {
init_topological_sort(distances);
for (size_t i = 0; i < predecessors.size(); ++i) {
predecessors[i] = i;
}
}

/**
* @brief Single-source topological sort.
*
* @param g The graph.
* @param source The source vertex.
* @param predecessors The predecessor vertex for g[uid].
*/

// C++
inline void topological_sort(const Graph& g, const Source& source, Distances& distances, Predecessors& predecessors) {}

// C
inline void topological_sort(const Graph* g,
const Source source,
const size_t distances_len,
Distances* distances,
const size_t predecessors_len,
Predecessors* predecessors) {}


} // namespace graph

#endif // GRAPH_TOPO_SORT_ALGO_HPP
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ set(UNITTEST_SOURCES
"co_dijkstra_tests.cpp"
"visitor_dijkstra_tests.cpp"

"breadth_first_search_tests.cpp"
"depth_first_search_tests.cpp"
"topological_sort_tests.cpp"

"csv_routes_vofl_tests.cpp"
"csv_routes.cpp"
"csv_routes_dov_tests.cpp"
Expand Down
Loading

0 comments on commit 3f0ac1b

Please sign in to comment.