Lec 2
Lec 2
Lec 2
and Computability
Agenda
1- Python:
Overview, Hello, World!, Variables and Types, Lists, Basic Operators,
String Formatting, Basic String Operations, Conditions, Loops,
Functions, Classes and Objects, Dictionaries, Modules and Packages
2- What is computation and computational thinking?
3- Complexity analysis:
• Asymptotic performance
• Asymptotic (big-O) notation
• Analysis of Algorithms
Self-Learning Resources
Interactive tutorials
https://www.programiz.com/python-
1 programming
http://www.learnpython.org/
2
https://python-
3 course.eu/python3_course.php
https://www.tutorialspoint.com/pyth
4 on/
Self-Learning Resources
books
• Tutorial 1 Series:
https://www.youtube.com/watch?v=P74
JAYCD45A&list=PLGzru6ACxEALhcvY1
8A-iox-mEoieHMVG
• Tutorial2 Series:
https://www.youtube.com/watch?v=HB
xCHonP6Ro&list=PL6gx4Cwl9DGAcbMi
1sH6oAMk4JHw91mC_
• extension-language work
• rapid prototyping and development
• language-based modules
• instead of special-purpose parsers
http://www.learnpython.org/
What is Computation?
What does each of them mean (try to write something down in your own words,
without looking them up)?
• Computational thinking
• Computational problem
Answer:
• computational thinking as not merely knowing how to use an algorithm or
a data structure, but, when faced with a problem, to be able to analyse it
with the techniques and skills that computer science puts at our disposal.
A computational thinker won’t, however, be satisfied with just any solution: the solution has
to be a ‘good’ one. You have already seen that some solutions for finding a word in a
dictionary are much better (in particular, faster) than others. The search for good
computational solutions is a theme that runs throughout this module. Finally,
computational thinking goes beyond finding solutions: if no good solution exists, one
should be able to explain why this is so. This requires insight into the limits of computational
problem solving.
Analysis of Algorithms
17
Our goal:
Analysis of Algorithms
Measure Performance
Measure Space Complexity
Running Time
• Most algorithms transform best case
input objects into output average case
objects. worst case
Running Time
80
8000
implementing the algorithm
• Run the program with inputs 7000
Time (ms)
composition, noting the time 5000
needed:
4000
3000
2000
1000
0
• Plot the results 0 50 100
Input Size 21
Limitations of Experiments
22
So we need another way to measure the
performance of the algorithms
• So we need to learn about Theoretical analysis or
Asymptotic analysis.
• Uses a high-level description of the algorithm
instead of an implementation (Pseudo code).
• Characterizes running time as a function of the
input size, n.
• Takes into account all possible inputs.
• Allows us to evaluate the speed of an algorithm
independent of the hardware/software
environment.
Pseudo code
• High-level description of an
algorithm.
• More structured than English prose.
• Less detailed than a program.
• Preferred notation for describing
algorithms.
• Hides program design issues.
Big-Oh Notation
• Given functions f(n) and g(n),
we say that f(n) is O(g(n)) if
there are positive constants
c and n0 such that
f(n) cg(n) for n n0
• Example: 2n + 10 is O(n)
• 2n + 10 cn
• (c − 2) n 10
• n 10/(c − 2)
• Pick c = 3 and n0 = 10
24
Big-Oh and Growth Rate
• The big-Oh notation gives an upper bound on the growth
rate of a function
• The statement “f(n) is O(g(n))” means that the growth rate
of f(n) is no more than the growth rate of g(n)
• We can use the big-Oh notation to rank functions according
to their growth rate
big-Theta
◼ f(n) is (g(n)) if there are constants c’ > 0 and c’’
> 0 and an integer constant n0 1 such that
c’•g(n) f(n) c’’•g(n) for n n0
26
Essential Seven functions to estimate algorithms
performance
g(n) = 1
Print(“Hello Algorithms”)
Essential Seven functions to estimate algorithms
performance
g(n) = n
Def power_of_2(a):
x = 0
g(n) = lg n while a > 1:
a = a/2
x = x+1
return x
Essential Seven functions to estimate algorithms
performance
for i in range(0,n):
Def power_of_2(a):
x = 0
while a > 1:
a = a/2
x = x+1
return x
g(n) = n lg n
Essential Seven functions to estimate algorithms
performance
for i in range(0,n):
for j in range(0,n):
print(i*j);
g(n) = n2
Essential Seven functions to estimate algorithms
performance
g(n) = n3
Essential Seven functions to estimate algorithms
performance
def F(n):
if n == 0:
return 0
elif n == 1:
return 1
g(n) = 2n else: return
F(n-1) + F(n-2)
Seven Important Functions
• Seven functions that often appear in
algorithm analysis:
• Constant 1
• Logarithmic log n
• Linear n
• N-Log-N n log n
• Quadratic n2
• Cubic n3
• Exponential 2n
c n2 ~ c n2 + 2c n 4c n2 16c n2
c n3 ~ c n3 + 3c n2 8c n3 64c n3
c 2n c 2 n+1 c 2 2n c 2 4n
Comparison of Two Algorithms
insertion sort is
n2 / 4
merge sort is
2 n lg n
sort a million items?
insertion sort takes
roughly 70 hours
while
merge sort takes
roughly 40 seconds
39
Prefix Averages 2 (Looks Better)
The following algorithm uses an internal Python
function to simplify the code