Data Structures & Algorithms

Slides:



Advertisements
Similar presentations
2.3 Recursion do automated demo of towers of hanoi before class
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
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.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
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,
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.
Data Structures & Algorithms Recursion and Trees.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications.
Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN.
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Data Structures & Algorithms Recursion and Trees Richard Newman.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Discrete Structures Trees (Ch. 11)
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
IS 2610: Data Structures Recursion, Divide and conquer Dynamic programming, Feb 2, 2004.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
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.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Trees A non-linear implementation for collection classes.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Recursion CSE 2320 – Algorithms and Data Structures
CS 201 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Chapter 5 : Trees.
Elementary Data Structures Jan 26, 2004
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Week 6 - Wednesday CS221.
Lecture Trees Chapter 9 of textbook 1. Concepts of trees
Trees Another Abstract Data Type (ADT)
Recursion CSE 2320 – Algorithms and Data Structures
ITEC 2620M Introduction to Data Structures
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
Binary Trees, Binary Search Trees
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.
TREES General trees Binary trees Binary search trees AVL trees
Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103 Chapter 10 Trees Slides are adopted from “Discrete.
Chapter 4: Divide and Conquer
Trees Another Abstract Data Type (ADT)
Week nine-ten: Trees Trees.
Trees Another Abstract Data Type (ADT)
Trees (Part 1, Theoretical)
Design and Analysis of Algorithms
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Trees.
Algorithms Recurrences.
ITEC324 Principle of CS III
Binary Trees, Binary Search Trees
Presentation transcript:

Data Structures & Algorithms Recursion and Trees 1

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-1)); } 2

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) 3

Recursion 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 the root of a binary tree 4

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 9

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 10

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! 11

Towers 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

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

Towers of Hanoi How many moves does this take?

Towers 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) + 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 Metric Recurrence Approx. Soln. Binary Search comparisons C(N) = C(N/2)+1 lg N MergeSort recursive calls A(N) = 2 A(N/2) + 1 N C(N) = 2 C(N/2) + N N lg N 16

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. This is Dynamic Programming Issue – table may be too big!

Dynamic Programming Fibonacci Numbers: 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!!!! 13 8 5 2 5 3 3 2 2 1 2 1 1 1 3 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Dynamic Programming Can we do better? How? Make a table – compute once (yellow shapes) Fill up table 13 8 8 5 3 2 3 2 3 2 1 2 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 3 4 5 6 7 8 1 1 2 3 5 8 13

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.

Types of Trees root internal node leaf external node Binary Tree Ternary Tree

Types of Trees root parent node sibling child Rooted Tree Free Tree

Tree Representation Binary Tree Representation

Tree Representation Ordered Tree Representation Use linked list for siblings at each level, Pointer to left child

Properties of Trees A binary tree with N internal nodes has N+1 external nodes A binary tree with N internal nodes has 2N links: N-1 to internal nodes and N+1 to external nodes The level of a node is one higher than the level of its parent, with the root at level 0. The path length of a tree is the sum of the levels of all the tree’s nodes The internal path length is the sum of levels of internal nodes; external path length is sum of levels of external nodes.

Properties of Trees The external path length of any binary tree with N nodes is 2N greater than the internal path length The height of a binary tree with N internal nodes is at least lg N and at most N-1. The internal path length of a binary tree with N internal nodes is at least N lg(N/4) and at most N(N-1)/2.

Tree Traversal Infix: Visit the left subtree, visit the root, then visit the right subtree Prefix: Visit the root, visit the left subtree, visit the right subtree. Postfix: Visit the left subtree, visit the right subtree, visit the root.