new mexico computer science for all more algorithms maureen psaila-dombrowski
TRANSCRIPT
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
Counting
•Something we all know about
•Three counting problems
▫Students in your class▫Students in your school▫High school students in your state
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
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)
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
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)
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
Mazes
Easy - Don’t need an algorithm
Mazes
Perhaps you need an algorithm (systematic approach) for this one
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
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
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
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
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!
Dead End Filling VideoReference:
Mazemaster225. (2013, October 10). Maze Strategy: Dead End Filling. [Video file]. Retrieved from http://www.youtube.com/watch?v=yqZDYcpCGAI
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