Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in...
Transcript of Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in...
![Page 1: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/1.jpg)
Priority Queues, Heaps, Graphs, and Sets
![Page 2: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/2.jpg)
Priority QueueQueue • Enqueue an item • Dequeue: Item returned has been in the
queue the longest amount of time. Priority Queue • Enqueue a pair <item, priority> • Dequeue: Item returned has highest
priority.
![Page 3: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/3.jpg)
Priority Queue: application layer
• A priority queue is an ADT with the property that only the highest-priority element can be accessed at any time.
• Server systems use priority queue to manage jobs/requests
• priority: can be based upon users importance, or based upon deadline, …
• Some graph algorithms: Dijkstra algorithm, Spanning Tree algorithm use it too.
![Page 4: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/4.jpg)
ADT Priority Queue Operations
Transformers – MakeEmpty – Enqueue – Dequeue
Observers – IsEmpty – IsFull
change state
observe state
![Page 5: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/5.jpg)
Implementation Level • There are many ways to implement a priority queue
– An unsorted List- – dequeue: requires searching through entire list, O(N) – enqueue: constant time
– An Array-Based Sorted List – Enqueue: O(N) – dequeue: constant time O(1)
– A linked structure based Sorted List – enqueue: O(N) – dequeue: constant time O(1)
– N: The number of elements in the queue
![Page 6: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/6.jpg)
Implementation Level (cont’d)• There are many ways to implement a priority queue
– A Binary Search Tree- – enqueue? – dequeue?
– A Heap: – enqueue and dequeue: both O(log2N) steps , – even in the worst case!
![Page 7: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/7.jpg)
A full tree: a binary tree in which each node has 0 or two children.
A complete binary tree: a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
Filled? yes
filled? yes, at most 2 nodes at level 1
filled? no, maximally 4 nodes, only 3.
![Page 8: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/8.jpg)
A complete binary tree: a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
Can you draw a complete binary tree with 5 nodes?
with 10 nodes?
Note that the shape of the tree is completely decided!
![Page 9: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/9.jpg)
What is a Heap?
A heap is a binary tree that satisfies these special SHAPE and ORDER properties:
– Its shape must be a complete binary tree.
– For each node in the heap, the value stored in that node is greater than or equal to the value in each of its children.
![Page 10: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/10.jpg)
Are these Both Heaps?
C
A T
treePtr
50
20
18
30
10
![Page 11: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/11.jpg)
Is this a Heap?
70
60
40 30
12
8 10
tree
![Page 12: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/12.jpg)
Where is the Largest Element in a Heap Always Found?
70
60
40 30
12
8
tree
![Page 13: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/13.jpg)
Numbering Nodes Left to Right by Level:
70
0
60
1
40 3
30
4
12
2
8
5
tree
![Page 14: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/14.jpg)
And store tree nodes in array, using the numbering as array Indexes
70
0
60
1
40 3
30
4
12
2
8
5
tree[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
70
60
12
40
30
8
tree.nodes
![Page 15: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/15.jpg)
And store tree nodes in array, using the numbering as array Indexes
70
0
60
1
40 3
30
4
12
2
8
5
tree
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
70
60
12
40
30
8
tree.nodes
Notice: the relation between a node’s numbering with that of its parent, that of its left child and right child?
![Page 16: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/16.jpg)
Use an array to store a complete binary tree
tree elements stored in by level, from left to right:
13 3 4 10 23 31 100 32
0 1 2 3 4 5 6 7
Can you draw the complete binary tree?
Can you find the parent of node 5? (without drawing the tree?)
Where are the left child of node 2, right child?
![Page 17: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/17.jpg)
// HEAP SPECIFICATION
// Assumes ItemType is either a built-in simple data // type or a class with overloaded relational operators.
template< class ItemType > struct HeapType { void ReheapDown (int root , int bottom ) ; void ReheapUp (int root, int bottom ) ; ItemType* elements; //ARRAY to be allocated dynamically
int numElements ; };
![Page 18: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/18.jpg)
HeapDown
![Page 19: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/19.jpg)
9-12
ReheapDown// IMPLEMENTATION OF RECURSIVE HEAP MEMBER FUNCTIONS
template< class ItemType > void HeapType<ItemType>::ReheapDown ( int root, int bottom )
// Pre: root is the index of the node that may violate the // heap order property // Post: Heap order property is restored between root and bottom
{ int maxChild ; int rightChild ; int leftChild ;
leftChild = root * 2 + 1 ; rightChild = root * 2 + 2 ;
![Page 20: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/20.jpg)
ReheapDown (cont) if ( leftChild <= bottom ) // ReheapDown continued { if ( leftChild == bottom ) maxChild = leftChld; else { if (elements [ leftChild ] <= elements [ rightChild ] ) maxChild = rightChild; else maxChild = leftChild; } if ( elements [ root ] < elements [ maxChild ] ) { Swap ( elements [ root ] , elements [ maxChild ] ); ReheapDown ( maxChild, bottom ) ; } } }
![Page 21: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/21.jpg)
HeapUp
![Page 22: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/22.jpg)
// IMPLEMENTATION continued
template< class ItemType > void HeapType<ItemType>::ReheapUp ( int root, int bottom )
// Pre: bottom is the index of the node that may violate the heap // order property. The order property is satisfied from root to // next-to-last node. // Post: Heap order property is restored between root and bottom
{ int parent ;
if ( bottom > root ) { parent = ( bottom - 1 ) / 2; if ( elements [ parent ] < elements [ bottom ] ) { Swap ( elements [ parent ], elements [ bottom ] ); ReheapUp ( root, parent ); } } }
![Page 23: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/23.jpg)
Class PQType Declarationclass FullPQ(){}; class EmptyPQ(){}; template<class ItemType> class PQType { public: PQType(int); ~PQType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType newItem); void Dequeue(ItemType& item); private: int length; HeapType<ItemType> items; int maxItems; };
![Page 24: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/24.jpg)
Class PQType Function Definitionstemplate<class ItemType>PQType<ItemType>::PQType(int max){ maxItems = max; items.elements = new ItemType[max]; length = 0;}template<class ItemType>void PQType<ItemType>::MakeEmpty(){ length = 0;}template<class ItemType>PQType<ItemType>::~PQType(){ delete [] items.elements;}
![Page 25: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/25.jpg)
Class PQType Function Definitions
Dequeue Set item to root element from queue Move last leaf element into root position Decrement length items.ReheapDown(0, length-1)
Enqueue Increment length Put newItem in next available position items.ReheapUp(0, length-1)
![Page 26: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/26.jpg)
Code for Dequeuetemplate<class ItemType>void PQType<ItemType>::Dequeue(ItemType& item){ if (length == 0) throw EmptyPQ(); else { item = items.elements[0]; items.elements[0] = items.elements[length-1]; length--; items.ReheapDown(0, length-1); }}
![Page 27: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/27.jpg)
Code for Enqueue
template<class ItemType>void PQType<ItemType>::Enqueue(ItemType newItem){ if (length == maxItems) throw FullPQ(); else { length++; items.elements[length-1] = newItem; items.ReheapUp(0, length-1); }}
![Page 28: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/28.jpg)
Comparison of Priority Queue Implementations
Enqueue Dequeue
Heap O(log2N) O(log2N)
Linked List O(N) O(N)
Binary Search Tree
Balanced O(log2N) O(log2N)
Skewed O(N) O(N)
![Page 29: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/29.jpg)
Definitions• Graph: A data structure that consists of a set of
models and a set of edges that relate the nodes to each other
• Vertex: A node in a graph • Edge (arc): A pair of vertices representing a
connection between two nodes in a graph • Undirected graph: A graph in which the edges
have no direction • Directed graph (digraph): A graph in which each
edge is directed from one vertex to another (or the same) vertex
![Page 30: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/30.jpg)
Formally
• a graph G is defined as follows: G = (V,E)
where
V(G) is a finite, nonempty set of vertices
E(G) is a set of edges (written as pairs of vertices)
![Page 31: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/31.jpg)
An undirected graph
![Page 32: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/32.jpg)
A directed graph
![Page 33: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/33.jpg)
A directed graph
![Page 34: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/34.jpg)
More Definitions
• Adjacent vertices: Two vertices in a graph that are connected by an edge
• Path: A sequence of vertices that connects two nodes in a graph
• Complete graph: A graph in which every vertex is directly connected to every other vertex
• Weighted graph: A graph in which each edge carries a value
![Page 35: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/35.jpg)
Two complete graphs
![Page 36: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/36.jpg)
A weighted graph
![Page 37: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/37.jpg)
Definitions• Depth-first search algorithm: Visit all the nodes in a branch to its deepest point before moving up • Breadth-first search algorithm: Visit all the nodes on
one level before going to the next level • Single-source shortest-path algorithm: An algorithm
that displays the shortest path from a designated starting node to every other node in the graph
![Page 38: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/38.jpg)
Depth First Search: Follow Down
![Page 39: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/39.jpg)
Depth First Uses Stack
![Page 40: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/40.jpg)
Breadth First: Follow Across
![Page 41: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/41.jpg)
Breadth First Uses Queue
![Page 42: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/42.jpg)
Single Source Shortest Path
![Page 43: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/43.jpg)
Single Source Shortest Path
• What does “shortest” mean? • What data structure should you use?
![Page 44: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/44.jpg)
Array-Based Implementation
• Adjacency Matrix: for a graph with N nodes, and N by N table that shows the existence (and weights) of all edges in the graph
![Page 45: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/45.jpg)
Adjacency Matrix for Flight Connections
![Page 46: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/46.jpg)
Linked Implementation
• Adjacency List: A linked list that identifies all the vertices to which a particular vertex is connected; each vertex has its own adjacency list
![Page 47: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/47.jpg)
Adjacency List Representation of Graphs
![Page 48: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/48.jpg)
ADT Set DefinitionsBase type: The type of the items in the set Cardinality: The number of items in a set Cardinality of the base type: The number of items in the base type Union of two sets: A set made up of all the items in either sets Intersection of two sets: A set made up of all the items in both sets Difference of two sets: A set made up of all the items in the first set that are not in the second set
![Page 49: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/49.jpg)
Beware: At the Logical Level
• Sets can not contain duplicates. Storing an item that is already in the set does not change the set.
• If an item is not in a set, deleting that item
from the set does not change the set. • Sets are not ordered.
![Page 50: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/50.jpg)
Implementing SetsExplicit implementation (Bit vector) Each item in the base type has a representation in each instance of a set. The representation is either true (item is in the set) or false (item is not in the set). Space is proportional to the cardinality of the base type. Algorithms use Boolean operations.
![Page 51: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/51.jpg)
Implementing Sets (cont.)
Implicit implementation (List) The items in an instance of a set are on a list that represents the set. Those items that are
not on the list are not in the set. Space is proportional to the cardinality of the set instance. Algorithms use ADT List operations.
![Page 52: Priority Queues, Heaps, Graphs, and SetsDifference of two sets: A set made up of all the items in the first set that are not in the second set . Beware: At the Logical Level • Sets](https://reader033.fdocuments.net/reader033/viewer/2022042111/5e8cfe57dbff6d0171378147/html5/thumbnails/52.jpg)
Explain:
If sets are not ordered, why is the SortedList ADT a better choice as the implementation structure for the implicit representation?