#pragma once #include #include #include #include #include #include #include #include #include #include #include namespace Void::Graph { // ── DiGraph ───────────────────────────────────────────────────────────── // Directed graph with vertex labels of type V and edge weights of type E. // Vertices are identified by a user-supplied key of type V (must be hashable). template class DiGraph { public: struct Edge { V to; E weight; }; void add_vertex(const V& v) { adj_[v]; } void add_edge(const V& from, const V& to, E weight = E{}) { adj_[from].push_back({to, weight}); adj_.try_emplace(to); } [[nodiscard]] bool has_vertex(const V& v) const { return adj_.contains(v); } [[nodiscard]] bool has_edge(const V& from, const V& to) const { auto it = adj_.find(from); if (it == adj_.end()) return false; return std::ranges::any_of(it->second, [&](const Edge& e) { return e.to == to; }); } [[nodiscard]] const std::vector& neighbors(const V& v) const { return adj_.at(v); } [[nodiscard]] std::size_t vertex_count() const noexcept { return adj_.size(); } [[nodiscard]] std::size_t edge_count() const noexcept { std::size_t n = 0; for (const auto& [_, edges] : adj_) n += edges.size(); return n; } // ── BFS ────────────────────────────────────────────────────────────────── void bfs(const V& start, std::function visit) const { if (!adj_.contains(start)) return; std::unordered_set visited; std::queue q; visited.insert(start); q.push(start); while (!q.empty()) { const V cur = q.front(); q.pop(); visit(cur); for (const auto& e : adj_.at(cur)) { if (!visited.contains(e.to)) { visited.insert(e.to); q.push(e.to); } } } } [[nodiscard]] std::vector bfs_order(const V& start) const { std::vector result; bfs(start, [&](const V& v) { result.push_back(v); }); return result; } // ── DFS ────────────────────────────────────────────────────────────────── void dfs(const V& start, std::function visit) const { std::unordered_set visited; dfs_impl(start, visited, visit); } [[nodiscard]] std::vector dfs_order(const V& start) const { std::vector result; dfs(start, [&](const V& v) { result.push_back(v); }); return result; } // ── Topological sort (Kahn's algorithm) ────────────────────────────────── // Returns nullopt if the graph has a cycle. [[nodiscard]] std::optional> topological_sort() const { std::unordered_map in_degree; for (const auto& [v, _] : adj_) in_degree[v] = 0; for (const auto& [v, edges] : adj_) { for (const auto& e : edges) in_degree[e.to]++; } std::queue q; for (const auto& [v, deg] : in_degree) { if (deg == 0) q.push(v); } std::vector order; while (!q.empty()) { V cur = q.front(); q.pop(); order.push_back(cur); for (const auto& e : adj_.at(cur)) { if (--in_degree[e.to] == 0) q.push(e.to); } } if (order.size() != adj_.size()) return std::nullopt; // cycle return order; } // ── Reachability ───────────────────────────────────────────────────────── [[nodiscard]] bool can_reach(const V& from, const V& to) const { bool found = false; bfs(from, [&](const V& v) { if (v == to) found = true; }); return found; } // ── Shortest path (BFS, unweighted) ────────────────────────────────────── [[nodiscard]] std::optional> shortest_path(const V& from, const V& to) const { if (!adj_.contains(from) || !adj_.contains(to)) return std::nullopt; std::unordered_map parent; std::unordered_set visited; std::queue q; visited.insert(from); q.push(from); bool found = false; while (!q.empty() && !found) { V cur = q.front(); q.pop(); for (const auto& e : adj_.at(cur)) { if (!visited.contains(e.to)) { visited.insert(e.to); parent[e.to] = cur; if (e.to == to) { found = true; break; } q.push(e.to); } } } if (!found && from != to) return std::nullopt; std::vector path; V cur = to; while (cur != from) { path.push_back(cur); cur = parent.at(cur); } path.push_back(from); std::ranges::reverse(path); return path; } private: void dfs_impl(const V& v, std::unordered_set& visited, std::function& visit) const { if (visited.contains(v)) return; visited.insert(v); visit(v); if (adj_.contains(v)) { for (const auto& e : adj_.at(v)) { dfs_impl(e.to, visited, visit); } } } std::unordered_map> adj_; }; } // namespace Void::Graph