Title: COMP108%20Algorithmic%20Foundations%20Basics
1COMP108Algorithmic FoundationsBasics
Prudence Wong http//www.csc.liv.ac.uk/pwong/tea
ching/comp108/201112
2Crossing Bridge _at_ Night
each time, 2 persons share a torch they walk _at_
speed of slower person
Can you do it in 17 mins?
Target all cross the bridge
3Module Information
- Dr Prudence Wong
- Rm 3.18 Ashton Building, pwong_at_liverpool.ac.uk
- office hours Wed 10-11am, Thu 3-4pm
- Demonstrators
- Mr Mihai Burcea, Mr Jude-Thaddeus Ojiaku
- References
- Main Introduction to the Design and Analysis of
Algorithms. A. V. Levitin. Addison Wesley. - Reference Introduction to Algorithms. T. H.
Cormen, C. E. Leiserson, R. L. Rivest, C. Stein.
The MIT Press
4Module Information (2)
- Teaching, Assessments and Help
- 33 lectures, 11 tutorials
- 2 assessments (20), 1 written exam (80)
- Office hours, email
- Tutorials/Labs
- Location
- Seminar Rooms G12 or 2.23 (theoretical) or
- Labs 1, 2, 3 (practical)
- Week 2 Seminar Rooms, G12 Ashton Bldg or 2.23
Holt Bldg - Class Tests Weeks 7 11 (15 Mar 3 May)
5Why COMP108?
- Pre-requisite for COMP202, COMP218(COMP202 is
pre-requisite for COMP309) - Algorithm design is a foundation for efficient
and effective programs - "Year 1 modules do not count towards honour
classification" - (Career Services Employers DO consider year 1
module results)
6Aims
- To give an overview of the study of algorithms in
terms of their efficiency. - To introduce the standard algorithmic design
paradigms employed in the development of
efficient algorithmic solutions. - To describe the analysis of algorithms in terms
of the use of formal models of Time and Space.
What do we mean by good?
How to achieve?
Can we prove?
7Ready to start
- Learning outcomes
- Able to tell what an algorithm is have some
understanding why we study algorithms - Able to use pseudo code to describe algorithm
8What is an algorithm?
- A sequence of precise and concise instructions
that guide you (or a computer) to solve a
specific problem - Daily life examples cooking recipe, furniture
assembly manual(What are input / output in each
case?)
Algorithm
Input
Output
9Why do we study algorithms?
The obvious solution to a problem may not be
efficient
- Given a map of n cities traveling cost between
them. - What is the cheapest way to go from city A to
city B?
B
- Simple solution
- Compute the cost of each path from A to B
- Choose the cheapest one
A
10Shortest path to go from A to B
The obvious solution to a problem may not be
efficient
- How many paths between A B? involving 1
intermediate city?
- Simple solution
- Compute the cost of each path from A to B
- Choose the cheapest one
B
2
A
11Shortest path to go from A to B
The obvious solution to a problem may not be
efficient
- How many paths between A B? involving 3
intermediate cities?
- Simple solution
- Compute the cost of each path from A to B
- Choose the cheapest one
B
2
Number of paths 2
A
12Shortest path to go from A to B
The obvious solution to a problem may not be
efficient
- How many paths between A B? involving 3
intermediate cities?
- Simple solution
- Compute the cost of each path from A to B
- Choose the cheapest one
B
3
Number of paths 2
Number of paths 2 3
A
13Shortest path to go from A to B
The obvious solution to a problem may not be
efficient
- How many paths between A B? involving 3
intermediate cities?
- Simple solution
- Compute the cost of each path from A to B
- Choose the cheapest one
B
Number of paths 2 3
A
14Shortest path to go from A to B
The obvious solution to a problem may not be
efficient
- How many paths between A B? involving 3
intermediate cities?
- Simple solution
- Compute the cost of each path from A to B
- Choose the cheapest one
B
Number of paths 2 3
Number of paths 2 3 6 11
A
6
15Shortest path to go from A to B
The obvious solution to a problem may not be
efficient
- How many paths between A B? involving 5
intermediate cities?
TOO MANY!!
- Simple solution
- Compute the cost of each path from A to B
- Choose the cheapest one
B
For large n, its impossible to check all
paths! We need more sophisticated solutions
A
16Shortest path to go from A to B
There is an algorithm, called Dijkstra's
algorithm, that can compute this shortest path
efficiently.
B
A
17Idea of Dijkstra's algorithm
Go one step from A, label the neighbouring cities
with the cost.
B
10
0
5
A
7
18Idea of Dijkstra's algorithm
Choose city with smallest cost and go one step
from there.
B
10
0
5
A
7
19Idea of Dijkstra's algorithm
This path must NOT be used because we find a
cheaper alternative path
If an alternative cheaper path is found, record
this path and erase the more expensive one.
B
10
8
Then repeat repeat
0
5
A
7
20Shortest path to go from A to B
Lesson to learn Brute force algorithm may run
slowly. We need more sophisticated algorithms.
B
A
21How to represent algorithms
- Able to tell what an algorithm is and have some
understanding why we study algorithms - Able to use pseudo code to describe algorithm
22Algorithm vs Program
An algorithm is a sequence of precise and concise
instructions that guide a person/computer to
solve a specific problem
- Algorithms are free from grammatical rules
- Content is more important than form
- Acceptable as long as it tells people how to
perform a task - Programs must follow some syntax rules
- Form is important
- Even if the idea is correct, it is still not
acceptable if there is syntax error
23Compute the n-th power
- Input a number x a non-negative integer n
- Output the n-th power of x
- Algorithm
- Set a temporary variable p to 1.
- Repeat the multiplication p p x for n times.
- Output the result p.
24Pseudo Code
C p 1 for (i1 iltn i) p p
x printf("d\n", p)
pseudo code p 1 for i 1 to n do p p
x output p
C p 1 for (i1 iltn i) p p
x cout ltlt p ltlt endl
Pascal p 1 for i 1 to n do p p
x writeln(p)
Java p 1 for (i1 iltn i) p p
x System.out.println(p)
25Pseudo Code
suppose n4, x3
iteration i p
start 1
1 1 3
2 2 9
3 3 27
4 4 81
end 5
Another way to describe algorithm is by pseudo
code
p 1 for i 1 to n do p p x output p
trace table
similar to programming language
Combination of both
more like English
26Pseudo Code conditional
if a lt 0 then a -a b a output b
Conditional statement if condition then
statement if condition then statement else
statement
if a gt 0 then b a else b -a output b
What is computed?
absolute value
27Pseudo Code iterative (loop)
Iterative statement for var start_value to
end_value do statement while condition do
statement repeat statement until condition
condition to CONTINUE the loop
condition to STOP the loop
condition for while loop is NEGATION of
condition for repeat-until loop
28for loop
for var start_value to end_value do statement
Sum of 1st n nos. input n sum 0 for i 1 to
n do begin sum sum i end output sum
sum0
i1
No
i lt n?
ii1
Yes
sum sumi
the loop is executed n times
29for loop
for var start_value to end_value do statement
iteration i sum
start 0
1 1 1
2 2 3
3 3 6
4 4 10
end 5
suppose n4
Sum of 1st n nos. input n sum 0 for i 1 to
n do begin sum sum i end output sum
the loop is executed n times
30while loop
condition to CONTINUE the loop
while condition do statement
Sum of 1st n numbers input n sum 0 i 1
while i lt n do begin sum sum i i i
1 end output sum
do the same as for-loop in previous slides
31while loop example 2
execute undetermined number of times
Sum of all input numbers sum 0 while (user
wants to continue) do begin ask for a number
sum sum number end output sum
No
continue?
Yes
ask for number sum sumnumber
32repeat-until
repeat statement until condition
ask for number sum sumnumber
Stop?
No
condition to STOP the loop
Sum of all input numbers sum 0 repeat ask
for a number sum sum number until (user
wants to stop) output sum
Yes
- also execute undetermined number of times
- How it differs from while-loop?
execute at least once
33More Example 1
suppose m14, n4
(_at_ end of) iteration r q
14 0
1 10 1
2 6 2
3 2 3
input m, n r m q 0 while r ? n do begin
r r - n q q 1 end output r and q
remainder quotient
suppose m14, n5
(_at_ end of) iteration r q
1 9 1
2 4 2
suppose m14, n7
What is computed?
(_at_ end of ) iteration r q
1 7 1
2 0 2
34More Example 2
suppose m12, n4
(_at_ end of ) iteration output (this iteration) i
4
1 4 3
2 2
3 2 1
4 1 0
all common factors
input m, n if m lt n then swap m n i
n while i ? 1 do begin if mi0 ni0
then output i i i-1 end
suppose m15, n6
6
1 5
2 4
3 3
4 3 2
5 1
6 1 0
ab remainder ofa divided b
What values are output?
35More Example 3
Highest Common Factor (HCF) Greatest Common
Divisor (GCD)
input m, n if m lt n then swap m n i
n found false while i ? 1 !found do begin
if mi0 ni0 then found true
else i i-1 end output i
What value is output?
- Questions
- what value of found makes the loop stop?
- when does found change to such value?
36Pseudo Code Exercise
assuming x and y are both integers
- Write a while-loop to
- Find the product of all integers in interval x,
y - e.g., if x is 2 y is 5, then output is 2345
120
product ?? i ?? while ?? do begin ??
i ?? end output ??
37Pseudo Code Exercise
assuming x and y are both integers
- Write a while-loop to
- Find the product of all integers in interval x,
y
product 1 i x while i lt y do begin
product product i i i1 end output
product
38Pseudo Code Exercise 2
- Write a while-loop for this
- Given two positive integers x and y, list all
factors of x which are not factors of y - if x is 30 y is 9, output is 2, 5, 6, 10, 15,
30 (not 1 or 3)
i ?? while ?? do begin if ?? then
output ?? i ?? end
39Pseudo Code Exercise 2
- Write a while-loop for this
- Given two positive integers x and y, list all
factors of x which are not factors of y
i 1 while i lt x do begin if xi 0
yi ! 0 then output i i i1 end
40Challenges
- Convert while-loops to for-loops repeat-loop
41Convert to for loops
- Find the product of all integers in interval x,
y assuming x and y are both integers - product ??
- for i ?? to ?? do
- begin
- ??
- end
- output ??
42Convert to for loops
- Find the product of all integers in interval x,
y assuming x and y are both integers - product 1
- for i x to y do
- begin
- product product i
- end
- output product
43Convert to repeat loops
- Find the product of all integers in interval x,
y assuming x and y are both integers - product 1
- i x
- repeat
- product product i
- i i1
- until i gt y
- output product
Whats wrong with this? If given x is larger than
y?
44Convert to repeat loops
- Find the product of all integers in interval x,
y assuming x and y are both integers - product 1
- if x lt y then begin
- i x
- repeat
- product product i
- i i1
- until i gt y
- end
- output product
45Convert to for loops (2)
- Given two positive integers x and y, list all
factors of x which are not factors of y - for i ?? to ?? do
- begin
- if ?? then
- ??
- end
46Convert to for loops (2)
- Given two positive integers x and y, list all
factors of x which are not factors of y - for i 1 to x do
- begin
- if xi 0 yi ! 0 then
- output i
- end
47Convert to repeat loops (2)
- Given two positive integers x and y, list all
factors of x which are not factors of y - i ??
- repeat
- if ?? then
- ??
- i ??
- until ??
48Convert to repeat loops (2)
- Given two positive integers x and y, list all
factors of x which are not factors of y - i 1
- repeat
- if xi0 yi!0 then
- output i
- i i1
- until i gt x
49Searching
50Searching
- Input n numbers a1, a2, , an and a number X
- Output determine if X is in the sequence or not
- Algorithm (Sequential search)
- From i1, compare X with ai one by one as long as
i lt n. - Stop and report "Found!" when X ai .
- Repeat and report "Not Found!" when i gt n.
51Sequential Search
To find 7
- 12 34 2 9 7 5 six numbers7 number X
- 12 34 2 9 7 5 7
- 12 34 2 9 7 5 7
- 12 34 2 9 7 5 7
- 12 34 2 9 7 5 7 found!
52Sequential Search (2)
To find 10
- 12 34 2 9 7 510
- 12 34 2 9 7 5 10
- 12 34 2 9 7 5 10
- 12 34 2 9 7 5 10
- 12 34 2 9 7 5 10
- 12 34 2 9 7 5 10 not found!
53Sequential Search Pseudo Code
- i 1
- while i lt n do
- begin
- if X ai then
- report "Found!" and stop
- else
- i i1
- end
- report "Not Found!"
Challenge Modify it to include stopping
conditions in the while loop
54Number of comparisons?
i 1 while i lt n do begin if X ai then
report "Found!" stop else i
i1 end report "Not Found!"
- How many comparisons this algorithm requires?
Best case X is 1st no. ? 1 comparison Worst
case X is last OR X is not found ? n comparisons
55Finding maximum / minimum...
56Finding max from n ve numbers
input a1, a2, ..., an M 0 i 0 while (i
lt n) do begin i i 1 M max(M,
ai) end output M
What about minimum?
57Finding min from n ve numbers
input a1, a2, ..., an M a1 i 1 while
(i lt n) do begin i i 1 M min(M,
ai) end output M
How many comparisons?
n-1
58Finding 1st and 2nd min
input a1, a2, ..., an M1 min(a1,
a2) M2 max(a1, a2) i 2 while (i lt n)
do begin i i 1 if (ai lt M1) then M2
M1, M1 ai else if (ai lt M2) then
M2 ai end output M1, M2
Two variables M1, M2
How to update M1, M2?
59Finding min among 3 numbers
Input 3 numbers a, b, c Output the min value of
these 3 numbers Algorithm
Input a, b, c
a ? b ?
N
Y
a ? c ?
b ? c ?
Y
Y
N
N
Output b
Output a
Output c
Output c
60Finding min among 3 numbers
Example 2
Example 1
Input a, b, c
a6, b5, c7
a3, b5, c2
a ? b ?
N
Y
a ? c ?
b ? c ?
Y
Y
N
N
Output b
Output a
Output c
Output c
5 is output
2 is output
61Finding min among n numbers
Input a0, a1, ...
Any systematic approach?
a0 ? a1 ?
N
Y
a0 ? a2 ?
a1 ? a2 ?
Y
Y
N
N
a0 ? a3 ?
a1 ? a3 ?
a2 ? a3 ?
a2 ? a3 ?
62Finding min among n numbers
Input a0, a1, ..., an-1
min 0 i 1
i i1
i lt n?
N
Y
Output amin
ai lt amin?
Y
min i
N
63Finding location of minimum
input a1, a2, ..., an loc 1 // location
of the min number i 1 while (i lt n) do begin
i i 1 if (ai lt aloc) then loc
i end output aloc
Example
a50,30,40,20,10
(_at_ end of) Iteration loc aloc i
1
2
3
4
50
1
1
30
2
2
30
2
3
20
4
4
10
5
5
64Finding min using for-loop
- Rewrite the above while-loop into a for-loop
input a0, a1, ..., an-1 min 0 for i 1
to n-1 do begin if (ai lt amin) then min
i end output amin