转:Monoids and Finger Trees
转自:http://apfelmus.nfshost.com/articles/monoid-fingertree.html
This post grew out of the big monoid discussion on the haskell-cafe mailing list.
Introduction
A very powerful application of monoids are 2-3 finger trees, first described by Ralf Hinze and Ross Patterson.
Basically, they allow you to write fast implementations for pretty much every abstract data type mentioned in Okasaki’s book on purely functional data structures. For example, you can do sequences, priority queues, search trees and priority search queues. Moreover, any fancy and custom data structures like interval trees or something for stock trading are likely to be implementable in this framework as well.
How can one tree be useful for so many different data structures? The answer: monoids! Namely, the finger tree works with elements that are related to a monoid, and all the different data structures mentioned above arise by different choices for this monoid.
Let me explain how this monoid magic works.
A list with random access
We begin with the simplest of all data structures, the linked list. As you well know, retrieving the head is fast but random access is much slower:
xs !! n
needs O(n) i.e. linear time to retrieve the n-th element of the list. We would like to create a faster list-like data structure that reduces this to O(log n) i.e. logarithmic time.
For that, we use a binary tree that stores the elements a at the leaves. Furthermore, every node is annotated with a value of type v
data Tree v a = Leaf v a
| Branch v (Tree v a) (Tree v a)
In other words, our trees look like this
v
/ \
v v
/ \ / \
v v v v
a a a / \
v v
a a
The leaves store the elements of our list from left to right.
toList :: Tree v a -> [a]
toList (Leaf _ a) = [a]
toList (Branch _ x y) = toList x ++ toList y
Annotations are fetched by
tag :: Tree v a -> v
tag (Leaf v _) = v
tag (Branch v _ _) = v
We can also implement the head operation which retrieves the leftmost element
head :: Tree v a -> a
head (Leaf _ a) = a
head (Branch _ x _) = head x
Ok, so accessing the 1st leaf was easy, how about the 2nd, 3rd, the n-th leaf?
The solution is to annotate each subtree with its size.
type Size = Int
Our example tree has 5 leaves in total and the subtree on the right contains 3 leaves.
5
/ \
2 3
/ \ / \
1 1 1 2
a a a / \
1 1
a a
Thus, we set v = Size and we want the annotations to fulfill
tag (Leaf ..) = 1
tag (Branch .. x y) = tag x + tag y
We can make sure that they are always correct by using smart constructors: instead of using Leaf and Branch to create a tree, we use custom functions
leaf :: a -> Tree Size a
leaf a = Leaf 1 a
branch :: Tree Size a -> Tree Size a -> Tree Size a
branch x y = Branch (tag x + tag y) x y
which automatically annotate the right sizes.
Given size annotations, we can now find the n-th leaf:
(!!) :: Tree Size a -> Int -> a
(Leaf _ a) !! 0 = a
(Branch _ x y) !! n
| n < tag x = x !! n
| otherwise = y !! (n - tag x)
And assuming that our tree is balanced, this will run in O(log n) time. But for now, let’s ignore balancing which would become relevant when implementing cons or tail.
A priority queue
Let’s consider a different data structure, the priority queue. It stores items that have different “priorities” and always returns the most urgent one first. We represent priorities as integers and imagine them as points in time so the smallest ones are more urgent.
type Priority = Int
Once again, we use a binary tree. This time, we imagine it as a tournament tree, so that every subtree is annotated with the smallest priority it contains
2
/ \
4 2
/ \ / \
16 4 2 8
a a a / \
32 8
a a
In other words, our annotations are to fulfill
tag (Leaf .. a) = priority a
tag (Branch .. x y) = tag x `min` tag y
with corresponding smart constructors. Given the tournament table, we can reconstruct the element that has the smallest priority in O(log n) time
winner :: Tree Priority a -> a
winner t = go t
where
go (Leaf _ a) = a
go (Branch _ x y)
| tag x == tag t = go x -- winner on left
| tag y == tag t = go y -- winner on right
Again, we forgo balancing and thus insertion or deletion.
Monoids - the grand unifier
As we can see, one and the same tree structure can be used for two quite different purposes, just by using different annotations. And by recognizing that the tags form amonoid, we can completely unify both implementations. Moreover, the retrieval operations (!!) and winner are actually special cases of one and the same function!
For brevity, we will denote the associative operation of a monoid with <>
(<>) = mappend
Think of the <> as a small diamond symbol.
Annotations are monoids
The observation is that we obtain the tag of a branch by combining its children with the monoid operation
tag (Branch .. x y) = tag x <> tag y
of the following monoid instances
instance Monoid Size where
mempty = 0
mappend = (+)
instance Monoid Priority where
mempty = maxBound
mappend = min
Hence, a unified smart constructor reads
branch :: Monoid v => Tree v a -> Tree v a -> Tree v a
branch x y = Branch (tag x <> tag y) x y
For leaves, the tag is obtained from the element. We can capture this in a type class
class Monoid v => Measured a v where
measure :: a -> v
so that the smart constructor reads
leaf :: Measured a v => a -> Tree v a
leaf a = Leaf (measure a) a
For our examples, the instances would be
instance Measured a Size where
measure _ = 1 -- one element = size 1
instance Measured Foo Priority where
measure a = priority a -- urgency of the element
How does the annotation at the top of a tree relate to the elements at the leaves? In our two examples, it was the total number of leaves and the least priority respectively. These values are independent of the actual shape of the tree. Thanks to the associativity of <>, this is true for any monoid. For instance, the two trees
(v1<>v2) <> (v3<>v4) v1 <> (v2<>(v3<>v4))
/ \ / \
/ \ v1 v2 <> (v3<>v4)
/ \ a1 / \
v1 <> v2 v3 <> v4 v2 v3 <> v4
/ \ / \ a2 / \
v1 v2 v3 v4 v3 v4
a1 a2 a3 a4 a3 a4
have the same annotations
(v1<>v2) <> (v3<>v4) = v1 <> (v2<>(v3<>v4)) = v1 <> v2 <> v3 <> v4
as long as the sequences of leaves are the same. In general, the tag at the root of a tree withn elements is
measure a1 <> measure a2 <> measure a3 <> ... <> measure an
While independent of the shape of the branching, i.e. on the placement of parenthesis, this may of course depend on the order of elements.
It makes sense to refer to this combination of measures of all elements as the measure of the tree
instance Measured a v => Measured (Tree a v) v where
measure = tag
Thus, every tree is annotated with its measure.
Search
Our efforts culminate in the unification of the two search algorithms (!!) and winner. They are certainly similar; at each node, they descend into one of the subtrees which is chosen depending on the annotations. But to see their exact equivalence, we have to ignore branches and grouping for now because this is exactly what associativity “abstracts away”.
In a sequence of elements
a1 , a2 , a3 , a4 , ... , an
how to find say the 3rd one? Well, we scan the list from left to right and add 1 for each element encountered. As soon as the count exceeds 3, we have found the 3rd element.
1 -- is not > 3
1 + 1 -- is not > 3
1 + 1 + 1 -- is not > 3
1 + 1 + 1 + 1 -- is > 3
...
Similarly, how to find the element of a least priority say v? Well, we can scan the list from left to right and keep track of the minimum priority so far. We have completed our search once it becomes equal to v.
v1 -- still bigger than v
v1 `min` v2 -- still bigger than v
v1 `min` v2 `min` v3 -- still bigger than v
v1 `min` v2 `min` v3 `min` v4 -- equal to v!
...
In general terms, we are looking for the position where a predicate p switches from Falseto True.
measure a1 -- not p
measure a1 <> measure a2 -- not p
measure a1 <> measure a2 <> measure a3 -- not p
measure a1 <> measure a2 <> measure a3 <> measure a4 -- p
... -- p
In other words, we are looking for the position k where
p (measure a1 <> ... <> measure ak) is False
p (measure a1 <> ... <> measure ak <> measure a(k+1)) is True
The key point is that p does not test single elements but combinations of them, and this allows us to do binary search! Namely, how to find the element where p flips? Answer: divide the total measure into two halves
x <> y
x = measure a1 <> ... <> measure a(n/2)
y = measure a(n/2+1) <> ... <> measure an
If p is True on the first half, then we have to look there for the flip, otherwise we have to search the second half. In the latter case, we would have to split y = y1 <> y2 and test p (x <> y1).
In the case of our data structures, the tree shape determines how the measure is split into parts at each step. Here is the full procedure
search :: Measured a v => (v -> Bool) -> Tree v a -> Maybe a
search p t
| p (measure t) = Just (go mempty p t)
| otherwise = Nothing
where
go i p (Leaf _ a) = a
go i p (Branch _ l r)
| p (i <> measure l) = go i p l
| otherwise = go (i <> measure l) p r
Since we have annotated each branch with its measure, testing p takes no time at all.
Of course, this algorithm only works if p really does flip from False to True exactly once. This is the case if p fulfills
p (x) implies p (x <> y) for all y
and we say that p is a monotonic predicate. Our two examples (> 3) and (== minimum)have this property and thus, we can finally conclude with
t !! k = search (> k)
winner t = search (== measure t)
Where to go from here
I hope you have enjoyed this excursion into the land of trees and monoids. If you want to stay a bit longer, implement a data structure that do both look up the k-th element and retrieve the element with the least priority at the same time. This is also known as priority search queue.
If you still long for more, the finger tree paper knows the way; I have tried to closely match their notation. In particular, they solve the balancing issue which turns the binary search on monoids into a truly powerful tool to construct about any fancy data structure with logarithmic access times you can imagine.
转:Monoids and Finger Trees的更多相关文章
- Finger Trees: A Simple General-purpose Data Structure
http://staff.city.ac.uk/~ross/papers/FingerTree.html Summary We present 2-3 finger trees, a function ...
- The Swiss Army Knife of Data Structures … in C#
"I worked up a full implementation as well but I decided that it was too complicated to post in ...
- [C#] C# 知识回顾 - 表达式树 Expression Trees
C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达 ...
- hdu2848 Visible Trees (容斥原理)
题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...
- [LeetCode] Minimum Height Trees 最小高度树
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 2 Unique Binary Search Trees II_Leetcode
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- Linux下的Finger指令
Linux finger命令 Linux finger命令可以让使用者查询一些其他使用者的资料.会列出来的资料有: Login Name User Name Home directory Shell ...
随机推荐
- 如何打造100亿SDK累计覆盖量的大数据系统
作为推送行业领导者,截止目前个推SDK累计安装覆盖量达100亿(含海外),接入应用超过43万,独立终端覆盖超过10亿 (含海外).个推系统每天会产生大量的日志和数据,面临许多数据处理方面的挑战. 首先 ...
- [转载+实践理解]Android动画---如何正确使用平移动画(关于fillBefore和fillAfter的一点说明)(转载)
红色部分为自己的实践理解 如何实现将View向上平移自身高度一半的距离? TranslateAnimation translate = new TranslateAnimation( Animatio ...
- ASP.NET MVC路由
ASP.NET MVC路由(四) 前言 在前面的篇幅中我们讲解路由系统在MVC中的运行过程以及粗略的原理,想必看过前面篇幅的朋友应该对路由有个概念性的了解了,本篇来讲解区域,在读完本篇后不会肯定的让你 ...
- 基于.NET的微信SDK
超级懒汉编写的基于.NET的微信SDK 一.前言 特别不喜欢麻烦的一个人,最近碰到了微信开发.下载下来了一些其他人写的微信开发“框架”,但是被恶心到了,实现的太臃肿啦. 最不喜欢的就是把微信返回的 ...
- JSP技术模型(五)JSP隐含变量
在JSP页面的转换阶段,容器在_jspService()方法中申明并初始化一些变量,可以在JSP页面小脚本中或表达式中直接使用这些变量. 一.JSP页面中可使用的隐含变量 1.applicationj ...
- 使用Repository模式构建数据库访问层
使用Repository模式构建数据库访问层 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程[二]——使用Repository模式构建数据库访问层 系列导航地址http:// ...
- noip模拟赛:部队[技巧?思想?]
王国军总指挥——卡西乌斯准将决定重建情报局,需要从全国各地挑选有能力的士兵,选择的标准为A,B两种能力.对于每个候选士兵,如果存在另一名士兵的两项能力均大于等于他,那么他将被淘汰.(注意:若两名士兵两 ...
- GNU Make 学习系列一:怎样写一个简单的Makefile
编程通常遵循一个相当简单的程序:编辑源文件,编译源代码成可执行的格式,调试结果.尽管将源代码翻译成可执行程序是常规的过程,如果做的不正确,程序员可能会浪费大量的时间去追踪问题.大多数的开发者都经历过这 ...
- C++拷贝构造函数总结
C++拷贝构造函数总结 目录: 拷贝构造函数的基础知识 拷贝构造函数的使用 拷贝构造函数的行为 1.拷贝构造函数的基础知识 拷贝构造函数(copy constructor)是构造函数,是拷贝已经存在的 ...
- [转]Adventures in Xen exploitation
Source:https://www.nccgroup.com/en/blog/2015/02/adventures-in-xen-exploitation/ tl;dr This post is ...