Anda di halaman 1dari 31

Lecture # 01

By:
M.Sultan Zia
Assistant Professor
(DCS),

TODAY

What is an Algorithm?
And
How do we analyze one?

CIIT, Sahiwal

CIIT, Sahiwal

ALGORITHMS
Informally,
A tool for solving a well-specified computational
problem.

Input

Algorithm

Output

Example: sorting
input: A sequence of numbers.
output: An ordered permutation of the input.
issues: correctness, efficiency, storage, etc.
CIIT, Sahiwal

STRENGTHENING THE INFORMAL DEFINITION

An algorithm is a finite sequence of


unambiguous instructions for solving a
well-specified computational problem.
Important Features:
Finiteness.
Definiteness.
Input.
0
Output. 1
Effectiveness.
CIIT, Sahiwal

ALGORITHM IN FORMAL TERMS


In terms of mathematical models of computational
platforms (general-purpose computers).
One definition Turing Machine that always halts.
Other definitions are possible (e.g. Lambda Calculus.)
Mathematical basis is necessary to answer questions
such as:
Is a problem solvable? (Does an algorithm exist?)
Complexity classes of problems. (Is an efficient algorithm
possible?)

Interested in learning more?


Take AAA
CIIT, Sahiwal

EXAMPLE OF COMPUTATIONAL PROBLEM:


SORTING
Statement of problem:
Input: A sequence of n numbers <a 1, a2, , an>
Output: A reordering of the input sequence <a 1, a2, , an> so that ai
aj whenever i < j

Instance: The sequence <5, 3, 2, 8, 3>


Algorithms:

Selection sort
Insertion sort
Merge sort
(many others)
CIIT, Sahiwal

SOME WELL-KNOWN COMPUTATIONAL PROBLEMS

Sorting
Searching
Shortest paths in a graph
Minimum spanning tree
Primality testing
Traveling salesman problem
Knapsack problem
Chess
Towers of Hanoi
Program termination

Some of these problems dont have efficient algorithms, or


algorithms at all!
CIIT, Sahiwal

ANALYSIS OF ALGORITHMS

The theoretical study of computer programs


Performance, and Resource Usage.
Or
Abstract/Mathematical Study/Comparison of
Algorithms
CIIT, Sahiwal

ANALYSIS OF ALGORITHMS

What is more Important than


Performance ???

CIIT, Sahiwal

10

WHY STUDY ALGORITHMS AND PERFORMANCE

Performance correlated with user friendliness


Real time constraints
Performance measure the line b/w what is
feasible and what is not
Algorithms gives you a language to talk about
programs behavior
Performance is like money
Its Fun
Algorithm Designing Capabilities
CIIT, Sahiwal

11

Sequential Search
O (n)

A sequential search of a list/array


begins at the beginning of the
list/array and continues until the
item is found or the entire list/array
has been searched

CIIT, Sahiwal

12

Sequential Search
bool LinSearch(double x[ ], int n, double item){
for(int i=0;i<n;i++){
if(x[i]==item) return true;
else return false;
}
return false;
}

CIIT, Sahiwal

13

Search Algorithms
Suppose that there are n elements in the array. The following expression
gives the average number of comparisons:

It is known that

Therefore, the following expression gives the average number of comparisons


made by the sequential search in the successful case:

CIIT, Sahiwal

14

Search Algorithms

CIIT, Sahiwal

15

Binary Search
O(log2n)

A binary search looks for


an item in a list using a
divide-and-conquer
strategy

CIIT, Sahiwal

16

Binary Search
Binary search algorithm assumes that the
items in the array being searched are
sorted
The algorithm begins at the middle of
the array in a binary search
If the item for which we are searching is
less than the item in the middle, we
know that the item wont be in the second
half of the array
Once again we examine the middle
element
The process continues with each
comparison cutting in half the portion of
CIIT, Sahiwal

17

Binary Search

CIIT, Sahiwal

18

Binary Search: middle


element

mid =

left + right
2

CIIT, Sahiwal

19

Binary Search

bool BinSearch (double list[ ], int n, double item,


int&index)
{
int left=0;
int right=n-1;
int mid;
while(left<=right){
mid=(left+right)/2;
if(item> list [mid]) { left=mid+1; }
else if(item< list [mid]) {right=mid-1;}
else{
item= list [mid];
index=mid;
return true; }
}// while
return false;
CIIT, Sahiwal
}

20

Binary Search: Example

CIIT, Sahiwal

21

BASIC ISSUES RELATED TO ALGORITHMS


How to design algorithms
How to express algorithms
Proving correctness
Efficiency (or complexity) analysis
Theoretical analysis
Empirical analysis

Optimality
CIIT, Sahiwal

22

BUBBLE SORT ALGORITHM

for i = 1 to n
for j = 1 to n-1
if A[j] < A[j+1]

swap(A,i,j)

for(int x=0; x<n; x++)


for(int y=0; y<n-1; y++)
if(array[y]>array[y+1])
{
int temp = array[y+1];
array[y+1] = array[y];
array[y] = temp;
}
CIIT, Sahiwal

23

SELECTION SORT
Input: array a[1],,a[n]
Output: array a sorted in non-decreasing order
Algorithm:

for i =1 to n
swap a[ i ] with smallest of a[ i ],,a[ n ]

CIIT, Sahiwal

24

INSERTION SORT
void insertionSort(int numbers[], int array_size)
{
int i, j, index;
for(i=1; i < array_size; i++)
{
index = numbers[i]; j = i;
while ((j > 0) && (numbers[j-1] > index))
{
numbers[j] = numbers[j-1]; j = j - 1; }
numbers[j] = index;
}
}
}
CIIT, Sahiwal

25

BASIC ISSUES RELATED TO ALGORITHMS


How to design algorithms
How to express algorithms
Proving correctness
Efficiency (or complexity) analysis
Theoretical analysis
Empirical analysis

Optimality
CIIT, Sahiwal

26

ALGORITHM ANALYSIS
Determining performance characteristics.
(Predicting the resource requirements.)
Time, memory, communication bandwidth etc.
Computation time (running time) is of primary
concern.

Why analyze algorithms?


Choose the most efficient of several possible
algorithms for the same problem.
Is the best possible running time for a problem
reasonably finite for practical purposes?
Is the algorithm optimal (best in some sense)? Is
something better possible?
CIIT, Sahiwal

27

EFFICIENCY OF ALGORITHMS
What resources are required to accomplish the
task
How one algorithm compares with other
algorithms

CIIT, Sahiwal

28

EFFICIENCY AND COMPLEXITY


Efficiency
How much time or space is required
Measured in terms of common basic operations

Complexity
How efficiency varies with the size of the task
Expressed in terms of standard functions of n
E.g. O(n), O(n2), O(log n), O(n log n)
CIIT, Sahiwal

29

EUCLIDS ALGORITHM
Problem: Find gcd(m,n), the greatest common divisor of two
nonnegative, not both zero integers m and n
Examples: gcd(60,24) = 12, gcd(60,0) = 60, gcd(0,0) = ?
Euclids algorithm is based on repeated application of
equality
gcd(m,n) = gcd(n, m mod n)
until the second number becomes 0, which makes the
problem trivial.
Example: gcd(60,24) = gcd(24,12) = gcd(12,0) = 12
CIIT, Sahiwal

30

(((
)))

CIIT, Sahiwal

31

Anda mungkin juga menyukai