Strange graphing results (Cormen) Red and black wood insert

I implemented in Red-Black trees in Python according to the pseudo-code in Cormen Introduction to Algorithms.

I wanted to see with my own eyes that mine insertreally is O(logn), so I drew a timeline for inserting nodes n=1, 10, 20, ..., 5000into a tree.

This is the result:

enter image description here

the axis xis equal n, and the axis yis the time spent in milliseconds .

For me, the graph looks more linear than the logarithmic. What can explain this?

+5
source share
3 answers

, n , x - , , y - .

, , n f(n).

, f:

f(1) < k*log(1)                 for some constant k.

f(2) < k*log(1) + k*log(2)      for some constant k

...

f(n) < k * [log(1) + log(2) + ... + log(n)]   for some constant k.

- , , log(1) + ... + log(n):

f(n) < k * [log(1*2*3*...*n)]     for some constant k

f(n) < k * log(n!)                for some constant k

, , log(n!). . .:)

, , :

for n in (5000, 50000, 500000):
    startTime = ...
    ## .. make a fresh tree
    ## insert n elements into the tree
    stopTime = ...
    ## record the tuple (n, stopTime - startTime) for plotting

n, n:

for n in range(50000):
    startTime = ...
    ## insert an element into the tree
    stopTime = ...
    ## record the tuple (n, stopTime - startTime) for plotting

, f(n)/n, . , log(n!) n*log(n) (. ). :

f(n) < k * log(n!)                for some constant k

:

f(n) < k * n * log(n)             for some constant k

, f(n) n, .

+4

5000 , "" - 50000 500000. , . , . "" , .

+3

"" . , , , . , . " " , , , .

, .

+2

All Articles