エンジニアとしての市場価値を測りませんか?PR

企業からあなたに合ったオリジナルのスカウトを受け取って、市場価値を測りましょう

7
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

競技プログラミングなんでもAdvent Calendar 2024

Day 3

【計算量】なぜ O(max(n, m)) を O(n + m) と書けるのか?その理由を解説!

Last updated at Posted at 2024-12-02

はじめに

証明に入る前に、そもそも、計算量が O(max(n,m))O(n+m) で表されるような具体例はあるのでしょうか。

Python で書かれた以下の関数 f(x) を見てみてください。関数 f(x) は正整数 x が与えられたときに 1kxk を返す関数です。例えば x=4 のときは f(4)=1+2+3+4=10 となるので 10 を返します。

関数 f(x)
def f(x):
    total = 0
    for k in range(1, x + 1):
        total += k
    return total

これから、標準入力として 2 つの正整数 n,m が与えられるとします。関数 f(x) を使った次の 2 つのプログラム P,Q を考えてみると、(直感的には)プログラム P の計算量は O(max(n,m)) 、プログラム Q の計算量は O(n+m) と見積もれます。

プログラム P
# 関数 f(x)
def f(x):
    total = 0
    for k in range(1, x + 1):
        total += k
    return total

# 標準入力
n = int(input())
m = int(input())

if n > m:
    print(f(n))
else:
    print(f(m))
プログラム Q
# 関数 f(x)
def f(x):
    total = 0
    for k in range(1, x + 1):
        total += k
    return total

# 標準入力
n = int(input())
m = int(input())

print(f(n))
print(f(m))

証明

O(max(n,m))O(n+m) と書き表しても良いことをこれから証明します。
n>mnm の場合分けで示します。

  • n>m のとき
    • max(n,m)=n となるので、O(max(n,m))=O(n) が言えます。
    • m<n の両辺に正の定数 k を掛けると、km<kn となります。両辺に kn を加えると、kn+km<kn+kn=2kn3kn すなわち、k(n+m)3kn が成り立ちます。 O 記法の定義より、O(k(n+m))=O(n+m),O(3kn)=O(n) なので、O(n+m)=O(n) が言えます。

以上より、n>m のときは O(max(n,m))=O(n+m) が成り立ちます。

  • nm のとき
    • max(n,m)=m となるので、O(max(n,m))=O(m) が言えます。
    • nm の両辺に正の定数 k を掛けると、knkm となります。両辺に km を加えると kn+kmkm+km=2km すなわち、k(n+m)2km が成り立ちます。 O 記法の定義より、O(k(n+m))=O(n+m),O(2km)=O(m) なので、O(n+m)=O(m) が言えます。

以上より、nm のときは O(max(n,m))=O(n+m) が成り立ちます。

したがって、O(max(n,m))O(n+m) と書き表しても良いことが分かります。

7
0
3

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

@NAVYSHUNTA's pickup articles

NAVYSHUNTA

@NAVYSHUNTA(Shunta Nakamura)

数学が好きです。大学で数学と情報科学を学んでいます(B4)。

Comments

fujitanozomu
@fujitanozomu(藤田 望)
(Edited)

1=2を証明する」的なネタ記事だと思いますがネタタグを使うだとか記事の最後でネタばらしをされるだとかされると良いと思います。

0
NAVYSHUNTA
@NAVYSHUNTA(Shunta Nakamura)

@fujitanozomu
あなたがオーダー記法についてどの程度理解されているのか知りませんが、ネタではありません。ネタであれば証明のどこかに不備があることになりますが、それを指摘せずにネタと決めつけるのは良くないです。

実際、O(max(n, m)) を O(n + m) と書き表しても問題ないことから、AtCoder の解説記事などでもこの表現は用いられています。例えば、以下の解説記事は O(max(N, Q)) を O(N + Q) で書き表している例です。
https://atcoder.jp/contests/ABC328/editorial/7648

0
fujitanozomu
@fujitanozomu(藤田 望)

ネタのつもりではないのですか。それは失礼しました。

O(max(n,m))について、n>mの条件ではmの値は無視できるので、O(n+m)=O(n+0)=O(n)ですね。

nmではnの値が無視できるのであとは省略。

0

Let's comment your feelings that are more than good

Qiita Advent Calendar is held!

Qiita Advent Calendar is an article posting event where you post articles by filling a calendar 🎅

Some calendars come with gifts and some gifts are drawn from all calendars 👀

Please tie the article to your calendar and let's enjoy Christmas together!

7
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Login to continue?

Login or Sign up with social account

Login or Sign up with your email address