Data Structures and Algorithms Pseudocode Practice

cse 373 data structures algorithms n.w
1 / 35
Embed
Share

Explore examples of pseudocode for various algorithms, including counting elements in a list greater than a value, mapping names, finding the index of the maximum integer, and more.

  • Algorithms
  • Pseudocode
  • Data Structures
  • Winter 2017
  • Java

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CSE 373: Data Structures & Algorithms Pseudocode; ADTs; Priority Queues; Heaps Riley Porter Winter 2017 Winter 2017 CSE373: Data Structures & Algorithms 1

  2. Course Logistics HW 1 released Monday. Due a week from Tuesday. Java Review Session early next week, room and time TBA and posted on the course website Slides posted and updated from last time with links correctly working in PDF version Winter 2017 CSE373: Data Structures and Algorithms 2

  3. Pseudocode Describe an algorithm in the steps necessary, write the shape of the code but ignore specific syntax. Algorithm: Count all elements in a list greater than x Pseudocode: int counter // keeps track of number > x while list has more elements { increment counter if current element is > than x move to next element of list } Winter 2017 CSE373: Data Structures and Algorithms 3

  4. More Pseudocode 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 } Winter 2017 CSE373: Data Structures and Algorithms 4

  5. Pseudocode Practice Come up with pseudocode for the following algorithm: Algorithm: Given a list of integers, find the index of the maximum integer in the list. Winter 2017 CSE373: Data Structures and Algorithms 5

  6. Pseudocode Practice Solution Algorithm: Given a list of integers, find the index of the maximum integer in the list. if list is not empty: int maxIndex starts at 0 for first index for each index i in the list: if the element at index i is greater than the element at index maxIndex: reset maxIndex to i return maxIndex else: error case: return -1? throw exception? Winter 2017 CSE373: Data Structures and Algorithms 6

  7. Terminology Review Abstract Data Type (ADT) Mathematical description of a "thing" with set of operations Algorithm A high level, language-independent description of a step- by-step process Data structure A specific organization of data and family of algorithms for implementing an ADT Implementation of a data structure A specific implementation in a specific language Winter 2017 CSE373: Data Structures and Algorithms 7

  8. 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: dictionaries, sorting Typically elements are comparable types, or have two fields: the priority and the data CSE373: Data Structures & Winter 2017 8 Algorithms

  9. Priority Queue vs Queue Queue: follows First-In-First-Out ordering Example: serving customers at a pharmacy, based on who got there first. Priority Queue: compares priority of elements to determine ordering Example: emergency room, serves patients with priority based on severity of wounds CSE373: Data Structures & Winter 2017 9 Algorithms

  10. Priorities 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 ties arbitrarily Operations: insert deleteMin is_empty 6 2 15 12 18 45 3 7 23 insert deleteMin deleteMinreturns 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 priorities instead of FIFO CSE373: Data Structures & Winter 2017 10 Algorithms

  11. Priority Queue Example Given the following, what values are a, b, c and d? insertelement1 with priority 5 insertelement2 with priority 3 insertelement3 with priority 4 a = deleteMin // a = ? b = deleteMin // b = ? insertelement4 with priority 2 insertelement5 with priority 6 c = deleteMin // c = ? d = deleteMin // d = ? CSE373: Data Structures & Winter 2017 11 Algorithms

  12. Priority Queue Example Solutions insertelement1 with priority 5 insertelement2 with priority 3 insertelement3 with priority 4 a = deleteMin // a = element2 b = deleteMin // b = element3 insertelement4 with priority 2 insertelement5 with priority 6 c = deleteMin // c = element4 d = deleteMin// d = element1 CSE373: Data Structures & Winter 2017 12 Algorithms

  13. Some Applications Run multiple programs in the operating system "critical" before "interactive" 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) Sorting (first insert all, then repeatedly deleteMin) CSE373: Data Structures & Winter 2017 13 Algorithms

  14. Possible Implementations Unsorted Array insert by inserting at the end deleteMin by linear search Sorted Circular Array insert by binary search, shift elements over deleteMinby moving front CSE373: Data Structures & Winter 2017 14 Algorithms

  15. More Possible Implementations Unsorted Linked List insert by inserting at the front deleteMin by linear search Sorted Linked List insert by linear search deleteMin remove at front Binary Search Tree insert by search traversal deleteMin by find min traversal CSE373: Data Structures & Winter 2017 15 Algorithms

  16. One Implementation: Heap Heaps are implemented with Trees A binary min-heap (or just binary heap or heap) is a data structure with the properties: Structure property: A complete binary tree Heap property: The priority of every (non- root) node is greater than the priority of its parent Not a binary search tree CSE373: Data Structures & Winter 2017 16 Algorithms

  17. 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: A B C D F G E H perfect tree: complete tree: CSE373: Data Structures & Winter 2017 17 Algorithms

  18. Tree Review root of tree: A leaves of tree: H,E,F,G children of B: D, E, F parent of C: A subtree C: in blue height of tree: 3 height of E: 0 depth of E: 2 degree of B: 3 A B C D F G E H perfect tree: every level is completely full complete tree: all levels full, with a possible exception being the bottom level, which is filled left to right CSE373: Data Structures & Winter 2017 18 Algorithms

  19. Structure Property: Completeness A Binary Heap is a complete binary tree: A binary tree with all levels full, with a possible exception being the bottom level, which is filled left to right Examples: 10 10 20 80 20 80 40 60 85 99 30 40 400 50 700 are these trees complete? CSE373: Data Structures & Winter 2017 19 Algorithms

  20. Structure Property: Completeness A Binary Heap is a complete binary tree: A binary tree with all levels full, with a possible exception being the bottom level, which is filled left to right Examples: 10 complete incomplete 10 20 80 20 80 40 60 85 99 30 40 400 50 700 CSE373: Data Structures & Winter 2017 20 Algorithms

  21. Heap Order Property The priority of every (non-root) node is greater than (or equal to) that of it's parent. AKA the children are always greater than the parents. 10 1 20 80 30 33 20 40 40 20 20 40 800 400 which of these follow the heap order property? CSE373: Data Structures & Winter 2017 21 Algorithms

  22. Heap Order Property The priority of every (non-root) node is greater than (or equal to) that of it's parent. AKA the children are always greater than the parents. heap property not the heap property 10 1 20 80 30 33 20 40 40 20 20 40 800 400 CSE373: Data Structures & Winter 2017 22 Algorithms

  23. Heaps A binary min-heap (or just binary heap or just heap) is: Structure property: A complete binary tree Heap property: The priority of every (non-root) node is greater than (or equal to) the priority of its parent. AKA the children are always greater than the parents. Not a binary search tree 10 10 1 20 80 20 80 50 3 40 60 85 99 30 15 450 75 60 8 50 700 10 which of these are heaps? CSE373: Data Structures & Winter 2017 23 Algorithms

  24. Heaps A binary min-heap (or just binary heap or just heap) is: Structure property: A complete binary tree Heap property: The priority of every (non-root) node is greater than (or equal to) the priority of its parent. AKA the children are always greater than the parents. Not a binary search tree 10 10 1 20 80 20 80 50 3 40 60 85 99 30 15 450 75 60 8 50 700 a heap not a heap 10 10 not a heap CSE373: Data Structures & Winter 2017 24 Algorithms

  25. Heaps Where is the highest-priority item? What is the height of a heap with n items? How do we use heaps to implement the operations in a Priority Queue ADT? CSE373: Data Structures & Winter 2017 25 Algorithms

  26. Heaps Where is the highest-priority item? At the root (at the top) What is the height of a heap with n items log2n (We ll look at computing this next week) How do we use heaps to implement the operations in a Priority Queue ADT? See following slides CSE373: Data Structures & Winter 2017 26 Algorithms

  27. Operations: basic idea deleteMin: 1. answer = root.data 2. Move right-most node in last row to root to restore structure property 3. "Percolate down" to restore heap property insert: 1. Put new node in next position on bottom row to restore structure property 2. "Percolate up" to restore heap property 10 20 80 40 60 85 99 50 700 Overall strategy: Preserve structure property Break and restore heap property CSE373: Data Structures & Winter 2017 27 Algorithms

  28. deleteMin 2 1. Delete (and later return) value at root node 4 3 7 5 8 9 11 9 6 10 Winter 2017 CSE373: Data Structures & Algorithms

  29. 2. Restore the Structure Property We now have a "hole" at the root Need to fill the hole with another value 4 3 7 5 8 9 11 9 6 10 When we are done, the tree will have one less node and must still be complete 4 3 7 5 8 9 11 9 6 10 Winter 2017 CSE373: Data Structures & Algorithms

  30. 3. Restore the Heap Property 4 or 3 ? 8 or 9 3 10 3 ? 4 8 4 3 4 10 7 5 10 9 7 5 8 9 7 5 8 9 11 9 6 11 9 6 11 9 6 Percolate down: Keep comparing with both children Swap with lesser child and go down one level What happens if we swap with the larger child? Done if both children are item or reached a leaf node Winter 2017 CSE373: Data Structures & Algorithms

  31. Insert Add a value to the tree 2 1 Afterwards, structure and heap properties must still be correct 4 8 7 5 10 9 11 9 6 Where do we insert the new value? CSE373: Data Structures & Algorithms Winter 2017

  32. Insert: Maintain the Structure Property There is only one valid tree shape after we add one more node 1 4 8 7 5 10 9 So put our new data there and then focus on restoring the heap property 11 9 6 2 Winter 2017 CSE373: Data Structures & Algorithms

  33. Maintain the heap property 1 1 ? 1 1 4 8 4 8 2 8 7 5 10 9 7 2 10 9 7 4 10 9 ? ? 11 9 6 2 11 9 6 11 9 6 5 5 5 4 Percolate up: Put new data in new location If parent larger, swap with parent, and continue Done if parent item or reached root Winter 2017 CSE373: Data Structures & Algorithms

  34. Array Representation of Binary Trees From node i: 1 A left child: i*2 right child: i*2+1 parent: i/2 2 3 B C 6 7 4 5 F G D 9 E 11 8 10 12 H I J K L (wasting index 0 is convenient for the index arithmetic) implicit (array) implementation: A 1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 I 9 J 10 K 11 L 12 0 13 CSE373: Data Structures & Winter 2017 34 Algorithms

  35. Judging the array implementation Plusses: Less "wasted" space Just index 0 and unused space on right In conventional tree representation, one edge per node (except for root), so n-1 wasted space (like linked lists) Array would waste more space if tree were not complete Multiplying and dividing by 2 is very fast (shift operations in hardware) Last used position is just index size Minuses: Same might-be-empty or might-get-full problems we saw with stacks and queues (resize by doubling as necessary) Plusses outweigh minuses: "this is how people do it" CSE373: Data Structures & Winter 2017 35 Algorithms

More Related Content