Question? Leave a message!
I Agree to Thesis Scientist
Terms and Conditions
Not a member?
Sign Up Now
Register a Free Account
I Agree to Thesis Scientist
Terms and Conditions
Sign up At Thesis Scientist
Done, your profile is created.Finish your profile by filling in the following fields
Earn Money,Free Notes
Password sent to your Email Id, Please Check your Mail
Updating Cart........ Please Wait........
Containers, Relations, and Abstract Data Types
Containers, Relations, and Abstract Data Types
ECE 250 Algorithms and Data Structures Containers, Relations, and Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering Abstract Data Types University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca dwharderalumni.uwaterloo.ca © 20062013 by Douglas Wilhelm Harder. Some rights reserved.Containers, Relations and Abstract Data Types 2 Outline This topic will describe – 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 relationsContainers, Relations and Abstract Data Types 3 2.1 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 computerContainers, Relations and Abstract Data Types 4 2.1.1 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 – The objects stored within a containerContainers, Relations and Abstract Data Types 5 2.1.1 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 intersectionContainers, Relations and Abstract Data Types 6 2.1.1 Operations on a Container Many of these operations on containers are in the Standard Template Library Container() Constructor Copy Constructor Container( Container const ) Destructor Container() Empty it void clear() int size() const How many objects are in it bool empty() const Is it empty How many objects can it hold int maxsize() const Merge with another container void insert( Container const )Containers, Relations and Abstract Data Types 7 2.1.1 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 containerContainers, Relations and Abstract Data Types 8 2.1.1 Operations on Objects Stored in a Container Many of these operations are also common to the Standard Template Library Insert an object void insert( Type const ) void erase( Type const ) Erase an object iterator find( Type const ) Find or access an object Count the number of copies int count( Type const ) Iterate through the objects in a container iterator begin() constContainers, Relations and Abstract Data Types 9 2.1.2 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 UW Student ID Number Temperature readings QUEST Student Circular Academic Academic Array Server RecordContainers, Relations and Abstract Data Types 10 2.1.2 Simple and Associative Containers 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 keyrecord pairsContainers, Relations and Abstract Data Types 11 2.1.3 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, codeContainers, Relations and Abstract Data Types 12 2.1.4 Standard Template Library We will begin by introducing four containers from the C++ Standard Template Library (STL) Unique Duplicate Objects/Keys Objects/Keys Simple setType multisetType Container Associative mapKeytype, Type multimapKeytype, Type ContainerContainers, Relations and Abstract Data Types 13 2.1.4 The STL set Container include iostream include set int main() std::setint ints; for ( int i = 100; i = 100; ++i ) ints.insert( ii ); // Ignores duplicates: (3)(3) == 33 // 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; Containers, Relations and Abstract Data Types 14 2.1.5 Operations In any application, the actual required operations may be only a subset of the possible operations – In the design of any solution, it is important to consider both current and future requirements – Under certain conditions, by reducing specific operations, the speed can be increased or the memory decreasedContainers, Relations and Abstract Data Types 15 2.1.5 Relationships 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 peopleContainers, Relations and Abstract Data Types 16 22.214.171.124 Relationships 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 for each person, have 24 mailboxes and place mail into the bin corresponding to the person’s last name A E I M R V B F J M S W C G K N T XY D H L PQ U Z • This works fine as long as there is not too much duplicationContainers, Relations and Abstract Data Types 17 126.96.36.199 Relationships 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 – Can I get from here to Roches’s Point in under two hoursContainers, Relations and Abstract Data Types 18 188.8.131.52 Relationships We will look at six relationships: – Linear orderings – Hierarchical orderings – Partial orderings – Equivalence relations – Weak orderings – Adjacency relationsContainers, Relations and Abstract Data Types 19 184.108.40.206 Relationships Relationships are often Booleanvalued binary operations Example: given two integers: – Are they equal x = y – Is one less than the other x y – Does the first divide the second x y xy – Do they have the same remainder modulo 16 16 – Do two integers differ by at most one prime factorContainers, Relations and Abstract Data Types 20 220.127.116.11 Classification of Relationships The relationships we look at can usually be classified by one of two properties based on symmetry: x y if and only if y x Ali is the same age as Bailey Symmetric Ali is shorter than Bailey at most one of x y or y x Antisymmetric can be true ECE 150 is a prereq of ECE 250 Ali is the same age as Ali Reflexive x x for all x Ali is not shorter than Ali Antireflexive x≮ x for all x Another common property is transitivity: – 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. – If x y and y z, it must be true that x z : If Ali is shorter than Bailey and Bailey is shorter than Casey, it follows that Ali is shorter than Casey.Containers, Relations and Abstract Data Types 21 2.1.6 Definitions and examples We will now define and consider examples of these relationships – Linear orderings – Hierarchical orderings – Partial orderings – Equivalence relations – Weak orderings – Adjacency relationsContainers, Relations and Abstract Data Types 22 18.104.22.168 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 antisymmetric – 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 listsContainers, Relations and Abstract Data Types 23 22.214.171.124.1 Lexicographical Orderings Another linearly ordered set is the set of English words: a, aardvark, aardwolf, ..., abacus, …, baa, …, bdellium, ..., zygote The order is induced by the linear ordering on a through z / / We will add the blank character b which has the property that b a The order is determined by comparing the first letters which differ: aardvark aardwolf since v w aadvark abacus since a b azygous baa since a b / cat catch since b c wilhelm william since h lContainers, Relations and Abstract Data Types 24 126.96.36.199.1 Lexicographical Orderings Such an order can also be used to linearly order vectors: – Say that (x , y ) (x , y ) if either: 1 1 2 2 x x or both x = x and y y 1 2 1 2 1 2 For example, (3, 4) (5, 1) (3, 4) (3, 8) cd ea cd chContainers, Relations and Abstract Data Types 25 188.8.131.52.2 Operations on Linear Orderings Queries that may be asked about linear orderings: – What are the first and last objects (the front and the back) th – What is the k 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 Operations that may be performed as a result: – Insert an object into a sorted list th – Insert an object at either the front, the back, or into the k position – Sort a collection of objectsContainers, Relations and Abstract Data Types 26 184.108.40.206 Hierarchical Orderings The next relation we will look at is a hierarchical ordering Consider directories in a file system: x ≺ y if x contains y within one of its subdirectories – In Unix, there is a single root director / Such structures allow us to organize informationContainers, Relations and Abstract Data Types 27 220.127.116.11.1 Hierarchical Orderings Other examples: Classes in Java and CContainers, Relations and Abstract Data Types 28 18.104.22.168.2 Hierarchical Orderings If x ≺ y, we say that x precedes y or x is a predecessor of y Even though all of these relationships may appear very different, they all exhibit the same properties: – For all x, x⊀ x (antireflexive) – If x ≺ y then y ⊀ x – If x ≺ y and y ≺ z, it follows that x ≺ z – There is a root r such that r ≺ x for all other x – If is x ≺ z and y ≺ z, it follows that either x ≺ y, x = y or x ≻ yContainers, Relations and Abstract Data Types 29 22.214.171.124.3 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 – What is the nearest common predecessorContainers, Relations and Abstract Data Types 30 126.96.36.199 Partial Orderings 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 prerequisitesContainers, Relations and Abstract Data Types 31 188.8.131.52 Partial Orderings 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 206Containers, Relations and Abstract Data Types 32 184.108.40.206 Partial Orderings There are no loops—otherwise, you could not take any courses in the loop…Containers, Relations and Abstract Data Types 33 220.127.116.11.1 Partial Orderings 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 dependenciesContainers, Relations and Abstract Data Types 34 18.104.22.168.2 Partial Orderings All partial orderings are antireflexive, antisymmetric and transitive You will note that these are the first three rules of a hierarchical ordering – What is lost – We are not guaranteed that there is a unique root – There may be multiple different paths between two objectsContainers, Relations and Abstract Data Types 35 22.214.171.124.3 Lattice A finite set L that is partially ordered is called a lattice if it has a there are unique elements ⊤ and ⊥ such that ⊥≼ x and x≼⊤ for all elements x∈ L – Here, A≺ B if A⊂ B – Graphically, A≺ B if there is a path from A to B ⊤ = ⊥ =Containers, Relations and Abstract Data Types 36 126.96.36.199.4 Operations on Partial Orderings 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 objectContainers, Relations and Abstract Data Types 37 188.8.131.52.1 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 xContainers, Relations and Abstract Data Types 38 184.108.40.206.1 Equivalence Relations 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 classesContainers, Relations and Abstract Data Types 39 220.127.116.11.2 Equivalence Relations Mathematically, we could say that two functions f(x) and g(x) are equivalent if fx lim c x gx 0 c for some value of c that is Two circuits, one from Motorola and the other from IBM, may be said to be equivalent if they perform the same operationsContainers, Relations and Abstract Data Types 40 18.104.22.168.3 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 yContainers, Relations and Abstract Data Types 41 22.214.171.124 Weak Orderings Finally, we will look at the relationship x y if x and y are the same age and x y if x is younger than y A weak ordering is a linear ordering of equivalence classes If x is the same age or younger than y, we would say x≲ yContainers, Relations and Abstract Data Types 42 126.96.36.199 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 propertiesContainers, Relations and Abstract Data Types 43 188.8.131.52.1 Weak Orderings The four containers, setT multisetT mapK, S multimapK, 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 equivalentContainers, Relations and Abstract Data Types 44 184.108.40.206.2 Operations on Weak Orderings 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 – The next or previous object may be equivalentContainers, Relations and Abstract Data Types 45 220.127.116.11 Adjacency Relations 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...Containers, Relations and Abstract Data Types 46 18.104.22.168 Adjacency Relations Such a relationship is termed an adjacency relationship – Two individuals who are related are also said to be adjacent to each other Here we see a hockey team and some of their friendsContainers, Relations and Abstract Data Types 47 22.214.171.124 Adjacency Relations Alternatively, the graph may be more complex http://xkcd.com/173/Containers, Relations and Abstract Data Types 48 126.96.36.199 Adjacency Relations In some cases, you do not have global relationships, but rather, you are simply aware of neighbouring, or adjacent, nodes Such a relationship defines a graph, where: – Nodes are termed vertices – Edges denote adjacenciesContainers, Relations and Abstract Data Types 49 188.8.131.52 Adjacency Relations Two examples: – City streets • intersections are vertices • streets are edges – Circuits • circuit elements are vertices • connections are edges http://maps.google.ca/ http://esci.unco.edu/resource/circuit.htmContainers, Relations and Abstract Data Types 50 184.108.40.206.1 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 = x , x , x , x , ..., x = b 0 1 2 3 n such that x is adjacent to x k k + 1 I.e., are the objects connectedContainers, Relations and Abstract Data Types 51 220.127.116.11 Summary of Relations We have now seen six relationships: – Linear orderings – Hierarchical orderings – Partial orderings – Equivalence relations – Weak orderings – Adjacency relations All of these are relationships that exist on the objects we may wish to store, access, and queryContainers, Relations and Abstract Data Types 52 2.1.7 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 individualsContainers, Relations and Abstract Data Types 53 2.1.7 Defining Relations Any relationship may be either implicitly defined or explicitly imposed – 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 pixelsContainers, Relations and Abstract Data Types 54 18.104.22.168 Defining Relations Relationships may be defined globally or locally – 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 otherContainers, Relations and Abstract Data Types 55 22.214.171.124 Defining Relations Relationships may be defined globally or locally – Prerequisites are defined locally: ECE 150 is a prerequisite of ECE 155 ECE 155 is a prerequisite of ECE 250 From these, we may deduce that ECE 150 is a prerequisite of ECE 250 – Relationships in a company are defined locally: • Person X reports directly to person Y • Person Z is the president (or root) of the hierarchy – Street grids and circuits are defined locally: • These two intersections are connected by a road • These two circuit elements are connected by a wireContainers, Relations and Abstract Data Types 56 126.96.36.199 Defining Relations In general, – Explicitly imposed relationships are usually defined locally – Implicitly defined relationships can usually be determined globallyContainers, Relations and Abstract Data Types 57 2.1.8 Abstract Data Types In engineering, 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 ADTsContainers, Relations and Abstract Data Types 58 2.1.8 Abstract Data Types 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 in the containerContainers, Relations and Abstract Data Types 59 2.1.8 Abstract Data Types 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 structureContainers, Relations and Abstract Data Types 60 2.1.8 Abstract Data Types 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…Containers, Relations and Abstract Data Types 61 2.1.8 What’s next We have discussed containers, relationships, and ADTs – What is it we want to store and access – What queries and operations are we interested in The next question is, how do we implement these efficiently on a computer The next step is to look at data structures – These are particular methods of storing and relating data on the computer – One data structure may be appropriate as an implementation for numerous ADTs – It may not be possible to find a data structure that allows optimal implementations for all queries and operations of a particular ADTContainers, Relations and Abstract Data Types 62 Summary In this topic, we have covered: – The Container ADT as a basic model of organizing data • Queries and operations on containers • Simple and associative containers • Unique or duplicate objects – Relationships between data • Linear ordering – Lexicographical ordering • Hierarchical ordering • Partial ordering • Equivalence relation • Weak ordering • Adjacency relation – In each case, we considered relationshipspecific queries and operations – Abstract Data Types as a model for organizing informationContainers, Relations and Abstract Data Types 63 References Wikipedia, http://en.wikipedia.org/wiki/Container(abstractdatatype) http://en.wikipedia.org/wiki/Binaryrelation These slides are provided for the ECE 250 Algorithms and Data Structures course. The material in it reflects Douglas W. Harder’s best judgment in light of the information available to him at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. Douglas W. Harder accepts no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.
Add To Cart
Types of Officemates we Meet At Work Place
Writing Abstracts for Bachelor’s and Master’s Thesis
Data communications and Networking for beginners
What is Computer Network and its Types
Lecture notes for Advanced data structures and Algorithms
Data Structures and Algorithms PPT
Data Structures and Algorithms in Python
Lecture Notes on Data Structures and Algorithms for Big Databases
Lecture notes on Data structures and Algorithms and C
LECTURE NOTES ON DATA STRUCTURES USING C
Lecture notes on Data structure using C
Problem Solving with Algorithms and Data Structures
Data Structures Trees Basics
Lecture notes on Graphs in Data Structures
Essay and its Types
Study With Thesis Scientist
How to Sell
Tips and Tricks
Get To Know Us
© Copyright @ Thesis Scientist Pvt. Ltd.
Terms & Conditions
Tips & Tricks