2. 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
3. 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
▪ E.g.: sorting of a set of numbers,
▪ raw data: set of numbers in the original order
▪ Processed data: sorted numbers
Machine domain
▪ storage medium - consists of serially arranged bits that are
addressable as a unit
▪ processing unit - allow us to perform basic operations(i.e.
arithmetic, comparisons)
Solution domain - links the problem and machine domains
7. Problem Solving Process
Two related tasks at the solution domain
Structuring of higher level data representations
Synthesis of algorithms
NOTE: Data Structures and Algorithms are the
building blocks of computer programs
8. 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
9. 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
10. 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
11. 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;
}
}
12. 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;
}
}
13. 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; /* adds the new node at the start of the avail list */
head.link = n;
}
14. Mathematical Functions
Floor of x ( ⎣ x ⎦ ) - greatest integer less than or
equal to x, x is any real number
e.g.: ⎣ 3.14 ⎦ = 3
⎣ 1/2 ⎦ = 0
⎣ -1/2 ⎦ = -1
15. Mathematical Functions
Ceiling of x ( ⎡ x ⎤ ) - smallest integer greater
than or equal to x, where x is any real number
e.g.: ⎡ 3.14 ⎤ = 4
⎡ 1/2 ⎤ = 1
⎡ -1/2 ⎤ = 0
17. Mathematical Functions
Identities
⎡ x ⎤ = ⎣ x ⎦ if and only if x is an integer
⎡ x ⎤ = ⎣ x ⎦ + 1 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
18. 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