190 likes | 315 Views
Science Fellows, Computer Science Lesson 1. Fall 2010. Algorithm . Way to solve a problem (Write a program.) But, as computer SCIENTISTS we care about more than getting *a* solution. We want to know – how good a solution is it?. Example.
E N D
Algorithm • Way to solve a problem (Write a program.) • But, as computer SCIENTISTS we care about more than getting *a* solution. We want to know – how good a solution is it?
Example • Packs of word cards. Try to find the alphabetically first card in the pack. Count how many times you turn over a card. • Can’t memorize cards. • Usually keep the cards in numbered slots, but can switch them around or keep out a few “special” cards. • Only one card not in special slot can be turned over at a time. • With each step, the cards that aren’t in special slots have to get turned back over. • Can remember counts if you need to.
Find alphabetically first card 1 5 2 6 3 • 7 8 4 Turn over count: 0
Find alphabetically first card 1 Lane 5 2 6 3 • 7 8 4 Turn over count: 1
Find alphabetically first card 1 5 Lane 2 6 3 • 7 8 4 Turn over count: 1
Find alphabetically first card 1 5 Lane 2 6 Boy 3 • 7 8 4 Turn over count: 2
Find alphabetically first card 1 5 Boy 2 6 3 • 7 8 4 Turn over count: 2
Find alphabetically first card 1 5 Boy 2 6 3 • 7 Violin 8 4 Turn over count: 3
Find alphabetically first card 1 5 Boy 2 6 3 • 7 8 4 Apple Turn over count: 4
Find alphabetically first card 1 5 Apple 2 6 3 • 7 8 4 Turn over count: 4
Examine Algorithm • How many times do I turn over a card? • What if I double the number of cards? • If there are n cards, how many times do I turn over a card?
Your turn • Sort your cards alphabetically • We’ll see what algorithms we come up with and whose is best
What if there are N cards? Some math you may want to know: 1+2+3+…+n = n(n-1)/2 Number of times I can cut the number n in half until I hit 1 = lg n
Why do we care? exponential quadratic Num card turns (Time) linear logarithmic constant Number of cards ->
So… • If my function is time = 2(n-1) all I really care about is that it is a LINEAR function. • What’s the best I can do for sorting, if I know NOTHING about my data? • What’s the best I can do for sorting if I do know something about my data?
Searching • New problem: Find the card that has a given word on it. How long will it take? What could we do to make it fast?
Back to the real world • Are these “real” problems? • Oh, yes! How does Google do such a good job? • They SORT (PageRank algorithm) better than the rest • They SEARCH quickly