introduction to algorithms - mcmaster...
TRANSCRIPT
Introduction to Algorithms
Engineering 1D04, TeachingSession 1
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 1
Introductions
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 2
Course Webpage:http://elm.mcmaster.ca/
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 3
Introduction to Algorithms
Problems require solutions.
The solution to a problem must be statedclearly.
A clear sequence of steps for solving aspecific problem is called an algorithm.
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 4
Introduction to Algorithms
Task 1 Get into groups of 4 and create an algorithm
to safely cross a road. Try to be as precise aspossible. Some groups will get to share theiralgorithms.
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 5
Task 1 Problems
The order in which tasks are performed isoften not obvious.
The individual steps themselves are oftenambiguous.
Often, the chicken never gets across theroad.
It would be nice to have a more formalway of describing the actions involved inperforming a task.
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 6
Waking up!!
Get Out of Bed Brush Teeth Eat Breakfast Brush Teeth Again Wash Get Dressed Put on Shoes Run to Catch Bus
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 7
Get Out of Bed
Start
Brush Teeth
Wash Get Dressed
Eat BreakfastBrush Teeth Run to Catch Bus
Put on Shoes
Finish
Start / Stop Box Statement Box
Waking in the Morning
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 8
Start / Stop Box Statement Box
Flowchart Elements
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 9
Waking up!!
Unfortunately, we are not all thatorganized. We can’t all have such astructured morning experience.
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 10
Waking up!!
Unfortunately, we are not all thatorganized. We can’t all have such astructured morning experience.
What if we’re short on time? Does thischange our decision making process?
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 11
Start / Stop Box
Decision Box
Statement Box
Flowchart Elements
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 12
Decision Box
Are youhungry? Yes
No
Eat breakfast
Catch bus
Decision Box
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 13
Get Out of Bed
Start
Brush Teeth
Wash Get Dressed
Eat BreakfastBrush Teeth Run to Catch Bus
Put on Shoes
FinishIs There Extra
Time?Yes
No
Waking in the Morning with Decision
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 14
Waking in the Morning with Decision Get Out of Bed Brush Teeth If There is Extra Time
Eat Breakfast Brush Teeth (Again) Wash
Get Dressed Put on Shoes Run to Catch Bus
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 15
Crossing the Road
Some tasks need to be repeated until acondition is satisfied.
Walking can be described as move leftfoot forward, move right foot forward, leftfoot, right foot … .
This can be visualized by a loop in theflowchart.
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 16
Move Left Foot Forward
Move Right Foot Forward
Are you on the other side? Yes
No
Finish
…
Crossing the Road
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 17
Crossing the Road
… While you’re not on the other side
Move Left Foot Forward Move Right Foot Forward
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 18
Introduction to Algorithms
Task 2 Return to your groups. Make a flowchart for
safely crossing a road and derive a naturallanguage description from it. Be as detailed aspossible. Compare it to your previous naturallanguage description.
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 19
Algorithms
What are the most importantcharacteristics of an algorithm?
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 20
Algorithms
What are the most importantcharacteristics of an algorithm? A step-by-step procedure for solving a
problem Finite number of steps Depends only on input
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 21
Algorithms
Need only: sequential actions decisions “while” loops (iteration)
Anything that is computable can becomputed using just these controlstructures
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 22
A More Mathematical Example
Let’s try an algorithm that is moremathematical
Do these two lines intersect? Line 1: (x1, y1) → (x2, y2) Line 2: (u1, v1) → (u2, v2)
(same groups - work it out)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 23
Intersecting Lines
!
Y = mX + c
Line1:m1
=y2" y
1
x2" x
1
Line2 :m2
=v2" v
1
u2" u
1
!
Line1: y1
= m1x1+ c
1
so c1
= y1"m
1x1
Line2 : v1
= m2u1+ c
2
so c2
= v1"m
2u1
Then solve
s = m1r + c
1
s = m2r + c
2
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 24
Intersecting Lines
!
Y = mX + c
Line1:m1
=y2" y
1
x2" x
1
Line2 :m2
=v2" v
1
u2" u
1
!
Line1: y1
= m1x1+ c
1
so c1
= y1"m
1x1
Line2 : v1
= m2u1+ c
2
so c2
= v1"m
2u1
Then solve
s = m1r + c
1
s = m2r + c
2
What problems will you face?What problems will you face?
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 25
Intersecting Lines
Work on the problem between now andLab 2 - you will need to solve this in thatlab session.