how to solve it? algorithms. an algorithm an algorithm is any well-defined (computational) procedure...

31
HOW TO SOLVE IT? Algorithms

Upload: augusta-barton

Post on 26-Dec-2015

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

HOW TO SOLVE IT?

Algorithms

Page 2: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

An Algorithm

An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input and produces some value, or set of values, as output.

An algorithm is thus a sequence of (computational) steps that transforms the input into the output.

Page 3: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Algorithms

The output we would like to obtain as the result of the algorithm is usually the solution of a problem.

Many problems do not have closed-form solutions that we can just plug-in the input values to obtain the solution.

To obtain the solution requires the performance of a procedure.

Page 4: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Algorithms

Even the simplest of computational tasks requires algorithms when one thinks of it.

Multiple digit addition. Input: Two integers a and b Output: An integer that is the sum of a and b Algorithm?

The sorting problem Input: A sequence of n numbers (a1, a2, …, an) Output: A permutation (reordering) ( a1’, a2’, …, an’ )

of the input sequence such that a1’<= a2’<= …<= an’ Algorithm?

Page 5: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Algorithms

Consider the input sequence (33,22,11, 45,42)

When sorting algorithm is applied, it returns the output sequence (11,22,33,42,45)

Such an input sequence is called an instance of the sorting problem.

In general an instance of a problem consists of the input (satisfying the input requirements of the problem) needed to compute a solution to the problem

Page 6: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Algorithms

Sorting is a fundamental operation that are used in many computer programs

A large number of good sorting algorithms have been developed

The best algorithm for a given application depends on The number of times to be sorted The extent to which the items are already somehow sorted Possible restrictions on the values The kind of storage device used to keep the sequence

(RAM, hard drive, tapes, …) …

Page 7: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Algorithms

An algorithm is called correct if, for every input sequence it stops with the correct output.

A correct algorithm solves any given instance of the problem.

Page 8: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Examples of Important Algorithms

Sorting algorithmsDNA sequencing algorithmsData mining algorithmsCryptology algorithmsOptimization algorithms

Simplex Algorithm Metaheuristics Algorithms for TSP (Travelling Salesman Problem)

Algorithms to analyze Markov chainsSimulation algorithms….

Page 9: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Efficiency of Algorithms

The devices that execute the algorithms (?) are not infinitely fast and the memory capacity is limited

Algorithms devised to solve the same problem often differ in their efficiency.

Efficiency is about how many basic operations are done and how much memory is used

The number of basic operations required by the algorithm determines the execution time

Page 10: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Insertion Sort

An efficient algorithm for sorting a small number of elements

It works the way many people sort a hand of playing cards

We start with an empty left hand and the cards face down on the table

We then remove one card at a time from the table and insert it into the correct position between the cards already in the hand

Page 11: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Insertion Sort

How can we adapt this to a sequence?Assume there is a barrier that separates the

sorted part of the sequence from the unsorted part (the left hand from the cards on the table)

This barrier will move by one at each step of the algorithm.

Page 12: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Insertion Sort - Example

Input: (5, 2, 4, 6, 1, 3)Step 1: (5,| 2, 4, 6, 1, 3)Step 2: (2, 5,| 4, 6, 1, 3)Step 3: (2, 4, 5,| 6, 1, 3)Step 4: (2, 4, 5, 6,| 1, 3)Step 5: (1, 2, 4, 5, 6,| 3)Step 6: (1, 2, 3, 4, 5, 6|)

Page 13: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Insertion Sort- Pseudocode

Input: Array A

1 for to {2 3 % Insert into the sorted sequence 4 5 while and {6 7 8 }

Page 14: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Loop Invariant for Insertion Sort

At the start of each iteration of the for loop of lines 1-8, the subarray consists of the elements in the original subarray but in sorted order.

Page 15: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Loop Invariants

We use loop invariants to help us understand why an algorithm is correct.

We must show three things to check if a loop invariant is true1. Initialization: It is true prior to the first iteration of

the loop.2. Maintenance: If it is true before an iteration of the

loop, it remains true before the next iteration.3. Termination: When the loop terminates, the invariant

gives us useful property that helps us to show that the algorithm is correct.

Is it similar to another concept you know?

Page 16: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Analysis of Insertion Sort

Let and be the number of times the while loop is executed for that value of j.

Cost Times1 for to { n2 n-13 % Insert into the sorted 0 n-1

sequence 4 n-15 while and { 6 7 8 } n-1

Page 17: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Analysis of Insertion Sort

Even for inputs of a given size (length(A)), an algorithm’s running time may depend on the actual input.

Let us assume that the array A is already sorted.For each j, we then find .Hence, for all j.This would give the best running time which is The running time can be expressed as The running time is a linear function of n. ( )

Page 18: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Analysis of Insertion Sort

What about the worst time?Assume that the array is in reverse sorting

order.Then, for all j.(Why?)Noting and

The worst-case running time can be expressed as .

It is a quadratic function of n. ( )

Page 19: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Analysis of Algorithms

In our previous analysis, we looked at both the best case, in which the input array was already sorted, and the worst case, in which the input array was reverse sorted.

Usually the worst-case analysis is the one that merits most attention, because:1. It gives an upper bound on the running time for any

input.2. For some algorithm, worst case occurs very

frequently (search algorithms)3. The average case behaves usually the same with the

worst case.

Page 20: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Merge-Sort Algorithm

Another algorithm for sortingInstead of the incremental approach of

insertion sort, it makes use of a divide-and-conquer approach

Divide: Divide the n-element sequence into two subsequences of n/2 elements each.

Conquer: Sort the two subsequences recursively using merge sort.

Combine: Merge the two sorted sequences to produce the sorted answer.

Page 21: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Merge-Sort Algorithm

In order to execute this algorithm we need to merge to sorted (in order) sequences in the combine step.

For that we are going propose another algorithm:

MERGE (A, p, q, r)Where A is an array, p, q, and r are indices

such that.The algorithm assumes and are already

sorted and merges them into to sorted subarray that replaces the current .

Page 22: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

MERGE (A, p, q, r)

1 2 3 % C and 4 for to {5 } 6 for to {7 }8 9

Page 23: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

MERGE (A, p, q, r)

10 % M and into 111213 for to {13 if {14 15 }16 else {17 18 }

Page 24: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Loop Invariant for Merge Algorithm

At the start of each iteration of the for loop of lines 13-18, the subarray contains smallest elements of and , in sorted order.

Moreover, and are the smallest elements of their arrays that have not been copied back into A. Initialization: (?) Maintenance (?) Termination: (?)

Page 25: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Running Time of Merge Algorithm

Let number of operations computational complexity

Lines 1-3 constant o(1)Lines 8-12 constant o(1)Lines 4-7 times o(n)Lines 13-18 n times o(n)

The computational complexity is o(n). (Linear function of n)

Page 26: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

MERGE-SORT (A, p, r)

1 if {2 3 MERGE-SORT (A, p, q)4 MERGE-SORT (A, q+1, r)5 MERGE(A, p, q, r)}

Initial call: MERGE-SORT (A, 1, length )

Page 27: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Computational Complexity

Computational complexity of merge sort is Why?This is better than of insertion sort.

Page 28: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Counting Sort

It assumes that each of the n input elements is an integer in the range 1 to k, for some integer k.

If k is a linear function of n, then the algorithm runs in time.

Input: A(1:n)Temporary Storage: C(1:k) (Counts)

Page 29: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

COUNTING-SORT (A)

1 2 3 for to { 4 5 % Now we have the count vector C6 7 for { 8 while C {910 C C-111 }}

Page 30: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Counting Sort

It is a very good algorithmIt is able to run in linear time since it does

not do comparisonsYet beware of its limitations

Page 31: HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input

Project Work

Write MATLAB functions for the three sorting algorithms presented: Insertion Sort Merge Sort (In order to implement this sort you will need

to write a function for the Merge algorithm) Counting Sort

Create random arrays of elements between 0 and 10 of sizes 10, 1000, 1000000

Compare the times it takes to for each algorithm to sort these three arrays. (Don’t forget that the counting sort necessitates integers)

Present your findings in a report.