26-CS106X-Practice-Midterm

Write a function that returns true if and only if its

Info icon This preview shows pages 7–9. Sign up to view the full content.

View Full Document Right Arrow Icon
that begins at the same time another movie ends. Write a function that returns true if and only if it’s possible to see all the movies you want to see, and false otherwise. Notice that you needn’t generate the schedule itself—just a yes or no as to whether a schedule exists. Your routine should return as soon as it can produce a true or false , as all of our recursive backtracking examples have. Use this and the next two pages for your work. struct movie { string name; int duration; // in minutes Vector<int> showTimes; // each in minutes since midnight }; bool canSchedule(Vector<string>& titles, Map<movie>& schedule) { Problem 8: Implementing the Map: Take II Many hash table implementations—including the hash table that backs our Map class—use external probing, so each bucket is actually a data structure storing all of the keys that hash to the same number. But external probing is generally equated with linked lists, and we’re not testing you on that stuff until the final. For this version of the Map , you’re going to use quadratic internal probing, which means that each bucket stores at most one key-value pair. If the Map wants to store a second pair in a particular bucket, we just won’t let it. We’ll force it to look elsewhere. When entering a new key, the key is hashed and reduced to a number h. If that particular bucket is empty, then the key-value pair is assigned and that’s that. If the bucket is occupied, but the new key matches the key already residing there, the old value is replaced with the new one. If the bucket is occupied, but occupied by a key different from the one 7
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
being inserted, the search for an unoccupied bucket advances to examine slot h + 1, and if that fails, h + 3, and if that fails, h + 6, then h + 10, then h + 15, and so forth. (Of course, all of these numbers—h, h + 1, h + 3, etc—are modulo the number of buckets.) You might ask why we’re going with the triangular numbers—0, 1, 3, 6, 10, 15, 21, etc— for offsets. (A number is triangular if can be expressed as the sum of the form 1 + 2 + 3 + … + k. For instance, 10 is triangular because it can be written as 1 + 2 + 3 + 4.) We could try h, then h + 1, then h + 2, then h + 3, etc. But quadratic probing, using the triangular number offsets, does a better job of distributing the elements across the full range of buckets. And as long as the number of buckets is a power of 2, this quadratic probing method will explore every bucket if necessary. Of course, there’s the danger that we’ll run out of buckets! After all, there are a limited number of them, and each one can accommodate at most one key-value pair. We’re going to adopt the strategy that, at the beginning of each insertion request, we’ll check to see if strictly more than 75% of the buckets are full, and if so, we’ll double the number of buckets and rehash all previously inserted elements to their new home as if they’re being inserted for the very first time.
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern