Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 146: Data Structures and Algorithms June 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Similar presentations


Presentation on theme: "CS 146: Data Structures and Algorithms June 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak."— Presentation transcript:

1 CS 146: Data Structures and Algorithms June 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak

2 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 2 Download the Textbook’s Source Code  http://users.cis.fiu.edu/~weiss/dsaajava3/code/ http://users.cis.fiu.edu/~weiss/dsaajava3/code/

3 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 3 Binary Heap  A binary heap (or just heap) is a binary tree that is complete. All levels of the tree are full except possibly for the bottom level which is filled from left to right: Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

4 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 4 Binary Heap  Conceptually, a heap is a binary tree.  But we can implement it as an array.  For any element in array position i : Left child is at position 2i Right child is at position 2i + 1 Parent is at position Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

5 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 5 Heap-Order Priority  We want to find the minimum value (highest priority) value quickly.  Make the minimum value always at the root. Apply this rule also to roots of subtrees.  Weaker rule than for a binary search tree. Not necessary that values in the left subtree be less than the root value and values in the right subtree be greater than the root value.

6 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 6 Heap-Order Priority Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

7 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 7 Build a Heap from Scratch  Two ways: 1. Repeatedly do a heap insertion on the list of values to insert.  Need to percolate up the hole each time from the bottom of the heap. 2. Simply stuff all the values into the heap in any order.  Since we implement a heap as a complete binary tree, which in turn we implement as a simple array, “stuffing the values” is just appending to the end of the array.  Afterward, establish the heap-order priority by repeatedly percolating down the nodes above the bottom row.  This way takes O(N) time for N nodes.

8 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 8 Build a Heap from Scratch, cont’d  Construct the heap from an array of values. First stuff the underlying array with values in their original order. Then call buildHeap () to establish heap-order priority. public BinaryHeap(AnyType[] items) { currentSize = items.length; array = (AnyType[]) new Comparable[(currentSize + 2)*11/10]; int i = 1; for (AnyType item : items) { array[i++] = item; } buildHeap(); } Constructor

9 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 9 Build a Heap from Scratch, cont’d  Call percolateDown() on nodes above the bottom row. private void buildHeap() { for (int i = currentSize/2; i > 0; i--) { percolateDown(i); }

10 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 10 Build a Heap from Scratch, cont’d Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

11 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 11 Build a Heap from Scratch, cont’d Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

12 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 12 How Long Does buildHeap() Take?  Each dashed line in the figures corresponds to two comparisons during a call to percolateDown() : One to find the smaller child. One to compare the smaller child to the node.  Therefore, to bound the running time of buildHeap(), we must bound the number of dashed lines. Each call to percolate down a node can possibly go all the way down to the bottom of the heap.  There could be as many dashed lines from a node as the height of the node. The maximum number of dashed lines is the sum of the heights of all the nodes in the heap. Prove that this sum is O(N).

13 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 13 How Long Does buildHeap() Take? cont’d  Prove: For a perfect binary tree of height h containing 2 h+1 – 1 nodes, the sum of the heights of the nodes is 2 h+1 – 1 – (h+1) There is one node (the root) at height h, 2 nodes at height h-1, 4 nodes at height h-2, and in general, 2 i nodes at height h-i. (b) (a) Multiply both sides by 2: Subtract (b) – (a). Note that 2h – 2(h-1) = 2, 4(h-1)-4(h-2) = 4, etc.

14 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 14  A complete tree is not necessarily a perfect binary tree, but it contains between N = 2 h and 2 h+1 nodes. Therefore, S = O(N).  And so buildHeap() runs in O(N) time. How Long Does buildHeap() Take? cont’d

15 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak Break 15

16 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak Midterm Thursday, July 2  Open book, laptop, Internet. Not open neighbor.  Short answer, paper and pencil.  “Buffet style” – more questions than you can probably answer, so pick which ones to do.  `  1 hour 55 minutes. 16

17 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 17 Review for the Midterm  Data structures Software model of real-world data  Algorithms Want to be as efficient as possible How to measure and compare How to analyze

18 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 18 Review for the Midterm, cont’d  Java data structures Primitive types and reference types Generic types Boxing and unboxing Comparable interface Covariant arrays Type parameters Function objects

19 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 19 Review for the Midterm, cont’d  Algorithm analysis What to measure How well an algorithm scales Comparing growth rates  linear, logarithmic, quadratic, cubic, exponential Big-Oh: T(N) = O(f(N)) Theta: T(N) = Θ(h(N)) Omega: T(N) = Ω(g(N)) Little-Oh: T(N) = o(p(N))  General rules for computing run time

20 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 20 Review for the Midterm, cont’d  Towers of Hanoi puzzle Recursive solution Recurrence relation  Let f ( n ) be the number of moves for n disks.  Recurrence relation Solve..  Find a “closed form” solution such as 2 n -1 Prove the solution using induction. f(n) = 1n = 1 2f(n-1) + 1n > 1 {

21 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 21 Review for the Midterm, cont’d  Abstract data type (ADT) Values Operations Hidden implementations  The list ADT ArrayList and LinkedList  iterators  singly linked and doubly linked lists Advantages and disadvantages  get() and set()  add() and remove()

22 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 22 Review for the Midterm, cont’d  The Java collections framework The Iterable interface Special form of the for loop  Nested Java classes  Insertions into a sorted ArrayList Modified binary search algorithm Multithreaded insertions  critical region  The stack ADT Solve Towers of Hanoi using a stack

23 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 23 Review for the Midterm, cont’d  The queue ADT Circular buffer Multithreaded producer-consumer solution  The tree ADT Root, leaf, edge, path, depth, height Traversals  preorder  inorder  postorder  level order

24 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 24 Review for the Midterm, cont’d  Binary trees  Binary search trees (BST) Find, insert, delete Minimum and maximum values  AVL trees Balance condition Single and double, left and right rotations  Splay trees Splaying  B-trees Insertions and deletions

25 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 25 Review for the Midterm, cont’d  Proof that an average BST operation takes O(log N) time. How to get rid of summations  The priority queue ADT  Binary heap Complete tree Implemented by an array Heap-order priority Percolate up and down Proof that buildHeap() runs in O(N) time.

26 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 26 Sample Midterm Question #1  Consider a singly linked list that is either null-terminated or ends in a cycle: Describe an O ( N ) algorithm that, given the head of a singly linked list, determines whether or not the list contains a cycle. Answer: Start two node pointer variables slow and fast at the head of the list. Repeatedly advance fast two nodes forward for each time slow advances one node forward. If there’s a cycle, both pointers will be trapped in it, and fast will overlap slow.

27 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 27 Sample Midterm Question #2  Consider two singly linked lists that merge: Describe an algorithm that finds the node where the lists first merge (shown darker above), either with words or pseudocode. You are given the heads of the two lists.  If the number of nodes in the first list before the merge is M and the number of nodes in the second list before the merge is N and the number of nodes in the merged part is P, then your algorithm should take O(M + N + P) time.

28 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 28 Sample Midterm Question #2, cont’d Answer 1: Start a pointer at the head of each list. Get the size (length) of each list. If there’s a difference, say d, advance the pointer of the longer list d nodes. Then advance both pointers in tandem. The first time they point to the same node, you’ve found where the lists first merge. Answer 2: Create two stacks, one per list. For each list, start a pointer variable at the head and advance it to the end of the list, pushing a reference to each node into its stack. When both pointer variables are at the ends of their lists, repeatedly pop the stacks in tandem. The last time the popped values from the two stacks point to the same node, that node is where the lists first merge.

29 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 29 Sample Midterm Question #3  Write an algorithm (pseudocode is OK) that, given the root of a binary search tree containing N nodes of integer values, and an integer value k, 0 < k ≤ N returns the node containing the k th smallest value in the tree. Answer: An inorder traversal of a BST visits each node in sorted order. During the traversal, keep track of the number of nodes visited. The k th node visited has the desired value.

30 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 30 Sample Midterm Question #4  Consider the following recurrence relation, where a and b are integers > 0: a.What is Q 5 (2) ? Q 5 (12) ? Answer: 0 Answer: 2 b.In general, what does function Q b do?  Answer: It performs integer division of its argument by b. c.Solve this recurrence relation. Answer: Q b (a) = 0if a < b Q b (a–b) + 1if a ≥ b {

31 Computer Science Dept. Summer 2015: June 30 CS 146: Data Structures and Algorithms © R. Mak 31 Sample Midterm Question #4, cont’d d. Answer: Proof by induction If a ≥ b, then assume the inductive hypothesis is true for all k ≤ a : Add 1 to both sides: Since b ≥ 1, then a + 1 – b ≤ a, and so by our hypothesis, we can write: But by the recurrence relation,and so: Therefore, iffor all k ≤ a, then If a < b, then Q b (a) = 0if a < b Q b (a–b) + 1if a ≥ b {


Download ppt "CS 146: Data Structures and Algorithms June 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak."

Similar presentations


Ads by Google