1 / 21

Question of the Day

Question of the Day. How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented differently, as before?. Question of the Day.

cher
Download Presentation

Question of the Day

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Question of the Day • How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented differently, as before?

  2. Question of the Day • How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented differently, as before?

  3. CSC 212 – Data Structures Lecture 20:Implementing Stack

  4. Using Stack • Last-In, First-Out principle used to access data • Also called LIFO ordering • Top of stack is where data added & removed

  5. Stack Interface public interface Stack<E> extends Collection {public Epeek()throws EmptyCollectionException;public Epop() throws EmptyCollectionException;public void push(E element); } • Any type of data stored within a Stack • Generics enable us to avoid rewriting this code • Minimum set of exceptions defined by interface • Classes could throw more unchecked exceptions

  6. Array-based Implementation … 0 1 2 • Array reference in a field • Another field tracks top element’s index • Stack size also: top • Add to next lowest index • Remove highest index Algorithm isEmpty()returntop== 0 Algorithmpop()ifisEmpty()then throw new EmptyCollectionExceptionelsetoptop 1returnstack[top]

  7. Array-based Implementation Algorithm isEmpty()returntop== 0 Algorithmpop()ifisEmpty()then throw new EmptyCollectionExceptionelsetoptop 1returnstack[top] … 0 1 2 top • Array reference in a field • Another field tracks top element’s index • Stack size also: top • Add to next lowest index • Remove highest index

  8. Array-based Implementation … X 0 1 2 top • Array reference in a field • Another field tracks top element’s index • Stack size also: top • Add to next lowest index • Remove highest index Algorithm isEmpty()returntop== 0 Algorithmpop()ifisEmpty()then throw new EmptyCollectionExceptionelsetoptop 1returnstack[top]

  9. Array-based Implementation … X 0 1 2 top • Array reference in a field • Another field tracks top element’s index • Stack size also: top • Add to next lowest index • Remove highest index Algorithm isEmpty()returntop== 0 Algorithmpop()ifisEmpty()then throw new EmptyCollectionExceptionelsetoptop 1returnstack[top]

  10. X 0 1 2 top • Could fill array with data • More push()s impossible • Throw exception on error • Specific to array-based • Unchecked exception must be used Algorithmpush(elem) iftop==stack.lengththen throw new FullStackExceptionelsestack[top]elemtoptop+ 1fi

  11. X 0 1 2 top • Could fill array with data • More push()s impossible • Throw exception on error • Specific to array-based • Unchecked exception must be used Algorithmpush(elem) iftop==stack.lengththen throw new FullStackExceptionelsestack[top]elemtoptop+ 1fi

  12. 0 1 2 top • Could fill array with data • More push()s impossible • Throw exception on error • Specific to array-based • Unchecked exception must be used Algorithmpush(elem) iftop==stack.lengththen throw new FullStackExceptionelsestack[top]elemtoptop+ 1fi

  13. Oops… My Bad … 0 1 2 top • Could fill array with data • More push()s impossible • Throw exception on error • Specific to array-based • Unchecked exception must be used Algorithmpush(elem) iftop==stack.lengththen throw new FullStackExceptionelsestack[top]elemtoptop+ 1fi

  14. Why It Rocks Why It Sucks Simple Array-based Stack • Easy to write & read • Simple to find bugs • Quick running times • Methods take O(1) time • Array must be huge • Max. possible elements • Problems occur when too many exist at once • When full,throws specific exception

  15. Better Approach (Maybe?) • Implement with array, but allow resizing array • Grow as needed rather that stating full • (In Java) Requires allocating new, larger array • Copy data into new array from current array • Reassign field so that it now aliases larger array • Arrays.copyOf()does everything in single call

  16. Better Approach (Maybe?) • Implement with array, but allow resizing array • Grow as needed rather that stating full • (In Java) Requires allocating new, larger array • Copy data into new array from current array • Reassign field so that it now aliases larger array • Arrays.copyOf()does everything in single call • Method call is O(1), but method takes O(n) time • push() now O(n) in worst-case, but how bad is that?

  17. Ways to Grow Array • Two ways to increase array size • Constant value (e.g., 2, 4, 6, 8, 10…) • Constant factor (e.g., 2, 4, 8, 16, 32…) • Both approaches requires O(n) time… • Instantiating & copying array are slow steps • …average (amortized) costs differ, however • Difference in how often slow step needed

  18. Increase Array Size by c • To hold n elements, must grow k = n/c times • Copy array each growth, so total copies is:1+ (c+1) + • • • + (((k-1)*c)+1) + (k*c+1)= 1 + (k*c+1) + (c+1) + (((k-1)*c)+1) + • • •= ((k * c)+ 2)+ ((k * c)+ 2) + • • •= k/2 * ((k * c) + 2)= O(c*k2)= O(c * (n/c)2) = O(n2 * 1/c)= O(n2) • Averaged over adding n elements = O(n) each!

  19. Grow by Constant Factor • To hold n elements, need to grow k = logn times • Still copy array each time, so will make this many copies: 1 + 2 + 4 + • • • + 2k-1 + 2k= (2k - 1) + 2k= 2k+1 - 1 = (2 * 2k) - 1 = (2 * 2logn) - 1= O(2n - 1) = O(n) • Average cost is O(1)(O(n) copies ÷ n elements)

  20. Your Turn • Get into your groups and complete activity

  21. For Next Lecture • Read 4.1 – 4.3 before Wednesday's class • Can we do better than resizable arrays? • Didn’t some GENIUSfind too much memory expensive? • How could we grow & shrink memory demands? • Is linked list an ADT? Why or why not? • Week #7 weekly assignment due Wednesday • Programming Assignment #1 due in 2 weeks • Start before it gets too late! Planning saves time!

More Related