Data Structures & Algorithms Recursion and Trees Richard Newman.

Slides:



Advertisements
Similar presentations
Algorithm Design Techniques
Advertisements

2.3 Recursion do automated demo of towers of hanoi before class
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Recurrences. What is a Recurrence Relation? A system of equations giving the value of a function from numbers to numbers in terms of the value of the.
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
CIT 596 Complexity. Tight bounds Is mergesort O(n 4 )? YES! But it is silly to use that as your measure More common to hear it as….? What about quicksort?
Recursion.
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Analysis of Recursive Algorithms
Recursion. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Data Structures & Algorithms Recursion and Trees.
COMPSCI 105 S Principles of Computer Science Recursion 3.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
COMP s1 Computing 2 Complexity
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 14: Recursion Starting Out with C++ Early Objects
Chapter 3 Recursion: The Mirrors. © 2004 Pearson Addison-Wesley. All rights reserved 3-2 Recursive Solutions Recursion –An extremely powerful problem-solving.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Analysis of Algorithms
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Algorithms  Al-Khwarizmi, arab mathematician, 8 th century  Wrote a book: al-kitab… from which the word Algebra comes  Oldest algorithm: Euclidian algorithm.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
IS 2610: Data Structures Recursion, Divide and conquer Dynamic programming, Feb 2, 2004.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Recursion COMP x1 Sedgewick Chapter 5. Recursive Functions problems can sometimes be expressed in terms of a simpler instance of the same problem.
Recursion Continued Divide and Conquer Dynamic Programming.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Algorithm Analysis 1.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Decrease-and-Conquer Approach
Elementary Data Structures Jan 26, 2004
Introduction to the Design and Analysis of Algorithms
Recursion CSE 2320 – Algorithms and Data Structures
Data Structures & Algorithms
Recursion.
Algorithms Recurrences.
ITEC324 Principle of CS III
Presentation transcript:

Data Structures & Algorithms Recursion and Trees Richard Newman

Recursion Fundamental concept in math and CS Recursive definition Defined in terms of itself aN = a*aN-1, a0 = 1 Recursive function Calls itself int exp(int base, int pow) { return (pow == 0? 1 : base*exp(base, pow)); }

Recursion Recursive definition (and function) must: 1. have a base case – termination condition 2. always call a case smaller than itself All practical computations can be couched in a recursive framework! (see theory of computation)

Trees Recursively defined structures e.g., binary tree Base case: – Empty tree has no nodes Recursion: – None-empty tree has a root node with two children, each a tree

Roadmap to Recursion Widely used in CS and with trees...  Mathematical recurrences  Recursive programs  Divide and Conquer  Dynamic Programming  Tree traversal  DFS

Recursive Algorithms Recursive algorithm – solves problem by solving one or more smaller instances of same problem Recurrence relation – factorial – N! = N(N-1)!, for N > 0, with 0! = 1. – In C++, use recursive functions Int factorial(int N) { if (N == 0) return 1; return N*factorial(N-1); }

Recursive Algorithms BTW, can often also be expressed as iteration E.g., can also write N! computation as a loop: int factorial(int N) { for (int t = 1, i = 1; i <= N; ++i) t *= i; return t; }

Euclid's Algorithm Euclid's Algorithm is one of the oldest known algorithms Recursive method for finding the GCD of two integers int gcd(int m, int n) {// expect m >= n if (n == 0) return m; return gcd(n, m % n); } Base case Recursive call to smaller instance

Divide & Conquer Recursive scheme that divides input into two (or some fixed number) of (roughly) equal parts Then makes a recursive call on each part Widely used approach Many important algorithms Depending on expense of dividing and combining, can be very efficient

Divide & Conquer Example: find the maximum element in an array a[N] Easy to do iteratively... Base case: – Only one element – return it Divide: – Split array into upper and lower halves Recursion: – Find maximum of each half Combine results: – Return larger of two maxima

Divide & Conquer Property 5.1: A recursive function that divides a problem of size N into two independent (non-empty) parts that it solves, recursively calls itself less than N times. Prf: T(1) = 0 T(N) = T(k) + T(N-k) + 1 for recursive call on size N divided into one part of size k and the other of size N-k Induct!

Tower of Hanoi 3 pegs N disks, all on one peg Disks arranged from largest on bottom to smallest on top Must move all disks to target peg Can only move one disk at a time Must place disk on another peg Can never place larger disk on a smaller one Legend has it that the world will end when a certain group of monks finishes the task in a temple with 40 golden disks on 3 diamond pegs

Tower of Hanoi Target peg Which peg should top disk go on first?

Tower of Hanoi How many moves does this take?

Tower of Hanoi Property 5.2: The recursive d&c algorithm for the Towers of Hanoi problem produces a solution that has 2N – 1 moves. Prf: T(1) = 1 T(N) = T(N-1) T(N-1) = 2 T(N-1) + 1 = 2N – 1 by induction

Divide & Conquer Two other important D&C algorithms: Binary search MergeSort Algorithm/metri c RecurrenceApprox. Soln. Binary Search comparisons C(N) = C(N/2)+1lg N MergeSort recursive calls A(N) = 2 A(N/2) + 1 N MergeSort comparisons C(N) = 2 C(N/2) + N N lg N

Dynamic Programming In Divide & Conquer, it is essential that the subproblems be independent (partition the input) When this is not the case, life gets complicated! Sometimes, we can essentially fill up a table with values we compute once, rather than recompute every time they are needed.

Dynamic Programming Fibonacci Numbers: F[0] = 0 F[1] = 1 F[N] = F[N-1] + F[N-2] Horribly inefficient implementation: int F(int N) { if (N < 1) return 0; if (N == 1) return 1; return F(N-1) + F(N-2); }

Dynamic Programming How bad is this code? How many calls does it make to itself? F(N) makes F(N+1) calls! Exponential!!!!

Dynamic Programming Can we do better? How? Make a table – compute once (yellow shapes) Fill up table

Dynamic Programming Property 5.3: Dynamic Programming reduces the running time of a recursive function to be at most the time it takes to evaluate the functions for all arguments less than or equal to the given argument, treating the cost of a recursive call as a constant.

Trees A mathematical abstraction Central to many algorithms Describe dynamic properties of algorithms Build and use explicit tree data structures Examples: Family tree of descendants Sports tournaments (Who's In?) Organization Charts (Army) Parse tree of natural language sentence File systems

Types of Trees Trees Rooted trees Ordered trees M-ary trees and binary trees Defn: A Tree is a nonempty collection of vertices and edges such that there is exactly one path between each pair of vertices. Defn: A path is a list of distinct vertices such that successive vertices have an edge between them Defn: A graph in which there is at most one path between each pair of vertices is a forest.