Recursive Definitions (Application in Generic Programming) Dr. Partha Pratim Das, Kaushik Biswas*, Kausik Datta.

Slides:



Advertisements
Similar presentations
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
Advertisements

MATH 224 – Discrete Mathematics
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Fourier Transforms and Their Use in Data Compression
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
ENG004 ALGORITHMS & INT. TO PROGRAMMING Week 3 “An introduction to arrays and matrices” Ahmet Anıl Dindar
A simple example finding the maximum of a set S of n numbers.
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
CS2420: Lecture 10 Vladimir Kulyukin Computer Science Department Utah State University.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
Math for CSLecture 11 Mathematical Methods for Computer Science Lecture 1.
Math for CSLecture 11 Mathematical Methods for Computer Science Lecture 1.
CSCI 5582 Fall 2006 CSCI 5582 Artificial Intelligence Lecture 22 Jim Martin.
1 Gentle Introduction to Programming Session 4: Arrays.
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
MATH 224 – Discrete Mathematics
Kansas State University Department of Computing and Information Sciences CIS 736: Computer Graphics Monday, 26 January 2004 William H. Hsu Department of.
1 9/11/2015 MATH 224 – Discrete Mathematics Iterative version Recursive version Normally i is initialized to 0 and j to n–1 for an array of size n. Recursive.
Towers of Hanoi. Introduction This problem is discussed in many maths texts, And in computer science an AI as an illustration of recursion and problem.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Recursion. Basic problem solving technique is to divide a problem into smaller sub problems These sub problems may also be divided into smaller sub problems.
The power of logarithmic computations Jordi Cortadella Department of Computer Science.
CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
1 Recurrences Algorithms Jay Urbain, PhD Credits: Discrete Mathematics and Its Applications, by Kenneth Rosen The Design and Analysis of.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
1 CS 177 Week 16 Recitation Recursion. 2 Objective To understand and be able to program recursively by breaking down a problem into sub problems and joining.
Korea Advanced Institute of Science and Technology, Dept. of EECS, Div. of CS, Information Systems Lab. 1/10 CS204 Course Overview Prof.
Discrete Mathematics 이재원 School of Information Technology
Relationships Between Structures “→” ≝ “Can be defined in terms of” Programs Groups Proofs Trees Complex numbers Operators Propositions Graphs Real.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
CPS120: Introduction to Computer Science Computer Math: Converting to Decimal.
CSE S. Tanimoto Introduction to ML 1 Introduction to ML History Special features Interacting with ML ML’s basic types ML’s composite types Math.
Lecture 3 Induction & Sort(1) Algorithm design techniques: induction Selection sort, Insertion sort, Shell sort...
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Relation. Combining Relations Because relations from A to B are subsets of A x B, two relations from A to B can be combined in any way two sets can be.
Lecture 1 Overview Topics 1. Proof techniques: induction, contradiction Proof techniques June 1, 2015 CSCE 355 Foundations of Computation.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Ch.19 Divide and Conquer. 2 BIRD’S-EYE VIEW Divide and conquer algorithms Decompose a problem instance into several smaller independent instances May.
Introduction to JPEG m Akram Ben Ahmed
Control flow Ruth Anderson UW CSE 160 Spring
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
CSE 332: C++ STL iterators What is an Iterator? An iterator must be able to do 2 main things –Point to the start of a range of elements (in a container)
CSCI 115 Course Review.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
MA/CSSE 473 Day 05 More induction Factors and Primes Recursive division algorithm.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
CSCE 355 Foundations of Computation
Chapter 9: Searching, Sorting, and Algorithm Analysis
Unit 1. Sorting and Divide and Conquer
CSCE 355 Foundations of Computation
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Discrete Structures for Computer Science
Applied Algorithms (Lecture 17) Recursion Fall-23
CS 3343: Analysis of Algorithms
Ruth Anderson UW CSE 140 Winter 2014
Michael Ernst UW CSE 140 Winter 2013
CSE S. Tanimoto Introduction to ML
CSE S. Tanimoto Introduction to ML
CSE 3302 Programming Languages
CSE S. Tanimoto Introduction to ML
CSE S. Tanimoto Introduction to ML
Chapter 4 (Part 2): Mathematical Reasoning, Induction & Recursion
Presentation transcript:

Recursive Definitions (Application in Generic Programming) Dr. Partha Pratim Das, Kaushik Biswas*, Kausik Datta

Order of topics Observing for recurrences in nature Expressing recursive way Recursive definition Recursive set and set operations Recursive structures Recursive functions Recursive algorithms Conversion process to C++ Template Exercises

Recursive pattern seen in nature

variables : X F constants : + start : X rules : (X F-[[X]+X]+F[+FX]-X), (F FF) Angle : 25° Description in L-system Algorithmic Beauty of Plants

Recursive Definition To compress a large set of objects in reduced form of basis clause, recursive clause and extremal clause such that it could be recreated/re- generated. Basis Clause : Assumptions/Constants/Terminals Recursive/Inductive clause: Rules of productions Extremal clause : Making rules exclusive Most cases, basis clause and recursive clause are sufficient.

Basis clause: Some objects are in Set Recursive clause: Rules how to combine elements of set to produce new elements Extremal Clause: Excludes objects from set which are not defined by basic clause and recursive clause Set of even integers a) Basis: 0 S b) Recursive : x S (x+2) S (x-2) S c) Extremal: If x can not be obtained by a and b, its not in S Recursive Definition of Set

Define a set having integers which are divisible by both 2 and 3. Basis clause: Recursive clause: Extremal clause: Define a set having integers which are divisible by either 2 or 3. Basis clause: Recursive clause: Extremal clause: Recursive Set Question

Generic Union Definition ( n i=1 A i ) : Basis Clause: For n = 1, n i=1 A i = A 1. Inductive Clause: n i=1 A i = ( n i=1 A i ) A n+1 Generic Intersection Definition ( n i=1 A i ) : Basis Clause: For n = 1, n i=1 A i = A 1. Inductive Clause: n i=1 A i = ( n i=1 A i ) A n+1 Recursive Definition of set-operation

Structure definition : H- x(n) - H Basis: x(n)= HCH if n=1 Recursive rules: x(n) = x(n-1)-HCH if n > 1 Recursive Structures

List Structure Definition List Basis : List = Recursive Rule: List = (T, List ) List Function Definition head : List -> T Basis : head ( ) = ?? Recursive rule: head (List ) = domain( T, List ) tail : List -> List Basis : tail( ) = Recursive rule: tail (List ) =range(T, List ) Recursive List Definition

Binary Tree Definition Tree Basis : Tree = Recursive Rule: Tree = (T, (Tree, Tree )) Quiz: Define following function recursively current: Tree -> T left: Tree -> Tree right: Tree ->Tree Recursive Binary Tree Definition

Higher order recursion R. Structures D, R R. decompose : D -> 2 D, R. compose : 2 R -> R, Terminal Fn: D -> R - Watch for several recursive component in it Recursive Algorithm D D D D DDD RR R R R R R

Recursive structure:- Array A (start, end) = { A[start] } if start== end = { A[end] } X A(start, end-1) => (A[start], A[start+1], …A[end]) Decomposition function:- small (A(s, e) ) = { y : y A(s,e), y < A[s] } mid (A(s, e) ) = { y : y A(s,e), y = A[s] } large (A(s, e) ) = { y : y A(s,e), y > A[s] } Recursive Algorithm:- qsort( A(s, e) ) Basis rule: none if s== e Recursive rule: qsort( small (A(s,e) ) ) X { mid (A(s,e) ) } X qsort( large (A(s,e) ) ) if s < e -Composition are defined by X product operator on array elements Quick sort

- Conditions must be checked using (partial ) specialization - Loops must be done by recursive structure fib (n) = 1 if n=0 = 1 if n=1 = fib(n-1) + fib(n-2) otherwise //// Template form//// template struct fib { static const int val= fib ::val + fib ::val; }; template <> struct fib { static const int val=1; }; template <> struct fib { static const int val=1; }; Conversion Process to Template form

A(m,n) = n+1 if m = 0 = A(m-1, 1) if m>0 and n=0 = A(m-1, A(m, n-1)) if m>0 and n>0 (Require partial specialization) template struct A { static const int val = A ::val>::val; }; template struct A { static const it val = A ::val; }; template struct A { static const it val = n+1; }; Converting Ackermann Function

Workshop Exercises 1) Write vector as recursive definition 2) Write value () recursive function to get value of vector. 3) Write vector dot-product algorithm recursively 4) Write matrix as recursive definition [ Hint: Could use vector defined above] 5) Write value () recursive function to get value of matrix. 6) Write product of two matrices using recursive algorithm 7) Convert all the above recursive structure, recursive function and recursive algorithm using C++ template based generic code

References [1994] A Gentle Introduction to Category Theory - the calculational approach by Maarten M. Fokkinga [1999] Category Theory Lecture Notes for ESSLLI by Michael Barr and Charles Wells [2001] Category Theory Lecture Notes by Daniele Turi

References [2004] The Algorithmic Beauty of Plants by Przemyslaw Prusinkiewicz & Aristid Lindenmayer: Original book available as digital version now. Its eye-opening book on re-writing rule based hierarchy of any biological or natural recursive pattern. [2009] CS381 Discrete Structures/Discrete Mathematics Web Course Material: rse.html Most of computer science discrete math materials could be found here including recursive definitions for easy reference.