Recitation 9. Prelim Review

Similar documents
Total Score /1 /20 /41 /15 /23 Grader

Prelim 2 SOLUTION. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Prelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Prelim 2, CS2110. SOLUTION

Data Structures Question Bank Multiple Choice

CS301 - Data Structures Glossary By

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

Prelim 2 Solution. CS 2110, April 26, 2016, 5:30 PM

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Prelim 2, CS :30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Computer Science E-22 Practice Final Exam

Prelim 2. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs. Max Score Grader

(D) There is a constant value n 0 1 such that B is faster than A for every input of size. n n 0.

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

Course Review for Finals. Cpt S 223 Fall 2008

1. Attempt any three of the following: 15

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

Second Examination Solution

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Prelim 2. CS 2110, April 26, 2016, 5:30 PM Total Question True/False Complexity Heaps Trees Graphs Max Score Grader

Section 1: True / False (1 point each, 15 pts total)

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

Basic Data Structures (Version 7) Name:

CSCE 2014 Final Exam Spring Version A

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Part 1: Multiple Choice Circle only ONE answer. Each multiple choice question is worth 3.5 marks.

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

Design and Analysis of Algorithms - - Assessment

Prelim 2. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

Analysis of Algorithms

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

Programming II (CS300)

Section 05: Solutions

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

INSTITUTE OF AERONAUTICAL ENGINEERING

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

CSL 201 Data Structures Mid-Semester Exam minutes

Binary Search Trees Treesort

Prelim 2 SOLUTION. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

Prelim 2 Solution. CS 2110, April 26, 2016, 7:30 PM

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Summer Final Exam Review Session August 5, 2009

Friday Four Square! 4:15PM, Outside Gates

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

Course Review. Cpt S 223 Fall 2009

Day 10. COMP1006/1406 Summer M. Jason Hinek Carleton University

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

Data Structures Brett Bernstein

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

ASSIGNMENTS. Progra m Outcom e. Chapter Q. No. Outcom e (CO) I 1 If f(n) = Θ(g(n)) and g(n)= Θ(h(n)), then proof that h(n) = Θ(f(n))

COMP251: Background. Jérôme Waldispühl School of Computer Science McGill University

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

COMP 251 Winter 2017 Online quizzes with answers

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY THIRD SEMESTER B.TECH DEGREE EXAMINATION, JULY 2017 CS205: DATA STRUCTURES (CS, IT)

CS-301 Data Structure. Tariq Hanif

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Section 05: Midterm Review

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

Final Exam in Algorithms and Data Structures 1 (1DL210)

Algorithmic Analysis. Go go Big O(h)!

Largest Online Community of VU Students

Graphs - II. Announcements. Where did I leave that book? Where did I leave that book? Where did I leave that book? CS 2110, Fall 2016

Spanning Trees 4/19/17. Prelim 2, assignments. Undirected trees

CISC 320 Midterm Exam

Spanning Trees. Lecture 22 CS2110 Spring 2017

10/31/18. About A6, Prelim 2. Spanning Trees, greedy algorithms. Facts about trees. Undirected trees

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

CSci 231 Final Review

You must include this cover sheet. Either type up the assignment using theory5.tex, or print out this PDF.

ASYMPTOTIC COMPLEXITY

INF2220: algorithms and data structures Series 1

Prelim 2 Solution. CS 2110, 24 April 2018, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph.

ECE250: Algorithms and Data Structures Midterm Review

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

1 5,9,2,7,6,10,4,3,8,1 The first number (5) is automatically the first number of the sorted list

1. AVL Trees (10 Points)

CS171 Final Practice Exam

CSE 373 Autumn 2012: Midterm #2 (closed book, closed notes, NO calculators allowed)

Question Bank Subject: Advanced Data Structures Class: SE Computer

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

Question 7.11 Show how heapsort processes the input:

1 Heaps. 1.1 What is a heap? 1.2 Representing a Heap. CS 124 Section #2 Heaps and Graphs 2/5/18

FINAL EXAM REVIEW CS 200 RECITATIOIN 14

CS171 Final Practice Exam

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Transcription:

Recitation 9 Prelim Review 1

Heaps 2

Review: Binary heap min heap 1 2 99 4 3 PriorityQueue Maintains max or min of collection (no duplicates) Follows heap order invariant at every level Always balanced! worst case: O(log n) insert O(log n) update O(1) peek O(log n) removal max heap 99 4 1 2 3 3

Review: Binary heap min heap 1 2 99 4 3 How do we insert element 0 into the min heap? After we remove the root node, what is the resulting heap? How are heaps usually represented? If we want the right child of index i, how do we access it? 4

Hashing 5

Review: Hashing HashSet<String> MA NY CA 0 1 2 3 4 5 Method Expected Runtime add O(1) O(n) contains O(1) O(n) remove O(1) O(n) Worst Case load factor, for open addressing: number of non-null entries ---------------------------------------- size of array load factor, for chaining: size of set ---------------------------------------- size of array If load factor becomes > 1/2, create an array twice the size and rehash every element of the set into it, use new array 6

Review: Hashing MA NY CA 0 1 2 3 4 5 Method HashSet<String> Expected Runtime add O(1) O(n) contains O(1) O(n) remove O(1) O(n) Worst Case HashMap<String,Integer> to 2 be 2 or 1 not 1 that 1 is 1 the 1 question 1 7

Review: Hashing Idea: finding an element in an array takes constant time when you know which index it is stored in value Hash Function int b 0 1 2 3 4 5 8

Collision resolution Two ways of handling collisions: 1. Chaining 2. Open Addressing with linear probing 9

Load factor: b s saturation Load factor: Hash MA 0 Function add( MA ) b MA NY VA 0 1 2 3 4 5 10

Question: Hashing Using linear probing to resolve collisions, 1. Add element SC (hashes to 3). 2. Remove VA (hashes to 5). 3. Check to see if MA (hashes to 0) is in the set. 4. What should we do if we override equals()? b MA NY SC VA 0 1 2 3 4 5 11

Trees 12

Definition Data structure with nodes Each node has: 0 or more children Exactly 1 parent (except the root which has none) All nodes are reachable from root Binary tree - each node has at most 2 children 13

Use in Recursion Data Structure well suited for recursion Binary tree is either Empty A value (root), left binary tree, right binary tree The first becomes the base case and the second becomes the recursive case 14

Traversals Pre-Order (Root, Left ST, Right ST) In-Order (Left ST, Root, Right ST) Post-Order (you get the pattern :) ) 15

Expression Trees Nodes represent operations or values Leaf nodes are values. Non-leaf nodes are operations that work on their children. 16

Post-Order Post-order traversal output is code for a stack machine. (recursive descent parsing) 1.Visit nodes in tree in post-order. 2.Push results from a node onto a stack. 3.When you hit a node that is an operation, pop off the required number of elements from the stack and then push the result. 17

Stack machine as postorder traversal + * 5 Each node evaluates its children (arguments) before itself (operator) Result becomes the argument for the next highest level 2 + 3 4 18

Stack machine as postorder traversal + * 5 Each node evaluates its children (arguments) before itself (operator) Result becomes the argument for the next highest level 2 + 2 is evaluated first. However, the other child subtree needs to be evaluated before the product operator can proceed. 3 4 Children are evaluated to 3 and 4, add operator yields 7. 19

Stack machine as postorder traversal + * 5 Each node evaluates its children (arguments) before itself (operator) Result becomes the argument for the next highest level 2 7 Children are evaluated to 2 and 7, multiply operator yields 14. 20

Stack machine as postorder traversal + 14 5 Each node evaluates its children (arguments) before itself (operator) Result becomes the argument for the next highest level Children are evaluated to 14 and 5, add operator yields 19. 21

Stack machine as postorder traversal 19 Each node evaluates its children (arguments) before itself (operator) Result becomes the argument for the next highest level 22

Graphs 23

Planar Graphs Graph is planar if it can be drawn in the plane without edges crossing. This allows you to 4-color a graph Yes NO NO 24

Question: What is BFS and DFS? A B D E C F 1. Starting from node A, run BFS and DFS to find node Z. What is the order in which nodes were processed? Visit neighbors in alphabetical order. 2. What is the difference between DFS and BFS? 3. What algorithm would be better to use if our graph were near infinite and a node was nearby? 4. Is Dijkstra s more like DFS or BFS? Why? 5. Can you run topological sort on this graph? 25

Depth-First Search /** Visit all nodes that are REACHABLE from u. Precondition: u is not visited*/ public static void dfs(int u) { Let u be 1 The nodes REACHABLE from 1 are 1, 0, 2, 3, 5 } 1 0 Start 2 3 4 1 End 2 3 4 5 6 0 5 26 6

Depth-First Search /** Visit all nodes REACHABLE from u. Precond: Node u is unvisited. */ public static void dfs(int u) { Let u be 1 } 1 2 3 4 The nodes REACHABLE from 1 are 1, 0, 2, 3, 5 0 5 6 27

Depth-First Search /** Visit all nodes REACHABLE from u. Precond: Node u is unvisited. */ public static void dfs(int u) { visited[u] = true; } 1 0 2 5 3 6 4 Let u be 1 The nodes REACHABLE from 1 are 1, 0, 2, 3, 5 28

Depth-First Search /** Visit all nodes REACHABLE from u. Precond: Node u is unvisited. */ public static void dfs(int u) { visited[u] = true; Let u be 1 (visited) The nodes to be visited are 0, 2, 3, 5 } 1 2 3 4 0 5 6 29

Depth-First Search /** Visit all nodes REACHABLE from u. Precond: Node u is unvisited. */ public static void dfs(int u) { visited[u] = true; for all edges (u, v) leaving u: if v is unvisited then dfs(v); } 2 3 1 0 5 6 4 Let u be 1 (visited) The nodes to be visited are 0, 2, 3, 5 Have to do DFS on all unvisited neighbors of u! 30

Depth-First Search /** Visit all nodes REACHABLE from u. Precond: Node u is unvisited. */ public static void dfs(int u) { visited[u] = true; for all edges (u, v) leaving u: if v is unvisited then dfs(v); } 1 0 2 5 3 6 4 Suppose the for loop visits neighbors in numerical order. Then dfs(1) visits the nodes in this order: 1 31

Depth-First Search /** Visit all nodes REACHABLE from u. Precond: Node u is unvisited. */ public static void dfs(int u) { visited[u] = true; for all edges (u, v) leaving u: if v is unvisited then dfs(v); } 1 0 2 5 3 6 4 Suppose the for loop visits neighbors in numerical order. Then dfs(1) visits the nodes in this order: 1, 0 32

Depth-First Search /** Visit all nodes REACHABLE from u. Precond: Node u is unvisited. */ public static void dfs(int u) { visited[u] = true; for all edges (u, v) leaving u: if v is unvisited then dfs(v); } 1 0 2 5 3 6 4 Suppose the for loop visits neighbors in numerical order. Then dfs(1) visits the nodes in this order: 1, 0, 2 33

Depth-First Search /** Visit all nodes REACHABLE from u. Precond: Node u is unvisited. */ public static void dfs(int u) { visited[u] = true; for all edges (u, v) leaving u: if v is unvisited then dfs(v); } 1 0 2 5 3 6 4 Suppose the for loop visits neighbors in numerical order. Then dfs(1) visits the nodes in this order: 1, 0, 2, 3 34

Depth-First Search /** Visit all nodes REACHABLE from u. Precond: Node u is unvisited. */ public static void dfs(int u) { visited[u] = true; for all edges (u, v) leaving u: if v is unvisited then dfs(v); } 2 3 1 0 5 6 4 Suppose the for loop visits neighbors in numerical order. Then dfs(1) visits the nodes in this order: 1, 0, 2, 3, 5 35

Depth-First Search /** Visit all nodes REACHABLE from u. Precond: Node u is unvisited. */ public static void dfs(int u) { visited[u] = true; for all edges (u, v) leaving u: if v is unvisited then dfs(v); } Suppose n nodes are REACHABLE along e edges (in total). What is Worst-case execution? Worst-case space? 36

Depth-First Search /** Visit all nodes REACHABLE from u. Precond: Node u is unvisited. */ public static void dfs(int u) { visited[u] = true; for all edges (u, v) leaving u: if v is unvisited then dfs(v); } Example: Use different way (other than array visited) to know whether a node has been visited Example: We really haven t said what data structures are used to implement the graph That s all there is to basic DFS. You may have to change it to fit a particular situation. If you don t have this spec and you do something different, it s probably wrong. 37

Recommended Practice for Dijkstra 1. Create a graph with labeled nodes and pick a starting node, x 2. Draw a table to keep track of the Frontier, Settled, and Far Off sets for each iteration 3. Include in the table, the distance from x to each node in the graph for each iteration 4. Fill in the table by working through Dijkstra s 38

Big O See the Study Habits Note on the course Piazza. There is a 2- page pdf file that says how to learn what you need to know for O-notation. 39

Big O definition f(n) is O(g(n)) iff There is a positive constant c and a real number N such that: f(n) c * g(n) for n N c * g(n) f(n) Is merge sort O(n 3 )? n Yes, but not tightest upper bound N 40

Review: Big O Is used to classify algorithms by how they respond to changes in input size n. Important vocabulary: Constant time: O(1) Logarithmic time: O(log n) Linear time: O(n) Quadratic time: O(n 2 ) Let f(n) and g(n) be two functions that tell how many statements two algorithms execute when running on input of size n. f(n) >= 0 and g(n) >= 0. 41

Review: Informal Big O rules 1. Usually: O(f(n)) O(g(n)) = O(f(n) g(n)) Such as if something that takes g(n) time for each of f(n) repetitions... (loop within a loop) 2. Usually: O(f(n)) + O(g(n)) = O(max(f(n), g(n))) max is whatever s dominant as n approaches infinity Example: O((n 2 -n)/2) = O((1/2)n 2 + (-1/2)n) = O((1/2)n 2 ) = O(n 2 ) 3. Why don t logarithm bases matter? For constants x, y: O(log x n) = O((log x y)(log y n)) Since (log x y) is a constant, O(log x n) = O(log y n) Test will not require understanding such rules for logarithms 42

Review: Big O 1. log(n) + 20 is O(log(n)) (logarithmic) 2. n + log(n) is O(n) (linear) 3. n/2 and 3*n are O(n) 4. n * log(n) + n is n * log(n) 5. n 2 + 2*n + 6 is O(n 2 ) (quadratic) 6. n 3 + n 2 is O(n 3 ) (cubic) 7. 2 n + n5 is O(2 n ) (exponential) 43

Review: Big O examples Analysis of Algorithms 1. What is the runtime of an algorithm that runs insertion sort on an array O(n 2 ) and then runs binary search O(log n) on that now sorted array? 1. What is the runtime of finding and removing the fifth element from a linked list? What if in the middle of that remove operation we swapped two integers exactly 100000 times, what is the runtime now? 1. What is the runtime of running merge sort 4 times? n times? 44

Other topics to know Refer to study guide :) 45

Spanning Trees NOT ON TEST Two approaches. A spanning tree is a: - max set of edges with no cycles Algorithm: Repeat until no longer possible: Find a cycle and delete one of its edges - minimal set of edges that connect all nodes Algorithm: Start with all nodes (each one is a component), no edges. Repeat until no longer possible: Add an edge that connects two unconnected components. 46

Kruskal s minimum spanning tree algorithm Definition: minimal set of edges that connect all nodes Algorithm: Start with all nodes (each one is a component), no edges. Repeat until no longer possible: Add an edge that connects two unconnected components. Kruskal says to choose an edge with minimum weight 47

Prim s minimum spanning tree algorithm Definition: minimal set of edges that connect all nodes Algorithm: Start with all nodes (each one is a component), no edges. Repeat until no longer possible: Add an edge that connects two unconnected components. Prim says (1) In the first step, choose one node n, arbitrarily. (2) When adding an edge, choose one with minimum weight that is connected to n. 48