Algo ch1 Role Algo 220730 011903
Algo ch1 Role Algo 220730 011903
in Computing
1-1
Introduction to Algorithms
Chapter 1
1-2
1
Outlines: Introduction
• Algorithm: Definitions
• Algorithm: Characteristics & Components
• Algorithm: Examples – Calculating Effort
• Algorithm: Efficiency
1-3
Computational problems
• A computational problem specifies an input-output
relationship
What does the input look like?
What should the output be for each input?
• Example:
Input: an integer number n
Output: Is the number prime?
• Example:
Input: A list of names of people
Output: The same list sorted alphabetically
• Example:
Input: A picture in digital format
Output: An English description of what the picture shows
1-4
2
Algorithms
• A tool for solving a well-specified computational problem
1-5
1-6
3
The Problem-solving Process
Analysis
Problem
specification
Design
Algorithm
Implementation
Program
Compilation
Executable
(solution)
1-7
Problem
Algorithm: A sequence of
instructions describing how
to do a task (or process)
C Program
1-8
4
Components of an Algorithm
• Variables and values
• Instructions
• Sequences
• Procedures
• Selections
• Repetitions
• Documentation
1-9
Values
• Represent quantities, amounts or measurements
• May be numerical or alphabetical (or other things)
• Often have a unit related to their purpose
• Example:
procedure Sum1_to_n(num)
{
count = 1
sum = 0
while (count <= num)
{
add count to sum
add 1 to count
}
}
1 - 10
5
Variables
1 - 11
Components of an Algorithm
• Values and Variables
• Instruction
• Sequence (of instructions)
• Procedure (involving instructions)
• Selection (between instructions)
• Repetition (of instructions)
• Documentation (beside instructions)
1 - 12
6
Instructions (Primitives)
• Some action that is simple...
• ...and unambiguous...
• ...that the system knows about...
• ...and should be able to actually do
• Examples:
procedure Sum1_to_n(num)
{
count = 1
sum = 0
while (count <= num) Directions to perform
{ specific actions on values
add count to sum
add 1 to count and variables.
}
}
1 - 13
Components of an Algorithm
• Values and Variables
• Instruction
• Sequence (of instructions)
• Procedure (involving instructions)
• Selection (between instructions)
• Repetition (of instructions)
• Documentation (beside instructions)
1 - 14
7
Sequence
• A series of instructions
• ...to be carried out one after the other...
• ...without hesitation or question
• Example:
How to cook Gourmet Meal
1 - 15
Sequence -- Example
1. Open freezer door
2. Take out Gourmet Meal™
3. Close freezer door
4. Open microwave door
5. Put Gourmet Meal™ on carousel
6. Shut microwave door
7. Set microwave on high for 5 minutes
8. Start microwave
9. Wait 5 minutes
10. Open microwave door
11. Remove Gourmet Meal™
12. Close microwave door
1 - 16
8
Components of an Algorithm
• Values and Variables
• Instruction
• Sequence (of instructions)
• Procedure (involving instructions)
• Selection (between instructions)
• Repetition (of instructions)
• Documentation (beside instructions)
1 - 17
Procedure
• A named sequence of instructions
• So that you can
Refer to it collectively (by name)
...instead of individually (by each instruction in the sequence)
• Example:
Drive_To_Uni
1 - 18
9
Procedure -- Example
procedure Drive_To_Uni
{
1. find car keys ...etc...etc...etc...
2. disable car alarm 52. find parking space
3. open car door 53. pull into parking
4. get in car space
5. shut car door 54. turn off engine
6. put keys in ignition 55. remove keys from
7. start car ignition
8. back car out of 56. open car door
driveway 57. get out
9. drive to end of street 58. shut car door
10. turn right 59. lock car door
11. drive to end of street
60. enable alarm
12. turn left
}
...etc...etc...etc
1 - 19
1 - 20
10
Procedure – Example (cont)
procedure Do_Wednesday
{ In this subject, we also use the
Wake_up following words to refer to a
“Procedure” :
Have_Shower
• Sub-routine
Eat_Breakfast • Module
Drive_To_Uni • Function
Sit_1301_Lecture
...etc...etc...etc...
Drive_From_Uni
...etc...etc...etc...
}
1 - 21
1 - 22
11
Procedure – Example (cont)
procedure Do_Wednesday
{
Wake_up An instruction invoking a
procedure is known as a
Have_Shower “procedure call”
Eat_Breakfast
Drive_To_Uni
Sit_1301_Lecture
...etc...etc...etc...
Drive_From_Uni
...etc...etc...etc...
}
1 - 23
Procedure
• A procedure may have a set of parameters
1 - 24
12
Components of an Algorithm
• Values and Variables
• Instruction
• Sequence (of instructions)
• Procedure (involving instructions)
• Selection (between instructions)
• Repetition (of instructions)
• Documentation (beside instructions)
1 - 25
Selection
• An instruction that decides which of two possible
sequences is executed
• The decision is based on a single true/false condition
• Examples:
Car repair
Reciprocals
1 - 26
13
Selection Example -- Car Repair
if (motor turns)
then
{
CheckFuel
CheckSparkPlugs
CheckCarburettor
}
else
{
CheckDistributor
CheckIgnitionCoil
}
1 - 27
Selection Example –
Car Repair (cont)
if (motor turns)
then
{
CheckFuel Should be a
CheckSparkPlugs
CheckCarburettor
true or false
} condition.
else
{
CheckDistributor
CheckIgnitionCoil
}
1 - 28
14
Selection Example --
Car Repair (cont)
if (motor turns)
then
{
CheckFuel
CheckSparkPlugs Sequence if
CheckCarburettor
the condition
}
else is true.
{
CheckDistributor
CheckIgnitionCoil
}
1 - 29
Selection Example --
Car Repair (cont)
if (motor turns)
then
{
CheckFuel
CheckSparkPlugs
CheckCarburettor
}
Sequence if the
else
{ condition is
CheckDistributor false.
CheckIgnitionCoil
}
1 - 30
15
Selection Example -- Reciprocals
Examples:
Q. Give an
algorithm for Reciprocal of 2: 1/2
computing the
Reciprocal of -3/4:
reciprocal of a
1/(-3/4) = -4/3
number.
Reciprocal of 0:
“undefined”
1 - 31
Algorithm:
input Num
if (Num is not equal 0)
Q. Give an then
algorithm for {
computing the output 1/Num
reciprocal of a }
number. else
{
output "infinity"
}
1 - 32
16
Selection Example-- Reciprocals
Algorithm:
input Num
if (Num is not equal 0)
then
{
Num is a variable output 1/Num
whose value }
depends on the else
actual number the {
output "infinity"
user provides.
}
1 - 33
1 - 34
17
Selection Example – Reciprocals (cont)
Algorithm:
input Num
if (Num is not equal 0)
then
For a given value {
of Num, only one output 1/Num
of these two }
else
sequences can be
{
executed
output "infinity"
}
1 - 35
1 - 36
18
Selection Example – Reciprocals (cont)
Algorithm:
input Num
if (Num is not equal 0)
then
{
output 1/Num
Executed if Num }
is equal to 0 else
{
output "infinity"
}
1 - 37
Selection -- Exercise
Will the following algorithms produce the same output?
Algorithm 1: Algorithm 2:
input Num input Num
if (Num is not equal 0) if (Num is not equal 0)
then then
{ {
output 1/Num output 1/Num
} }
else
{ output "infinity"
output "infinity"
}
1 - 38
19
Selection – Several Conditions
• What if several conditions need to be satisfied?
if ( today is Wednesday and the time is 10.00am )
then
{
Go to CSE1301 Lecture
}
else
{
Go to Library
}
Solution 1
1 - 39
1 - 40
20
Selection – At Least One of Several Conditions
• What if at least one of several conditions needs to be
satisfied?
1 - 41
Components of an Algorithm
• Values and Variables
• Instruction
• Sequence (of instructions)
• Procedure (involving instructions)
• Selection (between instructions)
• Repetition (of instructions)
• Documentation (beside instructions)
1 - 42
21
Repetition
• Repeat an instruction...
...while (or maybe until) some true or false condition occurs
Test the condition each time before repeating the instruction
1 - 43
Repetition -- Example
procedure Sum1_to_n(num)
{
count = 1
sum = 0
while (count <= num)
{
add count to sum
add 1 to count
}
}
1 - 44
22
Repetition – Example (cont)
procedure Sum1_to_n(num)
{
count = 1
sum = 0 Condition is tested
while (count <= num)
{
before sequence
add count to sum
add 1 to count
}
}
1 - 45
1 - 46
23
Repetition – Example (cont)
procedure Sum1_to_n(num)
{ Ensure initial
count = 1 values of variables
sum = 0 used in the
while (count <= num) conditions are set
{
add count to sum
correctly
add 1 to count
}
}
1 - 47
1 - 48
24
Repetition – Example (cont)
• What if we don’t increment the begging count?
procedure Sum1_to_n(num)
{
count = 1
sum = 0
while (count <= num)
{
add count to sum
}
} Infinite loop
1 - 49
Components of an Algorithm
• Values and Variables
• Instruction
• Sequence (of instructions)
• Procedure (involving instructions)
• Selection (between instructions)
• Repetition (of instructions)
• Documentation (beside instructions)
1 - 50
25
Documentation
• Records what the algorithm does
• Describes how it does it
• Explains the purpose of each component of the algorithm
• Notes restrictions or expectations
1 - 51
Documentation -- Example
// This procedure finds the summation from 1 to a given number
procedure Sum1_to_n(num)
{
// do initialization
count = 1
sum = 0
while (count <= num)
{
// add count to sum
sum = sum + count
// increment count by 1
count = count + 1
}
}
1 - 52
26
Components of an Algorithm
• Values and Variables
• Instruction
• Sequence (of instructions)
• Procedure (involving instructions)
• Selection (between instructions)
• Repetition (of instructions)
• Documentation (beside instructions)
1 - 53
1 - 54
27
Describing Algorithms
• Algorithms can be implemented in any programming
language
for j = 2 to n-1
{
if mod(n, j) = 0
output “n is non prime” and halt
}
output “n is prime”
1 - 55
Algorithm Efficiency
• Consider two sort algorithms
Insertion sort
• takes c1n2 to sort n items
• where c1 is a constant that does not depends on n
• it takes time roughly proportional to n2
Merge Sort
• takes c2 n lg(n) to sort n items
• where c2 is also a constant that does not depends on n
• lg(n) stands for log2 (n)
• it takes time roughly proportional to n lg(n)
Insertion sort usually has a smaller constant factor than merge sort
• so that, c1 < c2
Merge sort is faster than insertion sort for large input sizes
1 - 56
28
Algorithm Efficiency
• Consider now:
A faster computer A running insertion sort against
A slower computer B running merge sort
Both must sort an array of one million numbers
• Suppose
Computer A execute one billion (109) instructions per second
Computer B execute ten million (107) instructions per second
So computer A is 100 times faster than computer B
• Assume that
c1 = 2 and c2 = 50
1 - 57
Algorithm Efficiency
• To sort one million numbers
Computer A takes
2 . (106)2 instructions
109 instructions/second
= 2000 seconds
Computer B takes
50 . 106 . lg(106) instructions
107 instructions/second
100 seconds
1 - 58
29
Summary
• Algorithm: Definitions
• Algorithm: Characteristics & Components
• Algorithm: Examples – Calculating Effort
• Algorithm: Efficiency
1 - 59
30