CMSC 1041 Algorithms I An Introduction to Algorithms
CMSC 1042 Problem Solving Problem solving is the process of transforming the description of a problem into the solution of that problem by using our knowledge of the problem domain and by relying on our ability to select and use appropriate problem- solving strategies, techniques, and tools.
CMSC 1043 Algorithms l An algorithm is a step by step solution to a problem l Why bother writing an algorithm ? oFor your own use in the future - Don't have to rethink the problem. oSo others can solve the problem, even if they know very little about the principles behind how the solution was derived.
CMSC 1044 Examples of Algorithms l Washing machine instructions l Instructions for a ready-to-assemble piece of furniture l A Classic: GCD - Greatest Common Divisor - The Euclidean Algorithm
CMSC 1045 Washing Machine Instructions l Separate clothes into white clothes and colored clothes. l For white clothes: oSet water temperature knob to HOT. oPlace white laundry in tub. l For colored clothes: oSet water temperature knob to COLD oPlace colored laundry in tub. l Add 1 cup of powdered laundry detergent to the tub. l Close lid and press the start button.
CMSC 1046 Observations about the Washing Machine Instructions l There are a finite number of steps. l We are capable of doing each of the instructions. l When we have followed all of the steps, the washing machine will wash the clothes and then will stop. l Are all of the clothes clean ? l Do we want the washing machine to run until all of the clothes are clean ?
CMSC 1047 Refinement of the Definition l Our old definition: oAn algorithm is a step by step solution to a problem. l Adding our observations: oAn algorithm is a finite set of executable instructions that directs a terminating activity.
CMSC 1048 Instructions for a Ready-to-Assemble Piece of Furniture l "Align the marks on side A with the grooves on Part F “ l Why are these instructions typically so hard to follow ? oLack of proper tools - instructions are not executable oWhich side is A ? A & B look alike. Both line up with Part F - Ambiguous instructions.
CMSC 1049 Final Version of the Definition: An algorithm is a finite set of unambiguous, executable instructions that directs a terminating activity.
CMSC History of Algorithms l The study of algorithms began as a subject in mathematics. l The search for algorithms was a significant activity of early mathematicians. l Goal: To find a single set of instructions that could be used to solve any problem of a particular type.
CMSC GCD - The Euclidean Algorithm Assign M and N the value of the larger and the value of the smaller of the two positive integer input values, respectively. Divide M by N and call the remainder R. If R is not 0, then assign M the value of N, assign N the value of R and return to step 2, otherwise the greatest common divisor is the value currently assigned to N.
CMSC Finding the GCD or 24 and 9 MNR So 3 is the GCD of 24 and 9.
CMSC Euclidean GCD Algorithm (continued) l Do we need to know the theory that Euclid used to come up with this algorithm in order to use it ? l What intelligence is required to find the GCD using this algorithm ?
CMSC The Idea Behind Algorithms l Once an algorithm behind a task has been discovered oDon't need to understand the principles. oTask is reduced to following the instructions. oIntelligence is "encoded into the algorithm"
CMSC Algorithm Representation l Syntax and Semantics oSyntax refers to the representation itself. oSemantics refers to the concept represented.