Top Banner
Asymptotes and Algorithms What You’ve Forgotten Since University By Gary Short Developer Evangelist Developer Express DPR302
65

Asymptotes and Algorithms What You’ve Forgotten Since University

Feb 23, 2016

Download

Documents

vin

DPR302. Asymptotes and Algorithms What You’ve Forgotten Since University. By Gary Short Developer Evangelist Developer Express. Agenda. Introduction What is an Algorithm? Why Optimization is Important Optimisation is Child’s Play Why do we Use Mathematics? - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Asymptotes and Algorithms What You’ve Forgotten Since UniversityBy Gary ShortDeveloper EvangelistDeveloper Express

DPR302

Page 2: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Agenda

IntroductionWhat is an Algorithm?Why Optimization is ImportantOptimisation is Child’s PlayWhy do we Use Mathematics?One Tactic for Optimizing an AlgorithmBut .Net Saves me from all that, Right?Questions.

Page 3: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Introduction

Gary ShortDeveloper Evangelist DevExpressMicrosoft MVP in C#[email protected]@garyshort

Page 4: Asymptotes and Algorithms  What You’ve Forgotten  Since University

What is an Algorithm?

… a finite list of well defined steps to complete a task or calculation.

Page 5: Asymptotes and Algorithms  What You’ve Forgotten  Since University

There are simple ones…

Page 6: Asymptotes and Algorithms  What You’ve Forgotten  Since University

There are complex ones…

Page 7: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Prove no three positive integers a, b, and c can satisfy the equation an + bn = cn for any integer

value of n greater than two

Page 8: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Why is optimization important?

Page 9: Asymptotes and Algorithms  What You’ve Forgotten  Since University

http://www.flickr.com/photos/mushjazzlexima-leakpaalex/2237327967/

Page 10: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Optimization is Child’s Play

Page 11: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Take This Algorithm…

Stop current taskWash handsSit at table

Foreach item of food in MainCourseConsume food

Wait for dessertConsume dessertWait for meal completionForeach dish in dishes

Clear dish from tableForeach dish in dishes

Wash dishReturn to previous task

Page 12: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Optimized by my 4 Year Old Daughter to…

Pause current gameSet velocity = MAX_INTMove to tableTakeSliceBread(2)Foreach item of food in MainCourse

Place item on BreadSlice(1)Place BreadSlice(2) on topLeave tableResume current game

Page 13: Asymptotes and Algorithms  What You’ve Forgotten  Since University

And it’s not a skill we lose in adulthood

Page 14: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Morning Algorithm

RiseShowerBoil kettleMake teaToast breadSpread butter on toastConsume breakfastBrush teethCommute to work

Page 15: Asymptotes and Algorithms  What You’ve Forgotten  Since University
Page 16: Asymptotes and Algorithms  What You’ve Forgotten  Since University
Page 17: Asymptotes and Algorithms  What You’ve Forgotten  Since University
Page 18: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Optimized for Smallville…

RiseRush downstairsPut kettle on to boilPut toast in toasterRush back upstairsShowerRush downstairsMake tea and spread toastWatch SmallvilleConsume tea and toast

Page 19: Asymptotes and Algorithms  What You’ve Forgotten  Since University

The Only Thing Hard About This is…

The language we chose to use to describe it

𝐴=𝜋 𝑟2

(𝑥+𝑎 )𝑛=∑𝑘=0

𝑛

(𝑛𝑘)𝑥𝑘𝑎𝑛−𝑘

𝑓 (𝑥 )=𝑎0+∑𝑛=1

(𝑎𝑛cos𝑛𝜋 𝑥𝐿 +𝑏𝑛 sin

𝑛𝜋 𝑥𝐿 )

𝑎 2+𝑏 2=𝑐 2𝑥=−𝑏±√𝑏2−4𝑎𝑐

2𝑎

𝑒𝑥=1+ 𝑥1 !

+𝑥22 !

+𝑥33 !

+…,−∞<𝑥<∞

𝐴=𝜋 𝑟2

𝐴=𝜋 𝑟2

𝑎 2+𝑏 2=𝑐 2

𝑎 2+𝑏 2=𝑐 2

𝑎2+𝑏2=𝑐

2

𝐴=𝜋 𝑟2

(𝑥+𝑎)𝑛 =∑

𝑘=0

𝑛 (𝑛𝑘)𝑥𝑘 𝑎𝑛

−𝑘

Page 20: Asymptotes and Algorithms  What You’ve Forgotten  Since University

So Why do we use Math?

Page 21: Asymptotes and Algorithms  What You’ve Forgotten  Since University

http://www.flickr.com/photos/truelifeimages/2102930162/

Page 22: Asymptotes and Algorithms  What You’ve Forgotten  Since University

http://www.flickr.com/photos/29019866@N00/4783131736/

Page 23: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Our Model Has…

Single processorRAMUnderstands arithmetical instructions

AddSubtractPopPush

All instructions executed sequentiallyAll instructions take constant time to complete.

Page 24: Asymptotes and Algorithms  What You’ve Forgotten  Since University

demo

Page 25: Asymptotes and Algorithms  What You’ve Forgotten  Since University
Page 26: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Running Time is Then…

Sum running times for each statementProduct of the cost and the timeSo…T(n) = c1n + c2(n-1) + c3(n-1) + c4(sum of tj for 1 <= j <= n) + c5(sum of tj -1 for 1 <= j <= n) + c6(sum of tj -1 for 1 <= j <= n) + c7(n-1).

Page 27: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Best Case Running time

When the collection is already sortedT(n) = c1n + c2(n-1) + c3(n-1) + c4(n-1) + c7(n-1-)T(n) = (c1+c2+c3+c4+c7)n – (c2+c3+c4+c7)Which is a function in the form an + bThus T(n) is a linear function of n.

Page 28: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Let’s Quickly Prove That…

Page 29: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Worst Case Running Time

Reverse sorted orderT(n) = c1n + c2(n-1) + c3(n-1) + c4(n(n+1)/2 -1) + c5(n(n-1)/2) + c6(n(n-1)/2) + c7(n-1)T(n) = (c4/2 + c5/2 + c6/2)n^2 + (c1 + c2 + c3 + c4/2 – c5/2 – c6/2 + c7)n – (c2 + c3 + c4 + c7)Which is a function in the form an^2 + bn + cThus T(n) is a quadratic function of n.

Page 30: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Again Let’s Quickly Prove That

Page 31: Asymptotes and Algorithms  What You’ve Forgotten  Since University

So Now We Can Prove Speed

an + bIs faster thanan^2 + bn + c

Page 32: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Can we Make The Notation More Readable?

Page 33: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Asymptotic Notation

Big ‘O’ NotationFor the functions

an + ban^2 + bn + c

If n is sufficiently largeThen the largest term of n dominates the function

SoT(n) = an + b = O(n) T(n) = an^2 + bn + c + O(n^2).

Page 34: Asymptotes and Algorithms  What You’ve Forgotten  Since University

So Now we Can Say…

Insertion Sort isBest Case

O(n)Worst Case

O(n^2)

Page 35: Asymptotes and Algorithms  What You’ve Forgotten  Since University

But we only care about the worst case

Page 36: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Let’s Optimize Our Search Algorithm

Page 37: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Let’s do What we Did When we Were Kids

Divide the problem up into smaller parts and conquer each of those smaller parts before combining the solutions.

Page 38: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Demo

Page 39: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Okay, so How Fast is That Algorithm?

Page 40: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Merge Sort in 3 Easy Parts

DivideComputes the middle of the arrayConstant time

D(n) = O(1)

ConquerRecursively solve two n/2 sized sub problems

Each contribute 2T(n/2)

CombineCombine step is linear

C(n) = O(n)

So worst case2T(n/2) + O(n)

Page 41: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Use The Master Method to Solve This Recursion

Page 42: Asymptotes and Algorithms  What You’ve Forgotten  Since University

What is The Master Method?

‘Recipe’ for solving recurrences in the formT(n) = aT(n/b) + f(n)Where

The constants a >= 1 and b > 1And f(n) is asymptotically positive

Our function 2T(n/2) + O(n) is in this form

Page 43: Asymptotes and Algorithms  What You’ve Forgotten  Since University

How Does it Work?

Recall it works with function of the formaT(n/b) + f(n)

We compare f(n) with n log baThe larger of the two determines the solutionIf n log ba is larger then

T(n) = O(nlogba)If f(n) is larger then

T(n) = O(f(n))If they are equal then we multiply by log factor

T(n) = O(f(n) log n)

Page 44: Asymptotes and Algorithms  What You’ve Forgotten  Since University

So in Our Example

We’re in case threeWorst case Merge Sort is

O(n log n)Which is much better than Insertion Sort

O(n^2)

Page 45: Asymptotes and Algorithms  What You’ve Forgotten  Since University
Page 46: Asymptotes and Algorithms  What You’ve Forgotten  Since University

But .net Handles all this for me

Page 47: Asymptotes and Algorithms  What You’ve Forgotten  Since University

List<T> - Add

Page 48: Asymptotes and Algorithms  What You’ve Forgotten  Since University

List<T> - Growing

Page 49: Asymptotes and Algorithms  What You’ve Forgotten  Since University

List<T> - EnsureCapacity

Page 50: Asymptotes and Algorithms  What You’ve Forgotten  Since University

List<T> - Capacity

Page 51: Asymptotes and Algorithms  What You’ve Forgotten  Since University

List<T> - AddRange

Page 52: Asymptotes and Algorithms  What You’ve Forgotten  Since University

List<T> - AddRange

Page 53: Asymptotes and Algorithms  What You’ve Forgotten  Since University

List<T> - InsertRange

Page 54: Asymptotes and Algorithms  What You’ve Forgotten  Since University

So What are the Performance Implications?

Page 55: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Data Provider

Page 56: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Tester

Page 57: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Results

10 100 1000 10000 100000 10000000

5000

10000

15000

20000

25000

30000

Performance: Add Versus AddRange

AddAddRange

Number of Elements Added

Num

ber o

f Tic

ks

Page 58: Asymptotes and Algorithms  What You’ve Forgotten  Since University

What We’ve Learned

Performance is importantOptimization comes naturally to usDivide and conquerRunning time is the sum of product of cost and execution Maths syntax makes this all seem scaryAsymptotic notation simplifies languageThis stuff matters, even in the age of .net.

Page 59: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Questions?

[email protected]@garyshort

Page 60: Asymptotes and Algorithms  What You’ve Forgotten  Since University

DPR Track Resources

http://www.microsoft.com/visualstudio http://www.microsoft.com/visualstudio/en-us/lightswitch http://www.microsoft.com/expression/http://blogs.msdn.com/b/somasegar/http://blogs.msdn.com/b/bharry/http://www.microsoft.com/sqlserver/en/us/default.aspxhttp://www.facebook.com/visualstudio

Page 61: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Resources

www.microsoft.com/teched

Sessions On-Demand & Community Microsoft Certification & Training Resources

Resources for IT Professionals Resources for Developers

www.microsoft.com/learning

http://microsoft.com/technet http://microsoft.com/msdn

Learning

http://northamerica.msteched.com

Connect. Share. Discuss.

Page 62: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Complete an evaluation on CommNet and enter to win!

Page 63: Asymptotes and Algorithms  What You’ve Forgotten  Since University

Scan the Tag to evaluate this session now on myTech•Ed Mobile

Click icon to add picture

Page 64: Asymptotes and Algorithms  What You’ve Forgotten  Since University
Page 65: Asymptotes and Algorithms  What You’ve Forgotten  Since University