#1 designandanalysis of algo

Post on 24-May-2015

1,787 views 0 download

Tags:

description

CS202 Design and Analysis of Algorithm

transcript

Design And Analysis of Algorithms

Topic #1: Algorithm Basics

Prepared by: Brijida Charizma A. Navarro

Introduction

As future computer

scientists, you will usually

more concerned with writing

complex program

algorithm.

AlgorithmsStep by step method of solving

some problemsSolution that can be executed by

a computerDerived from the name of the

nith-century Persian mathematician al-Khowarizmi.

AlgorithmA complete, step-by-step

procedure for solving a specific problem.

Pakiisip nga…..

Does the computer

understand an algorithm?

Properties of Algorithms

InputOutputPrecision

Properties of Algorithms

DeterminismFiniteness CorrectnessGenerality

Example

Make an algorithm that finds the maximum of

three numbers a, b and c.

Algorithm: Finding the maximum in a list of 3 numbers

1.x = a2. If b > x then x = b3. If c > x, then x=c.

a = 1, b = 5,c = 3

Two Main Tasks in the Study of Algorithms

Algorithm DesignAnalysis of Algorithms

Algorithm DesignTo solve problemsAn art more than a science

Analysis of AlgorithmsWill provide insights into

designing new algorithms

in analysis of algorithms, we ask ourselves the following questions:

1. Correctness. Given an algorithm for a problem, does it solve the problem?

2. Termination. Does the algorithm always stop after a finite number of steps?

3. Time Analysis. How many instructions does the algorithm execute?

4. Space Analysis. How much memory does the algorithm need to execute?

Exercise

1.Write an algorithm that finds the smallest element among a, b and c

Solution

1. x = a2. If b < x, then x = b.3. If c < x, then x = c.

2. ExerciseWhich properties of an algorithm

– input, output, precision, determinism, finiteness, correctness, generality – if any, are lacking in the following. Explain. The input is a set of S of integers and an integer m. The output is all subsets of S that sum to m.

Here’s the algorithm:

1.List all subsets of S and their sums.

2.Proceed through the subsets listed in step 1 and output each whose sum is m.

Answer:If the set S is an infinite set, the algorithm will

not terminate, so it lacks finiteness and output properties.

Line 1 is not precisely stated since how to list the subsets of S and their sums is not specified; thus the algorithm lacks the precision property.

The order of the subsets listed in line 1 depends on the method used to generate them, so the algorithm lacks the determinism property.

Since line 2 depends on the order of the subsets generated in line 1, the determinism property is lacking here as well.

Pseudocode for AlgorithmsMade of ordinary languagePrecision, structure and

universalityClosely resembles the actual

code of computer languages such as C++ and Java

Any version of pseudocode is acceptable as long as the instructions are unambiguous.

Algorithm Structure

Input Parameters: a, b, cOutput Parameter: x

max(a,b,c){ x = a if (b > x) // b is larger than x, update x x = b if (c > x) // c is larger than x, update x x = c}

Algorithm: Finding the Maximum of Three Numbers. This algorithm finds the largest of the numbers a, b, and c

TitleBrief description of the algorithmThe input and output parametersFunction containing the

instructions of the algorithm

Do this…………

Design an algorithm that will find the largest number in an array s

s[1],s[2],…,s[n]Note: an array is a group of data of the same type

Use while