new mexico computer science for all more algorithms maureen psaila-dombrowski

17
New Mexico Computer Science For All More Algorithms Maureen Psaila-Dombrowski

Upload: alan-fisher

Post on 17-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

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

New Mexico Computer Science For All

More Algorithms

Maureen Psaila-Dombrowski

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

Algorithms•Algorithm: A set of instructions that can

be used repeatedly to solve a problem or complete a task.

▫As you problem grows, the need for a good algorithm grows

Need a systematic way to solve the problem

•Two Problems

▫Counting▫Mazes

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

Counting

•Something we all know about

•Three counting problems

▫Students in your class▫Students in your school▫High school students in your state

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

Count Students in Class

•Easy ▫Teacher can count▫Students can count off

•Pseudocode

set ClassCount to 0while (students left in class to count)

set ClassCount to ClassCount + 1

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

Count Students in School• Harder

▫One possible method – “Brute Force” Gather the students Line them up Count them

▫Have someone count the▫Have them count off

▫Another method – Divide and Conquer (algorithmic principle, break the problem into smaller pieces and then solve) Leave students in classrooms Have each classroom count their students

(ClassCount) Add up the students from classroom (SchoolCount)

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

Pseudocode: Count Students in School

In each classroomset ClassCount to 0while (students left in class to count)

set ClassCount to ClassCount + 1

For the schoolset SchoolCount to 0while (still classrooms left to count)

go to each classroomask number of student in class

(ClassCount)set SchoolCount = SchoolCount +

ClassCount

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

High School Students in State•Even Harder

▫Can’t line them up▫Divide and Conquer

Have each School count their students▫At each school

▫Leave students in classrooms▫Have each classroom count their students▫Add up the students from each classroom (ClassCount)

Have the schools report their student numbers (SchoolCount)

Add up all the School’s student numbers to get the number of high school students in the state (StateCount)

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

Pseudocode: Count Students in StateIn each classroom in each school

set ClassCount to 0while (students left in class to count)set ClassCount to ClassCount + 1

For the schoolset SchoolCount to 0while (still classrooms left to count)

go to each classroomask number of student in class (ClassCount)set SchoolCount = SchoolCount + ClassCount

For the stateset StateCount to 0while (still schools left to count)

go to each schoolask number of student in school (SchoolCount)set StateCount = StateCount + SchoolCount

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

Mazes

Easy - Don’t need an algorithm

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

Mazes

Perhaps you need an algorithm (systematic approach) for this one

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

Mazes – Two Approaches•Wall Following

▫Good if you can’t see the whole maze▫Works on simply connected mazes▫Pick a wall (right or left) and keep following it

– never change

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

Mazes – Two Approaches

•Wall Following Pseudocode (left wall following)

Find the nearest wall on the leftTurn left if you can and move

forwardIf you can’t turn left

Move forward if you canIf you can’t move forward

Turn right and move forward

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

Left Wall Following Video

Reference:AloucasHellas. (2013, October 10). Maze Solving Algorithms: Left Hand Rule (LHR) [Video file]. Retrieved from http://www.youtube.com/watch?v=NA137qGmz4s

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

Mazes – Two Approaches

•Dead End Filling▫Good if you CAN see the whole maze▫Find all the dead ends and fill them in▫Works if there are dead ends

Find All the Dead Ends Fill Them InMaze

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

Mazes – Two Approaches

•Dead End Filling Pseudocode:

Find dead endsScan the mazeIdentify dead end (walls on 3 sides)Remember the dead ends

Fill in the dead endsGo to dead endWhile not at junction (walls on 2

sides)Fill in maze

Solution is left – solve maze!

Page 16: New Mexico Computer Science For All More Algorithms Maureen Psaila-Dombrowski

Dead End Filling VideoReference:

Mazemaster225. (2013, October 10). Maze Strategy: Dead End Filling. [Video file]. Retrieved from http://www.youtube.com/watch?v=yqZDYcpCGAI

Page 17: New Mexico Computer Science For All More Algorithms Maureen Psaila-Dombrowski

Summary•Algorithm: A set of instructions that can

be used repeatedly to solve a problem or complete a task.▫The bigger the problem the more you need

a systematic way to solve the problem

•Two Problems

▫Counting Divide and Conquer: Algorithmic principle

▫Mazes Wall Following Dead End Filling