Paradigms - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Paradigms

Description:

Towers of Hanoi. The purpose of the game is to move the tower from one post to another with the ... To move a tower of n disks from the start peg to the ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 37
Provided by: davidg84
Category:
Tags: game | hanoi | math | of | paradigms | problem | tower | towers

less

Transcript and Presenter's Notes

Title: Paradigms


1
Paradigms
Lect20
  • Define the Problem.
  • Gather Information.
  • Generate a Possible Solution.
  • Implement the Solution.
  • Test the Solution.

2
(No Transcript)
3
Nine Dots
4
Nine Dots Solution
5
Six Matches
6
6 Matches - 2 Triangles
7
Six Matches Solution
8
Bottles Solution
  • All cases except 8 can be solved by
  • B-A-2C B contains the required amount.
  • 6 can also be solved by A-C
  • 7 can also be solved by AC
  • 8 can only be solved by A-C

9
Recursion
  • Sometimes it is possible and useful to offer an
    algorithm that calls itself.
  • This calling of itself is termed recursion.

10
Possible
  • It is possible because the structure of the
    algorithm naturally breaks down into
  • solution of a large problem
  • solution of the same problem of a smaller size
  • a small step.

11
Useful
  • It is useful because recursion may be the natural
    way to express the solution to a problem.

12
Factorial
  • The factorial is a classic example of recursion.
  • 5! 5 x 4 x 3 x 2 x1
  • 4! 4 x 3 x 2 x 1
  • 5! 5 x 4!
  • Generally n! n (n-1)!

13
Factorial Program
  • include ltstdio.hgt
  • long fact(int)
  • int main()
  • int n5
  • printf("Factorial d is ld\n", n, fact(n))
  • system("PAUSE") return 0
  • long fact(int n)
  • if(n1) return 1
  • else return ((long) n)fact(n-1)

14
Diagramatic Expansion
main
15
Diagramatic Expansion
main
fact(5)
16
Diagramatic Expansion
main
fact(5)
fact(4)
17
Diagramatic Expansion
main
fact(5)
fact(4)
fact(3)
18
Diagramatic Expansion
main
fact(5)
fact(4)
fact(3)
fact(2)
19
Diagramatic Expansion
main
fact(5)
fact(4)
fact(3)
fact(2)
fact(1)
20
Diagramatic Expansion
main
fact(5)
fact(4)
fact(3)
1
fact(2)
fact(1)
21
Diagramatic Expansion
main
fact(5)
fact(4)
2
fact(3)
1
fact(2)
fact(1)
22
Diagramatic Expansion
main
fact(5)
6
fact(4)
2
fact(3)
1
fact(2)
fact(1)
23
Diagramatic Expansion
main
24
fact(5)
6
fact(4)
2
fact(3)
1
fact(2)
fact(1)
24
Diagramatic Expansion
120
main
24
fact(5)
6
fact(4)
2
fact(3)
1
fact(2)
fact(1)
25
End Condition
  • Recursion can be dangerous.
  • fact() will continue to call itself unless we
    program the end condition
  • if(n1) return 1
  • An explicit end condition is essential.

26
Fibonacci Series
Nautilus Shell
27
Mathematics
  • fibonacci(n) fibonacci(n-1) fibonacci(n-2)
  • Ex 1,2,3,5,8,13,21, ....
  • long fibonacci(int n)
  • if(n0) return 1
  • if(n1) return 2
  • return fibonacci(n-1)fibonacci(n-2)

28
Towers of Hanoi
  • The purpose of the game is to move the tower from
    one post to another with the following rules
  • Only one disk can be moved at a time
  • no disk can be placed on one of smaller diameter
    and
  • the third post can be used for temporary storage.

29
Exhaustive Search
Disk1 to Peg3
Disk1 to Peg2
Disk2 to Peg3
Disk2 to Peg2
Disk1 to Peg1
Disk1 to Peg3
Disk1 to Peg2
30
A Careful Review
31
A Careful Review
32
Solution
  • To move a tower of n disks from the start peg to
    the destination peg using the intermediate peg
  • To perform
  • movetower(n, start, dest, inter)
  • Do
  • movetower(n-1, start, inter, dest)
  • movedisk(n, start, dest)
  • movetower(n-1, inter, dest, start)

33
End Condition
  • The remaining question is how to complete the
    recursion? The movement of a tower containing
    one disk is just the movement of the disk
  • if(n1) movedisk(1, start, dest)

34
movetower(4, 1, 3, 2)
movetower(3, 1, 2, 3) movedisk(4, 3)
35
movetower(3, 1, 2, 3)
movetower(2, 1, 3, 2) movedisk(3, 2)
36
movetower(2, 1, 3, 2)
movetower(1, 1, 2, 3) movedisk(2, 3)
Write a Comment
User Comments (0)
About PowerShow.com