J.E.D.I
while the following method in the class Avail returns a node to the avail list: void retNodeNode n{
n.link = head.link; adds the new node at the start
of the avail list head.link = n;
}
Figure 1.4 Return a Node Figure 1.5
The two methods could be used by data structures that use link allocation in getting nodes from, and returning nodes to the memory pool.
1.7 Mathematical Functions
Mathematical functions are useful in the creation and analysis of algorithms. In this section, some of the most basic and most commonly used functions with their properties
are listed.
• Floor of x - the greatest integer less than or equal to x, where x is any real number.
Notation:
x
e.g.
3.14
= 3
12
= 0
-12
= - 1
• Ceiling of x - is the smallest integer greater than or equal to x, where x is any real
number. Notation :
x
Data Structures 14
J.E.D.I
e.g.
3.14
= 4
12
= 1
-12
= 0
• Modulo - 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
e.g. 10 mod 3 = 1 24 mod 8 = 0
-5 mod 7 = 2
Identities
The following are the identities related to the mathematical functions defined above: •
x
=
x
if and only if x is an integer
•
x
=
x
if and only if x is not an integer •
- x
= -
x
•
x
+
y
=
x + y
• x =
x
+ x mod 1
• z x mod y = zx mod zy
1.8 Complexity of Algorithms
Several algorithms could be created to solve a single problem. These algorithms may vary in the way they get, process and output data. Hence, they could have significant
difference in terms of performance and space utilization. It is important to know how to analyze the algorithms, and knowing how to measure the efficiency of algorithms helps a
lot in the analysis process.
1.8.1 Algorithm Efficiency
Algorithm efficiency is measured in two criteria: space utilization and time efficiency. Space utilization is the amount of memory required to store the data while time
efficiency is the amount of time required to process the data.
Before we can measure the time efficiency of an algorithm we have to get the execution time. Execution time is the amount of time spent in executing instructions of a given
algorithm. It is dependent on the particular computer hardware being used. To express the execution time we use the notation:
Tn, where T is the function and n is the size of the input
There are several factors that affect the execution time. These are: •
input size •
instruction type •
machine speed •
quality of source code of the algorithm implementation •
quality of the machine code generated from the source code by the compiler
Data Structures 15
J.E.D.I
The Big-Oh Notation
Although Tn gives the actual amount of time in the execution of an algorithm, it is easier to classify complexities of algorithm using a more general notation, the Big-Oh or
simply O notation. Tn grows at a rate proportional to n and thus Tn is said to have “order of magnitude n” denoted by the O-notation:
Tn = On
This notation is used to describe the time or space complexity of an algorithm. It gives an approximate measure of the computing time of an algorithm for large number of
input. Formally, O-notation is defined as:
gn = Ofn if there exists two constants c and n such that
| gn | = c | fn | for all n = n .
The following are examples of computing times in algorithm analysis:
Big-Oh Description
Algorithm
O1 Constant
Olog
2
n Logarithmic
Binary Search On
Linear Sequential Search
On log
2
n Heapsort
On
2
Quadratic Insertion Sort
On
3
Cubic Floyd’s Algorithm
O 2
n
Exponential To make the difference clearer, lets compare based on the execution time where
n=100000 and time unit = 1 msec:
Fn Running Time
log
2
n 19.93 microseconds
n 1.00 seconds
n log
2
n 19.93 seconds
n
2
11.57 days n
3
317.10 centuries 2
n
Eternity
1.8.2 Operations on the O-Notation
Data Structures 16
J.E.D.I
• Rule for Sums Suppose that T
1
n = O fn and T
2
n = O gn . Then, tn = T
1
n + T
2
n = O max fn, gn .
Proof : By definition of the O-notation, T
1
n ≤
c
1
fn for n
≥ n
1
and T
2
n ≤
c
2
gn for n
≥ n
2
. Let n
= maxn
2
, n
2
. Then T
1
n + T
2
n ≤
c
1
fn + c
2
gn n
≥ n
. ≤
c
1
+ c
2
maxfn,gn n
≥ n
. ≤
c max fn, gn n
≥ n
.
Thus, Tn = T
1
n + T
2
n = O max fn, gn .
For example, 1. Tn = 3n
3
+ 5n
2
= On
3
2. Tn = 2
n
+ n
4
+ nlog
2
n = O2
n
• Rule for Products Suppose that T1n = O fn and T2n = O gn .
Then, Tn = T1n T2n = O fn gn . For example, consider the algorithm below:
forint i=1; in-1; i++{ forint i=1; i=n; i++{
steps taking O1 time }
} Since the steps in the inner loop will take
n + n-1 + n-2 + ... + 2 + 1 times, then
nn+12 = n
2
2 + n2 = On
2
Example: Consider the code snippet below:
for i=1; i = n, i++ for j=1; j = n, j++
steps which take O1 time Since the steps in the inner loop will take n + n-1 + n-2 + ... + 2 + 1 times,
then the running time is
n n+1 2 = n 2
2 + n 2 = O n
2
Data Structures 17
J.E.D.I
1.8.3 Analysis of Algorithms
Example 1: Minimum Revisited
1. public class Minimum { 2.
3. public static void mainString[] args {
4. int a[] = { 23, 45, 71, 12, 87, 66, 20, 33, 15, 69 };
5. int min = a[0];
6. for int i = 1; i a.length; i++ {
7. if a[i] min min = a[i];
8. }
9. System.out.printlnThe minimum value is: + min;
10. } 11.}
In the algorithm, the declarations of a and min will take constant time each. The constant time if-statement in the for loop will be executed n times, where n is the
number of elements in the array a. The last line will also execute in constant time.
Line Times Executed
4 1
5 1
6 n+1
7 n
9 1
Using the rule for sums, we have:
Tn = 2n +4 = On
Since gn = c fn for n = n , then
2n + 4 = cn 2n + 4 = c
--------- n
2 + 4n = c Thus c = 3 and n
= 4. Therefore, the minimum algorithm is in On.
Example 2: Linear Search Algorithm
1 found = false;
2 loc = 1;
3 while loc = n found{
4 if item == a[loc]found = true;
Data Structures 18
J.E.D.I
5 else loc = loc + 1;
6 }
STATEMENT of times executed
1 1
2 1
3 n + 1
4 n
5 n
T n = 3n + 3 so that T n = O n Since g n = c f n for n = n 0, then
3n + 3 = c n 3n + 3n = c = 3 + 3 n = c
Thus c = 4 and n0 = 3.
The following are the general rules on determining the running time of an algorithm: ● FOR loops
➔ At most the running time of the statement inside the for loop times the number of iterations.
● NESTED FOR loops ➔ Analysis is done from the inner loop going outward. The total running time of
a statement inside a group of for loops is the running time of the statement multiplied by the product of thesizes of all the for loops.
● CONSECUTIVE STATEMENTS ➔ The statement with the maximum running time.
● IFELSE ➔ Never more than the running time of the test plus the larger of the running
times of the conditional block of statements.
Data Structures 19
J.E.D.I
1.9 Summary
• Programming as a problem solving process could be viewed in terms of 3 domains
– problem, machine and solution. •
Data structures provide a way for data representation. It is an implementation of ADT.
• An algorithm is a finite set of instructions which, if followed, will accomplish a
task. It has five important properties: finiteness, definiteness, input, output and effectiveness.
• Addressing methods define how the data items are accessed. Two general types
are computed and link addressing. •
Algorithm efficiency is measured in two criteria: space utilization and time efficiency. The O-notation gives an approximate measure of the computing time
of an algorithm for large number of input
1.10 Lecture Exercises
1. Floor, Ceiling and Modulo Functions. Compute for the resulting value: a
-5.3
b
6.14
c 8 mod 7
d 3 mod –4 e –5 mod 2
f 10 mod 11 g
15 mod –9 +
4.3
2. What is the time complexity of the algorithm with the following running times?
a 3n
5
+ 2n
3
+ 3n +1 b n
3
2+n
2
5+n+1 c n
5
+n
2
+n d n
3
+ lg n + 34 3. Suppose we have two parts in an algorithm, the first part takes Tn
1
=n
3
+n+1 time to execute and the second part takes Tn
2
= n
5
+n
2
+n, what is the time complexity of the algorithm if part 1 and part 2 are executed one at a time?
4. Sort the following time complexities in ascending order. 0n log
2
n 0n
2
0n 0log
2
n 0n
2
log
2
n 01
0n
3
0n
n
02
n
0log
2
log
2
n
5. What is the execution time and time complexity of the algorithm below?
Data Structures 20
J.E.D.I
void warshallint A[][], int C[][], int n{ forint i=1; i=n; i++
forint j=1; j=n; j++ A[i][j] = C[i][j];
forint i=1; i=n; i++ forint j=1; j=n; j++
forint k=1; k=n; k++ if A[i][j] == 0 A[i][j] = A[i][k] A[k][j];
}
Data Structures 21
J.E.D.I
2 Stacks
2.1 Objectives