sirkit-cpp-library

競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。

View the Project on GitHub reinsirk/sirkit-cpp-library

閉路検出

Graph/Cycle_Detection.cpp · ソース · 文書 現行ACあり Last Commit

通常版・コンパイラ共通 · この版の一覧へ

通常版・GCC専用 · この版の一覧へ

通常版・Clang専用 · この版の一覧へ

通常版・MSVC専用 · この版の一覧へ

速度優先版・コンパイラ共通 · この版の一覧へ

速度優先版・GCC専用 · この版の一覧へ

速度優先版・Clang専用 · この版の一覧へ

速度優先版・MSVC専用 · この版の一覧へ

グラフに閉路があれば、辺番号を含めて閉路を1つ求めます。有向・無向の多重グラフに対応します。

std::vector<std::pair<int, int>> edges{{0, 1}, {1, 0}};
auto cycle = sirkit::find_cycle(2, edges);

find_cycle

std::optional<Cycle> find_cycle(int n, std::span<const std::pair<int, int>> edges, bool directed = true);

頂点番号は [0,n) です。戻り値の vertices[i] とその次の頂点を、入力位置 edge_ids[i] の辺が結びます。閉路がなければ std::nullopt を返します。$m$ 辺のとき時間・領域ともに $O(n+m)$ です。

使用例 / Verify

ソースのAPI注記
find_cycle(n,edges,directed=true): 有向/無向多重グラフの単純閉路を1つ返す。なければnullopt。
Cycle{vertices,edge_ids}: i番目の辺はvertices[i]から次の頂点へ進む。辺IDは入力位置。
頂点番号は[0,n)。自己辺と平行辺を扱う。O(n+m)時間・領域。
Verifyプログラムと検証結果

Verifyプログラム

通常版の検証結果です。

問題 プログラム 現行ソースの状態 提出結果
Cycle Detection (Directed) cycle_detection_directed.cpp ✓ 現行AC AC
Cycle Detection (Undirected) cycle_detection_undirected.cpp ✓ 現行AC AC

✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。

ローカル・CI検査

検査 状態
正常系 未実施
失敗系 未測定
Library Checker公式ケース 未測定
性能測定 未実施
ASan/UBSan 未実施
カバレッジ 未実施

到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。

実行条件・件数・測定結果 · 結果JSON

Verified with

Code

// SPDX-License-Identifier: CC0-1.0
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/cycle_detection.md
//
// API一覧
// find_cycle(n,edges,directed=true): 有向/無向多重グラフの単純閉路を1つ返す。なければnullopt。
// Cycle{vertices,edge_ids}: i番目の辺はvertices[i]から次の頂点へ進む。辺IDは入力位置。
// 頂点番号は[0,n)。自己辺と平行辺を扱う。O(n+m)時間・領域。

// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <cstddef>
#include <numeric>
#include <optional>
#include <span>
#include <stdexcept>
#include <utility>
#include <vector>

// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_CYCLE_DETECTION_DEFINED
#define SIRKIT_CYCLE_DETECTION_DEFINED

namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/
{
inline std::size_t cycle_arc_count(std::size_t edges, std::size_t directions, std::size_t limit)
{
    if (edges > limit / directions)
    {
        throw std::length_error("find_cycle: too many edges");
    }
    return edges * directions;
}
} // namespace detail

struct Cycle
{
    std::vector<int> vertices;
    std::vector<std::size_t> edge_ids;
};

inline std::optional<Cycle> find_cycle(int n, std::span<const std::pair<int, int>> edges, bool directed = true)
{
    if (n < 0)
    {
        throw std::length_error("find_cycle: negative vertex count");
    }
    struct Arc
    {
        int to;
        std::size_t id;
    };
    const auto directions = directed ? 1U : 2U;
    const auto arc_count = detail::cycle_arc_count(edges.size(), directions, std::vector<Arc>{}.max_size());

    std::vector<std::size_t> offset(static_cast<std::size_t>(n) + 1);
    for (const auto &[u, v] : edges)
    {
#ifndef SKIP_RUNTIME_CHECKS
        if (u < 0 || u >= n || v < 0 || v >= n)
        {
            throw std::out_of_range("find_cycle: vertex out of range");
        }
#endif
        ++offset[u];
        if (!directed)
        {
            ++offset[v];
        }
    }
    std::partial_sum(offset.begin(), offset.end(), offset.begin());
    std::vector<Arc> adjacency(arc_count);
    for (std::size_t id = 0; id < edges.size(); ++id)
    {
        const auto [u, v] = edges[id];
        adjacency[--offset[u]] = {v, id};
        if (!directed)
        {
            adjacency[--offset[v]] = {u, id};
        }
    }

    struct Frame
    {
        int vertex;
        std::size_t next;
        std::size_t incoming;
    };
    constexpr int kUnseen = -1;
    constexpr int kFinished = -2;
    std::vector<int> position(n, kUnseen);
    std::vector<Frame> stack;
    for (int start = 0; start < n; ++start)
    {
        if (position[start] != kUnseen)
        {
            continue;
        }
        position[start] = 0;
        stack.push_back({start, offset[start], edges.size()});
        while (!stack.empty())
        {
            auto &frame = stack.back();
            if (frame.next == offset[frame.vertex + 1])
            {
                position[frame.vertex] = kFinished;
                stack.pop_back();
                continue;
            }
            const auto [to, id] = adjacency[frame.next++];
            if (!directed && id == frame.incoming)
            {
                continue;
            }
            if (position[to] == kUnseen)
            {
                position[to] = static_cast<int>(stack.size());
                stack.push_back({to, offset[to], id});
            }
            else if (position[to] >= 0)
            {
                const auto first = static_cast<std::size_t>(position[to]);
                Cycle result;
                result.vertices.reserve(stack.size() - first);
                result.edge_ids.reserve(stack.size() - first);
                result.vertices.push_back(to);
                for (std::size_t i = first + 1; i < stack.size(); ++i)
                {
                    result.vertices.push_back(stack[i].vertex);
                    result.edge_ids.push_back(stack[i].incoming);
                }
                result.edge_ids.push_back(id);
                return result;
            }
        }
    }
    return std::nullopt;
}

#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1
Back to top page