# algorithms: an introduction

Post on 31-Dec-2015

55 views

Category:

## Documents

0 download

Tags:

• #### end end return calgorithms

Embed Size (px)

DESCRIPTION

Algorithms: An Introduction. Section 3.1 of Rosen Fall 2008 CSCE 235 Introduction to Discrete Structures Course web-page: cse.unl.edu/~cse235 Questions : cse235@cse.unl.edu. Outline. Introduction & definition Algorithms categories & types Pseudo-code Designing an algorithm Example: Max - PowerPoint PPT Presentation

TRANSCRIPT

• Algorithms: An IntroductionSection 3.1 of RosenFall 2008CSCE 235 Introduction to Discrete StructuresCourse web-page: cse.unl.edu/~cse235Questions: cse235@cse.unl.edu

Algorithms: An IntroductionCSCE 235, Fall 2008*

OutlineIntroduction & definitionAlgorithms categories & typesPseudo-codeDesigning an algorithmExample: MaxGreedy AlgorithmsChange

Algorithms: An IntroductionCSCE 235, Fall 2008*

Computer Science is About Problem SolvingA Problem is specified byThe givens (a formulation)A set of objectsRelations between themThe queryThe information one is trying to extract from the formulation, the question to answerAn algorithm is a method or procedure that solves instances of a problemAlgorithm is a distortion of Al-Khawarizmi, a Persian mathematician

Real WorldComputing WorldObjectsrepresented bydata Structures, ADTs, ClassesRelationsimplemented withrelations & functions (e.g., predicates)ActionsImplemented withalgorithms: a sequence of instructions

Algorithms: An IntroductionCSCE 235, Fall 2008*

Algorithms: Formal DefinitionDefinition: An algorithm is a sequence of unambiguous instructions for solving a problem.Properties of an algorithmFinite: the algorithm must eventually terminateComplete: Always give a solution when one existsCorrect (sound): Always give a correct solutionFor an algorithm to be an acceptable solution to a problem, it must also be effective. That is, it must give a solution in a reasonable amount of timeEfficient= runs in polynomial time. Thus, effective efficientThere can be many algorithms to solve the same problem.

Algorithms: An IntroductionCSCE 235, Fall 2008*

OutlineIntroduction & definitionAlgorithms categories & typesPseudo-codeDesigning an algorithmExample: MaxGreedy AlgorithmsChange

Algorithms: An IntroductionCSCE 235, Fall 2008*

Algorithms: General TechniquesThere are many broad categories of algorithmsDeterministic versus Randomized (e.g., Monte-Carlo)Exact versus ApproximationSequential/serial versus Parallel, etc.Some general styles of algorithms includeBrute force (enumerative techniques, exhaustive search)Divide & ConquerTransform & Conquer (reformulation)Greedy Techniques

Algorithms: An IntroductionCSCE 235, Fall 2008*

OutlineIntroduction & definitionAlgorithms categories & typesPseudo-codeDesigning an algorithmExample: MaxGreedy AlgorithmsChange

Algorithms: An IntroductionCSCE 235, Fall 2008*

Algorithms: Pseudo-CodeAlgorithms are usually presented using pseudo-codeBad pseudo-code gives too many details or is too implementation specific (i.e., actual C++ or Java code or giving every step of a sub-process such as set union)Good pseudo-code Is a balance between clarity and detailAbstracts the algorithmMakes good use of mathematical notationIs easy to read andFacilitates implementation (reproducible, does not hide away important information)

Algorithms: An IntroductionCSCE 235, Fall 2008*

Good Pseudo-Code: ExampleIntersectionInput: Two finite sets A, BOutput: A finite set C such that C = A BC0 If |A|>|B| Then Swap(A,B) End For every x A Do If x B Then C C {x} Union(C,{x}) End End Return C

Algorithms: An IntroductionCSCE 235, Fall 2008*

Writing Pseudo-Code: AdviceInput/output must properly definedAll your variables must be properly initialized, introduced Variables are instantiated, assigned using All `commands' (while, if, repeat, begin, end) bold face \bfFor i 1 to n DoAll functions in small caps Union(s,t) \sc All constants in courier: pi 3.14 \ttAll variables in italic: temperature 78 (\it, \em)LaTeX: Several algorithm formatting packages exist on WWW

Algorithms: An IntroductionCSCE 235, Fall 2008*

OutlineIntroduction & definitionAlgorithms categories & typesPseudo-codeDesigning an algorithmExample: MaxGreedy AlgorithmsChange

Algorithms: An IntroductionCSCE 235, Fall 2008*

Designing an AlgorithmA general approach to designing algorithms is as followsUnderstanding the problem, assess its difficultyChoose an approach (e.g., exact/approximate, deterministic/ probabilistic)(Choose appropriate data structures)Choose a strategyProve TerminationCompletenessCorrectness/soundnessEvaluate complexityImplement and test itCompare to other known approach and algorithms

Algorithms: An IntroductionCSCE 235, Fall 2008*

Algorithm Example: MaxWhen designing an algorithm, we usually give a formal statement about the problem to solveProblemGiven: a set A={a1,a2,,an} of integersQuestion: find the index i of the maximum integer aiA straightforward idea is Simply store an initial maximum, say a1Compare the stored maximum to every other integer in AUpdate the stored maximum if a new maximum is ever encountered

Algorithms: An IntroductionCSCE 235, Fall 2008*

Pseudo-code of MaxMaxInput: A finite set A={a1,a2,,an} of integersOutput: An index i such that ai = max{a1,a2,,an} index 1 For i =2 to n Do If ai > aindex Then index i End End Return index

Algorithms: An IntroductionCSCE 235, Fall 2008*

Algorithms: Other ExamplesCheck Bubble Sort and Insertion Sort in your textbooks which you should have seen ad nauseum in CSE 155 and CSE 156 And which you will see again in CSE 310Let us know if you have any questions

Algorithms: An IntroductionCSCE 235, Fall 2008*

OutlineIntroduction & definitionAlgorithms categories & typesPseudo-codeDesigning an algorithmExample: MaxGreedy AlgorithmsChange

Algorithms: An IntroductionCSCE 235, Fall 2008*

Greedy AlgorithmsIn many problems, we wish to not only find a solution, but to find the best or optimal solutionA simple technique that works for some optimization problems is called the greedy techniqueAs the name suggests, we solve a problem by being greedy: Choose what appears now to be the best choiceChoose the most immediate best solution (i.e., think locally)Greedy algorithmsWork well on some (simple) algorithmsUsually they are not guaranteed to produce the best globally optimal solution

Algorithms: An IntroductionCSCE 235, Fall 2008*

Change-Making ProblemWe want to give change to a customer but we want to minimize the number of total coins we give themProblemGiven: An integer n an a set of coin denominations (c1,c2,,cr) with c1>c2>>crQuery: Find a set of coins d1,d2,,dk such thati=1k di = n and k is minimized

Algorithms: An IntroductionCSCE 235, Fall 2008*

Greedy Algorithm: ChangeChangeInput: An integer n and a set of coin denominations {c1,c2,,cr} with c1 > c2> >crOutput: A set of coins d1,d2,,dk such that i=1k di = n and k is minimizedC For i =1 to n Do While n ci Do C C {ci} End Return index

Algorithms: An IntroductionCSCE 235, Fall 2008*

Change: Analysis (1)Will the algorithm always produce an optimal answer?ExampleConsider a coinage system where c1=20, c2=15, c3=7, c4=1We want to give 22 cents in change

What is the output of the algorithm?Is it optimal?

It is not optimal because it would give us one c4 and two c1 (3 coins). The optimal change is one c2 and one c3 (2 coins)

Algorithms: An IntroductionCSCE 235, Fall 2008*

Change: Analysis (2)What about the US currency system: is the algorithm correct in this case?Yes, in fact it is. We can prove it by contradiction.For simplicity, let us considerc1=25, c2=10, c3=5, c4=1

Algorithms: An IntroductionCSCE 235, Fall 2008*

Optimality of Change (1)Let C={d1,d2,,dk} be the solution given by the greedy algorithm for some integer n.By way of contradiction, assume there is a better solution C={d1,d2,,dl} with lq, the greedy algorithm would have used q by construction. Thus, it is impossible that the greedy uses q

Recommended