ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 ·...
Transcript of ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 ·...
ABSTRACT DATA TYPESECE 650 - REZA BABAEE
LEARNING OBJECTIVES
▸ Relate the data entities in a program with ADTs
▸ Discover the relation between the data entities
▸ Select appropriate operations on the data based on the selected ADT and their relations
Today’s class
REFERENCES
▸ Slides acknowledgement:
▸ Douglas Harder website
▸ https://ece.uwaterloo.ca/~dwharder/aads/Abstract_data_types/
▸ Chapter 3 of the 4th Edition of Data Structures & Algorithms in C++ by Mark Allen Weiss
▸ https://iips.icci.edu.iq/images/exam/DataStructuresAndAlgorithmAnalysisInCpp_2014.pdf
FOUNDATIONS
https://www.coulterfamilycounseling.com/building-a-strong-relationship-foundation/
DEFINITION
‣ An abstract way to define patterns of data
‣ Name and implement them in a standard way
‣ We model such patterns using Abstract Data Types (ADTs)
‣ Set of objects together with a set of operations on those objects
‣ They are mathematical abstractions
‣ Examples: Lists
DATA OBJECTS AND OPERATIONS
▸ Data attributes:
• key: a unique identifier for each data item
• value: some important information used in the algorithm
• satellite data: carried around other objects but otherwise unused
• pointers: pointers to other objects
‣ Operations:
• modify: changes the set/attributes of the objects (e.g., insert, delete)
• query: returns some information about the set/attributes of the objects
‣ Operations performed on the whole object or some of its attributes?
• Example: changing the value of an object vs searching for an object
EXAMPLE
‣ An undirected graph
‣ Data attributes:
• A set of nodes (coordinates (x, y))
• A set of edges <v,v’> where v and v’ are nodes
‣ Operations:
• Create(G)
• Insert(G, n)
• Delete(G,k)
• Shortest-Path(G, n, n’)
QUESTIONS
DATA RELATIONS
https://medium.com/techtrument/multithreading-javascript-46156179cf9a
RELATIONS BETWEEN DATA OBJECTS
‣ Relationship:
(often) Boolean-valued binary operations between objects
‣ Additional operations based on the relationships:
Which object has been stored the longest?
Are these two classes derived from the same base class?
Can I take ECE 655 if I failed ECE 650?
Do both these pixels represent pavement on this image?
Can I get from here to there in under two hours?
CLASSIFICATIONS OF DATA RELATIONS
▸ Classification is based on the following properties:Symmetric x ~ y if and only if y ~ x Ali is the same age as Bailey
Anti-symmetric at most one of x < y or y < x can be true
Ali is shorter than Bailey ECE 650 is a prereq of ECE 655
Reflexive x ~ x for all x Ali is the same age as AliAnti-reflexive x ≮ x for all x Ali is not shorter than Ali
Transitivy If x ~ y and y ~ z, it must be true that x ~ z
If Ali is the same age as Bailey and Bailey is the same age as Casey, it follows that Ali is the same age as Casey.
MAIN TYPES
‣ Linear orderings
‣ Hierarchical orderings
‣ Partial orderings
‣ Equivalence relations
‣ Weak orderings
‣ Adjacency relations
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 _____________
‣ Any collection can be sorted according to this relation
‣ We could store linearly ordered sets using arrays or linked lists
LEXICOGRAPHICAL ORDERINGS
‣ A specific linearly ordered relation over strings/vectors:
Say that (x1, y1) < (x2, y2) if either:
x1 < x2 or
both x1 = x2 and y1 < y2
‣ In other words: the order is determined by comparing the first letter/element which differs (linear orderings for each element):
(3, 4) < (5, 1) (3, 4) < (3, 8)
cd < ea cd < ch
LINEAR ORDERINGS - OPERATIONS
▸ Queries that may be asked about linear orderings:
‣ What are the first and last objects (the front and the back)?
‣ What is the kth object?
‣ What are all objects on a given interval [a, b]
‣ Given a reference to one object in the container:
‣ What are the previous and next objects?
‣ Modifications that may be performed as an operation:
‣ Insert an object into a sorted list
‣ Insert an object at either the front, the back, or into the kth position
‣ Sort a collection of objects
QUESTIONS
HIERARCHICAL ORDERINGS
‣ A binary relationship a ≤ b between two objects is said to be a hierarchical ordering if there is a single root object r and:
‣ For all a, a≤a and r≤a
‣ If both a≤b and b≤a, it follows that a=b
‣ The relationship is transitive: if a≤b and b≤c, this implies that a≤c
‣ If a≤c and b≤c, this implies that either a≤b or b≤a
HIERARCHICAL ORDERINGS - EXAMPLE
‣ Consider directories in a file system: x ≺ y if x contains y within one of its subdirectories
‣ In Unix, there is a single root directory /
HIERARCHICAL ORDERINGS - OPERATIONS
‣ Possible queries:
‣ Does one object precede the other?
‣ Are both objects at the same depth?
‣ What is the nearest common predecessor?
‣ Possible modifications:
‣ _____________
‣ _____________
PARTIAL ORDERINGS
‣ A binary relationship a ≤ b between two objects is said to be a partial ordering if there is some root object r and:
‣ For all a, a≤a
‣ If both a≤b and b≤a, it follows that a=b
‣ The relationship is transitive: if a≤b and b≤c, this implies that a≤c
PARTIAL ORDERINGS - EXAMPLE
▸ C++ classes with multiple inheritance
▸ Package dependencies for a particular package in Unix-based systems
PARTIAL ORDERINGS - OPERATIONS
‣ Some queries
‣ Given two objects, does one precede the other?
‣ Which objects have no predecessors?
‣ Which objects immediate precede an object?
‣ Which objects immediately succeed an object?
QUESTIONS
EQUIVALENCE RELATIONS
‣ A binary relationship a ~ b between two objects is said to be an equivalence relation iff ~ is reflexive, symmetric, and transitive
‣ Equivalence classes: all of the objects in each class are related
‣ Is equivalence related to equivalency in propositional logic?
EQUIVALENCE RELATIONS - EXAMPLE
▸ What is the relation defined on the following graph that created the equivalence classes?
https://en.wikipedia.org/wiki/Equivalence_class#/media/File:Equivalentie.svg
EQUIVALENCE RELATIONS - OPERATIONS
▸ 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)
WEAK ORDERINGS
▸ A weak ordering is a linear ordering of equivalence classes
▸ Example:
x ~ y if x and y are the same age
and x < y if x is younger than y
WEAK ORDERINGS - OPERATIONS
▸ The operations on weak orderings are the same as the operations on linear orderings and equivalence classes, however:
▸ There may be multiple smallest or largest objects
▸ There might multiple next or previous objects (equivalent)
QUESTIONS
ADJACENCY RELATIONS
▸ The most generic relation (no particular definition)
▸ Restrictions are added on top:
▸ Anti-reflexive and symmetric (undirected graph)
▸ Reflexive and anti-symmetric (directed graph)
▸ Friendship in social media (or real world!)
ADJACENCY RELATIONS - OPERATIONS
▸ 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?
DEFINING RELATIONS
▸ Implicit vs Explicit
• 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
‣ Global vs Local
• Any two integers may be compared without reference to other integers
• Any two sets can be compared to determine if one is a subset of the other
QUESTIONS
34
09-ADT - 3640746
CONTAINERS
https://www.puzzlemaster.ca/browse/wood/japanese/miscsun/11967-karakuri-three-cornered-deadlock-mbp
DEFINITION
▸ The most generic ADT is a container (also called a dynamic set):
• Describes structures that store and give access to a set of objects
• The relation between the objects is a simple adjacency (belonging to the container)
• Operations:
• Create/delete
• Insert/remove
• Test membership
CLASSIFICATIONS OF CONTAINERS
▸ Two general classifications of containers:
▸ Simple vs Associativity:
• Simple containers: Containers that store individual objects
• Associative containers: Containers that store keys but also store records associated with those keys
‣ With-duplicates vs No-duplicates:
• Duplicate objects are allowed (not a set)
• Objects are unique (a set) ! typical assumption
CONTAINERS IMPLEMENTATION IN C++
▸ Using STL: standard template library in C++
Unique Objects/Keys Duplicate Objects/Keys
Simple Container set<Type> multiset<Type>
Associative Container map<Key_type, Type> multimap<Key_type, Type>
STL EXAMPLE#include <iostream> #include <set>
int main() { std::set<int> ints;
for ( int i = -100; i <= 100; ++i ) { ints.insert( i*i ); // Ignores duplicates: (-3)*(-3) == 3*3 } // Inserts 101 values: 0, 1, 4, 9, ..., 10000
std::cout << "Size of 'is': " << ints.size() << std::endl; // Prints 101
ints.erase( 50 ); // Does nothing ints.erase( 9 ); // Removes 9 std::cout << "Size of 'is': " << ints.size() << std::endl; // Prints 100
return 0; }
QUESTIONS