0% found this document useful (0 votes)
11 views43 pages

Lecture 02

Uploaded by

uali93823
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views43 pages

Lecture 02

Uploaded by

uali93823
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

‫ُك‬ ‫َل‬

‫الَّس آلُم َع ْي ْم َوَر ْح َمُة الله‬


‫ح‬
‫َّر ٰمِن‬‫ال‬ ‫ِہ‬ ‫الل‬ ‫س‬
‫ِب ِم‬ ‫ه‬‫ُت‬ ‫كآ‬ ‫َر‬‫َب‬ ‫و‬
‫الَّر ِح يم‬
‫ُش روع َاللہ کے پاک نام سے جو بڑا مہر بان نہايت رحم‬
‫واال ہے‬
Data Structure & Algorithms
COSC-1102

Aqeel –Ur- Rehman


[email protected]
Week # 02

Containers, Relations, and


Abstract Data Types
Lecture Recap
In our last session we cover following:
Basic course rules.
Introduction of C++
Some topics:
Global variables and functions
The preprocessor, compilation, namespaces
Printing
Concluding with
Classes, templates
Pointers
Memory allocation and deallocation
Lecture Agenda
Today we will cover following:
• The storage of objects in containers
• 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
• The concrete data structures that can be used to store information
• The basic forms of memory allocation
• Contiguous
• Linked
• Indexed
• The prototypical examples of these: arrays and linked lists
• Other data structures:
• Trees
• Hybrids
• Higher-dimensional arrays
• Finally, we will discuss the run-time of queries and operations on arrays and linked lists
Outline
Any form of information processing or communication requires
that data must be stored in and accessed from either main or
secondary memory

There are two questions we should ask:


• What do we want to do?
• How can we do it?

This topic will cover Abstract Data Types:


• Models of the storage and access of information
The next topic will cover data structures and algorithms:
• The concrete methods for organizing and accessing data in the computer
Containers
The most general Abstract Data Type (ADT) is that
of a container
• The Container ADT

A container describes structures that store and


give access to objects

The queries and operations of interest may be


defined on:
• The container as an entity, or
Operations on a Container
The operations we may wish to perform on a
container are:
• Create a new container
• Copy or destroy an existing container
• Empty a container
• Query how many objects are in a container
• Query what is the maximum number of objects a
container can hold
• Given two containers:
• Find the union (merge), or
• Find the intersection
Operations on Objects Stored in a
Container
Given a container, we may wish to:
• Insert an object into a container
• Access or modify an object in a container
• Remove an object from the container
• Query if an object is in the container
• If applicable, count how many copies of an object are in a container
• Iterate (step through) the objects in a container

Insert an object void insert( Type const & )


Erase an object void erase( Type const & )
Find or access an object iterator find( Type const & )
Count the number of copies int count( Type const & )
Iterate through the objects in a iterator begin() const
container
Simple and Associative Containers
We may split containers into two general classifications:
Simple Containers Associative Containers
Containers that store individual objects Containers that store keys but also store
records associated with those keys
Temperature readings
UW Student ID Number

Circular Student
QUEST Academic
Array
Academic Record
Server

Any of the Abstract Data Types we will discuss can be implemented as either a simple container
or an associative container
We will focus on simple containers in this course
• Any container we discuss can be modified to store key-record pairs
Unique or Duplicate Objects
Another design requirement may be to either:
• Require that all objects in the container are unique, or
• Allow duplicate objects

Many of the containers we will look at will assume uniqueness


unless otherwise stated
• Dealing with duplicate objects is often just additional, and sometimes
subtle, code
Relationships 1/2
However, we may want to store not only objects, but relationships between the
objects
• Consequently, we may have additional operations based on the relationships
• Consider a genealogical database
• We don’t only want to store the people, but we want to also make queries about the relationships between
the people
If we are not storing relationships, there is a data structure that is always the same
speed no matter how many objects are stored
• A hash table takes the same time to find an object whether there
are 10 or one billion objects in the container
• It requires approximately 30 % more memory than is occupied by the objects being stored
• Example:
• Assume a department has 12 staff that are frequently changing
• Rather than having a mailbox
A for Eeach person,
I have 24
M mailboxes
R and place
V mail into the bin corresponding
to the person’s last name
B F J M S W
C G K N T XY
D H L PQ U Z
Relationships 2/2
Most interesting problems, however, have questions associated with
relationships:
• Which object has been stored the longest?
• Are these two classes derived from the same base class?
• Can I take ECE 427 if I failed ECE 250?
• Do both these pixels represent pavement on this image?
We willI get
• Can look at six
from hererelationships:
to Roches’s Point in under two hours?
1. Linear orderings

2. Hierarchical orderings

3. Partial orderings

4. Equivalence relations

5. Weak orderings
1. Linear Orderings
A linear ordering is any relationship where any two objects x
and y that can be compared, exactly one of:
x < y , x = y, or y < x
is true and where the relation is transitive
• Such a relation is therefore anti-symmetric
• Any collection can therefore be sorted according to this relation
Examples of sets which are linearly ordered include:
• Integers 1, 2, 3, 4, 5, 6, ...
• Real numbers 1.2, 1.2001, 1.24, 1.35, 2.78, ...
• The alphabet a, b, c, d, e, f, ..., x, y, z
• Memory 0x00000000, 0x00000001, ..., 0xFFFFFFFF

We could store linearly ordered sets using arrays or linked


lists
2. Hierarchical Orderings
Other examples:

Classes in Java and C#


3. Partial Orderings 1/2
The next relationship we will look at is
x ≺ y if x is a prerequisite of y
This is not a hierarchy, as there
are multiple starting points
and one class may have
multiple prerequisites.

Arrows are necessary to indicate the direction:


• Having completed ECE 140, you can now take ECE 124 and ECE 361
• If you want to take ECE 375 Electromagnetic fields and waves, you must take ECE 206
3. Partial Orderings 2/2
Examples:
• C++ classes (multiple inheritance–
a class can inherit
from more than one class), and
• A number of tasks which must be
completed
where particular tasks must be
completed
before other tasks may be
performed
• Compilation dependencies
4. Equivalence Relations
• Consider the relationship
x ~ y if x and y are of the same gender
• Here we have another set of properties:
• This relationship is symmetric and transitive, but it is also reflexive:
x ~ x for all x
• One nice property of equivalence relations is that you can create
equivalence classes of objects where each object in the class are
related
• If you consider genetics, there are four general equivalence classes
5. Weak Orderings
• One nice property of equivalence relations is that you can create groups of
objects where each object in the group has the same properties.
The four containers,
set<T> multiset<T> map<K, S> multimap<K, S>
expect that the objects/keys may be compared using the relational
operators and that relational operator must satisfy a weak ordering
The set/map will store only one object/key per equivalence class
The multiset/multimap will store multiple objects in each equivalence class
• The containers do not guarantee the same internal ordering for objects that are
equivalent
6. Adjacency Relations 1/2
• The last relationship we will look at is
x ↔ y if x and y are friends
• Like a tree, we will display such a relationship by displaying a line
connecting two individuals if they are friends (a graph)
• E.g., Jane and Ryan are friends, Elizabeth and Jane are friends,
but Elizabeth thinks Ryan is a little odd...
6. Adjacency Relations 2/2
• Alternatively, the graph may be more complex.
• In some cases, you do not have global relationships, but rather, you are simply
aware of neighboring, or adjacent, nodes
• Such a relationship defines a graph, where:
• Nodes are termed vertices
• Edges denote adjacencies
• Two examples:
• City streets
• intersections are vertices
• streets are edges
• Circuits
• circuit elements are vertices
• connections are edges
Defining Relations
• Any relationship may be either implicitly defined or explicitly imposed
• Integers are implicitly ordered based on their relative values
• Age groups are defined by the properties of the individuals
• A hierarchy in a company is explicitly defined
• The order of the letters on this slide are explicitly imposed by the author
• Pixels are defined as pavement based on explicitly imposed rules based on
colour and surrounding pixels
Abstract Data Types 1/2
• In computer science and information technology, we tend to see certain
patterns that occur over and over in applications.
• In these circumstances, we first name these patterns and then proceed to
define certain standard solutions or implementations
• In software in storing objects and relationships in containers, there are
reoccurring containers of objects and associated relationships where the
actual queries and operations are restricted
• We model such containers by Abstract Data Types or ADTs
• Any time you are intending to store objects, you must ask:
• What are the relationships on the objects?
• What queries will be made about the objects in the container?
• What operations will be performed on the objects in the container?
• What operations may be performed on the container as a whole?
• What queries will be made about the relationships between the objects in the
container?
• What operations may be made on the relationships themselves between the objects
Abstract Data Types 2/2
• Throughout this course, we will describe various ADTs and then
look at various data structures that will allow us to efficiently
implement the required queries and operations defined by the ADT
• We have already discussed one ADT and a corresponding
implementation:
• The Container ADT and the hash table data structure
• Another ADT is the Sorted List ADT
• A container that stores linearly ordered objects where we may want insert,
access, or erase objects at arbitrary locations
• You may immediately think that we could using either an array or a
linked list to implement the Sorted List ADT; however, we will see
that that is usually very inefficient
• They are so inefficient that if, by the end of the class, if the first thing you
think of is using an array or linked list to store sorted objects, I’ve done
something wrong…
Memory Allocation
• Memory allocation can be classified as either
• Contiguous
• Linked
• Indexed
• Prototypical examples:
• Contiguous allocation: arrays
• Linked allocation: linked lists
• Contiguous, adj.
• Touching or connected throughout in an unbroken sequence.
• Touching, in actual contact, next in space; meeting at a common
boundary, bordering, adjoining.
Contiguous Allocation
• An array stores ‘n’ objects in a single contiguous space of memory
• Unfortunately, if more memory is required, a request for new
memory usually requires copying all information into the new
memory
• In general, you cannot request for the operating system to allocate to you
the next ‘n’ memory locations
Linked Allocation
• Linked storage such as a linked list associates two pieces
of data with each item being stored:
• The object itself, and
• A reference to the next item
• In C++ that reference is the address of the next node
Linked Allocation
• This is a class describing such a node
1/2
template <typename Type>
class Node {
private:
Type node_value;
Node *next_node;
public:
// ...
}; Pointing to nothing has been represented as:
C NULL
• The operations on this node must include: Java/C# null
• Constructing a new node C++ (old) 0
• Accessing (retrieving) the value C++ (new) nullptr
Symbolically Ø
• Accessing the next pointer

Node( const Type& = Type(), Node* = nullptr );


Type value() const;
Linked Allocation 2/2
• For a linked list, however, we also require an object which links to the first object. The actual linked
list class must store two pointers
• A head and tail: Optionally, we can also keep a count
Node *head;
Node *tail;
int count;
The next_node of the last node is assigned nullptr
• The class structure would be:
template <typename Type>
class List {
private:
Node<Type> *head;
Node<Type> *tail;
int count;
public:
// constructor(s)...
// accessor(s)...
// mutator(s)...
};
Indexed Allocation 1/2
• With indexed allocation, an array of pointers
• (possibly NULL) link to a sequence of allocated memory locations
• Used in the C++ standard template library
• As a CS & IT students you have already observed indexed
allocation in your operating systems course
• Matrices can be implemented using indexed allocation:

 1 2 3
 
 4 5 6 
Indexed Allocation 2/2
Matrices can be implemented using indexed allocation
• Most implementations of matrices (or higher-dimensional arrays) use indices
pointing into a single contiguous block of memory

Row-major order Column-major order


 1 2 3
 
 4 5 6 

C, Python Matlab, Fortran


Other Allocation Formats
• We will look at some variations or hybrids of these
memory allocations including:
1. Trees
2. Graphs
3. Deques (linked arrays)
4. inode / Hybrid
1. Trees
• The linked list can be used to store linearly ordered data
• What if we have multiple next pointers?
• A rooted tree (weeks 4-6) is similar to a linked list
but with multiple next pointers

• A tree is a variation on a linked list:


• Each node points to an arbitrary number of subsequent nodes
• Useful for storing hierarchical data
• We will see that it is also useful for storing sorted data
• Usually we will restrict ourselves to trees where each node
points to at most two other nodes
2. Graphs
• Suppose we allow arbitrary relations between any
two objects in a container
• Given ‘n’ objects, there are n2 – n possible relations
2
• If we allow symmetry, this reduces to n
n
2
• For example: consider the network
A B C D E F G H I J K L
A × × ×
B × × × × ×

Array C × × × × × ×
D × × ×
• Suppose we allow arbitrary relations between
E × ×
F × ×
any two objects in a container G × × ×
• We could represent this using a two-dimensional
H array
× × ×
• In this case, the matrix is symmetric I × ×
J × × ×
K × × ×
L × × ×
3. Array of Linked List
• Suppose we allow arbitrary relations between any two objects in
a container
• Alternatively, we could use a hybrid: an array of linked lists
• Other hybrids are linked lists of arrays
• Something like this is used for the C++ STL deque container
A
B
C
• For example, the alphabet could be stored either
D as:
• An array of 26 entries, or E
F
• A linked list of arrays of 8 entries G
H
I
J
K
L
4. Hybrid data structures
• Unix inode was used to store information about large files
• The first twelve entries can reference the first twelve blocks (48 KiB)
• The next entry is a pointer to an array that stores the next 1024 blocks
• The next entry has two levels of indirection for files up to 4 GiB
• The last entry has three levels of indirection for files up to 4 TiB

This stores files up to 4 MiB on a 32-bit computer


Algorithm run times
Once we have chosen a data structure to store both the objects
and the relationships, we must implement the queries or
operations as algorithms
• The Abstract Data Type will be implemented as a class
• The data structure will be defined by the member variables
• The member functions will implement the algorithms

The question is, how do we determine the efficiency of the


algorithms?
Operations
• We will us the following matrix to describe operations at the
locations within the structure
• Given an sorted array, we have the following run times:

Arbitrary
Front/1st Location Back/nth

Find Good
? Okay
? Good
?
Insert Bad
? Bad
? Good* ? Bad
Erase
Erase Bad
? Bad
? Good
?
Operations on Lists
If the array is not sorted, only one operations changes:
Arbitrary
Front/1st Location Back/nth
Find Good Bad Good
Insert Bad Bad Good* Bad
Erase Bad Bad Good

*
only if the array is not full
Operations on Lists
However, for a singly linked list where we a head and tail pointer, we
have:
Arbitrary
Front/1st Location Back/nth
Find Good Bad Good
Insert Good Bad Good
Erase Good Bad Bad
Operations on Lists
If we have a pointer to the kth entry, we can insert or erase at that location quite easily

Arbitrary
Front/1st Location Back/nth
Find Good Bad Good
Insert Good Good Good
Erase Good Good Bad

• Note, this requires a little bit of trickery: we must modify the value stored in the kth node
• This is a common co-op interview question!
Operations on Lists
For a doubly linked list, one operation becomes more efficient:
Arbitrary
Front/1st Location Back/nth
Find Good Bad Good
Insert Good Good Good
Erase Good Good Good
Lecture Recap
Today we covered following:
• The storage of objects in containers
• 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
• The concrete data structures that can be used to store information
• The basic forms of memory allocation
• Contiguous
• Linked
• Indexed
• The prototypical examples of these: arrays and linked lists
• Other data structures:
• Trees
• Hybrids
• Higher-dimensional arrays
• Finally, we will discuss the run-time of queries and operations on arrays and linked lists
End of Lecture
02

You might also like