Design and Analysis of Algorithms

Download Report

Transcript Design and Analysis of Algorithms

Design and Analysis of
Algorithms
Dr. Ameer Ali
What is Algorithm?
Algorithm has come to refer: a
method that can be used by a
computer for the solution of a
problem.
Definition: An algorithm is a finite
set of instructions that, if followed,
accomplishes a particular task.
July 17, 2015
Dr. Ameer Ali
2
What is Algorithm?
Criteria:
i. Input: Zero or more input
ii. Output: At least one output
iii. Completeness: Must be complete
within time frame
iv. Definiteness: Each instruction is
clear and unambiguous
v. Finiteness: Finite number of steps
vi. Effectiveness: Very basic, feasible.
July 17, 2015
Dr. Ameer Ali
3
Why Algorithm?
Developing reasoning
Way to solve a real world problem
Effective and efficient
implementation in computer
language
Efficient algorithm designing
July 17, 2015
Dr. Ameer Ali
4
Application Algorithm
In all type of computer software as
well as in some hardware such as
ROM.
July 17, 2015
Dr. Ameer Ali
5
Books
Introduction to Algorithms: Thomas
H. Cormen
Fundamentals of Computer
Algorithms: S. Sahni
Algorithm Design: Michael T.
Goodrich
The Art of Computer Programming:
Donald E. Knuth
July 17, 2015
Dr. Ameer Ali
6
Overview
Introduction to Algorithm
Elementary Data Structures
Sorting
Divide and Conquer
Greedy Method
Dynamic Programming
Elementary Graph Algorithms
July 17, 2015
Dr. Ameer Ali
7
Overview
Minimum Spanning Tree
Single Source Shortest Path
All Pair Shortest Path
Maximum Flow
Sorting Networks
Computational Geometry
NP-Completeness
Some Research Papers
July 17, 2015
Dr. Ameer Ali
8
Example
INSERT-SORT (A)
For j=2 to length[A] do
key=A[j]
i=j-1
while i>0 and A[i]>key do
A[i+1]=A[i]
i=i-1
A[i+1]=key
July 17, 2015
Dr. Ameer Ali
9
Example







July 17, 2015
A=5,2,4,6,1,3
5, 2, 4, 6, 1, 3
2, 5 4 6 1 3
245613
245613
124563
123456
Dr. Ameer Ali
10
Analyzing Algorithms


July 17, 2015
Space
Time
Dr. Ameer Ali
11
Analyzing Insertion Sort
INSERT-SORT (A)
Cost
For j=2 to length[A] do
C1
key=A[j]
C2
/comments
i=j-1
C4
while i>0 and A[i]>key do C5
A[i+1]=A[i]
C6
i=i-1
C7
A[i+1]=key
C8
July 17, 2015
Dr. Ameer Ali
Times
n
n-1
n-1
n-1
∑nj=2tj
∑nj=2 (tj-1)
∑nj=2 (tj-1)
n-1
12
Analyzing Insertion Sort
T(n)=c1*n+c2(n-1)+c4(n1)+c5(∑nj=2tj)+c6(∑nj=2 (tj-1))+c7(∑nj=2
(tj-1))+c8(n-1)
 For best case: already sorted list
 T(n)=c1*n+c2(n-1)+c4(n-1)+c5(n1)+c8(n-1)
=(c1+c2+c4+c5+c8)n(c2+c4+c5+c8)

July 17, 2015
Dr. Ameer Ali
13
Analyzing Insertion Sort





July 17, 2015
For worst case: sorted in reverse
order here tj=j
∑nj=2 j=(n(n-1)/2) – 1
∑nj=2 (j-1)=n(n-1)/2
T(n)=(c5/2+c6/2+c7/2)n2
+(c1+c2+c4+c5/2-c6/2-c7/2+c8)n(c2+c4+c5+c8)
Which is quadratic equation
Dr. Ameer Ali
14
Analyzing Insertion Sort

July 17, 2015
For average case is similar to worst
case
Dr. Ameer Ali
15