Algorithm 汉诺塔问题迭代算法的时间复杂度和空间复杂度?

Algorithm 汉诺塔问题迭代算法的时间复杂度和空间复杂度?,algorithm,time-complexity,space-complexity,towers-of-hanoi,Algorithm,Time Complexity,Space Complexity,Towers Of Hanoi,我无法使用迭代算法找到河内塔问题的时间复杂度和空间复杂度。 有人能帮忙吗 迭代算法: 计算所需移动的总数,即“pow(2,n)-1”,其中n是磁盘数 如果磁盘数(即n)为偶数,则交换目标 电杆和辅助电杆 对于i=1的移动总数: 如果i%3==1: 顶部磁盘在源极和源极之间的合法移动 终点杆 如果i%3==2: 源极和源极之间顶部磁盘的合法移动 辅助极 如果i%3==0: 辅助极之间顶部圆盘的合法移动 和目的地杆 代码- // C Program for Iterative Tower of Ha

我无法使用迭代算法找到河内塔问题的时间复杂度和空间复杂度。 有人能帮忙吗

迭代算法:

  • 计算所需移动的总数,即“pow(2,n)-1”,其中n是磁盘数
  • 如果磁盘数(即n)为偶数,则交换目标 电杆和辅助电杆
  • 对于i=1的移动总数: 如果i%3==1: 顶部磁盘在源极和源极之间的合法移动 终点杆 如果i%3==2: 源极和源极之间顶部磁盘的合法移动 辅助极
    如果i%3==0: 辅助极之间顶部圆盘的合法移动 和目的地杆
  • 代码-

    // C Program for Iterative Tower of Hanoi
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <limits.h>
    
    // A structure to represent a stack
    struct Stack
    {
    unsigned capacity;
    int top;
    int *array;
    };
    
    // function to create a stack of given capacity.
    struct Stack* createStack(unsigned capacity)
    {
        struct Stack* stack =
            (struct Stack*) malloc(sizeof(struct Stack));
        stack -> capacity = capacity;
        stack -> top = -1;
        stack -> array =
            (int*) malloc(stack -> capacity * sizeof(int));
        return stack;
    }
    
    // Stack is full when top is equal to the last index
    int isFull(struct Stack* stack)
    {
    return (stack->top == stack->capacity - 1);
    }
    
    // Stack is empty when top is equal to -1
    int isEmpty(struct Stack* stack)
    {
    return (stack->top == -1);
    }
    
    // Function to add an item to stack. It increases
    // top by 1
    void push(struct Stack *stack, int item)
    {
        if (isFull(stack))
            return;
        stack -> array[++stack -> top] = item;
    }
    
    // Function to remove an item from stack. It
    // decreases top by 1
    int pop(struct Stack* stack)
    {
        if (isEmpty(stack))
            return INT_MIN;
        return stack -> array[stack -> top--];
    }
    
    //Function to show the movement of disks
    void moveDisk(char fromPeg, char toPeg, int disk)
    {
        printf("Move the disk %d from \'%c\' to \'%c\'\n",
            disk, fromPeg, toPeg);
    }
    
    // Function to implement legal movement between
    // two poles
    void moveDisksBetweenTwoPoles(struct Stack *src,
                struct Stack *dest, char s, char d)
    {
        int pole1TopDisk = pop(src);
        int pole2TopDisk = pop(dest);
    
        // When pole 1 is empty
        if (pole1TopDisk == INT_MIN)
        {
            push(src, pole2TopDisk);
            moveDisk(d, s, pole2TopDisk);
        }
    
        // When pole2 pole is empty
        else if (pole2TopDisk == INT_MIN)
        {
            push(dest, pole1TopDisk);
            moveDisk(s, d, pole1TopDisk);
        }
    
        // When top disk of pole1 > top disk of pole2
        else if (pole1TopDisk > pole2TopDisk)
        {
            push(src, pole1TopDisk);
            push(src, pole2TopDisk);
            moveDisk(d, s, pole2TopDisk);
        }
    
        // When top disk of pole1 < top disk of pole2
        else
        {
            push(dest, pole2TopDisk);
            push(dest, pole1TopDisk);
            moveDisk(s, d, pole1TopDisk);
        }
    }
    
    //Function to implement TOH puzzle
    void tohIterative(int num_of_disks, struct Stack
                *src, struct Stack *aux,
                struct Stack *dest)
    {
        int i, total_num_of_moves;
        char s = 'S', d = 'D', a = 'A';
    
        //If number of disks is even, then interchange
        //destination pole and auxiliary pole
        if (num_of_disks % 2 == 0)
        {
            char temp = d;
            d = a;
            a = temp;
        }
        total_num_of_moves = pow(2, num_of_disks) - 1;
    
        //Larger disks will be pushed first
        for (i = num_of_disks; i >= 1; i--)
            push(src, i);
    
        for (i = 1; i <= total_num_of_moves; i++)
        {
            if (i % 3 == 1)
            moveDisksBetweenTwoPoles(src, dest, s, d);
    
            else if (i % 3 == 2)
            moveDisksBetweenTwoPoles(src, aux, s, a);
    
            else if (i % 3 == 0)
            moveDisksBetweenTwoPoles(aux, dest, a, d);
        }
    }
    
    // Driver Program
    int main()
    {
        // Input: number of disks
        unsigned num_of_disks = 3;
    
        struct Stack *src, *dest, *aux;
    
        // Create three stacks of size 'num_of_disks'
        // to hold the disks
        src = createStack(num_of_disks);
        aux = createStack(num_of_disks);
        dest = createStack(num_of_disks);
    
        tohIterative(num_of_disks, src, aux, dest);
        return 0;
    }
    
    //河内迭代塔的C程序
    #包括
    #包括
    #包括
    #包括
    //表示堆栈的结构
    结构堆栈
    {
    无符号容量;
    int top;
    int*数组;
    };
    //函数创建具有给定容量的堆栈。
    结构堆栈*createStack(未签名容量)
    {
    结构堆栈*堆栈=
    (结构堆栈*)malloc(sizeof(结构堆栈));
    堆栈->容量=容量;
    堆栈->顶部=-1;
    堆栈->数组=
    (int*)malloc(堆栈->容量*sizeof(int));
    返回栈;
    }
    //当top等于最后一个索引时,堆栈已满
    int isFull(结构堆栈*堆栈)
    {
    返回(堆栈->顶部==堆栈->容量-1);
    }
    //当top等于-1时,堆栈为空
    int isEmpty(结构堆栈*堆栈)
    {
    返回(堆栈->顶部==-1);
    }
    //函数将项添加到堆栈中。它增加了
    //以1领先
    无效推送(结构堆栈*堆栈,int项)
    {
    如果(已满(堆栈))
    返回;
    堆栈->数组[++堆栈->顶部]=项;
    }
    //函数从堆栈中删除项。信息技术
    //将顶部减少1
    int-pop(结构堆栈*堆栈)
    {
    if(isEmpty(stack))
    返回INT_MIN;
    返回堆栈->数组[堆栈->顶部--];
    }
    //用于显示磁盘移动的函数
    void moveDisk(char fromPeg、char toPeg、int disk)
    {
    printf(“将磁盘%d从\'%c\'移动到\'%c\'\n”,
    花盘,fromPeg,toPeg);
    }
    //功能,以在
    //两极
    无效两极之间的移动磁盘(结构堆栈*src,
    结构堆栈*dest,char s,char d)
    {
    int pole1TopDisk=pop(src);
    int pole2TopDisk=pop(目的地);
    //当磁极1为空时
    如果(pole1TopDisk==INT_MIN)
    {
    推送(src、pole2TopDisk);
    移动磁盘(d、s、pole2TopDisk);
    }
    //当pole2极为空时
    else if(pole2TopDisk==INT_MIN)
    {
    推送(dest,pole1TopDisk);
    移动磁盘(s、d、pole1TopDisk);
    }
    //当pole1的顶部磁盘>pole2的顶部磁盘时
    否则如果(pole1TopDisk>pole2TopDisk)
    {
    推送(src、pole1TopDisk);
    推送(src、pole2TopDisk);
    移动磁盘(d、s、pole2TopDisk);
    }
    //当pole1的顶盘=1;i--)
    推力(src,i);
    
    对于(i=1;i让我们定义一下,这是否回答了您的问题?是的,谢谢。