cmsc 1041 algorithms i an introduction to algorithms

15
CMSC 104 Algorithms I An Introduction to Algorithms

Upload: winfred-quinn

Post on 03-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CMSC 1041 Algorithms I An Introduction to Algorithms

CMSC 104 1

Algorithms I

An Introduction to

Algorithms

Page 2: CMSC 1041 Algorithms I An Introduction to Algorithms

CMSC 104 2

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.

Page 3: CMSC 1041 Algorithms I An Introduction to Algorithms

CMSC 104 3

Algorithms

An algorithm is a step by step solution to a problem

Why bother writing an algorithm ?o For your own use in the future - Don't have

to rethink the problem.o So others can solve the problem, even if

they know very little about the principles behind how the solution was derived.

Page 4: CMSC 1041 Algorithms I An Introduction to Algorithms

CMSC 104 4

Examples of Algorithms

Washing machine instructions Instructions for a ready-to-assemble

piece of furniture A Classic: GCD - Greatest Common

Divisor - The Euclidean Algorithm

Page 5: CMSC 1041 Algorithms I An Introduction to Algorithms

CMSC 104 5

Washing Machine Instructions

Separate clothes into white clothes and colored clothes.

For white clothes:

o Set water temperature knob to HOT.

o Place white laundry in tub. For colored clothes:

o Set water temperature knob to COLD

o Place colored laundry in tub. Add 1 cup of powdered laundry detergent to the tub. Close lid and press the start button.

Page 6: CMSC 1041 Algorithms I An Introduction to Algorithms

CMSC 104 6

Observations about the Washing Machine Instructions

There are a finite number of steps. We are capable of doing each of the

instructions. When we have followed all of the steps, the

washing machine will wash the clothes and then will stop.

Are all of the clothes clean ? Do we want the washing machine to run until

all of the clothes are clean ?

Page 7: CMSC 1041 Algorithms I An Introduction to Algorithms

CMSC 104 7

Refinement of the Definition

Our old definition: o An algorithm is a step by step solution to a

problem. Adding our observations:

o An algorithm is a finite set of executable instructions that directs a terminating activity.

Page 8: CMSC 1041 Algorithms I An Introduction to Algorithms

CMSC 104 8

Instructions for a Ready-to-Assemble Piece of Furniture

"Align the marks on side A with the grooves on Part F “

Why are these instructions typically so hard to follow ?o Lack of proper tools - instructions are not

executable o Which side is A ? A & B look alike. Both

line up with Part F - Ambiguous instructions.

Page 9: CMSC 1041 Algorithms I An Introduction to Algorithms

CMSC 104 9

Final Version of the Definition:

An algorithm is a finite set of unambiguous, executable instructions

that directs a terminating activity.

Page 10: CMSC 1041 Algorithms I An Introduction to Algorithms

CMSC 104 10

History of Algorithms

The study of algorithms began as a subject in mathematics.

The search for algorithms was a significant activity of early mathematicians.

Goal: To find a single set of instructions that could be used to solve any problem of a particular type.

Page 11: CMSC 1041 Algorithms I An Introduction to Algorithms

CMSC 104 11

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.

Page 12: CMSC 1041 Algorithms I An Introduction to Algorithms

CMSC 104 12

Finding the GCD or 24 and 9

M N R

24 9 6

9 6 3

6 3 0

So 3 is the GCD of 24 and 9.

Page 13: CMSC 1041 Algorithms I An Introduction to Algorithms

CMSC 104 13

Euclidean GCD Algorithm (continued)

Do we need to know the theory that Euclid used to come up with this algorithm in order to use it ?

What intelligence is required to find the GCD using this algorithm ?

Page 14: CMSC 1041 Algorithms I An Introduction to Algorithms

CMSC 104 14

The Idea Behind Algorithms

Once an algorithm behind a task has been discovered

o Don't need to understand the principles.

o Task is reduced to following the instructions.

o Intelligence is "encoded into the algorithm"

Page 15: CMSC 1041 Algorithms I An Introduction to Algorithms

CMSC 104 15

Algorithm Representation

Syntax and Semantics

o Syntax refers to the representation itself.

o Semantics refers to the concept represented.