Presentation Number 8 - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Presentation Number 8

Description:

Towers of Hanoi. void hanoi(int size, int src, int dest, int aux) if ( size == 1 ) ... an alternative non-recursive solution available. Iterative Solution ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 29
Provided by: thearbe
Category:

less

Transcript and Presenter's Notes

Title: Presentation Number 8


1
Presentation Number 8
  • C Programming Course

2
Functions
  • Function definition
  • return-type name(arg_type1 arg_name1, arg_type2
    arg_name2, )
  • function body
  • return value

3
Recursion
  • Definition 1
  • A method for defining functions in which the
    function being defined is used within its own
    definition.
  • You must define a stopping criteria or you get an
    infinite loop.
  • Definition 2
  • Recursion If you still dont get it See
    Recursion.

4
Recursion
n! n (n-1) (n-2) ... 3 2 1
an iterative definition
n! n (n-1)! 1! 1
a recursive definition
6! 6 5!
4! 4 3!
5! 5 4!
3! 3 2!
2! 2 1!
1! 1
5
A recursive definition
  • C functions can call themselves!
  • When a function calls itself we call this a
    recursive call.
  • Two things you must remember about recursions
  • Change the parameters from call to call!!!
  • Make sure you defined a boundary condition!!!

6
Factorial Example
Reucrsive Version
Iterative Version
int factorial(int n) if (n lt 1) return
1 else return (n factorial(n1))
int factorial(int n) int product 1 int i
1 for( i1 i lt n i) product
i return product
Boundary condition
Recursive call
RecFactorial.c
7
Another Way To Think About It
  • I know how to solve simple problems.
  • Let the function deal with the complicated ones.

int factorial(int n) if (n lt 1) return
1 else return (n factorial(n1))
I know how to solve this
I know that n! n (n-1)! Let someone else
solve (n-1)!
8
Hanoi Towers
9
Towers of Hanoi
  • Goal
  • Move the entire tower to the right peg.
  • Constraints
  • Only move one disk at a time.
  • Never place a larger disk on a smaller one.

10
Towers of Hanoi
Step 1 Move n 1 disks to the middle peg.
11
Towers of Hanoi
Step 2 Move a single disk from the left peg to
the right peg.
12
Towers of Hanoi
Step 3 Move n 1 disks from the middle peg to
the right peg.
13
Towers of Hanoi
  • void hanoi(int size, int src, int dest, int aux)
  • if ( size 1 )
  • printf( "Shift disc from peg d to peg
    d.\n", src, dest )
  • return
  • hanoi( size 1, src, aux, dest )
  • hanoi( 1, src, dest, aux )
  • hanoi( size 1, aux, dest, src )
  • int main()
  • hanoi( 3, 1, 3, 2 )
  • return 0

Hanoi.c
14
Towers of Hanoi
Call order for Hanoi of size 4
H(4)
H(3)
H(3)
H(2)
H(2)
H(2)
H(2)
H(1)
H(1)
H(1)
H(1)
H(1)
H(1)
H(1)
H(1)
15
Write Backwards
  • include ltstdio.hgt
  • void wrt_back()
  • int c 0
  • if ((c getchar()) ! \n)
  • wrt_back()
  • printf(c, c)
  • int main()
  • printf(Input a line )
  • wrt_back()
  • printf(\n)
  • return 0

16
Fibonacci Number
  • int fib(int num)
  • switch(num)
  • case 1
  • return 1
  • break
  • case 2
  • return 1
  • break
  • default
  • return(fib(num1) fib(num2))
  • break
  • return 0

the recursive calls
Fibonacci.c
17
Trace Fibonacci
In this implementation the function calls itself
twice
Recursion can consume a lot of memory (possible
stack overflow) and are therefore considered
inefficient.
18
Iterative Solution
  • int fib(int n)
  • int i 0, fn 0, fn_1 1, fn_2 1
  • if (n 1)
  • return 1
  • if (n 2)
  • return 1
  • for (i3 iltn i)
  • fn fn_2 fn_1
  • fn_2 fn_1
  • fn_1 fn
  • return fn

Recursive functions are sometimes the simplest
and shortest answers
There is always an alternative non-recursive
solution available
Fibonacci.c
19
Eight Queens
20
Eight Queens
How do you place 8 queens on a chess board
without having them threaten each other?
21
  • include ltstdio.hgt
  • include ltmath.hgt
  • include ltassert.hgt
  • void print (int arr, int size)
  • int checkPosition (int arr, int col, int row)
  • int permutations (int arr, int col, int size)
  • int main()
  • int arr 8, num
  • num permutations (array, 0, 8)
  • printf ("Number of possible solutions d\n",
    num)
  • return 0

Array stores for each row the column number where
a queen is placed
Queens.c
22
Generate all the legal possibilities recursively
  • int permutations (int arr, int col, int size)
  • int row 0, num 0
  • if (col size) / only one permutation in this
    case /
  • print (arr, size)
  • num 1
  • else if ( col lt size )
  • for (row 0 row lt size row)
  • if ( checkPosition (arr, col, row) )
  • arrcol row
  • num permutations (arr, col 1, size)

The first pos positions are fixed. Place the next
position and recursively call the function with
the next positions.
Queens.c
23
Check if a partial permutation is legal. The
first pos positions do not occupy column value
diagonals are not occupied
  • int checkPosition (int arr, int col, int row)
  • int i
  • for (i 0 i lt col i)
  • if ( arri row (col - i) abs(arri
    - row) )
  • return 0
  • return 1

Queens.c
24
Print a permutation
  • void print(int arr, int size)
  • int i, j
  • for (i 0 i lt size i)
  • for (j 0 j lt size j)
  • printf ("c ", (arri j)? '' '-')
  • printf ("\n")
  • printf ("\n\n")

Queens.c
25
Merge Sort
  • A recursive method for sorting an array.
  • Question
  • How do you merge two sorted arrays into one
    sorted array?

26
Merge-Sort
Step 1 Sort the left half of the array.
27
Merge-Sort
Step 2 Sort the right half of the array.
28
Merge-Sort
Step 3 Merge the two halves into one.
Write a Comment
User Comments (0)
About PowerShow.com