Algorithms
description
Transcript of Algorithms
![Page 1: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/1.jpg)
AlgorithmsAlgorithms are a familiar idea. Our goal is to learn to specify
them right so someone or something else does the work
![Page 2: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/2.jpg)
![Page 3: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/3.jpg)
![Page 4: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/4.jpg)
Flowcharts Flowcharts are
a good way to describe an algorithm
![Page 5: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/5.jpg)
Previous AlgorithmsAlgorithm: a precise, systematic
method to produce a specified result• We have seen algorithms already...
• Copy a file from one folder to another• Driving instructions to get to your house• Filling out your IRS tax return
Not every process is an algorithm -- debugging
![Page 6: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/6.jpg)
3 Kinds of Algorithms Today• Editing Skills
• fix a broken web page• fix a text with ragged line break• keyboard shortcuts
• Conceptual/ problems• making a cup of tea, sorting CDs
• Drawing problems• Turtle graphics prep for JavaScript
![Page 7: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/7.jpg)
Broken WebpageThis web page has broken links: mpc01b/
project1
![Page 8: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/8.jpg)
Use Search and ReplaceWhat would you search for, and
what would you replace it with? (several ways)
• ?• ?• ?
These are sort of algorithms (actually programs)
![Page 9: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/9.jpg)
Text with Ragged EdgesOften when copied from web/email
![Page 10: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/10.jpg)
MS Word Search/Replace Has feature to work on special
formatting marks
![Page 11: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/11.jpg)
Two AttemptsFirst Try
• Replace ^p with nothing • But then you lose paragraph separators (^p^p)
Second Try• Replace ^p^p with #• Replace ^p with nothing• Replace # with ^p^p
![Page 12: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/12.jpg)
Properties of AlgorithmsFor an algorithm to be well specified
it must have …• Inputs specified• Outputs specified• Definiteness• Effectiveness• Finiteness
![Page 13: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/13.jpg)
Programs vs AlgorithmsA program is an algorithm specialized
to a particular situation Algorithm:
longStringWithShortStringInIt placeholder ShortString
placeholder longStringWithShortStringInIt Program: #
#
![Page 14: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/14.jpg)
Keyboard Shortcuts•Give the mouse a break!
•Home: begin line•End: end line•Shift-End: select to end line•Alt-e t (Ctrl-X): cut selected text
•Delete a whole line:•Home, Shift-End, Ctrl-X
Program or Algorithm?
![Page 15: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/15.jpg)
Alphabetize CDs1. Def Artist_of Use Artist_of to refer to the group name2. Pick Alpha Decide which end of rack is to be start of
alphabetic sequence, and call the first slot alpha3. Pick Beta Call the slot next to alpha, beta4. Exchange If Artist_of the CD in the alpha slot is later
in the alphabet than the Artist_of the CD in the beta slot, interchange the CDs, otherwise continue on
5. More Betas? If a slot follows beta slot, begin calling it the beta slot and go to step 4, otherwise continue on
6. More Alphas? If two slots follow the alpha slot, begin calling the next one the alpha slot and the one following it the beta slot; go to step 4; otherwise stop
Wynette
BeethovenSpoon
Hampton
Pearl Jam
![Page 16: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/16.jpg)
Flow Chart Define Artist_Of
Select starting end; name it alpha
Call beta the slot adjacent to alpha
Is Artist_Of CD in alpha slot later than Artist_Of CD in
beta slot
Is there aslot following the beta
slot?
Are there 2 slots following
alpha?
Interchange CDs in alpha & beta
Begin calling next slot beta
Advance alpha to next slot & slot after it beta
1
2
3
4
5
6
N
Y
N
N Stop
Y
Y
Start
![Page 17: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/17.jpg)
Demonstration
![Page 18: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/18.jpg)
AbstractionAbstraction means removing an idea or
process form a situationBeta sweep -- while alpha points to a fixed slot, beta sweeps through slots following alpha, interchanging as necessary
The beta sweep is a concept removed based on our understanding of the operation of the algorithm
Beta Sweep
![Page 19: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/19.jpg)
Flow Chart Define Artist_Of
Select starting end; name it alpha
Call beta the slot adjacent to alpha
Is Artist_Of CD in alpha slot later than Artist_Of CD in
beta slot
Is there aslot following the beta
slot?
Is there apair of slots following
alpha?
Interchange CDs in alpha & beta
Begin calling next slot beta
Advance alpha to next slot & slot after it beta
1
2
3
4
5
6
N
Y
N
N Stop
Y
Y
Start
![Page 20: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/20.jpg)
The Beta SweepBy abstracting we can analyze parts of
an algorithm … The beta sweep has 4 properties:
• Exhaustive -- it considers all CDs after alpha• Non-redundant -- no slot pair is checked twice• Progressive -- the alphabetically earliest CD
considered so far is always in the alpha slot• Effective -- at completion, the alphabetically
earliest CD from alpha to end is in alpha slot
These properties apply only to Alphabetize CDs
![Page 21: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/21.jpg)
Alpha SweepThe alpha sweep...
Process of sweeping through all of the CDs (but the last) performing the beta sweep
• Exhausitve -- considers all but last CD• Non-redundant -- a slot is alpha only once• Progressive -- when beta sweep completes
the alphabetically next CD in alpha• Complete -- when last beta sweep is done the
last slot’s CD is later than next to last slot• Effective -- the alpha sweep alphabetizes
![Page 22: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/22.jpg)
Turtle Graphics Algorithms
You drive a turtle that leaves a mark as it moves
• Turtle Commands (start at x)• forward(20)• left(90);• forward(20);• right(135);• backward(40);
x
![Page 23: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/23.jpg)
For today’s lab, Only use these commands:
forward -- move turtle forward some number of pixels
backward -- move turtle backwardleft – turn left some number of degrees right – turn right
More when we get to Programming
![Page 24: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/24.jpg)
SummaryWe figure out most algorithms on our
own, abstracting from specific casesAlso we abstract parts of an algorithm
or program to understand them Thinking of how the program works and
reasoning about its properties allows us to know why an algorithm works … and then we can let the computer do it
![Page 25: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/25.jpg)
In Sunday’s Paper...Google Bombing: To sabotage
Google’s page-rank system
Ask Google for ‘miserable failure’
![Page 26: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/26.jpg)
George W Bush?
The most highlyranked Web pagefor words ‘miserable failure’ is George Bush?
![Page 27: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/27.jpg)
Sample Query
![Page 28: Algorithms](https://reader035.fdocuments.net/reader035/viewer/2022070423/568167d5550346895ddd2bc9/html5/thumbnails/28.jpg)
What’s Happening?Many pages make their anchor text
‘miserable failure’ and make the anchor link to the Bush biography
<a href=“http://whitehouse.gov/gwbbio.html”>miserable failure</a>
Google trusts anchor text in the page rank calculation