170 likes | 299 Views
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
E N D
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 ClassCountto 0 while (students left in class to count) set ClassCountto 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 classroom set ClassCountto 0 while (students left in class to count) set ClassCountto ClassCount+ 1 For the school set SchoolCount to 0 while (still classrooms left to count) go to each classroom ask 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 State In each classroom in each school set ClassCount to 0 while (students left in class to count) set ClassCount to ClassCount + 1 For the school set SchoolCount to 0 while (still classrooms left to count) go to each classroom ask number of student in class (ClassCount) set SchoolCount = SchoolCount + ClassCount For the state set StateCountto 0 while (still schools left to count) go to each school ask 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 left Turn left if you can and move forward If you can’t turn left Move forward if you can If 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 In Maze
Mazes – Two Approaches • Dead End Filling Pseudocode: Find dead ends Scan the maze Identify dead end (walls on 3 sides) Remember the dead ends Fill in the dead ends Go to dead end While not at junction (walls on 2 sides) Fill in maze Solution is left – solve maze!
Dead End Filling Video Reference: 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