#1 designandanalysis of algo
-
Upload
brijida-charizma-ardona-navarro -
Category
Technology
-
view
1.792 -
download
0
description
Transcript of #1 designandanalysis of algo
![Page 1: #1 designandanalysis of algo](https://reader036.fdocuments.us/reader036/viewer/2022062704/5560e6fbd8b42afb7b8b4dee/html5/thumbnails/1.jpg)
Design And Analysis of Algorithms
Topic #1: Algorithm Basics
Prepared by: Brijida Charizma A. Navarro
![Page 2: #1 designandanalysis of algo](https://reader036.fdocuments.us/reader036/viewer/2022062704/5560e6fbd8b42afb7b8b4dee/html5/thumbnails/2.jpg)
Introduction
As future computer
scientists, you will usually
more concerned with writing
complex program
algorithm.
![Page 3: #1 designandanalysis of algo](https://reader036.fdocuments.us/reader036/viewer/2022062704/5560e6fbd8b42afb7b8b4dee/html5/thumbnails/3.jpg)
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.
![Page 4: #1 designandanalysis of algo](https://reader036.fdocuments.us/reader036/viewer/2022062704/5560e6fbd8b42afb7b8b4dee/html5/thumbnails/4.jpg)
AlgorithmA complete, step-by-step
procedure for solving a specific problem.
![Page 5: #1 designandanalysis of algo](https://reader036.fdocuments.us/reader036/viewer/2022062704/5560e6fbd8b42afb7b8b4dee/html5/thumbnails/5.jpg)
Pakiisip nga…..
Does the computer
understand an algorithm?
![Page 6: #1 designandanalysis of algo](https://reader036.fdocuments.us/reader036/viewer/2022062704/5560e6fbd8b42afb7b8b4dee/html5/thumbnails/6.jpg)
Properties of Algorithms
InputOutputPrecision
![Page 7: #1 designandanalysis of algo](https://reader036.fdocuments.us/reader036/viewer/2022062704/5560e6fbd8b42afb7b8b4dee/html5/thumbnails/7.jpg)
Properties of Algorithms
DeterminismFiniteness CorrectnessGenerality
![Page 8: #1 designandanalysis of algo](https://reader036.fdocuments.us/reader036/viewer/2022062704/5560e6fbd8b42afb7b8b4dee/html5/thumbnails/8.jpg)
Example
Make an algorithm that finds the maximum of
three numbers a, b and c.
![Page 9: #1 designandanalysis of algo](https://reader036.fdocuments.us/reader036/viewer/2022062704/5560e6fbd8b42afb7b8b4dee/html5/thumbnails/9.jpg)
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
![Page 10: #1 designandanalysis of algo](https://reader036.fdocuments.us/reader036/viewer/2022062704/5560e6fbd8b42afb7b8b4dee/html5/thumbnails/10.jpg)
Two Main Tasks in the Study of Algorithms
Algorithm DesignAnalysis of Algorithms
![Page 11: #1 designandanalysis of algo](https://reader036.fdocuments.us/reader036/viewer/2022062704/5560e6fbd8b42afb7b8b4dee/html5/thumbnails/11.jpg)
Algorithm DesignTo solve problemsAn art more than a science
![Page 12: #1 designandanalysis of algo](https://reader036.fdocuments.us/reader036/viewer/2022062704/5560e6fbd8b42afb7b8b4dee/html5/thumbnails/12.jpg)
Analysis of AlgorithmsWill provide insights into
designing new algorithms
![Page 13: #1 designandanalysis of algo](https://reader036.fdocuments.us/reader036/viewer/2022062704/5560e6fbd8b42afb7b8b4dee/html5/thumbnails/13.jpg)
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?
![Page 14: #1 designandanalysis of algo](https://reader036.fdocuments.us/reader036/viewer/2022062704/5560e6fbd8b42afb7b8b4dee/html5/thumbnails/14.jpg)
Exercise
1.Write an algorithm that finds the smallest element among a, b and c
![Page 15: #1 designandanalysis of algo](https://reader036.fdocuments.us/reader036/viewer/2022062704/5560e6fbd8b42afb7b8b4dee/html5/thumbnails/15.jpg)
Solution
1. x = a2. If b < x, then x = b.3. If c < x, then x = c.
![Page 16: #1 designandanalysis of algo](https://reader036.fdocuments.us/reader036/viewer/2022062704/5560e6fbd8b42afb7b8b4dee/html5/thumbnails/16.jpg)
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.
![Page 17: #1 designandanalysis of algo](https://reader036.fdocuments.us/reader036/viewer/2022062704/5560e6fbd8b42afb7b8b4dee/html5/thumbnails/17.jpg)
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.
![Page 18: #1 designandanalysis of algo](https://reader036.fdocuments.us/reader036/viewer/2022062704/5560e6fbd8b42afb7b8b4dee/html5/thumbnails/18.jpg)
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.
![Page 19: #1 designandanalysis of algo](https://reader036.fdocuments.us/reader036/viewer/2022062704/5560e6fbd8b42afb7b8b4dee/html5/thumbnails/19.jpg)
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.
![Page 20: #1 designandanalysis of algo](https://reader036.fdocuments.us/reader036/viewer/2022062704/5560e6fbd8b42afb7b8b4dee/html5/thumbnails/20.jpg)
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
![Page 21: #1 designandanalysis of algo](https://reader036.fdocuments.us/reader036/viewer/2022062704/5560e6fbd8b42afb7b8b4dee/html5/thumbnails/21.jpg)
TitleBrief description of the algorithmThe input and output parametersFunction containing the
instructions of the algorithm
![Page 22: #1 designandanalysis of algo](https://reader036.fdocuments.us/reader036/viewer/2022062704/5560e6fbd8b42afb7b8b4dee/html5/thumbnails/22.jpg)
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