Ecs30 winter 2012 programming and problem solving 16 chapters 9 10 strings to recurrsion
This presentation is the property of its rightful owner.
Sponsored Links
1 / 46

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


  • 64 Views
  • Uploaded on
  • Presentation posted in: General

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/. HW#6.1.

Download Presentation

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

An Image/Link below is provided (as is) to download presentation

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

Presentation Transcript


Ecs30 winter 2012 programming and problem solving 16 chapters 9 10 strings to recurrsion

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


Hw 6 1

HW#6.1

  • 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


Ecs30 winter 2012 programming and problem solving 16 chapters 9 10 strings to recurrsion

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


Recursion

Recursion

  • 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


Ecs30 winter 2012 programming and problem solving 16 chapters 9 10 strings to recurrsion

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

ecs30 Winter 2012 Lecture #17


Ecs30 winter 2012 programming and problem solving 16 chapters 9 10 strings to recurrsion

ecs30 Winter 2012 Lecture #17


Ecs30 winter 2012 programming and problem solving 16 chapters 9 10 strings to recurrsion

It gotta stop… at some point!!

Exit Strategy/Condition!

ecs30 Winter 2012 Lecture #17


Ecs30 winter 2012 programming and problem solving 16 chapters 9 10 strings to recurrsion

ecs30 Winter 2012 Lecture #17


Ecs30 winter 2012 programming and problem solving 16 chapters 9 10 strings to recurrsion

ecs30 Winter 2012 Lecture #17


The towers of hanoi

The Towers of Hanoi

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

ecs30 Winter 2012 Lecture #17


A legend

A Legend

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


A legend1

A Legend

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


To illustrate

To Illustrate

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


Example

Example

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


Example1

Example

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


Example2

Example

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

ecs30 Winter 2012 Lecture #17


Example3

Example

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 C to A.

ecs30 Winter 2012 Lecture #17


Example4

Example

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 C to B.

ecs30 Winter 2012 Lecture #17


Example5

Example

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

ecs30 Winter 2012 Lecture #17


Example6

Example

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

and we’re done!

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

ecs30 Winter 2012 Lecture #17


Our problem

Our Problem

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


Testing

Testing

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


Testing1

Testing

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


Testing2

Testing

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


Analysis

Analysis

The tower function:

void

tower

(char from_peg, char to_peg, char aux_peg,

int n)

{

}

ecs30 Winter 2012 Lecture #17


Ecs30 winter 2012 programming and problem solving 16 chapters 9 10 strings to recurrsion

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


Testing3

Testing

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


Testing4

Testing

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


Testing5

Testing

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 figure 10 24

The Tower FunctionFigure 10.24

void

tower

(char from_peg, char to_peg, char aux_peg,

int n)

{

}

ecs30 Winter 2012 Lecture #17


Design exit condition

Design: “Exit Condition”

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


Design

Design

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


The tower function figure 10 241

The Tower FunctionFigure 10.24

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


Design ct d

Design (Ct’d)

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


Design ct d1

Design (Ct’d)

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


Design ct d2

Design (Ct’d)

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


Design ct d3

Design (Ct’d)

Induction Step: n > 1

® How can recursion help us out?

d. We’re done!

ecs30 Winter 2012 Lecture #17


The tower function figure 10 242

The Tower FunctionFigure 10.24

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


The tower function figure 10 243

The Tower FunctionFigure 10.24

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


The tower function figure 10 244

The Tower FunctionFigure 10.24

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


The tower function figure 10 245

The Tower FunctionFigure 10.24

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


The tower function figure 10 246

The Tower FunctionFigure 10.24

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


Testing6

Testing

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


Ecs30 winter 2012 programming and problem solving 16 chapters 9 10 strings to recurrsion

HW#7

  • Tower of Hanoi with FOUR Pegs

ecs30 Winter 2012 Lecture #17


Ecs30 winter 2012 programming and problem solving 16 chapters 9 10 strings to recurrsion

HW#7

  • Tower of Hanoi with FOUR Pegs

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

ecs30 Winter 2012 Lecture #17


Ecs30 winter 2012 programming and problem solving 16 chapters 9 10 strings to recurrsion

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


  • Login