Chapter01-Basic Concepts and Notations

You might also like

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

1 Basic Concepts and

Notations

Data Structures Basic Concepts and Notations

Objectives
At the end of the lesson, the student should be able to:

Explain the process of problem solving

Define data type, abstract data type and data structure

Identify the properties of an algorithm

Differentiate the two addressing methods - computed


addressing and link addressing
Use the basic mathematical functions to analyze
algorithms
Measure complexity of algorithms by expressing the
efficiency in terms of time complexity and big-O notation
Data Structures Basic Concepts and Notations

Problem Solving Process

Programming a problem-solving process which could be


viewed in terms of the following domains:

Problem domain

input or the raw data to process

output or the processed data

Machine domain

storage medium - consists of serially arranged bits that are addressable


as a unit
processing unit - allow us to perform basic operations

Solution domain - links the problem and machine domains

Data Structures Basic Concepts and Notations

Problem Solving Process


Problem Domain

Data Structures Basic Concepts and Notations

Problem Solving Process


Machine Domain

Data Structures Basic Concepts and Notations

Problem Solving Process


Solution Domain

Data Structures Basic Concepts and Notations

Problem Solving Process

Two related tasks at the solution domain

Structuring of higher level data representations

Synthesis of algorithms

Data structures and algorithms are the building blocks of


computer programs
Data Structures Basic Concepts and Notations

Data Type, Abstract Data


Type and Data Structure

Data type - kind of data that variables can assume in a


programming language and for which operations are
automatically provided
Abstract Data Type (ADT) - mathematical model with
defined operations. In Java, an ADT can be expressed with
an interface

Data Structures Basic Concepts and Notations

Data Type, Abstract Data


Type and Data Structure
public interface Stack{
public int size();
/* returns the size of the stack */
public boolean isEmpty(); /* checks if empty */
public Object top() throws StackException;
public Object pop() throws StackException;
public void push(Object item) throws StackException;
}

Data structure the implementation of ADT in terms of the


data types or other data structures.In Java, a data structure
can be expressed with a class

Data Structures Basic Concepts and Notations

Algorithm

Finite set of instructions which, if followed, will accomplish a


task

Finiteness - an algorithm must terminate after a finite number of


steps

Definiteness - ensured if every step of an algorithm is precisely


defined

Input - domain of the algorithm which could be zero or more


quantities

Output - set of one or more resulting quantities; also called the range
of the algorithm

Effectiveness - ensured if all the operations in the algorithm are


sufficiently basic that they can, in principle, be done exactly and in
finite time by a person using paper and pen
Data Structures Basic Concepts and Notations

10

Addressing Methods

Computed Addressing Method - used to access the


elements of a structure in pre-allocated space
int x[10][20];
a = x[4][3];
Link Addressing Method used to manipulate dynamic
structures where the size and shape are not known
beforehand or changes at runtime
class Node{
Object info;
Node link;
Node() { }
Node (Object o, Node l){
info = o;
link = l;
}
}

Data Structures Basic Concepts and Notations

11

Addressing Methods

Memory pool or avail list - source of the nodes from which linked
structures are built

class AvailList {
Node head;
AvailList(){
head = null;
}

AvailList(Node n){
head = n;
}

Data Structures Basic Concepts and Notations

12

Addressing Methods

Two basic procedures that manipulate the avail list are getNode and
retNode, which requests for a node and returns a node respectively.
Node getNode(){
Node a;

if ( head == null) {
return null; /* avail list is empty */
} else {
a = head.link; /* assign node to return to a */
head = head.link.link;
return a;
}

void retNode(Node n){


n.link = head.link;
}

head.link = n;

/* adds the new node at the


start of the avail list */

Data Structures Basic Concepts and Notations

13

Mathematical Functions

Floor of x ( x ) - greatest integer less than or equal to x, x


is any real number
Ceiling of x ( x ) - smallest integer greater than or equal to
x, where x is any real number
Modulo - given any two real numbers x and y,

x mod y = x
=x-y*

x/y

if y = 0
if y <> 0

Data Structures Basic Concepts and Notations

14

Mathematical Functions

Identities

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

Data Structures Basic Concepts and Notations

15

Complexity of Algorithms

Algorithm Efficiency

Space utilization - amount of memory required to store the data

Time efficiency - amount of time required to process the data

Execution time - amount of time spent in executing instructions of a


given algorithm. Notation: T(n). Several factors that affect the execution
time include:

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 Basic Concepts and Notations

16

Complexity of Algorithms

The Big-Oh Notation (or simply O-Notation)

T(n) grows at a rate proportional to n and thus T(n) is said to have


order of magnitude n denoted by the O-notation: T(n) = O(n)

Formal definition:
g(n) = O(f(n)) if there exists two constants c and n0 such that
| g(n) | <= c * | f(n) | for all n >= n0.

Operations on the O-Notation:

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) ) ).
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) ).
Data Structures Basic Concepts and Notations

17

Complexity of Algorithms
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) ) ).
Proof: By definition of the O-notation,
T1(n) <= c1 f(n)
for n >= n1 and
T2(n) <= c2 g(n) for n >= n2.
Let n0 = max(n1, n2). Then
T1(n) + T2(n)
<= c1 f(n) + c2 g(n)
<= (c1 + c2) max(f(n),g(n))
<= c max ( f(n), g(n) )
Thus,

n >= n0.
n >= n0.
n >= n0.

T(n) = T1(n) + T2(n) = O( max( f(n), g(n) ) ).


e.g.

a. T(n) = 3n3 + 5n2 = O( n3 )


b. T(n) = 2n + n4 + n log n = O( 2n )
Data Structures Basic Concepts and Notations

18

Complexity of Algorithms
Examples
Consider the algorithm below :
for (i=1; i <= n, i++)
for (j=1; j <= n, j++)
// steps which take O(1) 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

= n2 / 2 + n / 2

= O( n2 )

Data Structures Basic Concepts and Notations

19

Complexity of Algorithms
Examples
LINEAR SEARCH ALGORITHM
1
2
3
4
5
6

T( n ) = 3n + 3 so that T( n ) = O( n )
found = false;
loc
= 1;
Since g( n ) <= c f( n ) for n >= n 0, then
while ((loc <= n) && (!found)){
if (item == a[loc]found = true;
else loc = loc + 1;
3n + 3 <= c n
}

STATEMENT
1
2
3
4
5

# of times executed
1
1
n+1
n
n

3n + 3 <= c

= 3 + 3 / n <= c

--------n
Thus c = 4 and n0 = 3.

Data Structures Basic Concepts and Notations

20

Complexity of Algorithms

The Big-Oh Notation


Big-Oh

Description

Algorithm

O(1)
O(log2n)

Constant
Logarithmic

Binary Searc h

O(n)
O(n log2n)

Linear

Sequential Search

O(n2)
O(n3)

Quadratic

Insertion Sort

Cubic

Floyds Algorithm

O( 2n )

Exponential

Heapsort

F(n)
log2n
n
n log2n
n2
n3
2n

Running Time
19.93 microseconds
1.00 seconds
19.93 seconds
11.57 days
317.10 centuries
Eternity

Data Structures Basic Concepts and Notations

21

Complexity of Algorithms
General rules on determining the running time of an algorithm

FOR loops

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 the sizes of all the
for loops.

CONSECUTIVE STATEMENTS

At most the running time of the statement inside the for loop times
the number of iterations.

The statement with the maximum running time.

IF/ELSE

Never more than the running time of the test plus the larger of the
running times of the conditional block of statements.
Data Structures Basic Concepts and Notations

22

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
Data Structures Basic Concepts and Notations

23

You might also like