Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Recursive Definitions (Application in Generic Programming) Dr. Partha Pratim Das, Kaushik Biswas*, Kausik Datta."— Presentation transcript:

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

2 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

3 Recursive pattern seen in nature

4 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 http://en.wikipedia.org/wiki/L-system

5 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.

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 - 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

15 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

16 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

17 References [1994] A Gentle Introduction to Category Theory - the calculational approach by Maarten M. Fokkinga http://wwwhome.cs.utwente.nl/~fokkinga/mmf92b.pdf http://wwwhome.cs.utwente.nl/~fokkinga/mmf92b.pdf [1999] Category Theory Lecture Notes for ESSLLI by Michael Barr and Charles Wells http://folli.loria.fr/cds/1999/library/pdf/barrwells.pdf http://folli.loria.fr/cds/1999/library/pdf/barrwells.pdf [2001] Category Theory Lecture Notes by Daniele Turi http://www.dcs.ed.ac.uk/home/dt/CT/categories.pdf

18 References [2004] The Algorithmic Beauty of Plants by Przemyslaw Prusinkiewicz & Aristid Lindenmayer: http://algorithmicbotany.org/papers/abop/abop.pdf 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: http://www.cs.odu.edu/~toida/nerzic/content/web_cou rse.html Most of computer science discrete math materials could be found here including recursive definitions for easy reference.


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

Similar presentations


Ads by Google