ADTs for Algorithm Design ADTs for Algorithm Design Efficiently

advertisement
ADTs for Algorithm Design
ADTs for Algorithm Design
Common categories of ADTs –
The ordering of elements imposed by different types of
containers can be exploited to achieve algorithmic goals.
• containers provide storage and retrieval of elements
independent of value
– ordering of elements depends on the structure of the container
rather than the elements themselves
• dictionaries provide access to elements by value
ADT
Stack
some applications of the ADT
match most recent thing, proper nesting, reversing
DFS – go deep before backing up
has ties to recursive procedures – supports iterative
implementation of recursive ideas
Queue
FIFO order minimizes waiting time
BFS – spread out in levels
– lookup according to an element's key
• priority queues provide access to elements in order by
content
– ordered by priority associated with elements
CPSC 327: Data Structures and Algorithms • Spring 2016
10
CPSC 327: Data Structures and Algorithms • Spring 2016
Efficiently Implementing ADTs
Efficiently Implementing ADTs
Design a dictionary data structure with O(1) search, insert,
delete. Elements come from the set 1..n. O(n) initialization
is allowed.
“Can we do better?”
There are often tradeoffs –
• insert/remove at one end of arrays and linked lists is more
efficient than the other
Questions.
• duplicates allowed?
→ for Queue, get O(1) insert and O(n) remove or vice versa
• ordering speeds up search/retrieval but has a greater cost
to maintain, slowing insert/remove
Key points.
• be aware of the basic building blocks
→ for Dictionary, tend to end up with O(1) vs O(n)
– O(1) – stored info, access array by index, successor in linked list
– O(n) – traversal of array, linked list
–…
CPSC 327: Data Structures and Algorithms • Spring 2016
11
12
Design strategy –
– target where there is a tradeoff
– distill to the essence
– address just that essence
CPSC 327: Data Structures and Algorithms • Spring 2016
13
Queue
Queue
Consider the array implementation with the head of the
queue at the beginning of the array.
Identify the essence of the problem.
– O(1) dequeue requires O(1) locating of the head of the queue
and O(1) finding of the successor element
– enqueue(x) is O(1) – insert at the end of the array
– dequeue() is O(n) – removing from head of array requires
shifting
Implement the essence.
– the head index is easy to locate if it is stored
– the successor index is easy to locate if the elements in the
queue are contiguous
(neither of which requires that the first element be in slot 0)
Do we have to shift?
→ circular array
– require only that the elements occupy a contiguous section of
the array
– store head and tail indexes
CPSC 327: Data Structures and Algorithms • Spring 2016
14
CPSC 327: Data Structures and Algorithms • Spring 2016
Dictionary
Dictionary
• unsorted leads to O(1) insert/delete but O(n) search for
both arrays and linked lists
• sorted leads to differences between arrays and linked lists
Identify the essence of the problem.
– O(log n) search and O(n) delete for arrays
– O(n) search and O(1) delete for linked lists
15
– binary search requires efficient random access
Or does it?
– the first iteration of binary search requires efficient access to the
median element
– successive iterations require efficient access to the median of
one of the halves
Can we exploit the sorted order for searching in linked lists?
This is achieved in arrays by arithmetic involving array
indexes, but having the information you need already stored
is also efficient.
– each median only needs to store two other medians → binary
tree structure
CPSC 327: Data Structures and Algorithms • Spring 2016
16
CPSC 327: Data Structures and Algorithms • Spring 2016
17
Download