Professional Documents
Culture Documents
Algorithms
(Introduction)
2
Questions you need to
consider in this topic
3
Data Type: the beginning
• A data type is a well-defined collection or
category of data with a well-defined set of
operations on it. (i.e., integers, float,
characters)
• A data type consists of
1. a domain (= a set of values)
2. a set of operations.
Question:
Why are data types important in programming?
Can we consider data types as basic representation of
data inside the computer?
4
What are data structures?
Question:
If we already have data types to represent data, why do we
need data structures?
What is the difference between ADTs and data types?
5
Database: An Example
• Let’s define a database as a collection of
interrelated data.
• A database as defined above will be composed
of records. Records are the basic unit that
provide a format for storing information.
• A record in turn, is usually divided into several
fields. A field holds a particular kind of data.
Question
How do records and fields in a database depict the
relationship between data types and data structures?
What possible advantage does the structure of a database
provide to the users of a program.
6
Algorithm
• Any method of solving a certain kind of
problem.
• A precise method usable by a computer
for the solution of a problem.
• A finite set of instructions which, if
followed accomplishes a particular task.
Question:
How can we determine if a particular
algorithm is efficient or not? 7
A note on algorithm
9
Analysis of algorithms
12
• Stack => A stack is a container for items which can be pushed
onto it, or removed from the top of it. Navigation through the
stack is not normally allowed, and access is limited to one unit
(or node) at a time.
14
Example 1
15
Example 1 (cont.)
tempsum = 0;
for (i=1; i<=n; i++)
{
x=System.in.read();
tempsum = tempsum + x;
}
16
Example 2
17
Example 2 (cont.)
Refinement and coding:
Representation: 1-dimensional array
void sort(int A[ ])
{
int temp, i, k, smallest;
for (i=0; i<=n-1; i++)
{
smallest = i;
for (k=i+1; k<=n; k++)
if A[smallest]>A[k]
smallest = k;
temp = A[i];
A[i] = A[smallest];
A[smallest] = temp;
}
}
18
How to analyze programs
2 Phases:
1. Priori estimates
Obtain a function which bounds the algorithm’s
complexity time.
The amount of time a single execution will take or the
number of times a statement is executed
However,
It is impossible to know the exact amount of time to execute any
command unless the following are known:
a. the machine we are executing
b. machine instruction set
c. time required by each machine instruction
d. translation of the compiler from source to machine language
19
How to analyze programs
(cont.)
2. Posteriori estimates
Something to do with memory spaces
consumed.
Question:
Which is better? Arrays or Linked list?
20
Time Complexity (Big-Oh)
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
• Algorithm efficiency is measured in two criteria:
space utilization and time efficiency.
– Space utilization is the amount of memory required to store
the data
– Time efficiency is the amount of time required to process the
data
Question:
What makes an algorithm if you sacrifice either of the
criterion?
21
Time Complexity (Big-Oh)
23
Time Complexity (Big-Oh)
24
Time Complexity (Big-Oh)
O(1)
constant; most instructions are executed
once or at most only a few times.
O(log n)
program slightly slower as N grows; normally
in programs that solve a big problem by
transforming it into a small problem, cutting
the size by some constant factor.
O(n)
linear; proportional to the size of N
25
Time Complexity (Big-Oh)
O(n log n)
occurs in algorithms that solve a problem by breaking
it up into smaller subproblems, solve them
independently and then combining the solution.
O(n2)
quadratic; can be seen in algorithms that process all
pairs of data items
O(n3)
cubic; algorithms that process triples of data items
O(2n)
exponential; brute-force solution
26
Time Complexity (Big-Oh)
• To make the difference clearer, let's compare based on the
execution time where n=100000 and time unit = 1 msec:
27
Example
P1 P2
10n n2/2
O(n) O(n2)
28
Operation Count
GENERAL RULES
30
Operation Count(cont.)
RULE 9: for Statement
Given a for statement of the form:
for (<initialization>; <condition>; <change of state>)
{
<S>;
}
The total operation count of the for statement is the operation count of the
<initialization> + operation count of the <condition> * (number of iterations + 1) +
operation count of <change of state> * (number of iterations) + operation count
of <S> * (number of iterations).
RULE 10: while Statement
Given the while statement of the form
while(<condition>)
{
<S>;
}
The total operation count is the operation count of the <condition> * (number of
iterations + 1) + operation count of <S> * (number of iterations).
31
Operation Count(cont.)
RULE 11: do-while Statement
Given the do-while statement of the form
do
{
<S>;
} while (<condition>);
The total operation count is the operation count of the <S> *
(number of iterations) + operation count of the <condition> *
(number of iteration).
RULE 12: Nested Loops
Analyze these inside out. The total operation count of a
statement inside a group of nested loops is the operation count
of the statement multiplied by the product of all the loops.
32
Examples
1. k= 100; 4. while (i<n)
for (i=1; i<=k; i++) {
x= x+1; k = k +1;
n= 200; i = i + 1;
2. for (k=1; k<=n+1; k++) }
{
System.out.println(k); 5. for (i=1; i<=c; i++)
System.out.println(n-k); for (j=c; j>=1; j--)
} System.out.println(i +” “+j);
3. while (i<=1) 6. ctr = 1;
{ while (ctr <= m-2)
k=k+1; {
I= I+1; for (k=1; k<=n; k++)
} System.out.println(ctr +” “+ k);
ctr = ctr+1;
}
33