Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Chapter Outline:
1. Chapter Goals
2. What are Algorithms
1. Real Life Examples (origami, recipes)
2. Definition
3. Example: A = B + C
3.
4.
5.
6.
Algorithms
Computing
How
of Solving Problems.
(UIT2201: Algorithms) Page 2
Chapter Outline:
1. Chapter Goals
2. What are Algorithms
1. Real Life Examples (origami, recipes)
2. Definition of Algorithm
3. Example: A = B + C
3.
4.
5.
6.
Real-Life Examples
Problem
Decomposition
Preparing Ingredients;
Preparing Sauce;
Problem
Decomposition
Imprecise Instructions;
Job can often be done even if instructions are
not followed precisely
Modifications may be done by the person
following the instructions;
But,
Definition of Algorithm:
An
Algorithm Examples?
am, am-1, ., a1
bm, bm-1, ., b1
Output: The sum c = a + b
A step-by-step instruction
to a dumb machine
Try
an example:
3492
8157
Imagine you looking at yourself solving it
(UIT2201: Algorithms) Page 13
Chapter Outline:
1. Chapter Goals
2. What are Algorithms
3. Expressing Algorithms Pseudo-Code
1.
2.
3.
4.
5.
4. Simple Algorithms
5. Recursive Algorithms
6. Time Complexity of Algorithms
(UIT2201: Algorithms) Page 15
Pseudo-Code instead
Examples:
Let c be 0;
Sort the list of numbers in increasing order;
Need
to know both
syntax representation
semantics meaning
(UIT2201: Algorithms) Page 17
Primitive Operations
To
Conditional primitive:
if statement
case statement
Statements
Conditional Primitives
if
statement
true
Step A
condition?
false
Step B
if (condition)
then (Step A)
endif
Semantics
(UIT2201: Algorithms) Page 19
D)
C)
B)
A);
the while-loop
loop a variable
number of times
Syntax
while (condition) do
(some sequence
of statements)
condition?
false
true
Some sequence
of statements;
endwhile
Semantics
(UIT2201: Algorithms) Page 21
Syntax
for j a to b do
(some sequence
of statements)
j a;
(j <= b)?
false
true
Some sequence
of statements;
endfor
Semantics
j j+1;
j 1;
while (j <= 4) do
print 2*j;
j j + 1;
endwhile
print --- Done ---
Output:
2
4
6
8
--- Done --(UIT2201: Algorithms) Page 23
Arrays:
Chapter Outline:
1.
2.
3.
4.
Chapter Goals
What are Algorithms
Expressing Algorithms Pseudo-Code
Simple Algorithms
1. Simple iterative algorithms
Computing Sum, Find Max/Min
2. Modular Program Design
3. Divisibility and Prime Numbers
5. Recursive Algorithms
6. Time Complexity of Algorithms
(UIT2201: Algorithms) Page 26
Processing:
Output:
k
?
1
2
3
4
5
6
6
Sum-sf
0
2
7
17
20
32
56
56
n=6
Sum
?
?
?
?
?
?
?
56
Sum is 56
(UIT2201: Algorithms) Page 28
2. Iteration
This is the KEY STEP
Where most of work is done
3. Post-Processing or Cleanup
What do we know?
A number n is prime iff
n has no positive factors except 1 and n
Exercise it with:
Prime (5); Prime (4);
Prime (55); Prime (41);
(UIT2201: Algorithms) Page 33
Divisible(m, n)
Algorithm
if n is divisible by m
Idea:
Exercise it with:
Divisible (3, 9);
Divisible (3,101);
Divisible(m,n)
(* To compute if n is divisible by m *)
begin
D false; (* assume false, first *)
for k 1 to n do
if (m*k = n) then (Dtrue; exit-loop)
else (* Do nothing *)
endfor
Divisible D; (* true or false *)
end;
(UIT2201: Algorithms) Page 35
Chapter Outline:
1.
2.
3.
4.
5.
6.
Chapter Goals
What are Algorithms
Expressing Algorithms Pseudo-Code
Simple Algorithms
Recursive Algorithms
Time Complexity of Algorithms
1. Sequential search algorithm
2. Binary search algorithm
3. Analysis of Time Complexity
7. Summary
(UIT2201: Algorithms) Page 36
Given:
Binary Search
If
Then,
we can do better,
Binary Search
n=100
What
if n=1000? 1,000,000?
(UIT2201: Algorithms) Page 41
Search (Alg):
Worst Case:
n comparisons
Best Case: 1 comparison
Avg Case: n/2 comparisons
Binary
Search (Alg):
Complexity of Algorithm
Chapter Outline:
1.
2.
3.
4.
5.
Chapter Goals
What are Algorithms
Expressing Algorithms Pseudo-Code
Simple Algorithms
Recursive Algorithms
1. Recursion the idea
2. Fibonacci sequence
3. Tower of Hanoi
5. Recursion
General Idea:
Solve simplest (smallest) cases DIRECTLY
usually these are very easy to solve
Abstraction:
To solve a given problem, we first assume that we
ALREADY know how to solve it for smaller instances!!
Dictionary definition:
recursion
see recursion
(UIT2201: Algorithms) Page 46
F6 = 5 + 3 = 8
F7 = 8 + 5 = 13
F8 = 13 + 8 = 21
F9 = 21 + 13 = 34
F10 = 34 + 21 = 55
F11 = 55 + 34 = 89
The
It
But,
very SLOW
(UIT2201: Algorithms) Page 48
slow is it?
F(4)
F(4)
F(3)
F(2)
F(3)
F(2)
F(2)
F(3)
F(1)
F(2)
F(2)
F(1)
F(1)
Tower of Hanoi
General Method:
First, move first (n-1) disks from A to B
Now, can move largest disk from A to C
Then, move first (n-1) disks from B to C
Recursive Algorithm
when (n=1), we have simple case
Else (decompose problem and make recursive-calls)
Hanoi(n, A, B, C);
(* Move n disks from A to C via B *)
begin
if (n=1) then Move top disk from A to C
else (* when n>1 *)
Hanoi (n-1, A, C, B);
Move top disk from A to C
Hanoi (n-1, B, C, A);
endif
end;
(UIT2201: Algorithms) Page 53
Characteristics of Algorithm
Correctness
Ease of understanding
Ease of coding
Ease of maintainence
If
The End