Date post: | 04-Jun-2018 |
Category: |
Documents |
Upload: | frankjamison |
View: | 216 times |
Download: | 0 times |
of 30
8/13/2019 02a-RunningTime
1/30
Dr. Ahmad R. Hadaegh
A.R. Hadaegh National University Page 1
Algorithm Analysis:
Running TimeBig Oand omega (
8/13/2019 02a-RunningTime
2/30
Dr. Ahmad R. Hadaegh
A.R. Hadaegh National University Page 2
Introduction
An algorithm analysis of a program is a step by step procedurefor accomplishing that program
In order to learn about an algorithm, we need to analyze it
This means we need to study the specification of the algorithmand draw conclusion about the implementation of that algorithm(the program) will perform in general
8/13/2019 02a-RunningTime
3/30
Dr. Ahmad R. Hadaegh
A.R. Hadaegh National University Page 3
The issues that should be considered in analyzing an algorithmare:
The running time of a program as a function of its inputs
The total or maximum memory space needed for programdata
The total size of the program code
hether the program correctly computes the desired result
The complexity of the program! "or example, how easy it is
to read, understand, and modify the program
The robustness of the program! "or example, how well does itdeal with unexpected or erroneous inputs
8/13/2019 02a-RunningTime
4/30
Dr. Ahmad R. Hadaegh
A.R. Hadaegh National University Page 4
In this course, we consider the running time of the algorithm!
The main factors that effect the running time are the algorithm
itself, input data, the computer system, etc!
The performance of a computer is determined by The hardware The programming language used and
The operating system
To calculate the running time of a general #$$ program, we firstneed to define a few rules!
In our rules , we are going to assume that the effect of hardwareand software systems used in the machines are independent ofthe running time of our #$$ program
8/13/2019 02a-RunningTime
5/30
Dr. Ahmad R. Hadaegh
A.R. Hadaegh National University Page 5
Rule 1:
The time re%uired to fetch an integer from memory is aconstant t(fetch),
The time re%uired to store an integer in memory is also aconstant t(store)
"or example the running time of
x y
is:
t(!etch" # t(store"
because we need to fetch y from memory store it into x
&imilarly the running time ofx 1
is also t(!etch" # t(store"because typically any constant is
stored in the memory before it is fetched!
8/13/2019 02a-RunningTime
6/30
8/13/2019 02a-RunningTime
7/30Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page %
Rule 3: The time re%uired to call a function is a constant, t(call)
And the time re%uired to return a function is a constant, t(return)
Rule 4:
The time re%uired to pass an integer argument to a function orprocedure is the same as the time re%uired to store an integer inmemory, t(store)
8/13/2019 02a-RunningTime
8/30Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page &
"or example the running time of
y !(x"
is:
t(!etch" # 2t(store" # t(call" # t(!(x""
.ecause you need To fetch the alue of x: t (fetch) /ass x to the function and store it into parameter: t (store)
#all the function f(x): t (call) 0un the function: t (f(x)) &tore the returned result into y: t (store)
8/13/2019 02a-RunningTime
9/30Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page '
Rule 5:
The time re%uired for the address calculation implied by an arraysubscripting operation li1e a2i3 is a constant, t(2 3)!
This time does not include the time to compute the subscriptexpression, nor does it include the time to access (fetch or store)the array element
"or example, the running time of
y ai)
is:
3t(!etch" # t( )" # t(store"
.ecause you need
To fetch the alue of i: t(fetch)To fetch the alue of a: t(fetch)
To find the address of a2i3: t(2 3)
To fetch the alue of a2i3: t(fetch)
To store the alue of a2i3 into y: t(store)
8/13/2019 02a-RunningTime
10/30Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 1*
Rule $: The time re%uired to calculate a fixed amount of storage from the
heap using operator newis a constant, t(new) This time does not include any time re%uired for initialization ofthe storage (calling a constructor)!
&imilarly, the time re%uired to return a fixed amount of storage
to the heap using operator deleteis a constant, t(delete)! This time does not include any time spent cleaning up the
storage before it is returned to the heap (calling destructor)
"or example the running time of
int+ ,tr ne- int.
is:
t(ne-" # t(store"
.ecause you needTo allocate a memory: t(new)And to store its address into ptr: t(store)
"or example the running time of
delete ,tr.
is:
t(!etch " # t(delete"
.ecause you needTo fetch the address from ptr : t(fetch)And delete specifies location: t(delete)
8/13/2019 02a-RunningTime
11/30Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 11
! int &um (int n)-! 45! int result 6789! for (int i68 i6n8 i6i$);! result 6 result $ i8
/tatement Time 0ode
5 t(fetch)$ t(store) result 6 7
9a t(fetch) $ t(store) i 6
9b (-t(fetch)$t(cmp)) (n$) i6n
9c (-t(fetch)$t($) $t(store)) n $$i
; (-t(fetch)$t($) $t(store)) n 0esult $6i
< t(fetch)$ t(return) 0eturn result
Total 2
8/13/2019 02a-RunningTime
12/30Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 12
! int func (int a2 3, int n, int x)-! 45! int result 6 a2n389! for (int i6n'8 i?678 i6i');! result 6result x $ a2i38
/tatement Time5 5t(fetch)$ t(2 3) $ t(store)
9a -t(fetch) $ t(') $ t(store)
9b (-t(fetch)$t(cmp)) (n$)
9c (-t(fetch)$t(') $t(store)) n
; (;t(fetch)$t(2 3)$t($)$t()$t(store)) n
< t(fetch)$ t(return)
Total 2(@t(fetch) $-t(store) $ t(cmp) $t(23) $ t() $ t(')3n$ 2(t(fetch) $ -t(store) $ t(23) $ t(') $t(cmp) $ t (return) )3
8/13/2019 02a-RunningTime
13/30Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 13
Bsing constant times such as t(fetch), t(store), t(delete), t(new), t($),C, ect ma1es our running time accurate
Doweer, in order to ma1e life simple, we can consider theapproximate running time of any constant to be the same time ast()!
"or example, the running time of
y x # 1
is 5 because it includes two EfetchesF and one EstoreF in which allare constants
"or a loop there are two cases: If we 1now the exact number of iterations, the running timebecomes constant t()
If we do not 1now the exact number of iterations, the runningtime becomes t(n) where nis the number of iterations
8/13/2019 02a-RunningTime
14/30Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 14
! int Geometric (int x, int n)
-! 45! int sum 6 789! for (int i678 i6n8 $$i);! 4
=! for (int H678 Hi8 $$H)! prod 6 prod x8@! sum 6 sum $ prod87! >! return result
-! >
/tatement Time
5 -
9a -
9b 5(n$-)
9c 9(n$)
< -(n$)
=a -(n$)
=b
=c
@ 9(n$)
-
Total (*-)n-$ (9=*-)n $ -=
i67n
9 i
i67n
9 i
i67n5 i$
8/13/2019 02a-RunningTime
15/30Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 15
! int /ower (int x, int n)-! 45! if (n6 67)9! return 8
;! else if (n- 6 6 7) ** n is een
/tatement
n* n* (n is een" n* (n is odd"
5 5 5 5
9 - ' '
; ' ; ;< ' 7 $ T( n*- ) '
' ' - $ T( n*- )
Total ; $ T( n*- ) -7 $ T( n*- )
8/13/2019 02a-RunningTime
16/30Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 1$
5 !or n*T(n" 1T( n2 " !or n* and n is een
2*#T( n2 " !or n * and n is odd
&uppose n 6 -1for some 1?7!
Jbiously -1is an een number, we get
T(2" 1& # T(21" 1& # (1& # T(22"" 1& # 1& # (1& # T(23"" 677 677 1& # T(2" 1& # T(2*" 1& # T(1"
8/13/2019 02a-RunningTime
17/30Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 1%
&ince T() is add number, the running time of T() is:
T(1" 2* # T(*" 2* # 5 25
Therefore,T(2" 1& # 25
If n 6 -1, then log n 6 log-1indicating that 1 6 log n
Therefore,T(n" 1&log n # 25
8/13/2019 02a-RunningTime
18/30Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page1&
Asym,totic 8otation
&uppose the running time of two algorithms A and . are TA(n) and
T.(n), respectiely wherenis the size of the problem
Dow can we determine TA(n) is better than T.(n)K
Jne way to do that is if we 1now the size nahead of time for some
n=no! Then we may say that algorithm A is performing better thanalgorithm . for n= n
o
.ut this is a special case for n=no! Dow about n = n
1, or n=n
2K Is A
better than . for other cases tooK
Bnfortunately, this is not an easy answer! e cannot expect that thesize of nto be 1nown ahead of time! .ut we may be able to say thatunder certain situations TA(n) is better than T.(n) for all n ?6 n
8/13/2019 02a-RunningTime
19/30Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 1'
To understand the running times of the algorithms we need toma1e some definitions:
9e!inition: #onsider a function f(n) which is non'negatie for all
integers n?67! e say that f(n) is big oh of g(n)whichwe write (f(n) is !(g(n)) if there exists an integer noand a
constant c ? 7 such that for all integers n ?6no,
f(n) 6c g(n)
xam,le: &how that f(n) 6 n $ - is J(n-)
n $ - 6 c n- (lets set c 6 )7 6 cn-'n '-7 6 (n'
8/13/2019 02a-RunningTime
20/30Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 2*
g5(n)6n-g-(n)6-n
-g(n)69n-
f(n)6n$-
; 7 ; -7
-77
977
n
f(n)
8/13/2019 02a-RunningTime
21/30Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 21
Theorem: If f(n) is J(g(n)) and f-(n) is J(g-(n)), then
f(n) $ f-(n) 6 J (max(g(n), g-(n)))
;roo!:
If f(n) is J(g(n)) then f(n) 6 cg(n) for some cand n ?6 n
If f-(n) is J(g-(n)) then f-(n) 6 c-g-(n) for some c-and n ?6 n-
Let no6 max(n, n-) and co6 -(max(c, c-)), consider the sum f(n)
$ f-(n) for some n ?6 no
f(n) $ f-(n) 6 cg(n) $ c-g-(n)
6 co(g(n) $ g-(n) )*-
6 co(max (g(n), g-(n))
Therefore, f(n) $ f-(n) is J (max(g(n), g-(n)) )
8/13/2019 02a-RunningTime
22/30Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 22
Theorem: If f(n) is J(g(n)) and f-(n) is J(g-(n)), then
!1(n" + !2(n" O(g1(n"+g2(n" "
;roo!:
If f(n) is J(g(n)) then f(n) 6 cg(n) for some cand n?6n
If f-(n) is J(g-(n)) then f-(n) 6 c-g-(n) for some c-and n?6n-
Let no6 max(n, n-) and co6 cc-, consider the product of
f(n)f-(n) for some n?6no
f(n) f-(n) 6 cg(n) c-g-(n)
6 co(g(n) g-(n) )
Therefore, f(n) f-(n) is J (g(n)g-(n) )
8/13/2019 02a-RunningTime
23/30Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 23
Theorem: If f(n) is J(g(n)) and g(n) is J(h(n)), then
!(n" is O(h(n""
;roo!:
If f(n) is J(g(n)) then f(n) 6 cg(n) for some cand n?6n If g(n) is J(h(n)) then g(n) 6 c-h(n) for some c-and n?6n-
Let no6 max(n, n-) and co6 cc-, then
f(n) 6 cg(n)
6 cc-h(n)
6 coh(n)
Therefore, f(n) is J (h(n))
8/13/2019 02a-RunningTime
24/30Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 24
The names o! common
8/13/2019 02a-RunningTime
25/30Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 25
0onentions !or -riting Big Oh x,ression
#ertain conentions hae eoled which concern how big ohexpression normally written:
"irst, it is common practice when writing big oh expression
to drop all but the most significant items! Thus instead ofJ(n-$ nlogn $ n) we simply write J(n-)
&econd, it is common practice to drop constant coefficients!
Thus, instead of J(5n-), we write J(n-)! As a special caseof this rule, if the function is a constant, instead of, sayJ(7-9), we simply write J()
8/13/2019 02a-RunningTime
26/30
Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 2$
Asym,totic =o-er Bound ("9e!inition:
#onsider a function f(n), which is non'negatie for allintegers n?67! e say that f(n) is omega of g(n) whichwe write(f(n) is (g(n))"if there exists an integer noand a
constant c ? 7 such that for all integers n?6 no, f(n) ?6 c g(n)
xam,le: &how that f(n) 6 ;n-'
8/13/2019 02a-RunningTime
27/30
Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 2%
f(n)6n-
f(n)6-n-
f(n)6;n-'
8/13/2019 02a-RunningTime
28/30
Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 2&
Other de!initions9e!inition:
#onsider a function f(n) which is non'negatie for allintegers n?67! e say that f(n) is theta of g(n) which wewrite(f(n) is (g(n)) if and only if f(n) is J(g(n)) and f(n) is(g(n))
9e!inition: #onsider a function f(n) which is non'negatie for all
integers n?67! e say that f(n) is little o of g(n) which wewrite(f(n) is o(g(n))if and only if f(n) is J(g(n)) and f(n) isnot (g(n))
Oow lets consider some of the preious examples in terms ofthe big J notations:
8/13/2019 02a-RunningTime
29/30
Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 2'
! int func (int a2 3, int n, int x)-! 45! int result 6 a2n389! for (int i6n'8 i?678 ''i)
;! result 6result x $ a2i38
/tatement /im,le
Timemodel
Big O
5 ; J()9a 9 J()9b 5n $ 5 J(n)9c 9n J(n); @n J(n)< - J()
Total
8/13/2019 02a-RunningTime
30/30
! int /refix&ums (int a2 3, int n)-! 45! for (int H6n'8 i?678 ''H)9! 4;! int sum 6 787! return result! >
/tatement Big O
5a J()5a J()J(n)
5c J()J(n); J()J(n)