Problem solving & C III Arijit Bishnu arijit@isical.ac.in - - PowerPoint PPT Presentation

problem solving c iii
SMART_READER_LITE
LIVE PREVIEW

Problem solving & C III Arijit Bishnu arijit@isical.ac.in - - PowerPoint PPT Presentation

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi Problem solving & C III Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. August 5, 2014 Introduction Fibonacci


slide-1
SLIDE 1

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Problem solving & C – III

Arijit Bishnu arijit@isical.ac.in

Indian Statistical Institute, India.

August 5, 2014

slide-2
SLIDE 2

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Outline

1 Introduction 2 Fibonacci sequence 3 Printing link list backwards 4 Space Filling Curve 5 Towers of Hanoi

slide-3
SLIDE 3

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Outline

1 Introduction 2 Fibonacci sequence 3 Printing link list backwards 4 Space Filling Curve 5 Towers of Hanoi

slide-4
SLIDE 4

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Recursion

What is recursion?

slide-5
SLIDE 5

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Recursion

What is recursion? A function calling itself with certain properties.

slide-6
SLIDE 6

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Recursion

What is recursion? A function calling itself with certain properties. There must be a base criteria for which the function does not call itself.

slide-7
SLIDE 7

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Recursion

What is recursion? A function calling itself with certain properties. There must be a base criteria for which the function does not call itself. After each call, the function must be closer to the base criteria.

slide-8
SLIDE 8

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Recursion

What is recursion? A function calling itself with certain properties. There must be a base criteria for which the function does not call itself. After each call, the function must be closer to the base criteria. An Example (Can you recognize the expression?) f (n) = n ∗ f (n − 1) if n ≥ 1 = 1 if n = 0

slide-9
SLIDE 9

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Outline

1 Introduction 2 Fibonacci sequence 3 Printing link list backwards 4 Space Filling Curve 5 Towers of Hanoi

slide-10
SLIDE 10

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Fibonacci sequence

The Definition fib(n) = fib(n − 1) + fib(n − 2) if n ≥ 2 = 1 if n = 1 = 1 if n = 0

slide-11
SLIDE 11

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Fibonacci sequence

The Definition fib(n) = fib(n − 1) + fib(n − 2) if n ≥ 2 = 1 if n = 1 = 1 if n = 0 Some values fib(2) = 2; fib(3) = 3; fib(4) = 5; fib(5) = 8; fib(6) = 13; fib(7) = 21;fib(8) = 34; fib(9) = 55; fib(10) = 89; fib(11) = 144;

slide-12
SLIDE 12

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

A recursive implementation

#include<stdio.h>

slide-13
SLIDE 13

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

A recursive implementation

#include<stdio.h> int main(void){ unsigned int m,val,i; printf("\n What is the value of m::> "); scanf("%d",&m); val = f(m); printf("\n Fibonacci(%d) = %d \n",m,val); return 0; }

slide-14
SLIDE 14

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

A recursive implementation

#include<stdio.h> unsigned int f(unsigned int num) { } int main(void){ unsigned int m,val,i; printf("\n What is the value of m::> "); scanf("%d",&m); val = f(m); printf("\n Fibonacci(%d) = %d \n",m,val); return 0; }

slide-15
SLIDE 15

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

A recursive implementation

#include<stdio.h> unsigned int f(unsigned int num) { if(num==0 || num==1) return 1; } int main(void){ unsigned int m,val,i; printf("\n What is the value of m::> "); scanf("%d",&m); val = f(m); printf("\n Fibonacci(%d) = %d \n",m,val); return 0; }

slide-16
SLIDE 16

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

A recursive implementation

#include<stdio.h> unsigned int f(unsigned int num) { if(num==0 || num==1) return 1; else return(f(num-1)+f(num-2)); } int main(void){ unsigned int m,val,i; printf("\n What is the value of m::> "); scanf("%d",&m); val = f(m); printf("\n Fibonacci(%d) = %d \n",m,val); return 0; }

slide-17
SLIDE 17

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursion tree

slide-18
SLIDE 18

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursion tree

f(5) f(3) f(4) f(1) f(2) f(2) f(3) f(0) f(1) f(0) f(1) f(1) f(2) f(0) f(1)

slide-19
SLIDE 19

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursion tree

f(5) f(3) f(4) f(1) f(2) f(2) f(3) f(0) f(1) f(0) f(1) f(1) f(2) f(0) f(1) How many function calls? f (5) : 1, f (4) : 1, f (3) : 2, f (2) : 3, f (1) : 5, f (0) : 3 Exercise: Find it experimentally and analytically.

slide-20
SLIDE 20

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

An iterative implementation

#include<stdio.h> int main(void){ unsigned int m,i,f,f_1=1,f_2=1; printf("\n What is the value of n::> "); scanf("%d",&m); printf("\n Fibonacci(%d) = %d \n",m,f); return 0; }

slide-21
SLIDE 21

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

An iterative implementation

#include<stdio.h> int main(void){ unsigned int m,i,f,f_1=1,f_2=1; printf("\n What is the value of n::> "); scanf("%d",&m); if(m==0 || m==1){ printf("\n Fibonacci(%d) = %d",m,1); return 0; } printf("\n Fibonacci(%d) = %d \n",m,f); return 0; }

slide-22
SLIDE 22

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

An iterative implementation

#include<stdio.h> int main(void){ unsigned int m,i,f,f_1=1,f_2=1; printf("\n What is the value of n::> "); scanf("%d",&m); if(m==0 || m==1){ printf("\n Fibonacci(%d) = %d",m,1); return 0; } else { } printf("\n Fibonacci(%d) = %d \n",m,f); return 0; }

slide-23
SLIDE 23

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

An iterative implementation

#include<stdio.h> int main(void){ unsigned int m,i,f,f_1=1,f_2=1; printf("\n What is the value of n::> "); scanf("%d",&m); if(m==0 || m==1){ printf("\n Fibonacci(%d) = %d",m,1); return 0; } else { for(i=2;i<=m;i++){ f = f_1+f_2; f_1=f_2, f_2=f; } } printf("\n Fibonacci(%d) = %d \n",m,f); return 0; }

slide-24
SLIDE 24

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

An iterative implementation

#include<stdio.h> int main(void){ unsigned int m,i,f,f_1=1,f_2=1; printf("\n What is the value of n::> "); scanf("%d",&m); if(m==0 || m==1){ printf("\n Fibonacci(%d) = %d",m,1); return 0; } else { for(i=2;i<=m;i++){ f = f_1+f_2; f_1=f_2, f_2=f; } } printf("\n Fibonacci(%d) = %d \n",m,f); return 0; }

Exercise: Compare running times of recursive and iterative versions.

slide-25
SLIDE 25

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Outline

1 Introduction 2 Fibonacci sequence 3 Printing link list backwards 4 Space Filling Curve 5 Towers of Hanoi

slide-26
SLIDE 26

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Printing linked list backwards

The Problem You have to create a singly linked list and then print it backwards without doing any changes on the links.

slide-27
SLIDE 27

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Printing linked list backwards

The Problem You have to create a singly linked list and then print it backwards without doing any changes on the links.

NULL head a s p l y

s p l a y

slide-28
SLIDE 28

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Printing linked list backwards

The Problem You have to create a singly linked list and then print it backwards without doing any changes on the links.

NULL head a s p l y

s p l a y

slide-29
SLIDE 29

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Printing linked list backwards

The Problem You have to create a singly linked list and then print it backwards without doing any changes on the links.

NULL head a s p l y

s p l a y Use recursion.

slide-30
SLIDE 30

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Printing linked list backwards using recursion

void PrintArray(data *head) { return; }

slide-31
SLIDE 31

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Printing linked list backwards using recursion

void PrintArray(data *head) { /* calling function recursively */ return; }

slide-32
SLIDE 32

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Printing linked list backwards using recursion

void PrintArray(data *head) { /*boundary condition to stop recursion*/ /* calling function recursively */ return; }

slide-33
SLIDE 33

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Printing linked list backwards using recursion

void PrintArray(data *head) { /*boundary condition to stop recursion*/ /* calling function recursively */ /* Printing current element*/ return; }

slide-34
SLIDE 34

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Printing linked list backwards using recursion

void PrintArray(data *head) { /*boundary condition to stop recursion*/ /* calling function recursively */ PrintArray(head -> next); /* Printing current element*/ return; }

slide-35
SLIDE 35

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Printing linked list backwards using recursion

void PrintArray(data *head) { /*boundary condition to stop recursion*/ if(head -> next == NULL){ printf(" %d->",head -> x); return; } /* calling function recursively */ PrintArray(head -> next); /* Printing current element*/ return; }

slide-36
SLIDE 36

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Printing linked list backwards using recursion

void PrintArray(data *head) { /*boundary condition to stop recursion*/ if(head -> next == NULL){ printf(" %d->",head -> x); return; } /* calling function recursively */ PrintArray(head -> next); /* Printing current element*/ printf(" %d ->",head -> x); return; }

slide-37
SLIDE 37

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Outline

1 Introduction 2 Fibonacci sequence 3 Printing link list backwards 4 Space Filling Curve 5 Towers of Hanoi

slide-38
SLIDE 38

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Space Filling Curve

slide-39
SLIDE 39

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Space Filling Curve

Z Curve order: 1 2 5 6 3 4 7 8 9 10 13 14 11 12 15 16

slide-40
SLIDE 40

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Space Filling Curve

Z Curve order: 1 2 5 6 3 4 7 8 9 10 13 14 11 12 15 16 Problem

slide-41
SLIDE 41

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Space Filling Curve

Z Curve order: 1 2 5 6 3 4 7 8 9 10 13 14 11 12 15 16 Problem Allocate a 2-D matrix of size 2m × 2m.

slide-42
SLIDE 42

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Space Filling Curve

Z Curve order: 1 2 5 6 3 4 7 8 9 10 13 14 11 12 15 16 Problem Allocate a 2-D matrix of size 2m × 2m. Fill it with integers 1, . . . , 22m in a row-major fashion.

slide-43
SLIDE 43

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Space Filling Curve

Z Curve order: 1 2 5 6 3 4 7 8 9 10 13 14 11 12 15 16 Problem Allocate a 2-D matrix of size 2m × 2m. Fill it with integers 1, . . . , 22m in a row-major fashion. Traverse the matrix in a z-curve order.

slide-44
SLIDE 44

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The main program

int main(void){ int m,N, i,j,k=1,**matrix; printf(" \n What is the value of m::>"); scanf("%d",&m); N=(int)(pow(2.0,m)); matrix = allocate2D(N,N); }

slide-45
SLIDE 45

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The main program

int main(void){ int m,N, i,j,k=1,**matrix; printf(" \n What is the value of m::>"); scanf("%d",&m); N=(int)(pow(2.0,m)); matrix = allocate2D(N,N); /*Fill up and print the matrix */ for(i=0;i<N;i++) for(j=0;j<N;j++){matrix[i][j]=k; k++;} for(i=0;i<N;i++){ printf("\n"); for(j=0;j<N;j++) printf("%d \t",matrix[i][j]); } }

slide-46
SLIDE 46

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The main program

int main(void){ int m,N, i,j,k=1,**matrix; printf(" \n What is the value of m::>"); scanf("%d",&m); N=(int)(pow(2.0,m)); matrix = allocate2D(N,N); /*Fill up and print the matrix */ for(i=0;i<N;i++) for(j=0;j<N;j++){matrix[i][j]=k; k++;} for(i=0;i<N;i++){ printf("\n"); for(j=0;j<N;j++) printf("%d \t",matrix[i][j]); } printf("\n \nReading the matrix in z-curve fashion::>"); zee(0,N-1,0,N-1,matrix); printf("\n"); return 0; }

slide-47
SLIDE 47

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Allocating the 2-D matrix

int** allocate2D(int row,int col){ int **S,k; S=(int **)calloc(row,sizeof(int *)); if(S==NULL){printf("\n No space \n"); exit(0);} for(k=0;k<row;k++){ S[k]=(int *)calloc(col,sizeof(int)); if(S[k]==NULL){ printf("\n No space \n"); exit(0);} } return S; }

slide-48
SLIDE 48

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursive function

slide-49
SLIDE 49

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursive function

void zee(int r_b,int r_e,int c_b,int c_e, int **matrix){ if((r_b==r_e)&&(c_b==c_e)){ printf(" %2d ",matrix[r_b][c_b]); return; } return; }

slide-50
SLIDE 50

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursive function

void zee(int r_b,int r_e,int c_b,int c_e, int **matrix){ if((r_b==r_e)&&(c_b==c_e)){ printf(" %2d ",matrix[r_b][c_b]); return; } zee( ); return; }

slide-51
SLIDE 51

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursive function

void zee(int r_b,int r_e,int c_b,int c_e, int **matrix){ if((r_b==r_e)&&(c_b==c_e)){ printf(" %2d ",matrix[r_b][c_b]); return; } zee( ); zee( ); return; }

slide-52
SLIDE 52

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursive function

void zee(int r_b,int r_e,int c_b,int c_e, int **matrix){ if((r_b==r_e)&&(c_b==c_e)){ printf(" %2d ",matrix[r_b][c_b]); return; } zee( ); zee( ); zee( ); return; }

slide-53
SLIDE 53

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursive function

void zee(int r_b,int r_e,int c_b,int c_e, int **matrix){ if((r_b==r_e)&&(c_b==c_e)){ printf(" %2d ",matrix[r_b][c_b]); return; } zee( ); zee( ); zee( ); zee( ); return; }

slide-54
SLIDE 54

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursive function

void zee(int r_b,int r_e,int c_b,int c_e, int **matrix){ if((r_b==r_e)&&(c_b==c_e)){ printf(" %2d ",matrix[r_b][c_b]); return; } zee(r_b,(r_e+r_b)/2,c_b,(c_e+c_b)/2, matrix); zee( ); zee( ); zee( ); return; }

slide-55
SLIDE 55

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursive function

void zee(int r_b,int r_e,int c_b,int c_e, int **matrix){ if((r_b==r_e)&&(c_b==c_e)){ printf(" %2d ",matrix[r_b][c_b]); return; } zee(r_b,(r_e+r_b)/2,c_b,(c_e+c_b)/2, matrix); zee(r_b,(r_e+r_b)/2,(c_e+c_b)/2+1,c_e, matrix); zee( ); zee( ); return; }

slide-56
SLIDE 56

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursive function

void zee(int r_b,int r_e,int c_b,int c_e, int **matrix){ if((r_b==r_e)&&(c_b==c_e)){ printf(" %2d ",matrix[r_b][c_b]); return; } zee(r_b,(r_e+r_b)/2,c_b,(c_e+c_b)/2, matrix); zee(r_b,(r_e+r_b)/2,(c_e+c_b)/2+1,c_e, matrix); zee((r_e+r_b)/2+1,r_e,c_b,(c_e+c_b)/2, matrix); zee( ); return; }

slide-57
SLIDE 57

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursive function

void zee(int r_b,int r_e,int c_b,int c_e, int **matrix){ if((r_b==r_e)&&(c_b==c_e)){ printf(" %2d ",matrix[r_b][c_b]); return; } zee(r_b,(r_e+r_b)/2,c_b,(c_e+c_b)/2, matrix); zee(r_b,(r_e+r_b)/2,(c_e+c_b)/2+1,c_e, matrix); zee((r_e+r_b)/2+1,r_e,c_b,(c_e+c_b)/2, matrix); zee((r_e+r_b)/2+1,r_e,(c_e+c_b)/2+1,c_e, matrix)); return; }

slide-58
SLIDE 58

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Outline

1 Introduction 2 Fibonacci sequence 3 Printing link list backwards 4 Space Filling Curve 5 Towers of Hanoi

slide-59
SLIDE 59

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Towers of Hanoi

The Problem

slide-60
SLIDE 60

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Towers of Hanoi

The Problem Three pegs A, B and C are given and on peg A there are n disks with decreasing size.

slide-61
SLIDE 61

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Towers of Hanoi

The Problem Three pegs A, B and C are given and on peg A there are n disks with decreasing size. We have to move disks from peg A to peg C using peg B as an auxiliary peg following some rules.

slide-62
SLIDE 62

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Towers of Hanoi

The Problem Three pegs A, B and C are given and on peg A there are n disks with decreasing size. We have to move disks from peg A to peg C using peg B as an auxiliary peg following some rules.

The top disk on any peg can be moved to any other peg.

slide-63
SLIDE 63

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Towers of Hanoi

The Problem Three pegs A, B and C are given and on peg A there are n disks with decreasing size. We have to move disks from peg A to peg C using peg B as an auxiliary peg following some rules.

The top disk on any peg can be moved to any other peg. A larger disk can never be placed on a smaller disk.

slide-64
SLIDE 64

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Towers of Hanoi

The Problem Three pegs A, B and C are given and on peg A there are n disks with decreasing size. We have to move disks from peg A to peg C using peg B as an auxiliary peg following some rules.

The top disk on any peg can be moved to any other peg. A larger disk can never be placed on a smaller disk.

Exercise Try with n = 3, 4, . . . and start thinking recursively.

slide-65
SLIDE 65

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Towers of Hanoi

Peg A Peg B Peg C

slide-66
SLIDE 66

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Towers of Hanoi

Peg A Peg B Peg C

1 2 3

slide-67
SLIDE 67

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Towers of Hanoi

Peg A Peg B Peg C

1 2 3

slide-68
SLIDE 68

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Towers of Hanoi

Peg A Peg B Peg C

1 2 3

slide-69
SLIDE 69

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Towers of Hanoi

Peg A Peg B Peg C

1 2 3

slide-70
SLIDE 70

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Towers of Hanoi

Peg A Peg B Peg C

1 2 3

slide-71
SLIDE 71

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Towers of Hanoi

Peg A Peg B Peg C

1 2 3

slide-72
SLIDE 72

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Towers of Hanoi

Peg A Peg B Peg C

1 2 3

slide-73
SLIDE 73

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

Towers of Hanoi

Peg A Peg B Peg C

1 2 3

slide-74
SLIDE 74

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The algorithm

The recursive method

slide-75
SLIDE 75

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The algorithm

The recursive method Move the top n − 1 disks from peg A to peg B using peg C as auxiliary.

slide-76
SLIDE 76

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The algorithm

The recursive method Move the top n − 1 disks from peg A to peg B using peg C as auxiliary. Move the top disk from peg A to peg C.

slide-77
SLIDE 77

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The algorithm

The recursive method Move the top n − 1 disks from peg A to peg B using peg C as auxiliary. Move the top disk from peg A to peg C. Move the top n − 1 disks from peg B to peg C using peg A as auxiliary.

slide-78
SLIDE 78

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The main program

int main(void) { int n; printf("Enter the number of disks : "); scanf("%d",&n); printf("The Tower of Hanoi involves the moves :\n \n"); return 0; }

slide-79
SLIDE 79

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The main program

int main(void) { int n; printf("Enter the number of disks : "); scanf("%d",&n); printf("The Tower of Hanoi involves the moves :\n \n"); towers(n,’A’,’C’,’B’); return 0; }

slide-80
SLIDE 80

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The main program

int main(void) { int n; printf("Enter the number of disks : "); scanf("%d",&n); printf("The Tower of Hanoi involves the moves :\n \n"); towers(n,’A’,’C’,’B’); printf("\n \n"); return 0; }

slide-81
SLIDE 81

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursive function

slide-82
SLIDE 82

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursive function

void towers(int n,char frompeg,char topeg,char auxpeg){ }

slide-83
SLIDE 83

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursive function

void towers(int n,char frompeg,char topeg,char auxpeg){ /* If only 1 disk, make the move and return */ }

slide-84
SLIDE 84

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursive function

void towers(int n,char frompeg,char topeg,char auxpeg){ /* If only 1 disk, make the move and return */ /* Move top n-1 disks from A to B, using C as auxiliary */ }

slide-85
SLIDE 85

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursive function

void towers(int n,char frompeg,char topeg,char auxpeg){ /* If only 1 disk, make the move and return */ /* Move top n-1 disks from A to B, using C as auxiliary */ /* Move remaining disks from A to C*/ }

slide-86
SLIDE 86

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursive function

void towers(int n,char frompeg,char topeg,char auxpeg){ /* If only 1 disk, make the move and return */ /* Move top n-1 disks from A to B, using C as auxiliary */ /* Move remaining disks from A to C*/ /* Move n-1 disks from B to C using A as auxiliary */ }

slide-87
SLIDE 87

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursive function

void towers(int n,char frompeg,char topeg,char auxpeg){ /* If only 1 disk, make the move and return */ if(n==1) { } /* Move top n-1 disks from A to B, using C as auxiliary */ /* Move remaining disks from A to C*/ /* Move n-1 disks from B to C using A as auxiliary */ }

slide-88
SLIDE 88

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursive function

void towers(int n,char frompeg,char topeg,char auxpeg){ /* If only 1 disk, make the move and return */ if(n==1) { printf("\n Move disk 1 from peg %c to peg %c", frompeg,topeg); return; } /* Move top n-1 disks from A to B, using C as auxiliary */ /* Move remaining disks from A to C*/ /* Move n-1 disks from B to C using A as auxiliary */ }

slide-89
SLIDE 89

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursive function

void towers(int n,char frompeg,char topeg,char auxpeg){ /* If only 1 disk, make the move and return */ if(n==1) { printf("\n Move disk 1 from peg %c to peg %c", frompeg,topeg); return; } /* Move top n-1 disks from A to B, using C as auxiliary */ towers(n-1,frompeg,auxpeg,topeg); /* Move remaining disks from A to C*/ /* Move n-1 disks from B to C using A as auxiliary */ }

slide-90
SLIDE 90

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursive function

void towers(int n,char frompeg,char topeg,char auxpeg){ /* If only 1 disk, make the move and return */ if(n==1) { printf("\n Move disk 1 from peg %c to peg %c", frompeg,topeg); return; } /* Move top n-1 disks from A to B, using C as auxiliary */ towers(n-1,frompeg,auxpeg,topeg); /* Move remaining disks from A to C*/ printf("\n Move disk %d from peg %c to peg %c", n,frompeg,topeg); /* Move n-1 disks from B to C using A as auxiliary */ }

slide-91
SLIDE 91

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The recursive function

void towers(int n,char frompeg,char topeg,char auxpeg){ /* If only 1 disk, make the move and return */ if(n==1) { printf("\n Move disk 1 from peg %c to peg %c", frompeg,topeg); return; } /* Move top n-1 disks from A to B, using C as auxiliary */ towers(n-1,frompeg,auxpeg,topeg); /* Move remaining disks from A to C*/ printf("\n Move disk %d from peg %c to peg %c", n,frompeg,topeg); /* Move n-1 disks from B to C using A as auxiliary */ towers(n-1,auxpeg,topeg,frompeg); }

slide-92
SLIDE 92

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The Last One Questions???

slide-93
SLIDE 93

Introduction Fibonacci sequence Printing link list backwards Space Filling Curve Towers of Hanoi

The Last One Questions??? Thanks!!!