4
2

More than 3 years have passed since last update.

rustのpetgraph crateを使ってみる

Posted at

TL;DR

rustのpetgraphクレートを使うとグラフ構造が色々扱えるらしいので、触ってみた。
順次追記していく予定。

union find tree

グループ分けを管理するデータ構造。

  1. 要素xとyが同じグループに属するかの判定
  2. 要素xとyの属するグループを併合

みたいなことができるらしい。自分で実装してみても良かったが、petgraphクレートでは経路圧縮とランクを考慮した実装がされていて、1, 2を$O(α(n))$で処理できるらしいので、そのまま使えばいいかってなった。

use petgraph::unionfind::UnionFind;

// Union Find Treeの初期化
let mut tree :UnionFind<usize> = UnionFind::new(N);

// x, yのグループを結合
tree.union(x, y);

// x, yが同じグループに属しているか判定
let flag: bool = tree.equiv(x, y);

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2