- 67 Views
- Uploaded on
- Presentation posted in: General

ecs30 Winter 2012: Programming and Problem Solving #16: Chapters 9~10, Strings to Recurrsion

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

ecs30 Winter 2012:Programming and Problem Solving#16: Chapters 9~10, Strings to Recurrsion

Prof . S. Felix Wu

Computer Science Department

University of California, Davis

http://dsl.ucdavis.edu/~wu/ecs30/

ecs30 Winter 2012 Lecture #17

- Game of Life – please read the wikipedia page to understand the game itself
- A partial/unfinished program for you to complete/finish:
- Learn to understand code/program by others
- Learn how to complete or modify other’s code.

- Reference executables (arch – 64/32 bits)
- Executables without source code (reverse engineering)

ecs30 Winter 2012 Lecture #17

FILE *out_file_p;

FILE *out_debug_file_p;

intmain(void)

{…

/* initialization */

out_file_p= fopen("output.hw6_1", "w");

out_debug_file_ p = fopen("debug.hw6_1", "w");

If (out_file_p == NULL) {exit(-1);}

/* using the FILE pointers */

/* regular output example */

fprintf(out_file_p, "...", ...);

/* debug output example */

#ifdef ECS30_DEBUG

fprintf(out_debug_file_p, "...", ...);

#endif /*ECS30_DEBUG */

/* finishing/edning */

fclose(out_file_p);

fclose(out_debug_file_p);

ecs30 Winter 2012 Lecture #17

- A function calls itself, maybe with a different set of parameter values…
- Different sets of local variables…

(n-1)!

N!

ecs30 Winter 2012 Lecture #17

(m * n) ~ (m + m * (n - 1))

ecs30 Winter 2012 Lecture #17

ecs30 Winter 2012 Lecture #17

It gotta stop… at some point!!

Exit Strategy/Condition!

ecs30 Winter 2012 Lecture #17

ecs30 Winter 2012 Lecture #17

ecs30 Winter 2012 Lecture #17

The Towers of Hanoi

http://en.wikipedia.org/wiki/Tower_of_Hanoi

ecs30 Winter 2012 Lecture #17

Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi.

Stacked upon the leftmost needle were 64 golden disks, each a different size, stacked in concentric order:

ecs30 Winter 2012 Lecture #17

The priests were to transfer the disks from the first needle to the second needle, using the third as necessary.

But they could only moveone disk at a time, and could never put a larger disk on top of a smaller one.

When they completed this task, the world would end!

ecs30 Winter 2012 Lecture #17

For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C...

Since we can only move one disk at a time, we move the top disk from A to B.

ecs30 Winter 2012 Lecture #17

For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C...

We then move the top disk from A to C.

ecs30 Winter 2012 Lecture #17

For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C...

We then move the top disk from B to C.

ecs30 Winter 2012 Lecture #17

We then move the top disk from A to B.

ecs30 Winter 2012 Lecture #17

We then move the top disk from C to A.

ecs30 Winter 2012 Lecture #17

We then move the top disk from C to B.

ecs30 Winter 2012 Lecture #17

We then move the top disk from A to B.

ecs30 Winter 2012 Lecture #17

and we’re done!

The problem gets more difficult as the number of disks increases...

ecs30 Winter 2012 Lecture #17

Today’s problem is to write a program that generates the instructions for the priests to follow in moving the disks.

While quite difficult to solve iteratively, this problem has a simple and elegant recursive solution.

ecs30 Winter 2012 Lecture #17

The Hanoi Towers

Enter how many disks: 2

move a disk from A to C

move a disk from A to B

move a disk from C to B

ecs30 Winter 2012 Lecture #17

The Hanoi Towers

Enter how many disks: 3

move a disk from A to B

move a disk from A to C

move a disk from B to C

move a disk from A to B

move a disk from C to A

move a disk from C to B

move a disk from A to B

ecs30 Winter 2012 Lecture #17

The Hanoi Towers

Enter how many disks: 4

move a disk from A to B

move a disk from C to B

move a disk from A to C

move a disk from B to A

move a disk from B to C

move a disk from A to C

move a disk from A to B

move a disk from C to B

move a disk from C to A

move a disk from B to A

move a disk from C to B

move a disk from A to C

move a disk from A to B

move a disk from C to B

ecs30 Winter 2012 Lecture #17

The tower function:

void

tower

(char from_peg, char to_peg, char aux_peg,

int n)

{

…

}

ecs30 Winter 2012 Lecture #17

void

tower

(char from_peg, char to_peg, char aux_peg,

int n)

{

…

}

int

main(void)

{

int n;

scanf(“%d”, &n);

tower(‘A’, ‘B’, ‘C’, n);

}

ecs30 Winter 2012 Lecture #17

The Hanoi Towers

Enter how many disks: 2

move a disk from A to C

move a disk from A to B

move a disk from C to B

ecs30 Winter 2012 Lecture #17

The Hanoi Towers

Enter how many disks: 3

move a disk from A to B

move a disk from A to C

move a disk from B to C

move a disk from A to B

move a disk from C to A

move a disk from C to B

move a disk from A to B

ecs30 Winter 2012 Lecture #17

The Hanoi Towers

Enter how many disks: 4

move a disk from A to B

move a disk from C to B

move a disk from A to C

move a disk from B to A

move a disk from B to C

move a disk from A to C

move a disk from A to B

move a disk from C to B

move a disk from C to A

move a disk from B to A

move a disk from C to B

move a disk from A to C

move a disk from A to B

move a disk from C to B

ecs30 Winter 2012 Lecture #17

void

tower

(char from_peg, char to_peg, char aux_peg,

int n)

{

…

}

ecs30 Winter 2012 Lecture #17

Basis: What is an instance of the problem that is trivial?

® n == 1

Since this base case could occur when the disk is on any needle, we simply output the instruction to move the top disk from src to dest.

ecs30 Winter 2012 Lecture #17

Basis: What is an instance of the problem that is trivial?

® n == 1

Since this base case could occur when the disk is on any needle, we simply output the instruction to move the top disk from src to dest.

ecs30 Winter 2012 Lecture #17

void

tower

(char from_peg, char to_peg, char aux_peg,

int n)

{

if (n == 1)

printf(“move a disk from %c to %c\n”,

from_peg, to peg);

…

}

ecs30 Winter 2012 Lecture #17

Induction Step: n > 1

® How can recursion help us out?

a. Recursively move n-1 disks from src to aux.

ecs30 Winter 2012 Lecture #17

Induction Step: n > 1

® How can recursion help us out?

b. Move the one remaining disk from src to dest.

ecs30 Winter 2012 Lecture #17

Induction Step: n > 1

® How can recursion help us out?

c. Recursively move n-1 disks from aux to dest...

ecs30 Winter 2012 Lecture #17

Induction Step: n > 1

® How can recursion help us out?

d. We’re done!

ecs30 Winter 2012 Lecture #17

void

tower

(char from_peg, char to_peg, char aux_peg,

int n)

{

if (n == 1)

printf(“move a disk from %c to %c\n”,

from_peg, to peg);

…

}

ecs30 Winter 2012 Lecture #17

void

tower

(char from_peg, char to_peg, char aux_peg,

int n)

{

if (n == 1)

printf(“move a disk from %c to %c\n”,

from_peg, to peg);

tower(from_peg, aux_peg, to_peg, n-1);

tower(from_peg, to_peg, aux_peg, 1);

tower(aux_peg, to_peg, from_peg, n-1);

}

ecs30 Winter 2012 Lecture #17

void

tower

(char from_peg, char to_peg, char aux_peg,

int n)

{

if (n == 1)

printf(“move a disk from %c to %c\n”,

from_peg, to peg);

tower(from_peg, aux_peg, to_peg, n-1);

tower(from_peg, to_peg, aux_peg, 1);

tower(aux_peg, to_peg, from_peg, n-1);

}

ecs30 Winter 2012 Lecture #17

void

tower

(char from_peg, char to_peg, char aux_peg,

int n)

{

if (n == 1)

printf(“move a disk from %c to %c\n”,

from_peg, to peg);

tower(from_peg, aux_peg, to_peg, n-1);

tower(from_peg, to_peg, aux_peg, 1);

tower(aux_peg, to_peg, from_peg, n-1);

}

ecs30 Winter 2012 Lecture #17

void

tower

(char from_peg, char to_peg, char aux_peg,

int n)

{

if (n == 1)

printf(“move disk %d from %c to %c\n”,

n, from_peg, to peg);

tower(from_peg, aux_peg, to_peg, n-1);

tower(from_peg, to_peg, aux_peg, 1);

tower(aux_peg, to_peg, from_peg, n-1);

}

ecs30 Winter 2012 Lecture #17

The Hanoi Towers

Enter how many disks: 2

move disk 1 from A to C

move disk 2 from A to B

move disk 1 from C to B

ecs30 Winter 2012 Lecture #17

- Tower of Hanoi with FOUR Pegs

ecs30 Winter 2012 Lecture #17

- Tower of Hanoi with FOUR Pegs

Number of Moves? (needs to be smaller than 3 pegs)

ecs30 Winter 2012 Lecture #17

http://www.exocortex.org/toh/

void

tower4P

(char from_peg, char to_peg,

char aux1_peg, char aux2_peg,

int n)

{

if (n == 1)

printf(“move disk %d from %c to %c\n”,

n, from_peg, to peg);

…

}

ecs30 Winter 2012 Lecture #17