Recitation 9 Prelim Review.

Slides:



Advertisements
Similar presentations
GRAPHS Lecture 18 CS2110 – Fall Graph Algorithms 2 Search –depth-first search –breadth-first search Shortest paths –Dijkstra's algorithm Minimum.
Advertisements

Graphs - II CS 2110, Spring Where did I leave that book?
BST Data Structure A BST node contains: A BST contains
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
Information and Computer Sciences University of Hawaii, Manoa
Starting at Binary Trees
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Recitation on analysis of algorithms. Formal definition of O(n) We give a formal definition and show how it is used: f(n) is O(g(n)) iff There is a positive.
Review for Final Exam – cs411/511 Definitions (5 questions, 2 points each) Algorithm Analysis (3 questions, 3 points each) General Questions (3 questions,
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
CISC220 Fall 2009 James Atlas Dec 07: Final Exam Review.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Breadth-First Search (BFS)
Final Exam Review COP4530.
Analysis of Algorithms and Inductive Proofs
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
B/B+ Trees 4.7.
Data Structure Interview Question and Answers
Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008
Heaps And Priority Queues
Recitation 10 Prelim Review.
Priority Queues and Heaps
UNIT III TREES.
Binary Search Trees.
Week 6 - Wednesday CS221.
12. Graphs and Trees 2 Summary
Graph Search Lecture 17 CS 2110 Fall 2017.
Lecture 18. Basics and types of Trees
Week 11 - Friday CS221.
Hashing Exercises.
Cse 373 April 26th – Exam Review.
November 1st – Exam Review
CMSC 341 Lecture 13 Leftist Heaps
i206: Lecture 13: Recursion, continued Trees
Binary Tree and General Tree
Data Structures and Database Applications Binary Trees in C#
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Data Structures and Algorithms for Information Processing
CSE 326: Data Structures: Midterm Review
Analysis of Algorithms
Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues
Algorithm design and Analysis
Graph Search Lecture 17 CS 2110 Spring 2018.
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
Find in a linked list? first last 7  4  3  8 NULL
Final Exam Review COP4530.
Heaps and the Heapsort Heaps and priority queues
Lesson 6. Types Equality and Identity. Collections.
Lecture 12 CS203 1.
Searching CLRS, Sections 9.1 – 9.3.
CMSC 202 Trees.
CSC 143 Java Applications of Trees.
Minimum Spanning Tree.
Algorithms: Design and Analysis
Recitation 10 Prelim Review.
Important Problem Types and Fundamental Data Structures
Lecture 14 Minimum Spanning Tree (cont’d)
Analysis and design of algorithm
Spanning Trees, greedy algorithms
Binary Search Trees CS 580U Fall 17.
Searching and Sorting Hint at Asymptotic Complexity
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Recitation 9 Prelim Review

Heaps

Review: Binary heap PriorityQueue min heap max heap 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 1 99 2 99 4 1 4 3 2 3

Review: Binary heap 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? 1 2 99 4 3

Hashing

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

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

Review: Hashing Idea: finding an element in an array takes constant time when you know which index it is stored in Hash Function value int 1 2 3 4 5 b Hash function should be O(1) to reap the benefits of hashing This is where the magic is to get our O(n) operations down to amortized O(1)

Collision resolution Two ways of handling collisions: Chaining 2. Open Addressing with linear probing Give a high-high-level of both approaches here.

Load factor: b’s saturation add(“MA”) Hash Function MA MA NY VA 1 2 3 4 5 b

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

Trees

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

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

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

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

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

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

Stack machine as postorder traversal + Each node evaluates its children (arguments) before itself (operator) Result becomes the argument for the next highest level * 5 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.

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

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

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

Graphs

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

Question: What is BFS and DFS? C F 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. What is the difference between DFS and BFS? What algorithm would be better to use if our graph were near infinite and a node was nearby? Is Dijkstra’s more like DFS or BFS? Why? Can you run topological sort on this graph?

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 Start End 1 2 5 3 4 6 1 2 5 3 4 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 The nodes REACHABLE from 1 are 1, 0, 2, 3, 5 1 2 5 3 4 6

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 The nodes REACHABLE from 1 are 1, 0, 2, 3, 5 1 2 5 3 4 6

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 5 3 4 6

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); } 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! 1 2 5 3 4 6

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 the for loop visits neighbors in numerical order. Then dfs(1) visits the nodes in this order: 1 … 1 2 5 3 4 6

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 the for loop visits neighbors in numerical order. Then dfs(1) visits the nodes in this order: 1, 0 … 1 2 5 3 4 6

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 the for loop visits neighbors in numerical order. Then dfs(1) visits the nodes in this order: 1, 0, 2 … 1 2 5 3 4 6

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 the for loop visits neighbors in numerical order. Then dfs(1) visits the nodes in this order: 1, 0, 2, 3 … 1 2 5 3 4 6

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 the for loop visits neighbors in numerical order. Then dfs(1) visits the nodes in this order: 1, 0, 2, 3, 5 1 2 5 3 4 6

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?

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); } 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. 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

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

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.

Big O definition Is merge sort O(n3)? 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(n3)? n Yes, but not tightest upper bound Explain the Big O definition and explain that this is why constants don’t matter. c * g(n) is our upper bound and we can set c to be any real valued number. Note: We don’t say f(n) = O(g(n)) because it is not an equality. Yes merge sort is O(n^3) technically, but when big O is used, it usually means it is the tightest bound. N

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(n2) 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. For all of the vocab words, go over an example, Adding two numbers - O(1) binary search - O(log n) linear search - O(n) selection sort - O(n^2)

Review: Informal Big O rules 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((n2-n)/2) = O((1/2)n2 + (-1/2)n) = O((1/2)n2) = O(n2) 3. Why don’t logarithm bases matter? –For constants x, y: O(logx n) = O((logx y)(logy n)) –Since (logx y) is a constant, O(logx n) = O(logy n) Test will not require understanding such rules for logarithms

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. n2 + 2*n + 6 is O(n2) (quadratic) 6. n3 + n2 is O(n3) (cubic) 7. 2n + n5 is O(2n) (exponential)

Analysis of Algorithms Review: Big O examples What is the runtime of an algorithm that runs insertion sort on an array O(n2) and then runs binary search O(log n) on that now sorted array? 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? What is the runtime of running merge sort 4 times? n times?

Other topics to know Refer to study guide :)

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.

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

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 In the first step, choose one node n, arbitrarily. When adding an edge, choose one with minimum weight that is connected to n.