What Is Algorithm?: - A Clearly Specified To Be Followed To Solve A Problem
What Is Algorithm?: - A Clearly Specified To Be Followed To Solve A Problem
• What is Algorithm?
– a clearly specified set of simple instructions to be followed to solve a
problem
• Takes a set of values, as input and
• produces a value, or set of values, as output
– May be specified
• In English
• As a computer program
• As a pseudo-code
• Data structures
– Methods of organizing data
• Program = algorithms + data structures
i 1
1
1
2 2N+2
3 4N
4 1
• f(N) = O(g(N))
• There are positive constants c and n0 such that
f(N) c g(N) when N n0
• f(N) = 3 𝑁 2 + 5.
• We can show that f(N) is O(N*2) by choosing
• F(n) <= c g(n)
• 3𝑁 2 +5 <= c 𝑁 2 c=4 𝑛0 =0 5<=0 false
• c = 4 and 𝑛0 = 3. c=4 𝑛0 =1 8<=4 false
c=4 𝑛0 =2 17 <= 16
c=4 𝑛0 =3 32<=36
n=10 305<=400
Lecture 1 Algorithms Analysis 19
• f(N) is not O(N), because whatever constant c
and value n0 you choose, There is always
• a value of N > 𝑛0 such that
(3𝑁 2 +5 ) > c (N)
i 1
N
i 2
N N 2
O ( N 3
)
• log N + N = O(N)
• logk N = O(N) for any constant k
• N = O(2N), but 2N is not O(N)
• 210N is not O(2N)
Lecture 1 Algorithms Analysis 22
Some rules
• f(N) = (g(N))
• There are positive constants c and n0 such that
f(N) c g(N) when N n0
• the growth rate of f(N) is the same as the growth rate of g(N)
• f(N) = (g(N)) if
f(N) = O(g(N)) and f(N) = (g(N))
• The growth rate of f(N) equals the growth rate of
g(N)
• Example: Let f(N)=N2 , g(N)=2N2
– Since f(N) = O(g(N)) and f(N) = (g(N)),
thus f(N) = (g(N)).
• Big-Theta means the bound is the tightest
possible.
Lecture 1 Algorithms Analysis 28
Some rules
Loops
for I in 1 .. N loop
sequence of statements
end loop;
The loop executes N times, so the sequence of
statements also executes N times. If we assume
the statements are O(1), the total time for the
for loop is N * O(1), which is O(N) overall.
Lecture 1 Algorithms Analysis 36
Determine Complexity
Nested loops
for I in 1 .. N loop
for J in 1 .. M loop
sequence of statements
end loop;
end loop;