Download Document

Document related concepts

Corecursion wikipedia , lookup

Computational complexity theory wikipedia , lookup

Path integral formulation wikipedia , lookup

Hardware random number generator wikipedia , lookup

Theoretical computer science wikipedia , lookup

Types of artificial neural networks wikipedia , lookup

Travelling salesman problem wikipedia , lookup

Genetic algorithm wikipedia , lookup

Multiplication algorithm wikipedia , lookup

Nonblocking minimal spanning switch wikipedia , lookup

Factorization of polynomials over finite fields wikipedia , lookup

Pattern recognition wikipedia , lookup

Algorithm wikipedia , lookup

Algorithm characterizations wikipedia , lookup

Time complexity wikipedia , lookup

Transcript
Algorithmic Foundations
COMP108
COMP108
Algorithmic Foundations
Basics
Prudence Wong
http://www.csc.liv.ac.uk/~pwong/teaching/comp108/201314
Algorithmic Foundations
COMP108
Crossing Bridge @ Night
1 min
each time, 2 persons share a torch
they walk @ speed of slower person
2 min
Target: all cross
the bridge
5 min
10 min
Can you do it
in 17 mins?
Algorithmic Foundations
COMP108
Module Information
Dr Prudence Wong
Rm 3.18 Ashton Building, [email protected]
office hours: Mon 3-5pm
Demonstrators
Mr David Hamilton, Miss Alison Liu, 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
3
(Basics)
Algorithmic Foundations
COMP108
Module Information (2)
Teaching, Assessments and Help
33 lectures, 11 tutorials
2 assessments (20%), 1 written exam (80%)
Office hours, email
Tutorials/Labs
Location :
Lecture/Seminar Rooms (theoretical) or
Lab 1 (practical)
Week 2: Theoretical – Lecture/Seminar Rooms
4
(Basics)
Algorithmic Foundations
COMP108
Module Information (3)



Each assessment has two components

Tutorial participation (25%)

Class Test (75%)
Assessment 1

Tutorials 1 – 5 (Weeks 2-6)

Class Test 1: Week 6, Thu 6th Mar
Assessment 2

Tutorials 6 – 11 (Weeks 7-12)

Class Test 2: Week 12, Thu 8th May
5
(Basics)
Algorithmic Foundations
COMP108
Why 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)
6
(Basics)
Algorithmic Foundations
COMP108
Aims

To give an overview of the study of algorithms in
terms of their efficiency.
What do we mean by good?

To introduce the standard algorithmic design
paradigms employed in the development of efficient
algorithmic solutions.
How to achieve?

To describe the analysis of algorithms in terms of
the use of formal models of Time and Space.
Can we prove?
7
(Basics)
Algorithmic Foundations
COMP108
Ready 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
Algorithmic Foundations
COMP108
What is an algorithm?
A sequence of precise and concise instructions
that guide you (or a computer) to solve a specific
problem
Input
Algorithm
Output
Daily life examples: cooking recipe, furniture
assembly manual
(What are input / output in each case?)
9
(Basics)
Algorithmic Foundations
COMP108
Why 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?
8
10
12 3
3
5
A
7
B
3
9
5
5
Simple solution
 Compute the cost of each
path from A to B
 Choose the cheapest one
6
4
2
5
5
1
6
9
7
11
10
(Basics)
Algorithmic Foundations
COMP108
Shortest 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?
2
B
Simple solution
 Compute the cost of each
path from A to B
 Choose the cheapest one
A
11
(Basics)
Algorithmic Foundations
COMP108
Shortest 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?
2
A
B
Simple solution
 Compute the cost of each
path from A to B
 Choose the cheapest one
Number of paths
=2
12
(Basics)
Algorithmic Foundations
COMP108
Shortest 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?
B
3
A
Simple solution
 Compute the cost of each
path from A to B
 Choose the cheapest one
Number of paths
=2+3
13
(Basics)
Algorithmic Foundations
COMP108
Shortest 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?
B
A
Simple solution
 Compute the cost of each
path from A to B
 Choose the cheapest one
Number of paths
=2+3
14
(Basics)
Algorithmic Foundations
COMP108
Shortest 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?
B
A
6
Simple solution
 Compute the cost of each
path from A to B
 Choose the cheapest one
Number of paths
=2+3+6
= 11
15
(Basics)
Algorithmic Foundations
COMP108
Shortest 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!!
B
A
Simple solution
 Compute the cost of each
path from A to B
 Choose the cheapest one
For large n, it’s impossible to
check all paths!
We need more sophisticated
solutions
16
(Basics)
Algorithmic Foundations
COMP108
Shortest path to go from A to B
There is an algorithm, called Dijkstra's algorithm,
that can compute this shortest path efficiently.
B
A
17
(Basics)
Algorithmic Foundations
COMP108
Idea of Dijkstra's algorithm
Go one step from A, label the
neighbouring cities with the cost.
10
10
0
A
8
12
3
5
5
3
9
7
7
B
5
5
3
6
4
2
5
5
1
6
9
7
11
18
(Basics)
Algorithmic Foundations
COMP108
Idea of Dijkstra's algorithm
Choose city with smallest cost and go one step from there.
10
10
0
A
8
12
3
5
5
3
9
7
7
B
5
5
3
6
4
2
5
5
1
6
9
7
11
19
(Basics)
Algorithmic Foundations
COMP108
Idea of Dijkstra's algorithm
This path must NOT
be used because we
find a cheaper
alternative path
10
8
10
0
A
12
5
3
9
7
7
B
8
3
5
If an alternative cheaper path is found,
record this path and erase the more
expensive one.
5
5
3
6
4
2
Then repeat & repeat …
5
5
1
6
9
7
11
20
(Basics)
Algorithmic Foundations
COMP108
Shortest path to go from A to B
B
Lesson to learn:
Brute force algorithm
may run slowly.
We need more
sophisticated algorithms.
A
21
(Basics)
Algorithmic Foundations
COMP108
How 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
Algorithmic Foundations
COMP108
Algorithm 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
23
(Basics)
Algorithmic Foundations
COMP108
Compute the n-th power
Input: a number x & a non-negative integer n
Output: the n-th power of x
Algorithm:
1.
2.
3.
Set a temporary variable p to 1.
Repeat the multiplication p = p * x for n times.
Output the result p.
24
(Basics)
Algorithmic Foundations
COMP108
Pseudo Code
pseudo code:
p = 1
for i = 1 to n do
p = p * x
output p
Pascal:
p := 1;
for i := 1 to n do
p := p * x;
writeln(p);
C:
p = 1;
for (i=1; i<=n; i++)
p = p * x;
printf("%d\n", p);
C++:
p = 1;
for (i=1; i<=n; i++)
p = p * x;
cout << p << endl;
Java:
p = 1;
for (i=1; i<=n; i++)
p = p * x;
System.out.println(p);
25
(Basics)
Algorithmic Foundations
COMP108
Pseudo Code
suppose n=4, x=3
Another way to
describe algorithm is
by pseudo code
p = 1
for i = 1 to n do
p = p * x
output p
iteration
start
1
2
3
4
end
i
1
2
3
4
5
similar to programming language
more like English
p
1
3
9
27
81
trace table
Combination of both
26
(Basics)
Algorithmic Foundations
COMP108
Pseudo Code: conditional
Conditional statement
if condition then
statement
if condition then
statement
else
statement
if a < 0 then
a = -a
b = a
output b
if a > 0 then
b = a
else
b = -a
output b
What is computed?
absolute
value
27
(Basics)
Algorithmic Foundations
COMP108
Pseudo Code: iterative (loop)
Iterative statement
var automatically increased by 1
after each iteration
for var = start_value to end_value do
statement
while condition do
statement
condition to CONTINUE the loop
repeat
statement
until condition
condition to STOP the loop
condition for while loop is NEGATION
of condition for repeat-until loop
28
(Basics)
Algorithmic Foundations
COMP108
for loop
for var = start_value to end_value do
statement
sum=0
i=1
i=i+1
i <= n?
No
Yes
sum = sum+i
the loop is executed n times
Sum of 1st n nos.:
input: n
sum = 0
for i = 1 to n do
begin
sum = sum + i
end
output sum
29
(Basics)
Algorithmic Foundations
COMP108
for loop
for var = start_value to end_value do
statement
suppose iteration
n=4
start
1
2
3
4
end
i
1
2
3
4
5
sum
0
1
3
6
10
the loop is executed n times
Sum of 1st n nos.:
input: n
sum = 0
for i = 1 to n do
begin
sum = sum + i
end
output sum
30
(Basics)
Algorithmic Foundations
COMP108
while loop
while condition do
statement
Sum of 1st n numbers:
input: n
sum = 0
i = 1
while i <= n do
begin
sum = sum + i
i = i + 1
end
output sum
condition to CONTINUE the loop
 Do
the same as forloop in previous slides
 It requires to
increment i explicitly
31
(Basics)
Algorithmic Foundations
COMP108
while 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
continue?
output sum
No
Yes
ask for number
sum = sum+number
32
(Basics)
Algorithmic Foundations
COMP108
repeat-until
repeat
statement
until condition
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
ask for number
sum = sum+number
No
Stop?
Yes
also execute
undetermined
number of times
 How it differs
from while-loop?

execute at
least once
33
(Basics)
More Example 1
suppose x=14, y=4
(@ end of)
iteration
input: x, y
remainder &
quotient
r = x
1
q = 0
2
while r = y do
3
begin
suppose x=14, y=5
r = r − y
(@ end of) iteration
q = q + 1
end
1
output r and q
2
Algorithmic Foundations
COMP108
r
q
14 0
10 1
6
2
2
3
r q
9 1
4 2
suppose x=14, y=7
What is computed?
(@ end of )
iteration
r
q
1
7 1
2
0 2
34
(Basics)
More Example 2
suppose x=12, y=4
(@ end of )
iteration
all common
input: x, y
factors
if x < y then
1
swap x & y
2
i = y
3
while i = 1 do
4
begin
suppose x=15, y=6
if x%i==0 && y%i==0
then output i
i = i-1
1
a%b
end
remainder of
2
a divided b
output (this
iteration)
4
3
2
2
1
1
0
6
5
4
3
3
5
6
i
4
3
4
What values are output?
Algorithmic Foundations
COMP108
2
1
1
0
35
(Basics)
Algorithmic Foundations
COMP108
More Example 3
Highest Common Factor (HCF)
Greatest Common Divisor (GCD)
input: x, y
if x < y then
swap x & y
i = y
What value is output?
found = false
while i = 1 && !found do
begin
if x%i==0 && y%i==0
then found = true
Questions:
else i = i-1
 what value of found makes
end
the loop stop?
output i
 when does found change
to such value?
36
(Basics)
Algorithmic Foundations
COMP108
Pseudo Code: Exercise
Write a while-loop to
1. Find

assuming x and y are both integers
the product of all integers in interval [x, y]
e.g., if x is 2 & y is 5, then output is 2*3*4*5 = 120
product = ??
i = ??
while ?? do
begin
??
i = ??
end
output ??
37
(Basics)
Algorithmic Foundations
COMP108
Pseudo Code: Exercise
Write a while-loop to
1. Find
assuming x and y are both integers
the product of all integers in interval [x, y]
product = 1
i = x
while i <= y do
begin
product = product * i
i = i+1
end
output product
38
(Basics)
Algorithmic Foundations
COMP108
Pseudo Code: Exercise 2
Write a while-loop for this:
2.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
39
(Basics)
Algorithmic Foundations
COMP108
Pseudo Code: Exercise 2
Write a while-loop for this:
2.Given
two positive integers x and y, list all factors
of x which are not factors of y
i = 1
while i <= x do
begin
if x%i == 0 && y%i != 0 then
output i
i = i+1
end
40
(Basics)
Algorithmic Foundations
COMP108
Challenges …
Convert while-loops to for-loops
& repeat-loop
Algorithmic Foundations
COMP108
Convert 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 ??
42
(Basics)
Algorithmic Foundations
COMP108
Convert 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
43
(Basics)
Algorithmic Foundations
COMP108
Convert 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 = i+1
until i > y
output product
What’s wrong with this?
If given x is larger than y?
44
(Basics)
Algorithmic Foundations
COMP108
Convert to repeat loops
Find the product of all integers in interval [x, y]
assuming x and y are both integers
product = 1
if x <= y then begin
i = x
repeat
product = product * i
i = i+1
until i > y
end
output product
45
(Basics)
Algorithmic Foundations
COMP108
Convert 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
46
(Basics)
Algorithmic Foundations
COMP108
Convert 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 x%i == 0 && y%i != 0 then
output i
end
47
(Basics)
Algorithmic Foundations
COMP108
Convert 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 ??
48
(Basics)
Algorithmic Foundations
COMP108
Convert 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 x%i==0 && y%i!=0 then
output i
i = i+1
until i > x
49
(Basics)
Algorithmic Foundations
COMP108
Searching …
Algorithmic Foundations
COMP108
Searching
 Input:
n numbers a1, a2, …, an; and a number X
 Output:
determine if X is in the sequence or not
 Algorithm
(Sequential search):
1.
From i=1, compare X with ai one by one as long as i <= n.
2.
Stop and report "Found!" when X = ai .
3.
Repeat and report "Not Found!" when i > n.
51
(Basics)
Algorithmic Foundations
COMP108
Sequential Search
To find 7

12
7
34
2
9
7
5

12
34
7
2
9
7
5

12
34
2
7
9
7
5

12
34
2
9
7
7
5

12
34
2
9
7
7
5
six numbers
number X
found!
52
(Basics)
Algorithmic Foundations
COMP108
Sequential Search (2)
To find 10

12
10
34
2
9
7
5

12
34
10
2
9
7
5

12
34
2
10
9
7
5

12
34
2
9
10
7
5

12
34
2
9
7
10
5

12
34
2
9
7
5
10
not found!
53
(Basics)
Algorithmic Foundations
COMP108
Sequential Search – Pseudo Code
i = 1
while i <= n do
begin
if X == a[i] then
report "Found!" and stop
else
i = i+1
end
Challenge: Modify
it to include
report "Not Found!"
stopping conditions
in the while loop
54
(Basics)
Algorithmic Foundations
COMP108
Number of comparisons?
i = 1
How many comparisons this
while i <= n do
algorithm requires?
begin
if X == a[i] then
report "Found!" & stop
Best case: X is 1st no.
else
 1 comparison
i = i+1
end
Worst case: X is last
OR X is not found
report "Not Found!"
 n comparisons
55
(Basics)
Algorithmic Foundations
COMP108
Finding maximum /
minimum...
2nd max / min…
Algorithmic Foundations
COMP108
Finding max from n +ve numbers
input: a[1], a[2], ..., a[n]
M = 0
i = 0
while (i < n) do
What about minimum?
begin
i = i + 1
M = max(M, a[i])
end
output M
57
(Basics)
Algorithmic Foundations
COMP108
Finding min from n +ve numbers
input: a[1], a[2], ..., a[n]
M = a[1]
i = 1
while (i < n) do
How many comparisons?
begin
i = i + 1
M = min(M, a[i])
end
n-1
output M
58
(Basics)
Algorithmic Foundations
COMP108
Finding 1st and 2nd min
input: a[1], a[2], ..., a[n]
M1 = min(a[1], a[2])
M2 = max(a[1], a[2])
Two variables: M1, M2
i = 2
while (i < n) do
begin
i = i + 1
if (a[i] < M1) then
M2 = M1, M1 = a[i]
else if (a[i] < M2) then
M2 = a[i]
end
How to update M1, M2?
output M1, M2
59
(Basics)
Algorithmic Foundations
COMP108
Finding location of minimum
input: a[1], a[2], ..., a[n]
loc = 1 // location of the min number
i = 1
while (i < n) do
Example
begin
a[]={50,30,40,20,10}
i = i + 1
(@ end of)
loc a[loc] i
if (a[i] < a[loc]) then
Iteration
loc = i
1
50
1
end
1
output a[loc]
2
30
2
2
2
30
3
3
4
20
4
4
5
10
5
60
(Basics)
Algorithmic Foundations
COMP108
Finding min using for-loop

Rewrite the above while-loop into a for-loop
input: a[1], a[2], ..., a[n]
loc = 1
for i = 2 to n do
begin
if (a[i] < a[loc]) then
loc = i
end
output a[loc]
61
(Basics)