properties of , and

advertisement

PROPERTIES OF

,

AND

For all the properties assume that f, g, h : N R. That is the functions map non negative integers in to non negative reals.

1.

If f E O(g) and g E O(h) then f E O(h); that is O is transitive .Also

,

are transitive.

2.

f E O(g) if and only if g E

(f).

3.

If f E

 g , then g

 

(f).

4.

defines an equivalence relation on the functions.

5.

(f+g) =

(max(f,g)). Similar equations hold for

and

.

(They are use when analysing complex algorithms, where f and g may describe the work done by different parts of the algorithm).

REPRESENTATIONS OF VARIOUS SERIES:

POLYNOMIAL :

n d d+1

i =

( n ) n+1

LOGARITHMIC : n

log(i) =

(n log(n))

i=1

POLYNOMIAL LOGARITHMIC :

n d d+1

i log(i) =

(n log(n)) i=1

The selection problem is the problem of finding an element with the k th.

smallest key in E. Such an element is said to have rank K.

(eg) A problem to find the second largest number.

A problem to find the median of set of numbers.

ADVERSORY ARGUMENTS

Suppose we have an algorithm that we think is efficient. Imagine an adversary who wants to prove otherwise. At each point in the algorithm where a decision ( a key comparison) is made , the adversary tells us the result of the decision . The adversary chooses its answers to try to force the algorithm to work hard. The only constraint is the answers must be internally consistent.

If the adversary can force the algorithm to perform f(n) steps, then f(n) is a lower bound for the number of steps done in the worst case.

FINDING MAX AND MIN

To find the max and to find the min among the remaining values we need (n-1) + (n-2) or 2n-3 comparisons.

By using adversary arguments method we find the largest of the winner and the smallest of the losers. The total no. of comparisons is

3n/2

-2.

TO FIND MAX AND MIN OF n KEYS BY COMPARISON OF KEYS

Assumption – keys are distinct

Let X is max Y is min

(ie) Every other key other than X has (ie) Every other key other than Y has

lost some comparison. won some comparison.

If we count each win and loss as one unit of information , then an algorithm must have at least

2n-2 units of information to be sure of giving the correct answer.

Key status Meaning

W Has won at least one comparison and never won.

L Has lost at least one comparison and never won.

WL Has won and lost at least one comparison.

N Has not yet participated in a comparison.

Status of keys compared by an algorithm

N,N

W,N or WL,N

L,N

W,W

L,L

W,L or WL,L or W,WL

WL,WL

Adversary response

x>y x>y x<y x>y x>y x>y

Consistent with assigned values

New status Units of new information

W,L 2

W,L or WL,L 1

L,W

W,WL

1

1

WL,L

No change

No change

1

0

0

Totally in worst case it takes around 3n/2 –2 comparisons.

If we give elements 8, 5, 16, 7

8 5 16 7 comparison x1 x2 x3 x4 .

status status status status

value value value value x1, x2 x1, x3 x1, x4 x2, x3

W,8

WL,8

L,5 N

L,5

W, 16

W,16

N

L, 7

WL,7

8 5 16 7 comparison x1 x2 x3 x4 .

status status

value value

status status

value value x1, x2 W,8 L,5 N N x1, x3 x1, x4 x2, x3

W,8

WL,8

L,7

L,5 WL,7

W,16

(3 * 4)/2 – 2 = 12/2 –2

=6-2 = 4

min max: 2n –3

=2*4 –3 = 8-3 = 5

Comparands winner

x1, x2

x1, x3

x1, x4 x1 x1 x4 x4 x4, x5

max = x4.

Second largest is either x5 or x1. Thus only one more comparison is needed to find the second largest in this example.

* Any key that loses to a key other than max cannot possibly be second largest.

TOURNAMENT METHOD

The tournament method is so named because performs comparisons in the same way that tournaments are played. Keys are paired off and compared in rounds. In each round after the first one, the winners from the preceding round are paired off and compared .If at any round the number of keys is odd one of them simply waits for the next round. A tournament can be described by a binary tree diagram. Each leaf contains a key and at each subsequent level the parent of each pair contains the winner. The root contains the largest key.

M

M

G

R

R M G D

C

M F G T D M

M is the winner , we need to find the runner. The runner must be someone who lost to M but who did not loose to anyone.

The runner may be G, R, F .The players who have lost to the winner play a second tournament to determine the runner up. This second tournament needs only to be repeated along the path that the winner (M) followed as he rose through the tree.

For a tournament with n players there are

 lg n

levels hence

 lg n

-1 comparisons are required for second tournament. The algorithm totally requires n-2 +

 lg n

comparisons.

AN ALGORITHM TO FIND THE LARGEST AND SECOND LARGEST:

If ( E[1]> E[2] )

max = E[1];

second = E[2]; else

max = E[2];

second = E[2]; for(i=3;i<=n;i++)

if ( E[i] > second)

if ( E[i] > max) second=max; else max= E[i]; second = E[i];

SEARCHING TECHNIQUES

Sequential search / linear search

Binary search

SEQUENTIAL SEARCH:

Sequential search looks at elements, one at a time, from the first in the list until a match for the target is found. If a particular key value is further down the list, the time taken will be longer to find that key.

SEQUENTIAL SEARCH ALGORITHM:

Sequential search (list, target, N)

list - The elements to be searched.

target -The value being searched for.

N – The number of elements in the list.

for i=1 to N do

if (target = list[i] )

return;

end if

end for

return 0

Worst case analysis

Two worst cases for sequential analysis.

The key element is the last element.

The element is not in the list.

In the cases the max number comparisons = N comparisons.

*NOTE

Upper bound

is a concept based on the problem to be solved

Worst case

is based on the way a particular algorithm solves that problem.

AVERAGE CASE ANALYSIS:

Target in the list + target not in the list

If target in the list

We can assume that all of the probabilities are equally likely (ie) the element can be

in first , second….n

th location in the list.

N

A(N) =1/N

i i=1

A(N) = 1/N*N(N+1)/2

=1/2(N+1)

Target not in the list, then there are N+1 possibilities

N

A(N) = 1/N+1 *[ (

 i )+N ] i=1

A(N) ~ 1/2(N+2) difference ½ not significant

BINARY SEARCH TECHNIQUE

Example for divide and conquer algorithm technique.

Divide and conquer approach divide the problem in to smaller instances of the same problem then solve the smaller instances recursively and finally combine the solutions to original input.

BINARY SEARCH

Input : E, fist, last and k, where E is an ordered array in the range first………last and k is the key sough.

For simplicity we assume that k and the entries of E are integers.

Output : Index such that E[index] = k , if k is in E within the range

First……..last and index= -1 if k is not in this range of E

int binary search ( int[]E, int first, int last, int K)

int (last < first)

index= -1;

else int mid= (first +last)/2

if ( k==E[mid])

index = mid;

else if (k < E[mid] )

index = binary search ( E, first, mid-1, k );

else

index = binary search ( E, mid+1, last, k );

return index

Binary search is an efficient algorithm for searching in a sorted array. If works by comparing a search key k with the arrays middle element A[m]. if they match algorithm stops; otherwise the same operation is repeated recursively for the first half of the array if k< A[m] and for the second half if k > A[m].

A[0]…………A[m-1] A[m] A[m+1]…………A[n-1]

Search here if k < A[m] search here if k > A[m]

Worst case analysis

When N= 2 k-1 the number of passes required is almost k passes.

No. of passes required = no. of levels.

Worst case analysis

K= lg (N+1)

Average case analysis

Target is in the list.

Target is not in the list.

k i-1 k

A(N) =1/N

i2 ofr N=2 -1

i=1

A(N) ~ lg (N+1) -1

4

If the target is not in the list

k i- 1 k

A( N)=1/(2N+1) [ (

i 2 ) + (N+1)k ] for N= 2 -1

i=1

A(N)~ k -1/2 = lg (N+1)-1/2 for N=2k-1

TOURNAMENT METHOD

Analysis

Every comparison produces one winner, one loser .The losers are eliminated and only the winners move up in the tree. Each element, except for the largest, must one comparison.

Therefore building the tournament tree will take comparisons.

The second largest element could only have last to the largest element. We go down the tree and get the set of element. We go down the tree and get the set of elements that lost to the largest one.

So we know there are almost

 lg N

of these elements.

There will be

 lg N

comparisons to find these elements in the tree and

 lg N

-1 comparisons to find the largest in this collection.

Entire process = N-1 +

 lg N

+

 lg N

- 1

= N + 2

 lg N

-2

6

8 u

8

6

6 3

3

2 8

8

7 1

5

5

Download