0% found this document useful (0 votes)
42 views54 pages

Representing Algorithms Algorithmic Problem Solving: Algorithm Discovery and Design

The algorithm calculates the average of three numbers in 3 steps: 1) Get values for the three numbers N1, N2, and N3 2) Set the value of Average to the sum of N1, N2, and N3 divided by 3 3) Print the value of Average

Uploaded by

Judah Martin
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)
42 views54 pages

Representing Algorithms Algorithmic Problem Solving: Algorithm Discovery and Design

The algorithm calculates the average of three numbers in 3 steps: 1) Get values for the three numbers N1, N2, and N3 2) Set the value of Average to the sum of N1, N2, and N3 divided by 3 3) Print the value of Average

Uploaded by

Judah Martin
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/ 54

Algorithm Discovery and Design

Representing Algorithms
By Yngvi Björnsson and Jia You
Algorithmic Problem Solving
Why are Algorithms Important?

IfIf we
we can
can discover
discover an an algorithm
algorithm toto perform
perform aa task,
task, we
we
can
can instruct
instruct aa computing
computing agent
agent toto execute
execute itit and
and solve
solve
the
the problem
problem for
for us.
us.
Representing Algorithms


What language to use?
 Expressive.
 Clear, presice, and unambiguous.

For example, we could use:
 Natural language (e.g. English).
 Formal programming languages (e.g. C++).
 Something else?
Example: Adding 2 numbers

•Assume we know how to add 2 single digit


numbers, but want to write an algorithm to add
any 2 numbers:
1
1 8 2
+ __________
2 6 3
4 144 5
Example using Natural Language

Initially,
Initially, set
set the
the value
value of of the
the variable
variable carry carry to to 0. 0.
When
When these
these initializations
initializations have have beenbeen completed,
completed,
begin
begin looping
looping untiluntil the
the value
value of of the
the variable
variable ii
becomes
becomes greater
greater thanthan m-1.
m-1. First
First addadd together
together
the
the values
values of of the
the two
two digits
digits aai i and
and bbi i andand the
the
current
current value
value of of the
the carry
carry digit
digit toto get
get the the result
result
called
called cci.i. Now
Now check
check thethe value
value of of cci i to
to see
see
whether
whether itit isis greater
greater than
than or or equal
equal to to 10.10. IfIf cci i isis
greater
greater than
than or or equal
equal to to 10,
10, then
then ...
...
Natural Languages

English or some other natural
language.

Are not particularly good:
 too verbose
 unstructured
 too rich in interpretation (ambiguous)
 imprecise
Example using Programming Language

{{
int
int I,
I, m,
m, Carry;
Carry;
int
int a[100],
a[100], b[100],
b[100], c[100];
c[100];
cin
cin >>
>> m;
m;
for
for (( int
int jj == 00 ;; kk <=
<= m-1
m-1 ;; j++
j++ )) {{
cin
cin >>
>> a[j];
a[j];
cin
cin >>
>> b[j];
b[j];
}}
Carry
Carry == 0;
0;
ii == 0;
0;
while
while (( ii << mm )) {{ ……
Programming Languages


Are not particularly good either:
 Too many implementation details to worry about
 Too rigid syntax

Easy to lose sight of the real task
 We don't see the forest because of all the trees!
Pseudo-code

We need a compromise between the two:

 Pseudo-code

Computer scientists use pseudo-code to express
algorithms:
 English like constructs (or other natural language), but
 modeled to look like statements in typical programming
languages.
Pseudo-code for the Addition Algorithm
Step Operation
1 Get the value of am-1, …, a0
2 Get the value of bm-1, …, b0
3 Set the value of carry to 0
4 Set the value of i to 0
5 Repeat steps 6-8 until i greater than m-1
6 Set the value of ci to ai + bi + carry
If ci >= 10, then set ci to ci - 10 and carry to 1;
7
otherwise set the value of carry to 0
8 Set value of i to i +1 (look at next digit)
9 Set cm to carry
10 Print out the final answer cm, cm-1, … c0
What kind of operations do we need?


Getting input and producing output
 Get the two numbers
 Display the outcome

Referring to values within our algorithm
 Add together the rightmost digits of the two
numbers
 Add together a0 and b0

Doing something if some condition is true
 If the outcome is greater or equal to 10 then ...

Doing something repeatedly
 Do this for all the digits in the numbers ...
Pseudo-code Primitives

Three basic kind of operations:



Sequential
 Computation ( Set … )
 Input/Output ( Get ... / Print ... )

Conditional
 If … Else
 If …

Iterative / looping
 Repeat ...
 While ...
Computation

General format:
Set
Set the
the value
value of
of <variable>
<variable> to
to <expression>
<expression>
Performs a computation and stores the result.
Example:
Set the value of C to (A + B)
Set the value of location to 0
Set the value of GPA to (sum / count)
Variables

A variable is a named storage.


- A value can be stored into it,
overwriting the previous value
- Its value can be copied
Examples:
Set the value of A to 3
The variable A holds the value 3 after its execution
Set the value of A to (A+1)
Same as: add 1 to the value of A ( A is now 4)
Not too Strict on Syntax

• Pseudo-code is kind of a programming language


without a rigid syntax, for example we can write:
– Set the value of A to (B+C)
• as
– Set A to (B+C)
• Or even:
• Set the value of sum to 0
• Set the value of GPA to 0
• as
• Set sum and GPA to 0
Sequential Operations -
Input/Output
Input
Outside world
Output

• The computing agent (computer) needs to communicate with the


outside world:
– INPUT operations allow the computing agent to receive from
the outside world data values to use in subsequent
computations.
– OUTPUT operations allow the computing agent to
communicate results of computations to the outside world.
Input

General format:

The computingGet
Get aa value
agent value forfor <variable>
(computer) <variable>
suspends
executions and waits for an input value.
Input - Examples


Examples:
 Get value for grade
 Get values for N, M

Can write:
 Get value for N1
 ...
 Get value for N100

as
 Get value for N1,..., N100
Output

General format:

Print
Print the
the value
value of
of <variable>
<variable>
Print
Print the
the message,
message, "<text>"
"<text>"
The computing agent (computer) displays the value of the variable(s).
Output - Examples


Examples:
 Print the value of grade
 Print the message, "Hello"

Can write:
 Print the value of N1
 ...
 Print the value of N100

as
 Print the values of N1,..., N100
Example

Write an algorithm to calculate the average of three
numbers.

Steps
Steps Operations
Operations
11 Get
Get values
values for
for N1
N1,, N2
N2,, and
and N3
N3
22 Set
Set the
the value
value of
of Average
Average toto ((N1
N1++N2
N2++N3)
N3)/3
/3
33 Print
Print the
the value
value ofof Average
Average
44 Stop
Stop
Conditional Operations

IfIf <condition>
<condition> then
then
operations
operations for
for the
the then-part
then-part
Else
Else
operations
operations for
for the
the else-part
else-part
1. Evaluate <condition> expression to see
whether it is true or false.
2. If true, then execute operations in then-part
3. Otherwise, execute operations in else-part.
Conditions, or Boolean Expressions

• A condition is one whose value is true or false, for


example:

–3>2 is greater than (true)


–3=2 is equal to (false)
– A > 2 is true if A’s value is
greater than 2 (at the time
this is executed), false
otherwise.
Conditions may be
E1 or E2 compounded
true if at least one of them is true; false
otherwise.
E.g. 3 > 2 or 2 >3 is true
E1 and E2
true if both are true; false otherwise
E.g. 3 > 2 and 2 >3 is false
not E
true if E is false, false if E is true
Example
1.
1. Get
Get aa value
value for
for AA
2.
2. IfIf AA == 00 then
then
3.
3. Print
Print the
the message,
message, “The
“The input
input is
is zero”
zero”

Else
Else
4.
4. Print Print the
the message,
message, “The“The input
input is
is not
not zero”
zero”
1.1. Get
Getaavalue
valuefor
forgrade
grade
2.2. IfIfgrade
grade<<11ororgrade
grade>>99 then
then
3.3. PrintPrintthe
themessage,
message,“Invalid
“Invalidgrade”
grade”
Else
Else
4.4. Set
Setthe
thevalue
valueofoftotal
totaltoto((grade
grade++total
total))
Iterative Operations - Repeat

Repeat steps i to j until <condition> becomes true


step i: operation
step i+1: operation

step j: operation

1. Execute steps i to j
2. Evaluate <condition>
3. If condition is false, go back to 1.
4. Otherwise, continue execution from step j+1.
Example

11 Get
Getaavalue
valuefor
for count
count
22 Repeat
Repeat steps
steps33 to
to55 until
until(count
(count >10)
>10)
33 Set
Set square
squareto
to(count
(count **count)
count)
44 Print
Printthe
thevalues
valuesof
of count
count and
and square
square
55 Add
Add11to
to count
count
Repeat Loops

What
What happens
happens when
when itit gets
gets
executed?
executed?

IfIfinitial
initialvalue
valuefor
for count
countisis8,
8,we
weget
getprintout
printout

88 64
64
99 81
81
10
10 100
100
Repeat Loops

IfIfinitial
initialvalue
valuefor
for count
count isis11,
11,we
weget
getprintout
printout

11
11 121
121
Why?
Why?
Because
Becausethe the body
bodyisisexecuted
executedonce
once before
beforeany
anytest
test isis
done!
done!
IfIfneed
needto
toexecute
executeloop
loop00or
ormore
moretimes
timeswe
weshould
shoulduse
use
While-loops.
While-loops.
Iterative Operation - While

While <condition> remains true do steps i to j


step i: operation
step i+1: operation

step j: operation

1. Evaluate <condition>
2. If condition is true, execute steps i to j,
then go back to 1.
3. Otherwise, if condition is false,continue
execution from step j+1.
Example

11 Get
Get aa value
value for
for count
count
22 While
While count
count << 10
10 do
do
33 Set
Set square
square toto (count
(count ** count)
count)
44 Print
Print the
the values
values ofof count
count and
and
square
square
55 Add
Add 11 to
to count
count
66 Stop
Stop
While Loops

What
What happens
happenswhen when ititgets
getsexecuted?
executed?
IfIf count
count starts
startswith
with7,
7, we
we get
get printout
printout
77 49
49
88 64
64
99 8181
What
Whatifif count
count starts
startswith
with11?
11?
Nothing
Nothingisisprinted,
printed, loop
loopisisexecuted
executed 00times.
times.
Example: Multiply (via
addition)
Steps
Steps Operations
Operations
11 Get
Get values
values for
for N
N and
and M
M
22 Set
Set the
the value
value of
of Result
Result to
to 00

33 While
While M
M >> 00 do
do steps
steps 44 and
and
55
44 Add
Add N
N to
to the
the value
value of
of
Result
Result
55 Subtract
Subtract 11 from
from M
M
N * M Example

Suppose initially N = 3 and M = 4.


During computation, the variable Result
held the following values, in that order:

Result:
Result: 00
33
66
99
12
12
Infinite Loops

Danger:
A loop can be infinite due to non-changing
conditions1
Example 1: Example 2:
Repeat until 2 > 3 While 3 > 2 do
loop body loop body
2 > 3 is false all the time. 3 >2 true all the time.
Why do these two algorithms not terminate?
1. Set the value of i to 1
Opppsss, we
2. While i < 10 do step 3 forgot to
3. Print value of i increase i
4. Stop
1. Set the value of A to 1 Even though we
2. While A is an odd change A’s value,
number do it does not change
3. Add 2 to the value the fact that “A is
of A an odd number”

4. Print the value of


Addition Algorithm Revisited
Step Operation
1 Get the value of am-1, …, a0
2 Get the value of bm-1, …, b0
3 Set the value of carry to 0
4 Set the value of i to 0
5 Repeat steps 6-8 until i greater than m-1
6 Set the value of ci to ai + bi + carry
If ci >= 10, then set ci to ci - 10 and carry to 1;
7
otherwise set the value of carry to 0
8 Set value of i to i +1 (look at next digit)
9 Set cm to carry
10 Print out the final answer cm, cm-1, … c0
Summary of Pseudocode

Sequential
Set the value of variable to expression
Input and Output
Get a value ……; Print …...
Conditional
If a condition is true then
the first set of operations
else
the second set of operations
Summary of Pseudocode

Iterative:

Repeat until a condition becomes true


the loop body

While a condition remains true do


the loop body
Exercises
I. Compute the average of 3 grades (1-9); if any one is 0
or negative, a message “Bad data” is printed

Get
Get values
values forfor x,
x, y,
y, zz
IfIf xx << 11 or
or yy << 11 or
or zz << 11 then
then
Print
Print message,
message, “Bad “Bad data”
data”
Else
Else
Set
Set Average
Average to to (x
(x ++ yy ++ z)
z) // 33
Print
Print the
the value
value of of Average
Average
Stop
Stop
Exercises
II. Compute the sum of n integers where n > 0
Get
Get value
value forfor n,
n, the
the number
number of of integers
integers
Get
Get values
values forfor II11,, II22,, …,
…, IIn,n,aa list
list of
ofnn integers
integers
Set
Set the
the value
value ofof SumSum to to 00
Set
Set the
the value
value ofof kk to to 11
Repeat
Repeat until
until kk >> nn
Add
Add IIkk toto Sum
Sum
Add
Add 11 to to kk
End
End of of the
the loop
loop
Print
Print the
the value
value of of SumSum
Stop
Stop
Exercises
III. What does the following algorithm do?

Repeat
Repeat until
until AA >> 00
Print
Print message,
message, “Enter
“Enter an
an integer”
integer”
Get
Get aa value
value for
for AA
End
End of
of the
the loop
loop
Stop
Stop
IV. Write an algorithm that does the same but using
a while loop instead of a repeat loop.
RECALL: Algorithms & Computing Agents

IfIf we
we can
can discover
discover an an algorithm
algorithm toto perform
perform
aa task,
task, we
we can
can instruct
instruct aa computing
computing agent
agent
to
to execute
execute itit to
to solve
solve the
the problem
problem for
for us.
us.
Algorithmic Problem Solving

Algorithm discovery
The process of finding a solution to a given problem

Typical Steps:
1. Understand the problem
2. Divide it into sub-problems
3. Sketch and refine, probably repeatedly
4. Test the correctness
Sequential search: an Example

Find the phone number of a given Name in


an (unsorted) list of names and their
phone numbers
Names Phone numbers

N1 T1
N2 T 2


N1000 T1000
Sequential search: 1st Attempt

1. Get values for Name, N1,…,N1000, T1,


…,T1000
2. If Name = N1 then print the value of
T1
3. If Name = N2 then print the value of
T2

1000. If Name = N999 then print the value of
T999
Sequential search: Using A Loop

Get
Get values
values for for Name,
Name, N N11,…,
,…, N N1000 , T ,…, T 1000
1000, T11,…, T1000
Set
Set the
the value
value ii to
to 11 and
and the
the value
value of of Found
Found to
to
NO
NO
Repeat
Repeat untiluntil Found
Found == Yes Yes oror ii >> 1000
1000
IfIf Name
Name == N Ni i then
then
Print
Print the
the value
value of
of TTi i
Set
Set the
the value
value ofof Found
Found to to YES
YES
Else
Else
Add
Add 11 to to the
the value
value of of ii
End
End ofof loop
loop
Stop
Stop
Selection: Find The Largest Number

Given a list of variables A1, A2, …, An, find the


largest value and its (first) location

Location A1 A2 A3 A4 A5 A6 A7
Value 5 2 8 4 8 6 4

The largest is 8 at location 3


Idea (sketch): Go through the entire list, at each
iteration find the largest-so-far and record its location
i

Location A1 A2 A3 A4 A5 A6 A7
Value 5 2 8 4 8 6 4

To begin with,
set largest-so-far to (the value of) A1
set location to 1
set i to 2
i

Location A1 A2 A3 A4 A5 A6 A7
Value 5 2 8 4 8 6 4

Compare A1 and A2
largest-so-far still holds the value of A1
set i to i+1
i

Location A1 A2 A3 A4 A5 A6 A7
Value 5 2 8 4 8 6 4

Compare A1 and A3
largest-so-far now holds the value of A3
location is 3
set i to i+1
i

Location A1 A2 A3 A4 A5 A6 A7
Value 5 2 8 4 8 6 4

Continue the similar process until i = 8


Selection: Find The Largest Number

Get
Get aa value
value for for n,
n, thethe size
size ofof the
the list
list
Get
Get values
values for for AA11,, AA22,, …,
…, AAn,n,the
the list
list to
to be
be
searched
searched
Set
Set largest_so_far
largest_so_far to to AA11 and
andset set location
location toto 11
Set
Set the
the value
value of of ii to
to 22
While
While ii is is less
less oror equal
equal to to nn do
do
IfIf AAi i >> largest_so_far
largest_so_far then then
Set
Set the
the value
value of of largest_so_far
largest_so_far to to AAi i
Set
Set the
the value
value of of location
location to to ii
Add
Add 11 to to the
the value
value of of ii
End
End ofof looploop
Print
Print the
the values
values of of largest_so_far
largest_so_far and and location
location
Algorithmic Problem Solving: Summary

Two examples of algorithmic problem solving

• Sequential search
Q: On the average, how many
comparisons (of names) does the
• algorithm
Selection make?
Q: Design a similar algorithm to find
-the smallest value and its first location
-the largest and all the locations holding it

You might also like