0% found this document useful (0 votes)
5 views30 pages

Algo ch1 Role Algo 220730 011903

The document provides an introduction to algorithms, defining their role in computing and outlining their characteristics and components. It discusses the problem-solving process, the structure of algorithms, and various components such as variables, instructions, sequences, procedures, selections, and repetitions. Additionally, it includes examples to illustrate how algorithms can solve computational problems efficiently.

Uploaded by

Shahd BA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views30 pages

Algo ch1 Role Algo 220730 011903

The document provides an introduction to algorithms, defining their role in computing and outlining their characteristics and components. It discusses the problem-solving process, the structure of algorithms, and various components such as variables, instructions, sequences, procedures, selections, and repetitions. Additionally, it includes examples to illustrate how algorithms can solve computational problems efficiently.

Uploaded by

Shahd BA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

The Role of Algorithms

in Computing

1-1

Introduction to Algorithms

Chapter 1

The Role of Algorithms in Computing

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

Input Algorithm Output

• An algorithm is said to be correct if, for every input


instance, it halts with the correct output

• Algorithms must be:


 Correct: For each input produce an appropriate output
 Efficient: run as quickly as possible, and use as little memory as
possible – more about this later

1-5

Problems and Algorithms


• We need to solve a computational problem
 “Convert a weight in pounds to Kg”

• An algorithm specifies how to solve it, e.g.:


 1. Read weight-in-pounds
 2. Calculate weight-in-Kg = weight-in-pounds * 0.455
 3. Print weight-in-Kg

• A computer program is a computer-executable description


of an algorithm

1-6

3
The Problem-solving Process

Analysis
Problem
specification
Design

Algorithm

Implementation

Program

Compilation

Executable
(solution)

1-7

From Algorithms to Programs

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

• Are containers for values – places to store


values
• Example:
procedure Sum1_to_n(num)
{
count = 1
sum = 0
while (count <= num)
{
add count to sum
add 1 to count
}
}

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

Procedure – Example (cont)


procedure Do_Wednesday
{ procedure Do_Week
Wake_up {
Do_Monday
Have_Shower Do_Tuesday
Eat_Breakfast Do_Wednesday
Drive_To_Uni Do_Thursday
...etc...etc...etc...
Sit_1301_Lecture }
...etc...etc...etc...
Drive_From_Uni
...etc...etc...etc...
}

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

Procedure – Example (cont)


procedure Do_Wednesday
{
Wake_up
Have_Shower
We use brackets to mark the
Eat_Breakfast beginning and end of a
Drive_To_Uni sequence.
Sit_1301_Lecture
...etc...etc...etc...
Drive_From_Uni
...etc...etc...etc...
}

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

procedure customerService ( myName ,timeOfDay )


{
say “Good timeOfDay”
say “My name is myName”
say “How can I help you?”
}

customerService ( “Ann”, “Morning” )


customerService (“Ann”, “Afternoon” )
customerService ( “Jeff”, “Afternoon” )

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

Selection Example – Reciprocals (cont)

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

Selection Example – Reciprocals (cont)


Algorithm:
input Num
if (Num is not equal 0)
then
Condition depends
{
on the value of output 1/Num
Num }
else
{
output "infinity"
}

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

Selection Example – Reciprocals (cont)


Algorithm:
input Num
if (Num is not equal 0)
Executed if Num then
is not equal to 0 {
output 1/Num
}
else
{
output "infinity"
}

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

Selection – Several Conditions (cont)


if ( today is Wednesday )
then
{
if ( the time is 10.00am )
then
{
Go to CSE1301 Lecture Often called a
} “nested selection”
}
else
...etc...etc...etc... Solution 2

1 - 40

20
Selection – At Least One of Several Conditions
• What if at least one of several conditions needs to be
satisfied?

if ( I feel hungry or the time is 1.00pm or my mate has his


eye on my lunch )
then
{
Eat my lunch now
}

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

• Also known as iteration or loop


• Example:
 Algorithm for finding the summation from 1 to a given number

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

Repetition – Example (cont)


procedure Sum1_to_n(num)
{
count = 1
sum = 0
while (count <= num)
{
add count to sum
add 1 to count
}
} Sequence may not
get executed at all

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

Repetition – Example (cont)


procedure Sum1_to_n(num)
{
count = 1
sum = 0
while (count <= num)
{
add count to sum Ensure the variables
add 1 to count used in the conditions
} are updated in each
} iteration

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

A Simple Algorithm – Calculating Effort


• INPUT: a sequence of n numbers,
 T is an array of n elements
 T[1], T[2], …, T[n]
• OUTPUT: the smallest number among them
min = T[1]
for i = 2 to n do
{
if T[i] < min
min = T[i]
}
Output min

• Performance of this algorithm is a function of n

1 - 54

27
Describing Algorithms
• Algorithms can be implemented in any programming
language

• Usually we use “pseudo-code” to describe algorithms

• Testing whether input n is prime:

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

• By using algorithm whose running time grows more


slowly, Computer B runs 20 times faster than Computer A
• For ten million numbers
 Insertion sort takes  2.3 days
 Merge sort takes  20 minutes

1 - 58

29
Summary

• Algorithm: Definitions
• Algorithm: Characteristics & Components
• Algorithm: Examples – Calculating Effort
• Algorithm: Efficiency

1 - 59

30

You might also like