40
ABSTRACT DATA TYPES ECE 650 - REZA BABAEE

ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

  • Upload
    others

  • View
    24

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

ABSTRACT DATA TYPESECE 650 - REZA BABAEE

Page 2: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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

Page 3: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

REFERENCES

▸ Slides acknowledgement:

▸ Douglas Harder website

▸ https://ece.uwaterloo.ca/~dwharder/aads/Abstract_data_types/

[email protected]

▸ 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

Page 4: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

FOUNDATIONS

https://www.coulterfamilycounseling.com/building-a-strong-relationship-foundation/

Page 5: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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

Page 6: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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

Page 7: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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’)

Page 8: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

QUESTIONS

Page 9: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

DATA RELATIONS

https://medium.com/techtrument/multithreading-javascript-46156179cf9a

Page 10: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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?

Page 11: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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.

Page 12: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

MAIN TYPES

‣ Linear orderings

‣ Hierarchical orderings

‣ Partial orderings

‣ Equivalence relations

‣ Weak orderings

‣ Adjacency relations

Page 13: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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

Page 14: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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

Page 15: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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

Page 16: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

QUESTIONS

Page 17: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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

Page 18: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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 /

Page 19: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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:

‣ _____________

‣ _____________

Page 20: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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

Page 21: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

PARTIAL ORDERINGS - EXAMPLE

▸ C++ classes with multiple inheritance

▸ Package dependencies for a particular package in Unix-based systems

Page 22: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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?

Page 23: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

QUESTIONS

Page 24: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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?

Page 25: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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

Page 26: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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)

Page 27: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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

Page 28: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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)

Page 29: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

QUESTIONS

Page 30: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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!)

Page 31: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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?

Page 32: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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

Page 33: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

QUESTIONS

Page 34: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

34

09-ADT - 3640746

Page 35: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

CONTAINERS

https://www.puzzlemaster.ca/browse/wood/japanese/miscsun/11967-karakuri-three-cornered-deadlock-mbp

Page 36: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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

Page 37: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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

Page 38: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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>

Page 39: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

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; }

Page 40: ECE 650 - REZA BABAEE ABSTRACT DATA TYPESrbabaeec/ece650/w20/assets/pdf/... · 2020-03-26 · DEFINITION ‣ An abstract way to define patterns of data ‣ Name and implement them

QUESTIONS