Professional Documents
Culture Documents
Section 2:: Data Structures & Algorithms
Section 2:: 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
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.
Section 2 Page 1 of 9
Jennifer Laraya-Llovido
Data Structures & Algorithms
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 ...
Section 2 Page 2 of 9
Jennifer Laraya-Llovido
Data Structures & Algorithms
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.
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.
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
For example
10 mod 3 =
24 mod 8 =
-5 mod 7 =
Section 2 Page 6 of 9
Jennifer Laraya-Llovido
Data Structures & Algorithms
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
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
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