new mexico computer science for all introduction to algorithms maureen psaila-dombrowski

15
New Mexico Computer Science For All Introduction to Algorithms Maureen Psaila-Dombrowski

Upload: oliver-townsend

Post on 22-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: New Mexico Computer Science For All Introduction to Algorithms Maureen Psaila-Dombrowski

New Mexico Computer Science For All

Introduction to Algorithms

Maureen Psaila-Dombrowski

Page 2: New Mexico Computer Science For All Introduction to Algorithms Maureen Psaila-Dombrowski

What do these things have in common?

•Doing the laundry•Going to the grocery store•Making chocolate chip cookies

Each is an repeated actionEach follows the same steps each time you do it

Each can be thought of as an algorithm

Page 3: New Mexico Computer Science For All Introduction to Algorithms Maureen Psaila-Dombrowski

Algorithm

•A set of instructions that can be used repeatedly to solve a problem or complete a task.

Page 4: New Mexico Computer Science For All Introduction to Algorithms Maureen Psaila-Dombrowski

Why Use Algorithms?

•Don’t have think about the details of the problem – we already know how to solve it. ▫Once you know how to go to the grocery store,

you do not need to look at a map to decide where to make the next turn.

•All the information is in the algorithm – just have to follow it.

•Same algorithm can be used ▫to sort a list whether it has 5 elements or 5000

•Anyone can follow it

Page 5: New Mexico Computer Science For All Introduction to Algorithms Maureen Psaila-Dombrowski

Computers and Algorithms•Computers

▫Speed ▫Accuracy▫Well-suited for solving tedious problems

large telephone directory adding a long column of numbers

•Computers solving problems▫A technique for solving the problem. ▫Algorithms▫No algorithm no solution▫A general case solution (in some cases)

Page 6: New Mexico Computer Science For All Introduction to Algorithms Maureen Psaila-Dombrowski

Characteristics of an Algorithm

•Receives input or initial conditions

•Produces a result (output)

•Contains steps ▫Well-ordered▫Clearly defined▫Do-able

•Clear stopping point or conditions

•General

Page 7: New Mexico Computer Science For All Introduction to Algorithms Maureen Psaila-Dombrowski

Already Use Algorithms

•Wiggle

Page 8: New Mexico Computer Science For All Introduction to Algorithms Maureen Psaila-Dombrowski

Types of Algorithms

•Similar types can be grouped together

▫Highlight how a problem can be attacked or solution used

▫Different ways to group

Implementation: Iteration, deterministic, etc…

Methodology: Brute Force, Divide and Conquer,…

Application: Sorting, Searching,…

Page 9: New Mexico Computer Science For All Introduction to Algorithms Maureen Psaila-Dombrowski

Example: Types of Algorithms•Counting – counting large groups of items

•Sorting - puts items in a certain order in a list

•Searching - finding an item in a list

•Mapping/Graphing - solving problems related to graph theory (shortest distance, cheapest distance,…)

•Encryption – Encodes or decodes information

•Packing – how to fit the most items in a given space

•Maze – Creating or solving mazes

Page 10: New Mexico Computer Science For All Introduction to Algorithms Maureen Psaila-Dombrowski

Developing an Algorithm• What’s the problem?

• Start with input and output

• Abstraction and Decomposition▫Break the problem into parts▫Detail the steps required for each step

• Write the pseudo-code

• Refine the solution, if necessary

• Write the code

• Debug, test the code with different cases

• Your done!

Page 11: New Mexico Computer Science For All Introduction to Algorithms Maureen Psaila-Dombrowski

Example Algorithm: Take the Average

•Input and output

▫Input – list of numbers ( 5, 10, 12, 14, 16)▫Output – average of the list

•Abstraction and decomposition – what are the steps

▫Add up the numbers in the list (total)▫Count the numbers in the list (count)▫Average = total/count

Page 12: New Mexico Computer Science For All Introduction to Algorithms Maureen Psaila-Dombrowski

Example Algorithm: Take the Average

•Write the Pseudo-Code

Initialize total and count to 0

For each number in the listadd that number to the totaladd 1 to count

Divide total by count

•Refine if necessary – might find improvements

•Write the code – in the language you are using

•Debug – always!

Page 13: New Mexico Computer Science For All Introduction to Algorithms Maureen Psaila-Dombrowski

Analyzing Algorithms•There are many ways to solve the same

problem

•Analyze algorithms▫Evaluate its suitability (speed, memory

needed)▫Compare it with other possible algorithms ▫Find improvements

•Algorithms tend to become better during analysis▫shorter▫simpler▫more elegant

Page 14: New Mexico Computer Science For All Introduction to Algorithms Maureen Psaila-Dombrowski

Big O•Used in computer science

▫When analyzing algorithms Measure of how an algorithm runs as you

increase the input size dramatically.▫Processing time▫Memory used

▫Another way to classify algorithms

•Common Big O values▫O(n)▫O(n log(n))▫O(n2)

Page 15: New Mexico Computer Science For All Introduction to Algorithms Maureen Psaila-Dombrowski

Summary• Algorithm - A set of instructions to solve a problem

• Use algorithms because Don’t have think about the details of the problem All the information is in the algorithm Anyone can follow it

• Characteristics Receives input and Produces a result (output) Contains steps

▫Well-ordered▫Clearly defined▫Doable

Stops by itself General

• Algorithm Analysis is important Choose the best one One analysis used is Big O notation