introduction to algorithms - mcmaster...

26
Introduction to Algorithms Engineering 1D04, Teaching Session 1

Upload: ledieu

Post on 27-Jun-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

Introduction to Algorithms

Engineering 1D04, TeachingSession 1

Page 2: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 1

Introductions

Page 3: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 2

Course Webpage:http://elm.mcmaster.ca/

Page 4: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© 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.

Page 5: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© 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.

Page 6: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© 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.

Page 7: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© 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

Page 8: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© 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

Page 9: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 8

Start / Stop Box Statement Box

Flowchart Elements

Page 10: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© 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.

Page 11: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© 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?

Page 12: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 11

Start / Stop Box

Decision Box

Statement Box

Flowchart Elements

Page 13: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 12

Decision Box

Are youhungry? Yes

No

Eat breakfast

Catch bus

Decision Box

Page 14: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© 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

Page 15: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© 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

Page 16: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© 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.

Page 17: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© 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

Page 18: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© 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

Page 19: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© 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.

Page 20: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 19

Algorithms

What are the most importantcharacteristics of an algorithm?

Page 21: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© 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

Page 22: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© 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

Page 23: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© 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)

Page 24: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© 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

Page 25: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© 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?

Page 26: Introduction to Algorithms - McMaster Universityimps.mcmaster.ca/.../tutorials/T1_IntroductionToAlgorithms.pdf · Introduction to Algorithms Task 1 Get into groups of 4 and create

© 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.