CompSci 100e Program Design and Analysis II March 29, 2011 Prof. Rodger CompSci 100e, Spring20111.

Slides:



Advertisements
Similar presentations
1 AVL Trees. 2 AVL Tree AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children.
Advertisements

AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
ADA: 4. Divide/Conquer1 Objective o look at several divide and conquer examples (merge sort, binary search), and 3 approaches for calculating their.
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
1 Issues with Matrix and Vector Issues with Matrix and Vector Quicksort Quicksort Determining Algorithm Efficiency Determining Algorithm Efficiency Substitution.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
CSE 326: Data Structures Lecture #3 Analysis of Recursive Algorithms Alon Halevy Fall Quarter 2000.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Analysis of Recursive Algorithms
Analysis of Algorithms CS 477/677
David Luebke 1 7/2/2015 Merge Sort Solving Recurrences The Master Theorem.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
SIGCSE Tradeoffs, intuition analysis, understanding big-Oh aka O-notation Owen Astrachan
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
CPS Wordcounting, sets, and the web l How does Google store all web pages that include a reference to “peanut butter with mustard”  What efficiency.
CPS 100, Spring Analysis: Algorithms and Data Structures l We need a vocabulary to discuss performance and to reason about alternative algorithms.
CPS 100, Spring Trees: no data structure lovelier?
Project 2 due … Project 2 due … Project 2 Project 2.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Compsci 201 Recitation 11 Professor Peck Jimmy Wei 11/8/2013.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Analysis of Algorithms CS 477/677
CSC 211 Data Structures Lecture 13
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
CompSci 102 Discrete Math for Computer Science April 17, 2012 Prof. Rodger.
CompSci 100e 7.1 From doubly-linked lists to binary trees l Instead of using prev and next to point to a linear arrangement, use them to divide the universe.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
CompSci 100e 7.1 Plan for the week l Review:  Union-Find l Understand linked lists from the bottom up and top-down  As clients of java.util.LinkedList.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
CPS Scoreboard l What else might we want to do with a data structure? l What are maps’ limitations? AlgorithmInsertionDeletionSearch Unsorted.
David Stotts Computer Science Department UNC Chapel Hill.
Foundations II: Data Structures and Algorithms
Binary Search Trees (BST)
CompSci 100E 15.1 What is a Binary Search?  Magic!  Has been used a the basis for “magical” tricks  Find telephone number ( without computer ) in seconds.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Chapter 4, Part II Sorting Algorithms. 2 Heap Details A heap is a tree structure where for each subtree the value stored at the root is larger than all.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
CSC317 1 Binary Search Trees (binary because branches either to left or to right) Operations: search min max predecessor successor. Costs? Time O(h) with.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
CompSci 100e 7.1 Plan for the week l Review:  Boggle  Coding standards and inheritance l Understand linked lists from the bottom up and top-down  As.
Help Session 3: Induction and Complexity Ezgi, Shenqi, Bran.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
CPS 100, Spring Tools: Solve Computational Problems l Algorithmic techniques  Brute-force/exhaustive, greedy algorithms, dynamic programming,
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
CompSci 100e 7.1 Plan for the week l Recurrences  How do we analyze the run-time of recursive algorithms? l Setting up the Boggle assignment.
From doubly-linked lists to binary trees
Printing a search tree in order
Analysis: Algorithms and Data Structures
Search Trees.
Recursive Objects (Part 4)
PFTFBH Binary Search trees Fundamental Data Structure
Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient.
Compsci 201 Recursion, Recurrences, & Trees
COSC160: Data Structures Binary Trees
Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient.
What’s the Difference Here?
Divide and Conquer divide and conquer algorithms typically recursively divide a problem into several smaller sub-problems until the sub-problems are.
Compsci 201 Trees, Tries, Tradeoffs
Divide and Conquer.
Binary Search Trees.
common code “abstracted out” same code written several times: don’t!
Traverse this binary search tree using:
Divide-and-Conquer 7 2  9 4   2   4   7
Compsci 201 Binary Trees Recurrence Relations
Binary SearchTrees [CLRS] – Chap 12.
Divide-and-Conquer 7 2  9 4   2   4   7
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

CompSci 100e Program Design and Analysis II March 29, 2011 Prof. Rodger CompSci 100e, Spring20111

Announcements One APT next week – BSTCount – Will do in class Written Assignment lists/trees due March 31 New assignment Boggle due April 7 – Will do part of it in lab (last time, and next lab) Today – More on trees and analysis with trees – Recurrence relations CompSci 100e, Spring20112

More on Trees Focus on binary trees – Includes binary search trees – Process tree: root (subtree) (subtree) – Analyze recursive tree functions Recurrence relation CompSci 100e, Spring20113

Review: Printing a search tree in order When is root printed? – After left subtree, before right subtree. void visit(TreeNode t){ if (t != null) { visit(t.left); System.out.println(t.info); visit(t.right); } Inorder traversal How long for n nodes? – O()? “llama” “tiger” “monkey” “jaguar” “elephant” “ giraffe ” “pig” “hippo” “leopard” 4CompSci 100e, Spring2011

Tree functions Compute height of a tree, what is complexity? int height(Tree root) { if (root == null) return 0; else { return 1 + Math.max(height(root.left), height(root.right) ); } Modify function to compute number of nodes in a tree, does complexity change? – What about computing number of leaf nodes? 5CompSci 100e, Spring2011

Balanced Trees and Complexity A tree is height-balanced if – Left and right subtrees are height-balanced – Left and right heights differ by at most one boolean isBalanced(Tree root){ if (root == null) return true; return isBalanced(root.left) && isBalanced(root.right) && Math.abs(height(root.left) – height(root.right)) <= 1; } 6CompSci 100e, Spring2011

What is complexity? Consider worst case? What does the tree look like? Consider average case? Assume trees are “balanced” in analyzing complexity – Roughly half the nodes in each subtree – Leads to easier analysis How to develop recurrence relation? – What is T(n)? – What other work is done? How to solve recurrence relation – formula for recursion Plug, expand, plug, expand, find pattern – A real proof requires induction to verify correctness 7CompSci 100e, Spring2011

Solving Recurrence Relation Recurrence relation is a formula that models how much time the method takes. T(n) – the time it takes to solve a problem of size n Basis – smallest case you know how to solve, such as n=0 or n=1 If two recursive calls formula might be: – T(n) = T(smaller problem) + T(smaller problem) + work to put answer together… On the right side, replace T(smaller) by plugging it in to the formula CompSci 100e, Spring20118

Solving Recurrence Relation (cont) Continue replacing the T(smaller) values until you see a pattern – use k for the pattern Then solve for k with respect to N to get a basis case that has a constant value – this removes the T term from the right hand side of the equation and you are left with T(N) = to terms of N and can easily compute big-Oh CompSci 100e, Spring20119

What is average big-Oh for height? Write a recurrence relation T(0) = T(1) = T(n) = CompSci 100e, Spring201110

What is worst case big-Oh for height? Write a recurrence relation T(0) = T(1) = T(n) = CompSci 100e, Spring201111

What is average case big-Oh for is-balanced? Write a recurrence relation T(1) = T(n) = CompSci 100e, Spring201112

Recognizing Recurrences Solve once, re-use in new contexts – T must be explicitly identified – n must be some measure of size of input/parameter T(n) is for quicksort to run on an n-element array T(n) = T(n/2) + O(1) binary search O( ) T(n) = T(n-1) + O(1) sequential search O( ) T(n) = 2T(n/2) + O(1) tree traversal O( ) T(n) = 2T(n/2) + O(n) quicksort O( ) T(n) = T(n-1) + O(n) selection sort O( ) Remember the algorithm, re-derive complexity 13CompSci 100e, Spring2011

Recognizing Recurrences Solve once, re-use in new contexts – T must be explicitly identified – n must be some measure of size of input/parameter T(n) is for quicksort to run on an n-element array T(n) = T(n/2) + O(1) binary search O( ) T(n) = T(n-1) + O(1) sequential search O( ) T(n) = 2T(n/2) + O(1) tree traversal O( ) T(n) = 2T(n/2) + O(n) quicksort O( ) T(n) = T(n-1) + O(n) selection sort O( ) Remember the algorithm, re-derive complexity n log n n log n n n2n2 14CompSci 100e, Spring2011

BSTCount APT Given values for a binary search tree, how many unique trees are there? – 1 value = one tree – 2 values = two trees – 3 values = 5 trees – N values = ? trees Will memoize help? CompSci 100e, Spring201115

Recurrences If T(n) = T(n-1) + O(1)… where do we see this? T(n) = T(n-1) + O(1) true for all X so, T(n-1) = T(n-2)+ O(1) T(n) = [T(n-2) + 1] + 1 = T(n-2) + 2 = [T(n-3) + 1] + 2 = T(n-3) + 3 True for 1, 2, so eureka! We see a pattern T(n) = T(n-k) + k, true for all k, let n=k T(n) = T(n-n) + n = T(0) + n = n We could solve, we could prove, or remember! 16CompSci 100e, Spring2011