Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Data Structures & Algorithms

SECTION 2:

OBJECTIVES:
At the end of the session, the student is expected to be able to:
1. write an EASY procedure to implement a given algorithm
2. Apply the floor, ceiling, and modulo functions and verify the correctness of
formulas involving these functions
3. explain the meaning of the O-notation
4. use the O-notation to express the complexity of a given function or segment code
5. give examples of algorithms belonging to a given complexity class

Algorithmic notation: the EASY way


The EASY notation is very similar to the notation called SPARKS. The following
are the statements and salient features of EASY:

1. The EASY assignment statement: the general form of the EASY assignment
statement is
variable Å expression
Where expression may be of type arithmetic, Boolean or character. To construct
these expressions, the following type of operators are provided:
(a) arithmetic operators: +, -, *, /, ^
(b) logical operators: and, or, not
(c) relational operators: <, <, =, <>, >, >
(d) other mathematical notations: └ ┘,┌ ┐, mod, log2, √, ↔, etc.
Examples: | (lower+upper)/2|, n<m and i<>j, ‘(’
Additionally, expressions may take the form fieldname(pointer variable) as in
LINK(α), INFO(LINK(α)), etc.

2. The EASY unconditional transfer statements


a. The go to statement
go to label
The go to statement causes a transfer to the statement labeled label. In general,
we avoid the use of the go to statement.

Section 2 Page 1 of 9
Jennifer Laraya-Llovido
Data Structures & Algorithms

b. The exit statement


exit
The exit statement causes transfer to the statement immediately following the
loop which contains the exit statement.

3. The EASY conditional transfer statements


a. The if statement
if cond then S1
else S2
S1 and S2 consist of one or more EASY statements. In the latter case, the group
of statements is enclosed in square brackets. The else clause is optional.
b. The case statement
case
: cond1: S1
: cond2: S2
.
.
.
:condn: Sn
:else: Sn+1
endcase

The case statement is used where we would otherwise use a sequence of if statements
of the form
if cond1 then S1
else if cond2 then S2
else ...

4. The EASY iteration statements


a. The while statement
while cond do
S
endwhile
The statement or group of statements S is repeatedly executed for as long as
cond is true. Some statement in S must either change cond to false (normal exit) or
cause transfer to a statement outside the loop (premature exit). If cond is initially
false, S is not executed at all.

Section 2 Page 2 of 9
Jennifer Laraya-Llovido
Data Structures & Algorithms

b. The repeat-until statement


repeat
S
until cond
The statement or group of statements S is repeatedly executed for as long as
cond is false. Some statement in S must either change cond to true (normal exit) or
cause transfer to a statement outside the loop (premature exit). S is executed at least
once regardless of the initial value of cond.

c. The loop-forever statement


loop
S
forever
Some statement in S must cause an exit from this otherwise infinite loop.

d. The for statement


for var Å start to finish by incr do
S
endfor
var is a variable while start, finish and incr are arithmetic expressions. var is
initially set to the value of start and is incremented [decremented] by the value of incr
after every execution of S. Looping continues until var exceeds [becomes less than] the
value of finish (normal exit) or some statement in S causes transfer to a statement
outside the for-loop (premature exit). Incr is assumed to be 1 by default; it may be
negative.

5. The EASY input/output statements


input list
output list
list is a sequence of one or more variables or quoted strings (for output).

6. The EASY declaration statements


a. The array statement
array name (l1:u1, l2:u2, ..., li:ui, ..., ln:un), ...

Section 2 Page 3 of 9
Jennifer Laraya-Llovido
Data Structures & Algorithms

name is the name given to the array; li and ui are the lower and upper bounds for the ith
dimension. By default, li is 1. More than one array may be declared in a single array
statement.
b. The node statement
node (f1, f2, ..., fi,..., fn)
fi is a field name. The node statement is used to specify the node structure of the nodes
used in a procedure.

7. The EASY control statements


a. The call statement
call name(ap1, ap2, .., api, ..., apn)
The call statement transfers control to the procedure named name.
b. The return statement
return or return(expression)
The return statement transfers control back to the procedure containing the statement.
c. The stop statement
stop
The stop statement terminates execution of the procedure and returns control to the
operating system.
d. The end statement
end
The end statement marks the physical end of a procedure. In the absence of a return
statement, reaching the end statement implies a return.

8. EASY program structure


An EASY program is a collection of one or more EASY procedures. An easy
procedure has the form
procedure name(fp1, fp2, .., fpi, ..., fpn)
S
end name
where name is the name given to the procedure and fp1, fp2,... are formal
parameters. A procedure is invoked using the call statement
call name(ap1, ap2, .., api, ..., apn)

Section 2 Page 4 of 9
Jennifer Laraya-Llovido
Data Structures & Algorithms

where name is the name of the procedure and ap1, ap2,... are the actual
parameters, which correspond on a one-to-one basis by type and size, with the
formal parameters fp1, fp2, ... in the called procedure.

A procedure can be used as a function by using the statement


return (expression)
where the value of expression is delivered as the value of the procedure. A
function-procedure is called implicitly, for instance, by using an assignment
statement,
var Å name(ap1, ap2, .., api, ..., apn)
A procedure may call itself (recursion).
Finally, EASY comments are enclosed in double slashes, as in
// Calculate allocation factors a and B.//

Sample EASY procedures


These examples are intended to illustrate the use of EASY as an algorithmic
notation.
1. Factorial function: Find n! given n> 0.
procedure FACTORIAL(n)
if n < 1 then value Å 1
else value Å n*FACTORIAL(n-1)
return(value)
end FACTORIAL
2. List inversion: Given a singly-linked linear list, invrt the list in-place.

Mathematical Functions Frequently Used in Describing Algorithms

1. Floor of x - |_ x _|
The greatest integer less than or equal to x, where x is any real number.
Example:
|_ 6.15 _| = 6, |_ 1/2 _| = 0, |_ -1/2 _| = -1

2. Ceiling of x - |¯ x ¯|
The smallest integer greater than or equal to x, where x is any real
number.

Section 2 Page 5 of 9
Jennifer Laraya-Llovido
Data Structures & Algorithms

Example:
|¯ 6.15 ¯| = 7, |¯ 1/2¯| = 1, |¯ -1/2 ¯| = 0

3. Given any two real numbers x and y, x mod y is defined as


X mod y =x if y = 0
= x – y . |_ x/y _| if y <> 0

For example

10 mod 3 =

24 mod 8 =

-5 mod 7 =

Exercises: Verify the following formulas:

1. |¯ x ¯| = |_ x _| if and only if x is an integer


2. |¯ x ¯| = |_ x _| + 1 if and only if x is not an integer
3. |_ -x _| = - |¯ x ¯|
4. |_ x _| + |_ y _| ≤ |_ x + y _|
5. x = |_ x _| + x mod 1
6. z(x mod y) = zx mod zy

The O-notation (“big-oh”)


- used to describe the time or space complexity of an algorithm
- we say that an algorithm has time complexity O(f(n)) if the actual number of
steps executed to completion is no more than some constant times f(n) for
sufficiently large n.
- we say that an algorithm has space complexity O(f(n)) if the actual amount of
space it uses to execute is no more than some constant times f(n) for
sufficiently large n.
- More formally, we say that g(n) = O(f(n)) if there exists two constants K and
n0 such that |g(n)| ≤ k . |_f(n)_| for all n ≥ n0. We do not specify the actual
values of K and n0; these constants may in fact be different for every
appearance of O.

Section 2 Page 6 of 9
Jennifer Laraya-Llovido
Data Structures & Algorithms

Example: g(n)= 13 + 23 + 33 + …+n3 steps. We assert that the algorithm runs


in, or has time complexity O(n4). To prove, let K=1 and n0 = 1. Then we have,
g(n)= 13 + 23 + 33 + …+n3 =
= [n(n+1)/2]2 ≤ 1 .n4
= n2 + n ≤ 2n2
= n ≤ n2
which is true for all n ≥ 1.
We would simply write the above example as
g(n) = [n(n+1)/2]2 = n4/4 + n3/2 + n2/4 = O(n4)
since for sufficiently large n, the dominant term will be n4/4.

The table below shows the complexity, or performance, classes of the


various algorithms we will encounter in this course:

TIME COMPLEXITY DESCRIPTION EXAMPLE


O(1) Constant Insertion into d-list
O(log2n) Logarithmic Binary search
O(n) Linear Linear search
O(n log2n) Heapsort
2
O(n ) Quadratic Dijkstra’s algo/ bubble
sort
O(n3) Cubic Floyd’s algorithm
O(2n) Exponential Algorithm for TSP

It is important to appreciate the differences among these complexity classes. One way to
do this is to determine the running time for an algorithm which takes f(n) steps to finish
for an input of size n assuming that each step takes t units of time. The table below
shows the results for t=1 usec and n=1,000,000.

Section 2 Page 7 of 9
Jennifer Laraya-Llovido
Data Structures & Algorithms

f(n) Running time


log2n 19.93 microseconds
n 1.00 second
n log2n 19.93 seconds
2
n 11.57 days
3
n 317.10 centuries
n
2 ETERNITY

Another way to illustrate the differences among time classes is to determine the size of
the largest problem that can be solved in time t by an algorithm w/c takes f(n) steps to
finish assuming that each step takes t units of time. The table below shows the results
for t=1 usec for five values of T.

T n n2 n3 2n
1 min 6.00x107 7.75 x103 3.91 x102 25
1 hour 3.60 x109 6.00 x104 1.53 x103 31
1 day 8.64 x1010 2.94 x105 4.42 x103 36
1 week 6.04 x1011 7.75 x105 8.45 x103 39
1 year 3.15 x1013 5.61 x106 3.16 x104 44

Operations On The O-Notation

1. Rule for sums: Suppose that T1(n) = O(f(n)) and T2(n) = O(g(n)). Then, T(n) =
T1(n) + T2(n) = O(max[f(n), g(n)]).

Examples:
(a) T(n) = 3n3 + 5n2 = O(n3)
(b) T(n) = 2n + n4 + n log2n = O(2n)

Section 2 Page 8 of 9
Jennifer Laraya-Llovido
Data Structures & Algorithms

2. Rule for products: Suppose that T1(n) = O(f(n)) and T2(n) = O(g(n)). Then, T(n) =
T1(n) . T2(n) = O(f(n). g(n)).
Example: The time complexity of the following segment of code is O(n2).

for i Å 1 to n do
for j Å i to n do
steps which take o(1) time
endfor
endfor

The steps in the inner loop (which take constant time) are executed n + n-1 + n-2
+ ...+ 2 + 1 times. This is the sum of the first n integers which is n(n+1)/2 = n2/2 + n/2
= O(n2).

Section 2 Page 9 of 9
Jennifer Laraya-Llovido

You might also like