Optimal Solution Tarjan’s Algorithm

  • 格式:pdf
  • 大小:449.54 KB
  • 文档页数:36

ML Workshop 2007
The Disjoint Sets Problem
goal: a data structure to maintain a partition of {0, 1, . . . , n − 1} imperative data structure: module type ImperativeUnionFind = sig type t val create : int → t val find : t → int → int val union : t → int → int → unit end
A Persistent Union-Find Data Structure
Sylvain Conchon and Jean-Christophe Filliˆtre a
Universit´ Paris Sud – CNRS e
S. Conchon & J.-C. Filliˆtre a
A Persistent Union-Find Data Structure
Persistent Union-Find
the previous solution is not adapted to backtracking no immediate undo operation copying the structure would be disastrous a solution would be a persistent data structure: module type PersistentUnionFind = sig type t val create : int → t val find : t → int → int val union : t → int → int → t end
S. Conchon & J.-C. Filliˆtre a
A Persistent Union-Find Data Structure
ML Workshop 2007
Persistent Union-Find
the previous solution is not adapted to backtracking no immediate undo operation copying the structure would be disastrous a solution would be a persistent data structure: module type PersistentUnionFind = sig type t val create : int → t val find : t → int → int val union : t → int → int → t end
S. Conchon & J.-C. Filliˆtre a
A Persistent Union-Find Data Structure
ML Workshop 2007
Persistent Arrays
to keep Tarjan’s efficiency, we need efficient persistent arrays solution known since 1978 and due to Henry Baker idea: a persistent array is either a usual array or a modification of another persistent array type α t = α data ref and α data = | Arr of α array | Diff of int × α × α t
S. Conchon & J.-C. Filliˆtre a
A Persistent Union-Find Data Structure
ML Workshop 2007
Optimal Solution: Tarjan’s Algorithm
(solution actually due to M. D. McIllroy and R. Morris, and “only” analyzed by Tarjan) within each class, elements are chained up to the representative
S. Conchon & J.-C. Filliˆtre a
A Persistent Union-Find Data Structure
ML Workshop 2007
A Persistent Version of Tarjan’s Algorithm
the same arrays as in the original solution: type t = { rank: int A.t; mutable link: int A.t } the mutable field allows path compression
S. Conchon & J.-C. Filliˆtre a
A Persistent Union-Find Data Structure
ML Workshop 2007
A Na¨ Solution ıve
module M = Map.Make(struct type t = int let compare = compare end) type t = int M.t let create n = M.empty let find m i = let rec lookup i = try lookup (M.find i m) with Not found → i in lookup i let union m i j = let ri = find m i in let rj = find m j in if ri <> rj then M.add ri rj m else m
S. Conchon & J.-C. Filliˆtre a
A Persistent Union-Find Data Structure
ML Workshop 2007
Our Solution
we propose a solution as efficient as Tarjan’s algorithm which is persistent (but uses side-effects) formally proved correct obtained by combining a data structure of persistent arrays a persistent version of Tarjan’s algorithm
ML Workshop 2007
A Persistent Version of Tarjan’s Algorithm
written independently of the structure for persistent arrays: module Make(A : PersistentArray) : PersistentUnionFind assuming module type PersistentArray = sig type α t val init : int → (int → α) → α t val get : α t → int → α val set : α t → int → α → α t end
S. Conchon & J.-C. Filliˆtre a
A Persistent Union-Find Data Structure
ML Workshop 2007
The Disjoint Sets Problem
goal: a data structure to maintain a partition of {0, 1, . . . , n − 1} imperative data structure: module type ImperativeUnionFind = sig type t val create : int → t val find : t → int → int val union : t → int → int → unit end
S. Conchon & J.-C. Filliˆtre a
A Persistent Union-Find Data Structure
ML Workshop 2007
A Persistent Version of Tarjan’s Algorithm
representative lookup with path compression: let rec find aux a i = let ai = A.get a i in if ai == i then a, i else let a, r = find aux a ai in let a = A.set a i r in a, r the path compression made effective: let find h x = let a,rx = find aux h.link x in h.link <- a; rx
ML Workshop 2007
The Stone Soup
Sylvain Conchon and Jean-Christophe Filliˆtre a
Universit´ Paris Sud – CNRS e
S. Conchon & J.-C. Filliˆtre a
A Persistent Union-Find Data Structure
S. Conchon & J.-C. Filliˆtre a
A Persistent Union-Find Data Structure