220 likes | 277 Views
Explore the world of algorithms, from tying shoes to parallel parking, and learn about their characteristics and importance. Discover pseudocode and the concept of Big “O” notation.
E N D
Announcements • MyITLab Access assessments have been extended to Nov 8… that’s it! • We have some programming chapters coming up that blast through the material at lighting speed • Let us slow down this week and examine the basics in detail
This week… • Today, Wednesday: Algorithms, Pseudo-code and Flowcharts • Wednesday, FridayIntro to programming with JavaScript
Name that algorithm?? Algorithms exist outside the world of computers. Think back to when you were little… What was thefirst algorithm that you learned?
Some examples of algorithms • Tying your shoes • Opening a door • Going down the stairs • Cooking Kraft Dinner • Parallel Parking a car • Landing an aircraft
Mark’s Definition • An algorithm is a sequence of steps that describe the solution to a problem. Building a fence constructing a building Cooking your lunch
Wiki’s Computer Science Definition • In mathematics, computing, linguistics and related subjects, an algorithm is a sequence of finite instructions, often used for calculation and data processing. It is formally a type of effective method in which a list of well-defined instructions for completing a task will, when given an initial state, proceed through a well-defined series of successive states, eventually terminating in an end-state. The transition from one state to the next is not necessarily deterministic; some algorithms, known as probabilistic algorithms, incorporate randomness.
Characteristics of algorithms • Finite • Finite number of steps • Finite amount of time to execute the steps • Ordered sequence • There is a first step • Each step has a unique next step • Unambiguous • Each step is understandable by executor • Each step has only one interpretation
Some terminology • Pre-conditions • Post-conditions • Inputs • Outputs • Start • End • Steps • Decisions
Pre-conditions • Pre-conditions are those things that must exist before we can ‘start’. • We might require certain ‘inputs’ to be present. Example: KD requires milk, butter, pot, water, heat
Post-conditions • Post-conditions is what we expect when everything is done. • It encompasses the ‘world’ of the algorithm when it has successfully ended. Example: Do we have a fine KD meal?Yes if we follow the recipe from start to end!
Steps and Decisions • Algorithms should fully describe every step from beginning to end. • It should describe what inputs are being modified and how. • Sometimes we need to make a decision (is the water boiling? No, wait another minute…) Note: Even KD will taste badly if we boil the water, cheese, milk and butter and forget to add the noodles at the proper time.
Not all created equal • In life, we frequently find multiple methods of solving a particular problem. • How do we know if one sequence of steps is faster than another? We could time it! Several times under various conditions just to be sure. But do we track the: • Best time? • Worst time?? • Average time???
Big “O” Notation • A math term that describes the worst case scenario of an algorithm. • Why WORST CASE and not BEST CASE? • Most algorithms give great results under ideal situations. • We only see an advantage under poor conditions.
Guess that number; 1..n Sequential1..10 Divide and Conquer 1..10 1 2 3 … 10 Guaranteed within 10 Guesses O(n) RANDOM 1..10 • 3 • 9 • 1 • … • 6 Guaranteed within 10 Guesses O(n) • >5 • <8 • >6 • If yes, it is 7 otherwise the number was 6. O(log n)
Divide and conquer? • Scales to larger input values extremely well. • Twisting the equation around, we get the following: 2x = n where x=number of guesses to find n In reality, we have a 50/50 chance on the last guestion meaning sometimes we need 1 more to have the answer. Some examples: 2^4 = 16 which means 1..16 within 4+1 guestions. 2^10 = 1024 2^20 = 1 million (We can guess any number from 1 to 1 million in 20+1 guestions!! Wow, bet a friend, loser buys a pizza)
Sample of 1 to 1 millionpsst… our number is 253,973 • > 500,000 no • > 250,000 yes • > 375,000 no • > 312,500 no • > 281,250 no • > 265,625 no • > 257,813 no • > 253,907 yes • > 255,860 no • > 254,884 no > 254,396 no > 254,152 no > 254,030 no > 253,969 yes > 254,000 no > 253,985 no > 253,977 no > 253,973 no > 253,975 no > 253,974 no 21) Is the number 253, 974? If the reply is no, we know that the number is: 253,973
pseudocode • A method of describing the steps of an algorithm using english-like sentences. • May contain formulae when necessary to explain the details • Not unlike a ‘recipe’ • A programmer can read the pseudocode and create a program
Sample Algorithm: Hair Washing • What’s wrong with this set of instructions (found on a shampoo bottle)? Lather Rinse Repeat • Is This better? Wet hair. Repeat 2 times. Apply shampoo. Rinse. Dry hair.
Example algorithm = Google maps Going from WLU to Rogers Centre (Toronto): • Exit UW campus on south side (University Ave W) • Head southwest from University Ave W - go 1.3 km • Turn left at ERB St W - go 1.6 km Yes, Google suggests this route! • Turn right at King St S - go 13.5 km • Take the HWY-401 E ramp - go 66 km • Bear right at HWY-401 Collectors E towards Dixie Road - go 6.5 km • Take the HWY-427 S towards Q.E.W. - go 7.7 km • Take the Gardiner Expy E - go 13.6 km • Take the Spadina Ave exit - go 0.9 km • Turn right at Blue Jays Way - go 0.1 km
Example algorithm: pseudo-code Problems: What if more than one person has the tallest height? What if there’s nobody in the group? Sample problem: given a group of people, find who is the tallest. for any one person in the group get persons_name get persons_height name_so_far persons_name tallest_so_far persons_height end for this person repeat for each remaining person in the group get persons_name get persons_height if persons_height > tallest_so_far then // this person is taller name_so_far persons_name tallest_so_far persons_height end of repetition display name_so_far " is the tallest, with height " tallest_so_far
Flowcharts Flowcharts visually describe an algorithm