1 / 94

Unordered List Properties and Sequential Search Analysis

Learn about the properties of an unordered list and analyze the running times of sequential search. Discover how rearranging data based on search patterns can improve search times. Develop a set of classes for an expense processing application using an unordered list.

tapiae
Download Presentation

Unordered List Properties and Sequential Search Analysis

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. Unordered List Chapter 4

  2. Learning Objectives • Describe the properties of an unordered list. • Study sequential search and analyze its worst-case and average running times. • Discover how the entries of a list may be dynamically rearranged at achieve better search times. • Understand the public interface of an unordered list class in Java and the running times of its methods.

  3. Learning Objectives • Develop a set of classes for an expense processing application based on an unordered list. • Understand how object-oriented programming can be used to write a single piece of code in Java that can perform equality checking based on different criteria for different input objects. • Learn what linked lists are, why they are useful, and how to build and manipulate them.

  4. Learning Objectives • Implement a linked lest class in Java and analyze the running times of its methods. • Implement an unordered list class in Java using a linked list component.

  5. 4.1 Unordered List Properties • Keeping track of daily expenses. • It would be useful to write a program that maintains an expense list of all recorded expenses, that can be used to find quick answers to simple budgeting type questions.

  6. 4.1 Unordered List Properties

  7. 4.1 Unordered List Properties

  8. 4.1 Unordered List Properties • Answer the following questions: • What is the maximum (or minimum) expense, and on what item? • What is the average expense? • What is the total amount spent on a given item? • What is the itemwise breakup of expenditure? • What is the total amount spent in a given time period? • All these question may be answered by scanning such a list from the beginning and terminating when our question is answered.

  9. 4.1 Unordered List Properties

  10. 4.2 Sequential Search • Operation contains searches for a specific itme in the list. • Since the list is unordered, the only way to conduct the search is to look at every element in the sequence. • If a match is found, the operation returns true, otherwise it returns false.

  11. 4.2 Sequential Search

  12. 4.2 Sequential Search • Best case • 1 • Worst case • n • Unsuccessful search? • n

  13. 4.2.1 Average Case Analysis • It takes Ci comparisons to find success at the i-th element, then the average number of comparisons is simply:

  14. 4.2.1 Average Case Analysis • It takes i comparisons to succeed at the i-th element, i.e. Ci=i • Assumption: • We are assuming that any element is searched for with the same likelihood as any other. In other words, if there are n elements, we are assuming that any one element has a 1/n chance or probability to being searched.

  15. 4.2.1 Average Case Analysis • Example for which we do not make this assumption:

  16. 4.2.1 Average Case Analysis

  17. 4.2.1 Average Case Analysis • Note that if we assume equal search probabilities, Pi would be 1/n. For sequential search on an unordered list, we would then arrive at (n + 1)/2 for the average number of comparisons, which tallies with one of the intuitive guesses.

  18. 4.2.2 Rearranging Data Based on Search Patterns • We would pay the lowest cost for the average number of comparisons if we were to arrange the elements from first position to last position based on decreasing search probabilities.

  19. 4.2.2 Rearranging Data Based on Search Patterns • Assumed that the search probabilities are all known beforehand. • If a search is made for element x of the list, then, after this element is found in the list, it is simply moved to the front of the list. • The more often an entry is searched for, the more towards the front of the list it will be.

  20. 4.3 A List Class • NoSuchElementException thrown back.

  21. 4.3 A List Class

  22. 4.3 A List Class

  23. 4.3 A List Class • Enumeration • The items of a List object may be enumerated with a simple device called a cursor. • To start, a call is made to the first method. • This sets the cursor at the first item of the list, and returns that item. • Every subsequent call to the next method moves the cursor to the next item, and returns that item. • When the cursor is at the end of the list, any subsequent call to next will return null.

  24. 4.3 A List Class • Example that enumerates:

  25. 4.3 A List Class • Running times • An implementation should be able to access the last item of the list in O(1) time, so that the add method may be implemented in O(1) time. • Maintain a count of the number of items in the list. • The size method can then simply return this count. • Empty the list in O(1) time. • Use a cursor to enumerate a list, so that each of the enumeration methods first and next may be implemented in O(1) time.

  26. 4.4 An ExpenseList Class Using List • An ExpenseList class would support operations for maintaining expenses. • Use the generic List class as a component, implementing all the ExpenseList class methods by reusing code from one or more of the appropriate List class methods. • Every expense will consists of the amount of expense, the date of expense, and the item on which the expense was incurred.

  27. 4.4.1 Expense Class Interface

  28. 4.4.1 Expense Class Interface

  29. 4.4.2 Expense Class

  30. 4.4.2 Expense Class

  31. 4.4.2 Expense Class

  32. 4.4.3 ExpenseList Class Interface

  33. 4.4.3 ExpenseList Class Interface

  34. 4.4.4 ExpenseList Class Implementation

  35. 4.4.4 ExpenseList Class Implementation • maxExpense, minExpense, and aveExpense scan every expense entry in the list.

  36. 4.4.4 ExpenseList Class Implementation • Time requirement of each of these methods is O(n). • amountSpentOn and amountSpentDuring involve sequential search. • Both methods are O(n)

  37. 4.4.4 ExpenseList Class Implementation • Returns a matching Expense object from the expense list. • If they are different, how come they match?

  38. 4.4.5 Equality of Objects and Searching • Rewrite the method by implementing a search in the method.

  39. 4.4.5 Equality of Objects and Searching • The notion of equality is defined by the equals method of the exp object. • Two expenses are equal if they have the same time, amount, and date. • What if we wanted to know whether we had incurred any expense on a particular day, say 2005/2/15? • We would need to redefine the equality of expenses in terms of date only.

  40. 4.4.5 Equality of Objects and Searching • Class Specialization for Special Equality • Define special classes that extend the Expense class, with the sole aim of implementing special, and different, kinds of expenses.

  41. 4.4.5 Equality of Objects and Searching

  42. 4.4.5 Equality of Objects and Searching • Subclasses DateExpense and ItemDateExpense are defined in an analogous manner, differing in their definitions of the equals method.

  43. 4.4.5 Equality of Objects and Searching • Searching in ExpenseList

  44. 4.4.5 Equality of Objects and Searching • An ItemExpense object, itemExp, is sent as actual argument to method contains in ExpenseList. • An Expense object, nextExpense, is picked up from budget. • This nextExpense object is shipped as the actual argument to the equals method of itemExp.

  45. 4.4.5 Equality of Objects and Searching • It is crucial to ask for an instance of Expense, and not of ItemExpense. • Budget list is populated with Expense objects, not ItemExpense objects.

  46. 4.4.5 Equality of Objects and Searching • About Keys • The get method is useful to extract an entire object from the list by matching its key part with a specified key. • A DateExpense date field would participate in equality checking. • An ItemDateExpense: item and date

  47. 4.4.5 Equality of Objects and Searching

  48. 4.4.5 Equality of Objects and Searching • Only send in the key part, date and get returns the entire matching entry (including amount), if any. • What data structure should be used to store the items in a list? • Removing items from anywhere in the list. • Leaves holes in the array. • Uses more space than necessary. • Search times would be greater than O(n). • If the holes are patched up by compacting the array, we would be doing a lot of data movement within the array.

  49. 4.5 Linked List

  50. 4.5 Linked List • To access the entries of the linked list, a reference to its first entry is all we need. • One can access any entry by simply following the chain of links. • When an entry is removed from some place in a linked list, all that needs to be done is to have its predecessor's link refer to its successor. • Similarly, an entry may be inserted anywhere in the list without having to move other entries over to create space.

More Related