Divide and conquer algorithm experiment: Topic 3 Programming of the Tower of Hanoi problem

Table of contents

foreword

1. Experimental content

2. The purpose of the experiment

3. Experimental steps

4. Experimental process

1. Algorithm analysis

2. Write pseudocode

3. Code implementation

4. Use case test

 5. Analysis complexity

Summarize


foreword

The Tower of Hanoi problem is a classic recursive problem, which can be solved by divide and conquer algorithm. The divide-and-conquer algorithm is an algorithm idea that decomposes a complex problem into smaller sub-problems, then solves the sub-problems one by one, and finally merges the solutions of the sub-problems. The purpose of this experiment is to master the basic principles and application methods of the divide-and-conquer algorithm, as well as the writing and calling skills of recursive functions through the programming of the Tower of Hanoi problem. This experiment requires using a high-level language to write a program for the Tower of Hanoi problem, and output the moving steps and the total number of steps. At the same time, it is required to analyze the time complexity and space complexity of the program under different disk numbers, and compare it with the non-recursive method.


1. Experimental content

There are n gold discs sorted by size on A. With the help of B, move the discs on A to C. During the moving process, the order of size must be strictly followed, and the discs cannot be placed on top of smaller ones. , output the result, a text description is required for the output, choose a language to write a program to realize the above algorithm, and analyze the complexity of the algorithm to get the result, a text description is required for the output, choose a language to write a program to realize the above algorithm, and analyze algorithmic complexity

2. The purpose of the experiment

(1) Master the concepts of recursion;

(2) Master the specific solution process of the Tower of Hanoi problem;

(3) On the basis of mastery, program to realize the specific details of the Tower of Hanoi

3. Experimental steps

  1. According to the experimental content, the pseudo code of the algorithm is designed to describe the algorithm;

  2. Use C++/C/Java and other programming languages ​​to realize the engineering pseudo-code of the algorithm;

  3. Enter test cases to verify the algorithm;

  4. List the algorithm time complexity model and compare and analyze it with the computer running statistics time.

4. Experimental process

1. Algorithm analysis

The Tower of Hanoi problem is a classic recursive problem. Its goal is to move a group of discs of different sizes from one column to another. Only one disc can be moved at a time, and the large disc cannot be placed on the small disc. disc top. The solution to the Tower of Hanoi problem can be described in the following steps:

1. If there is only one disc, move it directly from the original post to the target post.
2. If there are multiple discs, first move all discs except the largest disc from the original pillar to the auxiliary pillar. At this time, the original pillar can be regarded as the target pillar, the auxiliary pillar can be regarded as the original pillar, and the target pillar can be regarded as Make an auxiliary column, and then perform this step recursively.
3. Move the largest disc from the original post to the target post.
4. Then move all the discs on the auxiliary pillar from the auxiliary pillar to the target pillar. At this time, the auxiliary pillar can be regarded as the original pillar, the target pillar is regarded as the auxiliary pillar, and the original pillar is regarded as the target pillar, and then this step is performed recursively .

2. Write pseudocode

hanoi(n, A, B, C) {
    if (n == 1) {
        printf("%c -> %c\n", A, C);
    } else {
        hanoi(n - 1, A, C, B);
        printf("%c -> %c\n", A, C);
        hanoi(n - 1, B, A, C);
    }
}

3. Code implementation

#include<stdio.h>
int count=0;
void hanoi(int n,char A,char B,char C){
	 
	if(n==1){
		count+=1;
		printf("%c->%c\n",A,C);
	}else {
		hanoi(n-1,A,C,B);
		count+=1;
		printf("%c->%c\n",A,C);
		hanoi(n-1,B,A,C);
	}
}

int main(){
	int n;
	printf("请输入有多少个金碟:");
	scanf("%d",&n);
	printf("有%d个金碟",n);
	hanoi(n,'A','B','C');
	printf("移动次数为:%d次\n",count);
	return 0;
}

4. Use case test

 5. Analysis complexity

The complexity of the Towers of Hanoi algorithm depends on the number of plates. For n plates, at least 2n-1 steps are required to complete the move [^1^][4]. The time complexity of this algorithm is O(2n). If recursion is used to solve the Tower of Hanoi problem, when the number of plates is set to n, T(n) steps are required, and it takes T(n-1) steps to move n-1 plates from column A to column B, and column A is the last A plate is moved to the C pillar one step, and n-1 plates on the B pillar are moved to the C pillar for T(n-1) steps. Get the recursive formula T(n)=2T(n-1)+1. If it is a four-pillar Tower of Hanoi problem, the complexity of the algorithm is F(n)=O(sqrt(2*n)2^sqrt(2n)).


Summarize

This article is a programming experiment for the Tower of Hanoi problem. The experiment requires mastering the relevant concepts of recursion, the specific solution process of the Tower of Hanoi problem, and the specific realization process of the Tower of Hanoi by programming on the basis of the mastery. The specific steps include: design the algorithm pseudo-code according to the experimental content to describe the algorithm; use C++/C/Java and other programming languages ​​to implement the algorithm pseudo-code engineering; input test cases to verify the algorithm; list the algorithm time complexity model and compare with it Computer running statistics time for comparative analysis.

Guess you like

Origin blog.csdn.net/m0_72471315/article/details/129973383