5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SortedContainersの計算量まとめ

Last updated at Posted at 2025-04-28

はじめに

SortedContainersのデータ構造たちは内部に_loadという値を持っており、この値に計算量が依存します。この記事ではその値を$M$、要素数を$N$として話を進めます。
また、挿入する要素同士の比較は$O(1)$で可能なものとします。

SortedList

常にソートされた配列です。
他言語ではmultisetと呼ばれるかもしれません。

操作 償却計算量 $ M = \sqrt[3]{N}$の場合1
add $O(\log(N) + M + \frac{N}{M^2} )$ $O(\sqrt[3]{N})$
discard $O(\log(N) + M + \frac{N}{M^2} )$ $O(\sqrt[3]{N})$
pop $O(\log(N)) + M + \frac{N}{M^2} )$ $O(\sqrt[3]{N})$
getitem(一般) $O(\log(\frac{N}{M}))$ 2 $O(\log{N})$
getitem(先頭、末尾のバケット) $O(1)$ $O(1)$
contains $O(\log(N))$ $O(\log(N))$
bisect_left $O(\log(N))$ $O(\log(N))$
bisect_right $O(\log(N))$ $O(\log(N))$
count $O(\log(N))$ $O(\log(N))$
index $O(\log(N))$ $O(\log(N))$
remove $O(\log(N) + M + \frac{N}{M^2} )$ $O(\sqrt[3]{N})$
del $O(\log(N) + M + \frac{N}{M^2} )$ $O(\sqrt[3]{N})$
clear $O(1)$3 $O(1)$
update$(4K \geq N)$ $O(K\log(K))$ $O(K\log(K))$
update$(4K \lt N)$ $O(K(\log(N) + M + \frac{N}{M^2}) )$ $O(K\sqrt[3]{N})$

SortedSet

重複しない版のSortedListです。
内部にpythonのsetを持っているため、ハッシュ化できないオブジェクトの挿入やハッシュの衝突回避目的には利用できません。

操作 平均計算量 $ M = \sqrt[3]{N}$の場合1 最悪計算量
add(存在しない要素) $O(\log(N) + M + \frac{N}{M^2} )$ $O(\sqrt[3]{N})$ $O(N)$4
add(存在する要素) $O(1)$ $O(1)$ $O(N)$4
discard(存在しない要素) $O(1)$ $O(1)$ $O(N)$4
discard(存在する要素) $O(\log(N) + M + \frac{N}{M^2} )$ $O(\sqrt[3]{N})$ $O(N)$4
pop $O(\log(N)) + M + \frac{N}{M^2} )$ $O(\sqrt[3]{N})$ $O(N)$4
getitem(一般) $O(\log(\frac{N}{M}))$ $O(\log{N})$ $O(\frac{N}{M})$ 5
getitem(先頭、末尾のバケット) $O(1)$ $O(1)$ $O(1)$
contains $O(1)$ $O(1)$ $O(N)$4
bisect_left $O(\log(N))$ $O(\log(N))$ $O(\log(N))$
bisect_right $O(\log(N))$ $O(\log(N))$ $O(\log(N))$
count $O(\log(N))$ $O(\log(N))$ $O(\log(N))$
index $O(\log(N))$ $O(\log(N))$ $O(\log(N))$
remove $O(\log(N) + M + \frac{N}{M^2} )$ $O(\sqrt[3]{N})$ $O(N)$4
del $O(\log(N) + M + \frac{N}{M^2} )$ $O(\sqrt[3]{N})$ $O(N)$4
clear $O(N)$ $O(N)$ $O(N)$
update$(4K \geq N)$ $O(K\log(K))$ $O(K\log(K))$ $O(KN)$4
update$(4K \lt N)$ $O(K(\log(N) + M + \frac{N}{M^2}) )$ $O(K\sqrt[3]{N})$ $O(KN)$4

リンク(参考文献)

Sorted Containers ( GitHub )
https://github.com/grantjenks/python-sortedcontainers/tree/master

Performance at Scale
https://grantjenks.com/docs/sortedcontainers/performance-scale.html

  1. _loadの値を実際に$ \sqrt[3]{N} $と設定するのが、実行時間的に最速とは限りません。 2

  2. 実際には最悪で$O(\frac{N}{M})$かかりますが、addやpop時の計算量で償却可能。

  3. 実際には$O(N)$かかりますが、追加の際の計算量で償却可能。

  4. pythonのsetについて、ハッシュの衝突時n各操作の最悪計算量が$O(N)$となることが原因でこの計算量となっています。 2 3 4 5 6 7 8 9 10

  5. 実際にはaddやpopなどの計算量で償却されるのでボトルネックにはならない。

5
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
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?