CS151L Fall 2013 Week 9: Video Summary INTRODUCTION TO ALGORITHMS What is an algorithm? A set of instructions that can be used repeatedly to solve a problem or complete a task. Why do we use algorithms? • Don’t have think about the details of the problem – we already know how to solve it. • 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 Computers are problem solving machines and suited to using algorithms 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 Types of Algorithms sorted according to Application • 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 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! Example – Taking the Average Document1 • 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 • Write the Pseudo-Code Initialize total and count to 0 For each number in the list add that number to the total add 1 to count Divide total by count • Refine if necessary – might find improvements • Write the code – in the language you are using • Debug – always! 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 Big O Notation • 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) MORE 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 - Three counting problems ▫ Students in your class Document1 • • ▫ Students in your school ▫ High school students in your state Count students in a Classroom - Easy ▫ Teacher can count ▫ Students can count off Counting students in a school - Harder ▫ One possible method – “Brute Force” Gather the students Line them up Count them (Have someone count the or 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) Counting High School Students in the State - Even Harder ▫ Can’t line them up ▫ MUST USE 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 for the Student Counting problems 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 do this too 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 do this too set StateCount to 0 while (still schools left to count) go to each school ask number of student in school (SchoolCount) set StateCount = StateCount + SchoolCount LOOPING IN NETLOGO Document1 • • • • • Already know that there are three common types of loops in most programming languages: ▫ Infinite Loops ▫ Counted Loops ▫ Conditional Loops Components of a Loop ▫ Loops have some properties in common: • Body of the loop: The portion of the code that is repeated. • Iteration: Number of times the loop is executed ▫ Some types of loops have other properties • Loop Variable: A variable that is incremented during the looping process • Loop Condition: A conditional (true of false) statement that is checked during looping Infinite Loops ▫ An infinite loop repeats the body of the loop until one of the following occurs: • A“stop” command in the program • Manually turned off. • A runtime error. ▫ Already used them in NetLogo – Forever Loops Counted Loops ▫ Repeated a fixed maximum number of times. ▫ Must know how many times • Before programming • Be able to calculate maximum number of times ▫ Netlogo has a specific command for counted loops, the REPEAT command repeat Nnumber [ commands ] • The commands in the square brackets [] are repeated Nnumber of times. Conditional Loops ▫ Repeated while a certain conditional statement is true. ▫ Must establish the condition statement initially and it must be true for the loop to start ▫ The condition must become false during the looping process in order for the loop to stop, otherwise the loop becomes infinite. ▫ Netlogo has a specific command for conditional loops, the WHILE command while [condition] [ commands ] • The commands in the square brackets [] are repeated while the condition is true. • When the condition becomes false, the loop is exited. Document1