cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:heavy_check_mark: 強連結成分 (strongly connected components) 分解
(include/emthrm/graph/strongly_connected_components.hpp)

有向グラフを共通部分の存在しない強連結成分に分解することである。

時間計算量

$O(\lvert V \rvert + \lvert E \rvert)$

仕様

Kosaraju’s algorithm

template <typename CostType, bool IS_FULL_VER = false>
struct StronglyConnectedComponents;

メンバ変数

名前 説明 要件
std::vector<int> id id[i] は元のグラフの頂点 $i$ を含む頂点を表す。  
std::vector<std::vector<int>> vertices vertices[i] は縮約後のグラフの頂点 $i$ に含まれる頂点を表す。 完全版
std::vector<std::vector<Edge<CostType>>> g 強連結成分を一つの頂点に縮約したグラフ  

メンバ関数

名前 効果
explicit StronglyConnectedComponents(const std::vector<std::vector<Edge<CostType>>>& graph); 有向グラフ $\mathrm{graph}$ に対してオブジェクトを構築する。

備考

無向グラフの辺に向きをつけることで強連結成分分解できる $\Leftrightarrow$ グラフが2辺連結である

構築方法としては深さ優先探索木で後退辺を逆辺とすればよい。

参考文献

Kosaraju’s algorithm

備考

TODO

Submissons

Depends on

Verified with

Code

#ifndef EMTHRM_GRAPH_STRONGLY_CONNECTED_COMPONENTS_HPP_
#define EMTHRM_GRAPH_STRONGLY_CONNECTED_COMPONENTS_HPP_

// #include <algorithm>
#include <ranges>
#include <vector>

#include "emthrm/graph/edge.hpp"

namespace emthrm {

template <typename CostType, bool IS_FULL_VER = false>
struct StronglyConnectedComponents {
  std::vector<int> id;
  std::vector<std::vector<int>> vertices;
  std::vector<std::vector<Edge<CostType>>> g;

  explicit StronglyConnectedComponents(
      const std::vector<std::vector<Edge<CostType>>>& graph)
      : n(graph.size()), is_used(n, false), graph(graph), rgraph(n) {
    for (int i = 0; i < n; ++i) {
      if (!is_used[i]) dfs(i);
    }
    id.assign(n, -1);
    order.reserve(n);
    for (int i = 0; i < n; ++i) {
      for (const Edge<CostType>& e : graph[i]) {
        rgraph[e.dst].emplace_back(e.dst, e.src, e.cost);
      }
    }
    int m = 0;
    for (int i = n - 1; i >= 0; --i) {
      if (id[order[i]] == -1) {
        if constexpr (IS_FULL_VER) vertices.emplace_back();
        rdfs(order[i], m++);
      }
    }
    g.resize(m);
    for (int i = 0; i < n; ++i) {
      for (const Edge<CostType>& e : graph[i]) {
        if (id[i] != id[e.dst]) g[id[i]].emplace_back(id[i], id[e.dst], e.cost);
      }
    }
    // if constexpr (IS_FULL_VER) {
    //   for (int i = 0; i < m; ++i) {
    //     std::sort(vertices[i].begin(), vertices[i].end());
    //   }
    // }
  }

 private:
  const int n;
  std::vector<bool> is_used;
  std::vector<int> order;
  const std::vector<std::vector<Edge<CostType>>> graph;
  std::vector<std::vector<Edge<CostType>>> rgraph;

  void dfs(const int ver) {
    is_used[ver] = true;
    for (const int e : graph[ver]
                     | std::views::transform(&Edge<CostType>::dst)) {
      if (!is_used[e]) dfs(e);
    }
    order.emplace_back(ver);
  }

  void rdfs(const int ver, const int m) {
    id[ver] = m;
    if constexpr (IS_FULL_VER) vertices.back().emplace_back(ver);
    for (const int e : rgraph[ver]
                     | std::views::transform(&Edge<CostType>::dst)) {
      if (id[e] == -1) rdfs(e, m);
    }
  }
};

}  // namespace emthrm

#endif  // EMTHRM_GRAPH_STRONGLY_CONNECTED_COMPONENTS_HPP_
#line 1 "include/emthrm/graph/strongly_connected_components.hpp"



// #include <algorithm>
#include <ranges>
#include <vector>

#line 1 "include/emthrm/graph/edge.hpp"
/**
 * @title 辺
 */

#ifndef EMTHRM_GRAPH_EDGE_HPP_
#define EMTHRM_GRAPH_EDGE_HPP_

#include <compare>

namespace emthrm {

template <typename CostType>
struct Edge {
  CostType cost;
  int src, dst;

  explicit Edge(const int src, const int dst, const CostType cost = 0)
      : cost(cost), src(src), dst(dst) {}

  auto operator<=>(const Edge& x) const = default;
};

}  // namespace emthrm

#endif  // EMTHRM_GRAPH_EDGE_HPP_
#line 9 "include/emthrm/graph/strongly_connected_components.hpp"

namespace emthrm {

template <typename CostType, bool IS_FULL_VER = false>
struct StronglyConnectedComponents {
  std::vector<int> id;
  std::vector<std::vector<int>> vertices;
  std::vector<std::vector<Edge<CostType>>> g;

  explicit StronglyConnectedComponents(
      const std::vector<std::vector<Edge<CostType>>>& graph)
      : n(graph.size()), is_used(n, false), graph(graph), rgraph(n) {
    for (int i = 0; i < n; ++i) {
      if (!is_used[i]) dfs(i);
    }
    id.assign(n, -1);
    order.reserve(n);
    for (int i = 0; i < n; ++i) {
      for (const Edge<CostType>& e : graph[i]) {
        rgraph[e.dst].emplace_back(e.dst, e.src, e.cost);
      }
    }
    int m = 0;
    for (int i = n - 1; i >= 0; --i) {
      if (id[order[i]] == -1) {
        if constexpr (IS_FULL_VER) vertices.emplace_back();
        rdfs(order[i], m++);
      }
    }
    g.resize(m);
    for (int i = 0; i < n; ++i) {
      for (const Edge<CostType>& e : graph[i]) {
        if (id[i] != id[e.dst]) g[id[i]].emplace_back(id[i], id[e.dst], e.cost);
      }
    }
    // if constexpr (IS_FULL_VER) {
    //   for (int i = 0; i < m; ++i) {
    //     std::sort(vertices[i].begin(), vertices[i].end());
    //   }
    // }
  }

 private:
  const int n;
  std::vector<bool> is_used;
  std::vector<int> order;
  const std::vector<std::vector<Edge<CostType>>> graph;
  std::vector<std::vector<Edge<CostType>>> rgraph;

  void dfs(const int ver) {
    is_used[ver] = true;
    for (const int e : graph[ver]
                     | std::views::transform(&Edge<CostType>::dst)) {
      if (!is_used[e]) dfs(e);
    }
    order.emplace_back(ver);
  }

  void rdfs(const int ver, const int m) {
    id[ver] = m;
    if constexpr (IS_FULL_VER) vertices.back().emplace_back(ver);
    for (const int e : rgraph[ver]
                     | std::views::transform(&Edge<CostType>::dst)) {
      if (id[e] == -1) rdfs(e, m);
    }
  }
};

}  // namespace emthrm
Back to top page