Download CSE 373: Data Structures & Algorithms Pseudocode; ADTs and more Slides Data Structures and Algorithms in PDF only on Docsity!
CSE 373: Data Structures & Algorithms
Pseudocode; ADTs; Priority Queues;
Heaps
Riley Porter
Winter 2017
Winter 2017 CSE373: Data Structures & Algorithms 1
Course LogisGcs
Winter 2017 CSE373: Data Structures and Algorithms 2
• HW 1 released Monday. Due a week from Tuesday.
• Java Review Session early next week, room and Gme
TBA and posted on the course website
• Slides posted and updated from last Gme with links
correctly working in PDF version
More Pseudocode
Winter 2017 CSE373: Data Structures and Algorithms 4
Algorithm: Given a list of names in the format "firstName
lastName", make a Map of all first names as keys with sets of last
names as their values
Pseudocode:
create the empty result map while list has more names to process { firstName is name split up until space lastName is name split from space to the end if firstName not in the map yet { put firstName in map as a key with an empty set as the value } add lastName to the set for the first name move to the next name in the list }
Pseudocode PracGce
Winter 2017 CSE373: Data Structures and Algorithms 5
Come up with pseudocode for the following algorithm:
Algorithm: Given a list of integers, find the index of the
maximum integer in the list.
Terminology Review
• Abstract Data Type (ADT)
- MathemaGcal descripGon of a "thing" with set of
operaGons
• Algorithm
- A high level, language-‐independent descripGon of a step-‐
by-‐step process
• Data structure
- A specific organizaGon of data and family of algorithms for
implemenGng an ADT
• ImplementaGon of a data structure
- A specific implementaGon in a specific language Winter 2017 CSE373: Data Structures and Algorithms 7
Another ADT: Priority Queue
A priority queue holds comparable data
– Given x and y , is x less than, equal to, or greater
than y
– Meaning of the ordering can depend on your data
– Many data structures require this: dicGonaries,
sorGng
– Typically elements are comparable types, or have
two fields: the priority and the data
Winter 2017 8 CSE373: Data Structures & Algorithms
PrioriGes
- Each item has a "priority"
- The lesser item is the one with the greater priority
- So "priority 1" is more important than "priority 4"
- Can resolve Ges arbitrarily
- OperaGons:
- insert
- deleteMin
- is_empty
- deleteMin returns and deletes the item with greatest
priority (lowest priority value)
- insert is like enqueue , deleteMin is like dequeue
- But the whole point is to use prioriGes instead of FIFO Winter 2017 10 CSE373: Data Structures & Algorithms insert deleteMin
Priority Queue Example
Given the following, what values are a , b , c and d?
insert element1 with priority 5 insert element2 with priority 3 insert element3 with priority 4 a = deleteMin // a =? b = deleteMin // b =? insert element4 with priority 2 insert element5 with priority 6 c = deleteMin // c =? d = deleteMin // d =? Winter 2017 11 CSE373: Data Structures & Algorithms
Some ApplicaGons
• Run mulGple programs in the operaGng system
- "criGcal" before "interacGve" before "compute-‐ intensive", or let users set priority level
• Select print jobs in order of decreasing length
• "Greedy" algorithms (we’ll revisit this idea)
• Forward network packets in order of urgency
• Select most frequent symbols for data
compression (Huffman CSE 143)
• SorGng (first insert all, then repeatedly
deleteMin )
Winter 2017 13 CSE373: Data Structures & Algorithms
Possible ImplementaGons
• Unsorted Array
– insert by inserGng at the end
– deleteMin by linear search
• Sorted Circular Array
– insert by binary search, shij elements over
– deleteMin by moving “front”
Winter 2017 14 CSE373: Data Structures & Algorithms
One ImplementaGon: Heap
Heaps are implemented with Trees
A binary min-‐heap (or just binary heap or
heap ) is a data structure with the properGes:
• Structure property: A complet e binary tree
• Heap property: The priority of every (non-‐
root) node is greater than the priority of its
parent
– Not a binary search tree
Winter 2017 16 CSE373: Data Structures & Algorithms
Tree Review
• root of tree:
• leaves of tree:
• children of B:
• parent of C:
• subtree C:
• height of tree:
• height of E:
• depth of E:
• degree of B:
Winter 2017 17 CSE373: Data Structures & Algorithms
D F G
B^ C
A
H
E
• perfect tree:
• complete tree:
Structure Property: Completeness
• A Binary Heap is a complete binary tree:
– A binary tree with all levels full, with a possible
excepGon being the bomom level, which is filled
lej to right
Examples:
Winter 2017 19 CSE373: Data Structures & Algorithms
are these trees complete****?
Structure Property: Completeness
• A Binary Heap is a complete binary tree:
– A binary tree with all levels full, with a possible
excepGon being the bomom level, which is filled
lej to right
Examples:
Winter 2017 20 CSE373: Data Structures & Algorithms
incomplete 40
complete