Uploaded by Lewis Hamilton

cmput204 week 1 lecture 2 RAM InsertionSort

advertisement
CMPUT 204
ALGORITHMS I
Section B2, Winter 2022
Xiaoqi Tan
Recap: What is An Algorithm?
A
D
C
B
X
Y
2
A
B
C
D
Recap: Three Things to Argue
A
D
C
B
X
Y
A
B
C
D
Correctness
Amount of resources
Can we do better?
Is this algorithm doing
How much time and space do we
Can we use less time
what it is supposed to do?
need to run this algorithm?
and/or less space?
3
Recap: Recursive Algorithms
Its correctness can be proved by induction
4
Recap: Induction
Must have
all the time
A well-de ned
induction hypothesis
Alternatives
fi
5
Lecture 2
Pseudo code; Random access machine;
Example: Insertion sort
Pseudo Code
This is the keyword!!
7
Example: Fibonacci Numbers
8
Fibonacci: Recursive Implementation
Recursive implementation
1
9
0
What is the # of Recursive Calls?
Fibonacci numbers
10
Prove this by induction by yourself
Fibonacci: # of Recursive Calls
Recursive implementation
T1(n)
Exponential in n
11
Fibonacci: Other Implementations
Another non-recursive implementation
Non-recursive implementation
12
b1
b3
Exponential
Linear
Linear
b2
Time ef ciency
13
fi
fi
fi
fi
Fibonacci: Which is Better?
Things to Argue About Algorithms
A
D
C
B
X
Y
A
B
C
D
Correctness
Amount of resources
Can we do better?
Is this algorithm doing
How much time and space do we
Can we use less time
what it is supposed to do?
need to run this algorithm?
and/or less space?
14
fi
fi
Fibonacci: Which is Better?
b3
# of integers stored in
memory: linear in n
# of integers
stored in memory:
a few constant
b2
15
Is There Any Problem?
# of recursive
call
f
o
#
Exponential
# of for
-loop c
all
Linear
Linear
This measurement depends on how we
implement the recursion and for-loops!!
16
p
o
o
l
r
o
f
l
l
ca
Running Time (RT) of Algorithms
Also depends on hardware!!
We need an analytic way of measuring
RT independent of environment factors
(CPU speed, compiler, implementation)
RT: 10 days
RT: 10 minutes
RT: 10 seconds
17
Model of Computation
These instructions are
assumed to run in a
constant amount of time
Primitive operations
High-level programming language
Low-level (machine) language
What operations your computers can do in a single instruction
18
Random Access Machine (RAM)
Random access
memory (RAM)
I/O
CPU
Memory
Holds the program
19
RAM: Key Assumptions
I/O
CPU
Memory
Holds the program
Primitive operations take 1 time step
Loops count based on # of times executed
does not depend on
Memory access is instant physical location
“All models are wrong, some are useful”
20
Primitive operations
Example: Insertion Sort
21
6
5
3
1
8
7
2
4
1
2
3
4
5
6
7
8
Insertion Sort: Pseudo Code
Insertion
22
Insertion Sort: Running Trace
While-loop
While-loop
The steps inside the while-loop are not executed
23
Insertion Sort: Running Trace (Cont’d)
While-loop
While-loop
24
Insertion Sort: Running Time (RT)
The while-loop test is executed once
The while-loop test is executed 4 times
Expectation
25
Reality
Let’s Do Some Calculations First
# of times the whileloop test is executed
for-loop test: 2,3,⋯, n, n + 1
t4 = 1
t5 = 4
26
RT Analysis in Different Cases
27
RT of Insertion Sort
t4 = 1
The while-loop test
is executed once
Prove this by
yourself
tj ≈ j/2
The constant 2
is not important
28
Model of
computation
Summary
Model of
describing
algorithms
What operations
your computers
can do in a single
instruction with
unit cost
Pseudo code; Random access machine;
Example: Insertion sort
RT analysis: worst-case,
average-case, and best-case
Next Week
Loop invariants
Download