lawrence(snyder...

38
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004

Upload: others

Post on 23-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

Lawrence  Snyder  University  of  Washington,  Seattle  

© Lawrence Snyder 2004

Page 2: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

  Def.  An  algorithm  is  a  precise,  systematic  process  for  an  agent  to  produce  a  specified  result  

  Five  properties  characterize  algorithms  ▪  Input  specified  –  tell  form  and  amount  of  input  required  ▪  Output  specified  –  tell  form  and  amount  of  output  produced  ▪  Definiteness  –  say  explicitly  what  to  do  &  in  what  order  ▪  Effectiveness  –  operations  within  agent’s  abilities  ▪  Finiteness  –  will  stop  and  give  an  answer  or  say  “none”  

§   Programs  are  algorithms  

2/4/13 © 2011 Larry Snyder, CSE 2

Page 3: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

  The  binary  code  computers  execute  are  algorithms  

  Software  developers  create  algorithms  all  the  time    §  Using  languages  like  C,  Processing,  JavaScript,  etc.  §  A  compiler  (it’s  a  translator)  converts  to  binary  code  

  These  cases  specify  computation  in  complete  detail  because  computers  are  clueless  

  But  at  other  levels  the  agent  is  a  person  …  

2/4/13 © 2011 Larry Snyder, CSE 3

Page 4: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

  In  their  paper  Larry  Page  and  Sergey  Brin  gave  their  algorithm  for  processing  a  Google  query  

  Algorithms  are  “given”  at  many  levels  of  detail  –  it  depends  on  what  agent  needs  

2/4/13 © 2011 Larry Snyder, CSE 4

Page 5: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

  There  are  always  different  algorithms  to  pick    Consider  sorting  …  

§  Put  items  in  order  §  Exchange  Sort  for  ▪  walrus  ▪  seal  ▪  whale  ▪  gull  ▪  clam  

§  Compare  with  every    value  following  

wa se wh g c se wa wh g c se wa wh g c g wa wh se c c wa wh se g c wa wh se g c se wh wa g c g wh wa se c g wa wh se c g se wh wa c g se wa wh

2/4/13 © 2011 Larry Snyder, CSE 5

Page 6: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

§  Bubble  Sort  for  ▪  walrus  ▪  seal  ▪  whale  ▪  gull  ▪  clam  

§  Compare  in  pairs,  letting    larger  values  “bubble  up”  

 

wa se wh g c se wa wh g c se wa wh g c se wa g wh c se wa g c wh se wa g c wh se g wa c wh se g c wa wh g se c wa wh g c se wa wh c g se wa wh

2/4/13 © 2011 Larry Snyder, CSE 6

Page 7: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

  The  two  algorithms  take  the  same  amount  of  time,  but  they  are  different  as  we  see  from  the  patterns  of  their  comparisons  

2/4/13 © 2011 Larry Snyder, CSE 7

Page 8: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

  How  do  we  know  that  the  algorithms  work?  §  Developing  algorithms  is  not  just  thinking  them  up  §  It  is  also  reasoning  through  why  they  work  …  you  need  to  know  why  explicitly  enough  to  tell  someone  else  

  Let’s  see  how  to  do  that  

2/4/13 © 2011 Larry Snyder, CSE 8

Page 9: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

 Why  do  you  think  it  sorts?  

2/4/13 © 2011 Larry Snyder, CSE 9

wa se wh g c se wa wh g c se wa wh g c g wa wh se c c wa wh se g c wa wh se g c se wh wa g c g wh wa se c g wa wh se c g se wh wa c g se wa wh

Page 10: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

 Why  do  you  think  it  sorts?  §  There  are  several  passes    through  the  data  with  leading  item  unsorted  fixed  (marked    with  lines)  

  Notice  this  property:  After    the  pass,  the  leading  item  must  be  the  smallest  of  all  processed  on  the  pass  

2/4/13 © 2011 Larry Snyder, CSE 10

wa se wh g c se wa wh g c se wa wh g c g wa wh se c c wa wh se g c wa wh se g c se wh wa g c g wh wa se c g wa wh se c g se wh wa c g se wa wh

Page 11: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

 Why  do  you  think  it  sorts?    

2/4/13 © 2011 Larry Snyder, CSE 11

wa se wh g c se wa wh g c se wa wh g c se wa g wh c se wa g c wh se wa g c wh se g wa c wh se g c wa wh g se c wa wh g c se wa wh c g se wa wh

Page 12: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

 Why  do  you  think  it  sorts?  §  There  are  several  passes    through  the  data  comparing  pairs  of  data    (marked  w/  lines)  

  Notice  this  property:  After    the  pass,  pair  compares  will    locate  the  next  largest  item  and  bubble  it  into  position  

 2/4/13 © 2011 Larry Snyder, CSE 12

wa se wh g c se wa wh g c se wa wh g c se wa g wh c se wa g c wh se wa g c wh se g wa c wh se g c wa wh g se c wa wh g c se wa wh c g se wa wh

Page 13: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

 What  is  the  complexity  of  the  sorting  algorithms  that  we  just  looked  at?  

  Often  has  two  parts:  get  item  in  order;  repeat  §  Exchange  –  interchange  to  put  smallest  earlier  §  Bubble  –  compare  adjacent  values,  push  largest  further  on  

  How  many  times  do  we  do  that  §  Be  careful,  not  exactly  how  many  times  but  what  “order”  …      

Page 14: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

  This  exchange  sort  needs  4  +  3  +  2  +  1  =  10  compares  for  5  items      Generally  for  n  items  

(n-­‐1)  +  (n-­‐2)  +  …  +  2  +  1  =  n(n-­‐1)/2  =  1/2  (n2  –n)  

2/4/13 © 2011 Larry Snyder, CSE 14

wa se wh g c se wa wh g c se wa wh g c g wa wh se c c wa wh se g c wa wh se g c se wh wa g c g wh wa se c g wa wh se c g se wh wa c g se wa wh

Page 15: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

  Merging  two  sorted  arrays  into  a  single  sorted  array  is  straight  forward  

Page 16: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

2   3   8   3   4   6   7  

Page 17: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

2   3   8   3   4   6   7  

2  

Page 18: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

2   3   8   3   4   6   7  

2   3  

Page 19: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

2   3   8   3   4   6   7  

2   3   3  

Page 20: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

2   3   8   3   4   6   7  

2   3   3   4  

Page 21: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

2   3   8   3   4   6   7  

2   3   3   4   6  

Page 22: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

2   3   8   3   4   6   7  

2   3   3   4   6   7  

Page 23: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

2   3   8   3   4   6   7  

2   3   3   4   6   7   8  

Page 24: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

2   97   17   39   12   46   10   55   80   42   37  

Page 25: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

2   97   17   39   12   46   10   55   80   42   37  

2   97   17   39   12   46   10   55   80   42   37  

Page 26: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

2   97   17   39   12   46   10   55   80   42   37  

2   97   17   39   12   46   10   55   80   42   37  

2   97   17   39   12   46   10   55   80   42   37  

Page 27: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

2   97   17   39   12   46   10   55   80   42   37  

2   97   17   39   12   46   10   55   80   42   37  

2   97   17   39   12   46   10   55   80   42   37  

2   97   39   12  17   46   10   55   80   42   37  

Page 28: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

2   97   17   39   12   46   10   55   80   42   37  

2   97   17   39   12   46   10   55   80   42   37  

2   97   17   39   12   46   10   55   80   42   37  

2   97   39   12  17   46   10   55   80   42   37  

39   12   10   55   42   37  

Page 29: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

2   97   17   39   12   46   10   55   80   42   37  

2   97   17   39   12   46   10   55   80   42   37  

2   97   17   39   12   46   10   55   80   42   37  

2   97   39   12  17   46   10   55   80   42   37  

39   12   10   55   42   37  

Page 30: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

2   97   17   39   12   46   10   55   80   42   37  

2   97   17   39   12   46   10   55   80   42   37  

2   97   17   39   12   46   10   55   80   42   37  

2   97   12   39  17   46   10   55   80   37   42  

39   12   10   55   42   37  

Page 31: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

2   97   17   39   12   46   10   55   80   42   37  

2   97   17   39   12   46   10   55   80   42   37  

2   97   17   39   12   46   10   55   80   42   37  

2   97   12   39  17   46   10   55   80   37   42  

39   12   10   55   42   37  

Page 32: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

2   97   17   39   12   46   10   55   80   42   37  

2   97   17   39   12   46   10   55   80   42   37  

2   97   12   17   39   10   46   55   37   42   80  

2   97   12   39  17   46   10   55   80   37   42  

39   12   10   55   42   37  

Page 33: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

2   97   17   39   12   46   10   55   80   42   37  

2   97   17   39   12   46   10   55   80   42   37  

2   97   12   17   39   10   46   55   37   42   80  

2   97   12   39  17   46   10   55   80   37   42  

39   12   10   55   42   37  

Page 34: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

2   97   17   39   12   46   10   55   80   42   37  

2   12   17   39   97   10   37   42   46   55   80  

2   97   12   17   39   10   46   55   37   42   80  

2   97   12   39  17   46   10   55   80   37   42  

39   12   10   55   42   37  

Page 35: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

2   97   17   39   12   46   10   55   80   42   37  

2   12   17   39   97   10   37   42   46   55   80  

2   97   12   17   39   10   46   55   37   42   80  

2   97   12   39  17   46   10   55   80   37   42  

39   12   10   55   42   37  

Page 36: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

2   10   12   17   37   39   42   46   55   80   97  

2   12   17   39   97   10   37   42   46   55   80  

2   97   12   17   39   10   46   55   37   42   80  

2   97   12   39  17   46   10   55   80   37   42  

39   12   10   55   42   37  

Page 37: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

n

n/2 n/2

n/4 n/4 n/4 n/4

n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8

log2n

n

Complexity à nlog2n

Page 38: Lawrence(Snyder University(of(Washington,(Seattlecourses.cs.washington.edu/courses/cse120/13wi/lec/cse120... · 2013. 2. 4. · There!are!always!different!algorithms!to!pick! Considersorting!…!

  It  is  not  sufficient  to  think  up  a  clever  algorithm  …  you  need  to  know  why  it  works  

  It’s  usually  not  tough,  because  the  logic  of  your  method  typically  translates  into  an  explanation  of  why  it  works.  

But  you  must  think  about  it!!    

  Once  you  know  it  works  –  CS  people  figure  out  how  fast  it  is!  

2/4/13 © 2011 Larry Snyder, CSE 38