Sie sind auf Seite 1von 22

Applied Data Structures

Chapter I:
Algorithms

FAP:UC

Algorithm

FAP:UC

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.
2

Algorithm

FAP:UC

Consists of a set of explicit and


unambiguous finite steps, which
when carried out for a given set of
initial conditions, produce the
corresponding output, and
terminate in a fixed amount of
time.
- Donald E. Knuth
3

Guiding Principles

Input

Output

Each instruction must be clear and unambiguous.

Effectiveness

FAP:UC

Must terminate after a finite number of steps; traceability.

Definiteness

At least 1 quantity is produced.

Finiteness

0 or more quantities which are externally supplied.

Basic and feasible.


4

Analysis of Algorithms

FAP:UC

Know the behavior of the algorithm.


This includes the pattern and
performance profile of an algorithm,
that can be measured in terms of its
execution time and the amount of space
consumed.

How to create programs


Requirements:
Information given (input) and the results to be produced
(output) must be explicitly specified.
Understand the problem.

Design:
Write an algorithm that will solve the problem according
to the requirement.

Analysis:
Trying to come up with another algorithm and compare it
with the previous one.
FAP:UC

How to create programs


Refinement and Coding:
A representation is chosen and a complete version of the
program is made.

Verification:
Consist of 3 aspects:

Program

Proving

Testing and Debugging

FAP:UC

How to analyze programs:


Two Phases:
1. Priori Estimates:
Obtain a function which bounds the algorithms complexity
time. The amount of time a single execution will take or the
number of times a statement is executed.
However, it is possible to know the exact amount of time to
execute any command unless:
a. the machine we are executing is known;
b. machine instruction set
c. time required by each machine instruction;
d. translation of the compiler from source to machine lang.
FAP:UC

How to analyze programs:


2. Posteriori Estimates:
Something to do with memory spaces consumed.
Example:
Use of arrays vs. linked-list

FAP:UC

Frequency Count and


Time Complexity (Big-Oh)
Asymptotic notations mean N
if A(n) = Amnm + + A1n1 + A0 is a polynomial of degree m,
then A(n) = O(nm).
Common computing times.
*N is the input parameter
O(1) < O(log n) < O(n) < O(n log n) < O(n2) < O(nk) < O(2n)<O(n!)

FAP:UC

10

Frequency Count and


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

FAP:UC

11

Frequency Count and


Time Complexity (Big-Oh)
O(n log n)
occurs in algorithms that solve a problem by breaking it up
into smaller sub-problems, solve them independently and
then combining the solution.

O(n2)
quadratic; can be seen in algorithms that process all pairs of
data items.

O(nk)
polynomial; algorithms that process polynomial of data
items.

FAP:UC

12

Frequency Count and


Time Complexity (Big-Oh)
O(2n)
exponential; brute-force solution
O(n!)
factorial.
Given 2 algorithms performing the same task on N inputs:
P1
P2
10n
n2/2
O(n)
O(n2)
Which is faster and efficient?
FAP:UC

13

Frequency Count and


Time Complexity (Big-Oh)
N
1
5
10
15
20
30

P1
___
___
___
___
___
___

P2
___
___
___
___
___
___

Substitute values in N and determine which among the two


algorithms is more efficient and faster.
FAP:UC

14

Frequency Count and


Time Complexity (Big-Oh)
N
1
5
10
15
20
30

P1
10
50
100
150
200
300

P2
0.5
12.5
50
112.5
200
450

P2 is more faster and efficient for N <= 20, but for N>20, P1
proves to be more faster.
FAP:UC

15

Frequency Count and


Time Complexity (Big-Oh)
Measure of Efficiency for n = 10,000
Efficiency

Big-O

Iterations

Estimated Time

Logarithmic

O(logn)

14

Microsecond

Linear

O(n)

10,000

Seconds

Linear
Logarithmic

O(nlogn)

140,000

Seconds

Quadratic

O(n2)

10,0002

Minutes

Polynomial

O(nk)

10,000k

Hours

Exponential

O(cn)

210,000

Intractable

Factorial

O(n!)

10,000!

Intractable

FAP:UC

16

General Rules
Rule 1: For Loops
The running time for a for loop is at most the running time of
the statements inside the for loop (including tests) times the
number of iterations.

Rule 2: Nested For Loops


Analyze these inside out. The total running time of a
statement inside a group of nested for is the running time of
the statement multiplied by the product of the sizes of all the
for loops.

Rule 3: Consecutive Statements


These just add, which means that the maximum is the one
that counts.
FAP:UC

17

General Rules
Rule 4: if/else Statement
For the fragment:
if(cond)
S1;
else S2;
the running time of an if/else statement is never more than
the running time of the test plus the larger of the running
times of S1 and S2.

FAP:UC

18

Exercise:
Determine the frequency count and the corresponding
Big-Oh Notation.
1.

K = 500;
for(j=1; j<=K; j++)
x = x + 1;
n = 200;

Frequency Count
1
1 + K + 1 + K
K
1

Substituting actual value for K.


F.C. = 1504
O(1)

FAP:UC

19

Exercise:
1. {
for(j=1; j<=n; j++)
for(k=1; k<=n; k++)
{
c[j][k] = 0;
for(l=1; l<=n; l++)
c[j][k] = c[j][k] * b[l][k];
}
}

FAP:UC

20

Exercise:
2. for(k = 1; k<=n+1; k++)
{
a++;
b++;
}
3. for(l=1; l<=c; l++)
for(j=c; j>=1; j--)
System.out.println(l + j);
for(k=1; k<=c; k++)
System.out.println(k);
4. while(x <= 1)
{
k = k + 1;
x = x + 1;
}

FAP:UC

21

Exercise:
5. ctr = 1;
while ctr(<=n-2)
{ for(k=1; k<=n; k++)
System.out.println(ctr = +ctr);
ctr = ctr + 1;
}
6. for(l=4; l<=n-5; l++)
for(j=5; j<=n+10; j++)
for(k=1; k<=n*n*2; k++)
y = y + 1;
a = a + 1;

FAP:UC

22

Das könnte Ihnen auch gefallen