0% found this document useful (0 votes)
49 views

Relation-Based Operations On Containers: ECE 250 Algorithms and Data Structures

Uploaded by

saiknaram
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Relation-Based Operations On Containers: ECE 250 Algorithms and Data Structures

Uploaded by

saiknaram
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

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

Copyright © 2006-9 by Douglas Wilhelm Harder. All rights reserved.


Relation-based Operations on Containers

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

front arbitrary back


insert
searching
delete

8
Relation-based Operations on Containers

Operations on Sorted Lists


• Given an sorted array, we have the following run times:

front arbitrary back


insert bad bad good* bad

searching good okay good


delete bad bad good

* only if the array is not full

9
Relation-based Operations on Containers

Operations on Lists
• If the array is not sorted, only one operations changes:

front arbitrary back


insert bad bad good* bad

searching good bad good


delete bad bad good

* only if the array is not full

10
Relation-based Operations on Containers

Operations on Lists
• However, for a singly-linked list where we a head and tail
pointer, we have:

front arbitrary back


insert good bad good
searching good bad good
delete good bad bad

11
Relation-based Operations on Containers

Operations on Lists
• For a doubly-linked lists, one of the operations become
more efficient:

front arbitrary back


insert good bad good
searching good bad good
delete good bad good

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

Operations on Hierarchical Orders


• Given objects stored according to a hierarchical
ordering, questions which can be asked include:
– What is the root?
– Given an object
• Which object immediately precedes it?
• Which objects immediately succeed it?
• Can we iterate through all of the successors?
• Can we detach an object and its successors?

16
Relation-based Operations on Containers

Operations on Hierarchical Orders


• If the hierarchical order is explicitly defined (the usual
case), given two objects in the container, we may ask:
– Does one object precede the other?
– Are both objects at the same depth?

17
Relation-based Operations on Containers

Operations on Partial Orders


• Partial orders are similar to hierarchical orders;
consequently, some operations are similar:
– Given two objects, does one precede the other?
– Which objects have no predecessors?
• Not unique (unlike a hierarchy)
– Which objects immediate precede an object?
• A hierarchical order has only one immediate predecessor
– Which objects immediately succeed an object?

18
Relation-based Operations on Containers

Operations on Equivalence Relations


• Given an equivalence relation:
– Are two objects related?
– Iterate through all objects related to one particular object
– Count the number of objects related to one particular object
– Given two objects x and y which are not currently related, make
them related (union)
• Not so easy: everything related to x must now be related to
everything related to y

19
Relation-based Operations on Containers

Operations on Adjacency Relations


• Given an adjacency relation:
– Are two objects adjacent?
– Iterate through all objects adjacent to one object
– Given two objects a and b, is there a sequence of objects
a = x0, x1, x2, x3, ..., xn = b
such that xk is adjacent to xk + 1?
I.e., are the objects connected?

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

You might also like