Relation-Based Operations On Containers: ECE 250 Algorithms and Data Structures
Relation-Based Operations On Containers: ECE 250 Algorithms and Data Structures
Relation-based Operations
on Containers
Douglas Wilhelm Harder
Department of Electrical and Computer Engineering
University of Waterloo
Waterloo, Ontario, Canada
Outline
• This topic will describe additional operations due to
relationships
– The costs
– We will focus on linear orderings:
• Implicitly defined linear orderings (sorted lists)
• Explicitly defined linear orderings
– We will summarize this information
– We will also look briefly at:
• Hierarchical orderings
• Partial orderings
• Equivalence relations
• Adjacency relations
2
Relation-based Operations on Containers
Background
• Previously, we have seen that there are operations on
containers:
– Create a container
– Destroy a container
– Make a copy of a container
– Split a container into two or more containers
– Take the union of two containers (merge)
– Determine the intersection of two containers
3
Relation-based Operations on Containers
Background
• There are operations which can be performed on objects
stored in the container:
– Retrieve the number of objects in the container
• Determine if the container is empty
– Insert a new object
– Determine membership of an object
– Iterate through the objects within the container
– Modify an object in the container
– Remove an object from the container
• Remove all objects from the container
4
Relation-based Operations on Containers
Additional Operations
• Since then, we have introduced a number of relations
• Given a container with objects which are related, we
have additional queries or operations
– For many cases, we will discuss data structures which attempt to
optimize as many operations as possible
– The hash table (Weeks 7-8) is optimal for most operations for
unrelated data
– Storing relationships as well as objects will slow certain
operations down
5
Relation-based Operations on Containers
Additional Operations
• We will go through the five relations and determine some
of the additional operations:
– Linear ordering
– Hierarchical ordering
– Partial ordering
– Equivalence relation
– Adjacency relation
6
Relation-based Operations on Containers
Linear Ordering
• Given a linear order, additional operations include:
– Determine/remove the first (front), kth, and last (back) objects
– Given an object, determine the previous or next object
• Implicit linear orders specify the order
– The order determines what is 1st, 2nd, etc.
– There is only one location 17 can be inserted into the sorted list
2 5 12 16 24 27 30 42
• Explicit linear orders allow the programmer to place
objects:
– Given an object in the container, insert a new object either
immediately before or after it
– Explicitly insert “quick ” in front of ‘b’ in “The brown fox...”
7
Relation-based Operations on Containers
Operations
• We will us the following matrix to describe operations at
the locations within the structure
8
Relation-based Operations on Containers
9
Relation-based Operations on Containers
Operations on Lists
• If the array is not sorted, only one operations changes:
10
Relation-based Operations on Containers
Operations on Lists
• However, for a singly-linked list where we a head and tail
pointer, we have:
11
Relation-based Operations on Containers
Operations on Lists
• For a doubly-linked lists, one of the operations become
more efficient:
12
Relation-based Operations on Containers
Operations on Lists
• We may also wish to access the nth object in ordered
data:
– Very fast in an array
– Slow for a linked list unless n is small (e.g., if n < 10)
• Inserting a new entry immediately after a referenced
object:
– Very fast in a linked list
– Slow for arrays unless the referenced element is near the end of
the array
– Nonsensical with a sorted list (in general)
13
Relation-based Operations on Containers
Operations on Lists
• In other cases, we may have access to one particular
(but arbitrary) object and we may wish to
– Insert an element before the current object
– Access the previous or next element
– Delete the current element
• If the data is not sorted, then finding the largest object is
always going to be slow
14
Relation-based Operations on Containers
Operations on Lists
• Unfortunately, all the descriptions we’ve seen here are
qualitative:
– good, bad, fast, slow
• The next topic will discuss the mathematical tools so that
we can state these differences quantitatively
15
Relation-based Operations on Containers
16
Relation-based Operations on Containers
17
Relation-based Operations on Containers
18
Relation-based Operations on Containers
19
Relation-based Operations on Containers
20
Relation-based Operations on Containers
Following Classes
• Thirteen lectures focus on linear orderings
– We will focus on specific operations and attempt to optimize only
those operations
• A few lectures focus on trees used with both hierarchical
and linear orders
• One week focuses on partial orders and adjacency
relations
– Graphs
– Directed acyclic graphs
• We will use equivalence relations in the next section
– Landau symbols
21
Relation-based Operations on Containers
Summary
• In this topic, we introduced the idea of relation-based
operations
• We looked at operations on arrays and linked lists and
heuristically determined:
– Some operations are “fast” while others may be “slow”
– Some operations may only be slow under certain circumstances
• We have discussed the various operations related to
specific relations
22
Relation-based Operations on Containers
Usage Notes
• These slides are made publicly available on the web for anyone to use
• If you choose to use them, or a part thereof, for a course at another
institution, I ask only three things:
– that you inform me that you are using the slides,
– that you acknowledge my work, and
– that you alert me of any mistakes which I made or changes which you make, and
allow me the option of incorporating such changes (with an acknowledgment) in
my set of slides
Sincerely,
Douglas Wilhelm Harder, MMath
[email protected]
23