24
ALGORITHM CLASSS Algorithm Analysis DS C C++ JAVA TRAINING INSTITUTES IN KPHB HYDERABAD

Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally, KPHB, Hyderabad.)

Embed Size (px)

Citation preview

ALGORITHM CLASSS

Algorithm Analysis

DS C C++ JAVA TRAINING INSTITUTES IN KPHB HYDERABAD

Program=instructions + data

Description of algorithms and data structures to achieve a specific objective

Could be done in any language, even a natural language like Wnglish

Example add a and b

Read a ; read b; c=a+b; // Algorithm

A , b and c are data structures

Programming language: A standard notation for writing programs

Like english or telugu

Examples: C, Java, intel assemly language, machine language etc

Hardware understands machine language but we instructs using programming language.

Hence we need the program translators like compiler gcc

C++ TRAINING INSTITUTES IN KPHB HYDERABAD

What is a computer program

ALGORITHM CLASSS

ALGORITHM CLASSS

If you are strong with data structures, you can

evaluate the quality of a program

(Analysis of Algorithms: Running time and memory space )

write fast,efficient and quality programs with less memory usage

solve new problems efficiently by choosing appropriate data structures and

algorithms

C++ TRAINING INSTITUTES IN KPHB HYDERABAD

Why do we need this course

ALGORITHM CLASSS

How smart you are to pick the appropriate data structure for a given problem(if you

know pros and cons of different DS only you can decide)

How strong you are on programming basics (DS)

Ability to decompose problems

Ability to find solutions with a better logic

Efficient programming skills

… etc.

DS TRAINING INSTITUTES IN KPHB HYDERABAD

Why interviewer concentrates more on DS

ALGORITHM CLASSS

Data Structures: A systematic way of organizing and accessing data

.

--No single data structure works well for ALL purposes.

Algorithm is a step-by-step procedure for solving a problem in a finite amount of time.

Algorithm analysis a process of determining the amount of time, resource, etc. required when executing an

algorithm

Estimate the running time

Estimate the memory space required.

Depends on the input size

DS TRAINING INSTITUTES IN KPHB HYDERABAD

What is an algorithm analysis

ALGORITHM CLASSS

There are two aspects of algorithmic performance: Time

Instructions take time. How fast does the algorithm perform? What affects its runtime?

Space Data structures take space What kind of data structures can be used? How does choice of data structure affect the runtime?

We will focus on time: How to estimate the time required for an algorithm How to reduce the time required

Analysis of Algorithms is the area of computer science that provides tools to analyze the efficiency of different methods of solutions. How do we compare the time efficiency of two algorithms that solve the same problem? Naïve Approach: implement these algorithms in a programming language (C++), and run them to compare their time requirements. Comparing the programs (instead of algorithms) has difficulties.

JAVA TRAINING INSTITUTES IN KPHB HYDERABAD

What is an algorithm analysis

ALGORITHM CLASSS

To analyze algorithms: First, we start to count the number of significant operations in a particular solution to assess its efficiency. Then, we will express the efficiency of algorithms using growth functions.

Growth Rate We measure an algorithm’s time requirement as a function of the problem size.

Problem size depends on the application: e.g. number of elements in a list for a sorting algorithm, the number disks for towers of hanoi.

So, for instance, we say that (if the problem size is n) Algorithm A requires 5*n2 time units to solve a problem of size n. Algorithm B requires 7*n time units to solve a problem of size n.

The most important thing to learn is how quickly the algorithm’s time requirement grows as a function of the problem size.

Algorithm A requires time proportional to n2. Algorithm B requires time proportional to n.

An algorithm’s proportional time requirement is known as growth rate. We can compare the efficiency of two algorithms by comparing their growth rates.

C TRAINING INSTITUTES IN KPHB HYDERABAD

What is an algorithm analysis

ALGORITHM CLASSS

What is an algorithm analysis

Function Growth Rate Name

c Constant

log N Logarithmic

log2N Log-squared

N Linear

N log N

N2 Quadratic

N3 Cubic

2N Exponential

ALGORITHM CLASSS

Experimental Studies

Write a program implementing the algorithm

Run the program with inputs of varying size and composition

Use a method like System.currentTimeMillis() to get an accurate measure of the actual

running time

Plot the results and compare

Limitations of the experiments

It is necessary to implement the algorithm, which may be difficult

Results may not be indicative of the running time on other inputs not included in the

experiment.

In order to compare two algorithms, the same hardware and software environments must

be used

JAVA TRAINING INSTITUTES IN KPHB HYDERABAD

What is an algorithm analysis

ALGORITHM CLASSS

Theoretical Analysis

Uses a high-level description of the algorithm instead of an implementation

Characterizes running time as a function of the input size, n.

Takes into account all possible inputs

Allows us to evaluate the speed of an algorithm independent of the hardware/software

environment

Asymptotic Algorithm Analysis

The asymptotic analysis of an algorithm determines the running time in big-Oh notation

To perform the asymptotic analysis

We find the worst-case number of primitive operations executed as a function of the

input size

We express this function with big-Oh notation

What is an algorithm analysis

(Big-Oh)

T(n) is O(F(n)) if there are positive constants c and n0 such that

T(n)<= cF(n) when n >= n0

Provides an upper bound on the growth rate of the function.

(Big-Omega)

T(n) is Ω(F(n)) if there are positive constant c and n0 such that T(n) >= cF(n) when n >= n0

we want to say that an algorithm takes at least a certain amount of time,

Def: (Big-Theta) T(n) is Θ(F(n)) if and only if T(n) = O(F(n)) and T(n) = Ω(F(n)) => k1*F(n) <

Def: (Little-Oh) T(n) = o(F(n)) if and only if

T(n) = O(F(n)) and T(n) != Θ (F(n))

we cannot say here what the value of c_1c1 is, because it depends on the speed of the computer, the programming language used, the compiler or interpreter that translates the source program into runnable code, and other factors.

C++ TRAINING INSTITUTES IN KPHB HYDERABAD

Algorithm analysis: other notations

ALGORITHM CLASSS

Asymptotic Algorithm Analysis

The asymptotic analysis of an algorithm determines the running time in big-Oh notation

To perform the asymptotic analysis

We find the worst-case number of primitive operations executed as a function of

the input size

We express this function with big-Oh notation

Example:

We determine that algorithm arrayMax executes at most 6n 1 primitive

operations

We say that algorithm arrayMax “runs in O(n) time”

Since constant factors and lower-order terms are eventually dropped anyhow, we

can disregard them when counting primitive operations

Algorithm analysis: BIG-Oh notation

ALGORITHM CLASSS

Big O notation:

Big Oh notation is used to capture the most dominant term in a function, and to represent

the growth rate.

Also called asymptotic upper bound.

Ex: 100n3 + 30000n =>O(n3)

To simplify the running time estimation, for a function f(n), we ignore the constants and

lower order terms.

Example: 10n3+4n2-4n+5 is O(n3).

http://sites.google.com/site/algorithmclass

BIG-Oh notation

http://sites.google.com/site/algorithmclass

BIG-Oh notation

http://sites.google.com/site/algorithmclass

Theta

Definition: A theoretical measure of the execution of an algorithm, usually the time or memory needed, given the problem size n, which is usually the number of items. Informally, saying some equation f(n) = Θ (g(n)) means it is within a constant multiple of g(n). The equation is read, "f of n is theta g of n". Formal Definition: f(n) = Θ (g(n)) means there are positive constants c1, c2, and k, such that 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ k. The values of c1, c2, and k must be f ixed for the function f and must not depend on n.

http://plus.google.com/+AlgorithmClass

Omege

(Big-Omega)

T(n) is Ω(F(n)) if there are positive constant c and n0 such that T(n) >= cF(n) when n >= n0

we want to say that an algorithm takes at least a certain amount of time,

https://plus.google.com/+AlgorithmClass

BIG-Oh notation

Definition: A theoretical measure of the execution of an algorithm, usually the time or

memory needed, given the problem size n, which is usually the number of items. Informally,

saying some equation f(n) = O(g(n)) means it is less than some constant multiple of g(n).

The notation is read, "f of n is big oh of g of n".

Formal Definition: f(n) = O(g(n)) means there are positive constants c and k, such that 0 ≤ f(n) ≤ cg(n) for all n ≥ k. The values of c and k must be fixed for the function f and must not depend on n.

ALGORITHM CLASSS

Primitive Operations Basic computations performed by an algorithm

Largely independent from the programming language

Examples:

Evaluating an expression

Assigning a value to a variable

Indexing into an array

Calling a method

Returning from a method

Algorithm analysis: BIG-Oh notation

Algorithm arrayMax(A, n)

currentMax=a[0]; // 2

for (i=1;i<n;i++) // 2n //(i=1 once, i<n n

times, i++ (n-1) times)

if (a[i] > currentMax) 2(n-1)

currentMax=a[i] 2(n-1)

return currentMax; 1

total time= 6n-1

Function Name

C Constant

LogN Logarithmic

Log2N Log-squared

N Linear

NlogN NlogN

N2 Quaratic

N3 Cubic

2n Exponential

Algorithm analysis: BIG-Oh notation

Algorithm analysis: BIG-Oh notation

Algorithm analysis: BIG-Oh notation

Worst-case vs. Average-case

A worst-case bound is a guarantee over all inputs of size N.

In an average-case bound, the running time is measured as an average over all of the

possible inputs of size N.

We will mainly focus on worst-case analysis, but sometimes it is useful to do average

one.

Example:

Static Searching Problem

Given an integer X and an array A, return the position of X in A or an indication that it

is not present. If X occurs more than once, return any occurrence. The array A is never

altered.

Sequential search: =>O(n)

Binary search (sorted data): => O(logn)

BIG-Oh notation

Sequential search

A sequential search steps through the data sequentially until an match is found.

A sequential search is useful when the array is not sorted.

A sequential search is linear O(n) (i.e. proportional to the size of input)

Unsuccessful search --- n times

Successful search (worst) --- n times

Successful search (average) --- n/2 times

Binary Search

If the array has been sorted, we can use binary search, which is performed from

the middle of the array rather than the end.

We keep track of low_end and high_end, which delimit the portion of the array in

which an item, if present, must reside.

If low_end is larger than high_end, we know the item is not present.

Algorithm analysis: Example

Algorithm Class

Email: [email protected]

Website: http://sites.google.com/site/algorithmclass

G+ : http://plus.google.com/+AlgorithmClass/posts

Facebook : http://www.facebook.com/AlgorithmClassCCppDsJavaTrainingKphbHyderabad

ALGORITHM CLASSS

C C++ DS Data Structures CPP JAVA TRAINING INSTITUTE KPHB HYDERABAD

/