You are on page 1of 3

N squ s of the bars is about (N2)/2.

In other words, the nal to the square of the problem size; if the pro
druple. We say that the worst-case time for cre big-O notation. For a problem of size N: d is "order 1":
O(1) is "order N": O(N) ouble the number of 1+2+...+ (N-1) for some doubles, so createList is list (0, 1, 2...
N-1) for each vidual bars. You can see quare, so its area is N2; he time for roblem size doubles, the
reateList is quadratic in the Cairo University Faculty of Computers and Information CS214-Data
Structures, 2nd Term, 201 on 015-2016 • A quadratic-time method od is "order N squared": O(N2) Note
that the big-O expressions gets large enough, constants and than a linear-time method, which ns do not
have constants or low-order terms. Th nd low-order terms don't matter (a constant-tim ich will be faster
than a quadratic-time method Formal definition: A function T than some value n0: This is because; when
N ime method will be faster od). T (N) is O (F (N)) if for some constant c and d for all values of N greater T
(N) <= c * F (N) The idea is that T(N) is the exac size N, and that F(N) is an upper a problem of size N will
be no w the least upper bound on the actu act complexity of a method or algorithm as a fu per-bound on
that complexity (i.e., the actual tim worse than F(N)). In practice, we want the sm ctual complexity. For
example, consider T (N) = 3 n0 = 2. This is because for all va function of the problem time/space or
whatever for mallest F (N) -- 3 * N2 + 5. We c operations for each call to add is (because it has to move
every ite item at position 0). For the N ca time for all N calls? It is proport initial array is large enough to
hold N items, the is proportional to the number of items in the li item already in the array one place to
the right calls shown above, the list lengths are: 0, 1, 2... ortional to 0 + 1 + 2 + ... + N-1. Recall that we
don't care about t ent Measure the complexity are simple unless noted otherwis nts: (assume the
statements
This is code that really is exa to add shown above. The total ti Total time = time (statement 1) + If each
statement is "simple" (on constant and the total time is als exactly k statements; this is not an unrolled
loop l time is found by adding the times for all statem ) + time (statement 2) + ... + time (statement k)
(only involves basic operatiomethod createList, the "problem different for different values of N method
createList) is not indepe then the number of list when add is called ht to make room for the new ... N-1.
So what is the total t the exact time, just how the time depends on t em size" is the value of N (because
the number f N). It is clear that the time for the N calls (and endent of N (so createList is not a constant-
tim n the problem size. For er of operations will be and therefore the time for time method). Is it 23
Cairo University Faculty of Computers and Information CS214-Data Structures, 2nd Term, 201 24
proportional to N (linear in N)? operations performed by createL different values of N: Clearly, the value
of the sum do not linear in N. In the following of the N calls. The value of the sum (0+1+2+... that the
bars fill about half of the therefore, the sum of the areas o method createList is proportiona number of
operations will quadr problem size. Notes (Big O) We express complexity using bi • A constant-time
method • A linear-time method is on 015-2016 )? That would mean that doubling N would dou teList.
Here's a table showing the value of 0+1+ N 0+1+2+...+(N-1) 4 6 8 28 16 120 does more than double when
the value of N d ng graph, the bars represent the lengths of the li ...+ (N-1)) is the sum of the areas of the
individ the square. The whole square is an N-by-an show that T (N) is O (N2) values of N greater than 2: )
by choosing c = 4 and 3 * N2 + 5 <= 4 * N2 T(N) is not O(N), because whate N greater than n0 so that 3 *
N2 atever constant c and value n0 you choose, I ca + 5 is greater than c * N. can always find a value of
PROBLEM (4) Objective How to determine the ru kinds of statements are used. running time of a piece
of code? You will find d that it depends on what Problem Statemns) then the time fo also constant: O (1).
• Sample code2: if-then-e else statements Either sequence 1 will execu slower of the two possibilities:
Max (time (sequence 1), time (s For example, if sequence 1 is O else statement would be O (N). oop like
the N calls tements: k) for each statement is cute, or sequence 2 will execute. Therefore, the the worst-
case time is the sequence 2)) O (N) and sequence 2 is O (1) the worst-case ti time for the whole if-then •
Sample code3: for loops ps The loop executes N times, s the statements are O (1), the tota s, so the
sequence of statements also executes N otal time for the for-loop is N * O (1), which is • Sample code4:
Nested lo o Nested loop 1 d loops Here the number of itera index. The outer loop executes N times. As a
result, the statements complexity is O (N * M). In a co < N instead of j < M (i.e., the inn is O (N2). s N
times. Since we assume is O (N) overall. rations of the inner loop is independent of the v s N times. Every
time the outer loop executes, t nts in the inner loop execute a total of N * M ti common special case
where the stopping cond inner loop also executes N times), the total com e value of the outer loop's s,
the inner loop executes M times. Thus, the ndition of the inner loop is j omplexity for the two loops o
Nested loop2 In this code the number o index. Now we can't just multipl r of iterations of the inner loop
depends on the iply the number of iterations of the outer loop t 27 he value of the outer loop's p times
the number of Cairo University Faculty of Computers and Information CS214-Data Structures, 2nd Term,
201 on 015-2016 iterations of the inner loop, beca let's think about how many itera table: cause the
inner loop has a different number of rations that inner loop has. That information is V Value of i 0 0 1 1 2
2 ... .. N N-2 N N-1 N N-1 N-2 ... 2 1 So we can see that the total num ... + 3 + 2 + 1. We've seen that f f
iterations each time. So is given in the following Number of iterations of inner loop mber of times the
sequence of statements exec t formula before: the total is O (N2). ecutes is: N + N-1 + N-2 + PROBLEM (5)
Objective Find worst-case complex lexity for a code. Problem Statement What is the worst-case complex
exity of the each of the following code fragmen
This is code that really is exa to add shown above. The total ti Total time = time (statement 1) + If each
statement is "simple" (on constant and the total time is als exactly k statements; this is not an unrolled
loop l time is found by adding the times for all statem ) + time (statement 2) + ... + time (statement k)
(only involves basic operations) then the time fo also constant: O (1). • Sample code2: if-then-e else
statements Either sequence 1 will execu slower of the two possibilities: Max (time (sequence 1), time (s
For example, if sequence 1 is O else statement would be O (N). oop like the N calls tements: k) for each
statement is cute, or sequence 2 will execute. Therefore, the the worst-case time is the sequence 2)) O
(N) and sequence 2 is O (1) the worst-case ti time for the whole if-then • Sample code3: for loops ps The
loop executes N times, s the statements are O (1), the tota s, so the sequence of statements also
executes N otal time for the for-loop is N * O (1), which is • Sample code4: Nested lo o Nested loop 1 d
loops Here the number of itera index. The outer loop executes N times. As a result, the statements
complexity is O (N * M). In a co < N instead of j < M (i.e., the inn is O (N2). s N times. Since we assume is
O (N) overall. rations of the inner loop is independent of the v s N times. Every time the outer loop
executes, t nts in the inner loop execute a total of N * M ti common special case where the stopping
cond inner loop also executes N times), the total com e value of the outer loop's s, the inner loop
executes M times. Thus, the ndition of the inner loop is j omplexity for the two loops o Nested loop2 In
this code the number o index. Now we can't just multipl r of iterations of the inner loop depends on the
iply the number of iterations of the outer loop t 27 he value of the outer loop's p times the number of
Cairo University Faculty of Computers and Information CS214-Data Structures, 2nd Term, 201 on 015-
2016 iterations of the inner loop, beca let's think about how many itera table: cause the inner loop has a
different number of rations that inner loop has. That information is V Value of i 0 0 1 1 2 2 ... .. N N-2 N
N-1 N N-1 N-2 ... 2 1 So we can see that the total num ... + 3 + 2 + 1. We've seen that f f iterations each
time. So is given in the following Number of iterations of inner loop mber of times the sequence of
statements exec t formula before: the total is O (N2). ecutes is: N + N-1 + N-2 + PROBLEM (5) Objective
Find worst-case complex lexity for a code. Problem Statement What is the worst-case complex exity of
the each of the following code ). In a co < N instead of j < M (i.e., the inn is O (N2). s N times. Since we
assume is O (N) overall. rations of the inner loop is independent of the v s N times. Every time the outer
loop executes, t nts in the inner loop execute a total of N * M ti common special case where the stopping
cond inner loop also executes N times), the total com e value of the outer loop's s, the inner loop
executes M times. Thus, the ndition of the inner loop is j omplexity for the two loops o Nested loop2 In
this code the number o index. Now we can't just multipl r of iterations of the inner loop depends on the
iply the number of iterations of the outer loop t 27 he value of the outer loop's p times the number of
Cairo University Faculty of Computers and Information CS214-Data Structures, 2nd Term, 201 on 015-
2016 iterations of the inner loop, beca let's think about how many itera table: cause the inner loop has a
different number of rations that inner loop has. That information is V Value of i 0 0 1 1 2 2 ... .. N N-2 N
N-1 N N-1 N-2 ... 2 1 So we can see that the total num ... + 3 + 2 + 1. We've seen that f f iterations each
time. So is given in the following Number of iterations of inner loop mber of times the sequence of
statements exec t formula before: the total is O (N2). ecutes is: N + N-1 + N-2 + PROBLEM (5) Objective
Find worst-case complex lexity for a code. Problem Statement What is the fragmen

You might also like