SlideShare a Scribd company logo
1 of 20
二項ヒープ
@tokoroten_lab
はじめに
ただのデータ構造の紹介になってしまいました。
二項ヒープを知っている方は寝ましょう。
ヒープとは…
木構造
常に親ノードが子ノードよりも小さい(大きい)か等しい
(他) 二分ヒープ、フィボナッチヒープ
二項ヒープ
3
7
1
4
5
8
2
6
最小ヒープ
二項ヒープ
二項ヒープは二項木の集合からなるヒープ
ヒープ同士のマージをO(logN)で行える
二項木とは…
ランク0の二項木 単一ノード
ランク r (0 < r) 1つの根とランクr-1, r-2, ..., 0を子とする木
二項木
ランク 0 1 32
二項木
ランク r の2つの二項木を繋げることでランク r + 1 の二項木を生成できる
片方をルートと0..r-1のノードに分割し、それにrのノードを加える
ノード数は 2^r 個になる
構成する二項木の数
サイズ n の二項ヒープが持つ二項木はちょうど二進表記の1の位置に対応する。
→ 高々 log(n + 1) 個の木しか持たない。
二進数との対応
例) n = 11, 2^0 + 2^1 + 2^3
二進表記は 1011 となり、ランク 0, 1, 3 の二項木から構成される
1 2
3 6
4
7
8
5
9
ランク 0 1 32
11
10
マージ
ヒープのマージは二進数の足し算のように行う
具体的には、ランクの低い木から順に同じランクの木を結合していく
繰り上がりもあることに注意する
計算量は O(log n)
マージ
6
7
5
8
1
2
3
4
ヒープA
110
ヒープB
010
ヒープA+B
1000
マージ
6
7
5
8
1
2
3
4
ヒープA
110
ヒープB
010
ヒープA+B
1000
マージ
6
7
5
8
1
2
3
4
ヒープA
110
ヒープB
010
ヒープA+B
1000
マージ
6
7
5
8
1
2
3
4
ヒープA
110
ヒープB
010
ヒープA+B
1000
3
4
1
2
マージ
6
7
5
8
1
2
3
4
ヒープA
110
ヒープB
010
ヒープA+B
1000
3
4
1
2
6
7
5
8
挿入
要素を追加する際には、挿入する要素のみからなるヒープを作成する
それを挿入先のヒープとマージする
計算量はマージと同じになるため O(log n)
二項木のルートノードを調べる
計算量はヒープ内の二項木の数に依存するため O(log n)
最小値の検索
6 3
4 8
1
7
9
5
7
11
10
最小値削除
最小値を検索し、その要素を削除する
分離されたグループ内の部分木のリストを取得する
得たリストと元のヒープをマージする
計算量は検索及びマージと同じになるため O(log n)
計算量
操作 計算量
最小(大)値の検索 O(log n)
最小(大)値の削除 O(log n)
要素の挿入 O(log n)
ヒープ同士のマージ O(log n)
おまけ Haskellによるマージの実装
参考文献
純粋関数型データ構造 (著 : Chris Okasaki)
Wikipedia (https://ja.wikipedia.org/wiki/二項ヒープ)

More Related Content

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

二項ヒープ