2021-08-05算法学习(算法笔记319-327 439-442)

一、二叉平衡树

#include <bits/stdc++.h>
using namespace std;

typedef struct Node *Bintree;
struct Node
{
    int v, height; //v为结点权值
    Bintree left, right;
};

Bintree newNode(int v)
{
    Bintree node = (Bintree)malloc(sizeof(struct Node));
    node->v = v;
    node->height = 1;
    node->left = NULL;
    node->right = NULL;
    return node;
}

int getHeight(Bintree root)
{
    if (root == NULL)
        return 0;
    else
        return root->height;
}

int getBalanceFactor(Bintree root)
{
    return getHeight(root->left) - getHeight(root->right);
}

void updateHeight(Bintree root)
{
    root->height = max(getHeight(root->left), getHeight(root->right))+1;
}

//平衡二叉树的基本操作
//1. 查找操作 对于平衡二叉树而言,查找操作的复杂度为O(log n) 是二叉查找树
//找到并打印

void find(Bintree root, int x)
{
    if (root == NULL)
        return;
    else
    {
        if (root->v == x)
            printf("%d", root->v);
        else if(x<root->v)
            find(root->left,x);
        else find(root->right,x);
    }
}

void L(Bintree &root)

{
    Bintree temp=root->right;
    root->right=temp->left;
    temp->left=root;
    updateHeight(root);
    updateHeight(temp);
    root=temp;
}

void R(Bintree &root)
{
    Bintree temp=root->left;
    root->left=temp->right;
    temp->right=root;
    updateHeight(root);
    updateHeight(temp);
    root=temp;
}

void insert(Bintree &root,int v)
{
    if(root==NULL)
    {
        root=newNode(v);
        return;
    }
    if(v<root->v)
    {
        insert(root->left,v);
        updateHeight(root);
        if(getBalanceFactor(root)==2)
        {
            if(getBalanceFactor(root->left)==1) //LL
            {
                R(root);
            }
            else if(getBalanceFactor(root->left)==-1) //LR
            {
                L(root->left);
                R(root);
            }
        }
    }
    else if(v>root->v)
    {
        insert(root->right,v);
        updateHeight(root);
        if(getBalanceFactor(root)==-2)
        {
            if(getBalanceFactor(root->right)==-1)//RR
            {
                L(root);
            }
            else if(getBalanceFactor(root->right)==1)//RL
            {
                R(root->right);
                L(root);
            }
        }

    }
}

Bintree CreateAVL(int data[],int n)
{
    Bintree root=NULL;
    for(int i=0;i<n;i++)
    {
        insert(root,data[i]);
    }
    return root;
}

二、DAG最长路(DP+递归)

令dp[i]表示从i号顶点出发能获得的最长路径长度,这样所有dp[i]的最大值就是整个DAG的最长路径长度

递归法

#include<bits/stdc++.h>
using namespace std;
const int maxn=100;
const int INF=1000000;
int n;
int G[maxn][maxn];
int dp[maxn]; //主函数中初始化为0
int choice[maxn];//初始化为-1

//求DAG中最长路径长度并输出路径(字典序小优先)
int DP(int i)
{
    if(dp[i]>0) return dp[i];
    for(int j=0;j<n;j++)
    {
        if(G[i][j]!=INF)
        {
            int temp=DP(j)+G[i][j];// DP(j)返回过来的就是计算好的dp[j]
            if(temp>dp[i])
            {
                dp[i]=temp;
                choice[i]=j;
            }
        }
    }

    return dp[i];

}

//调用printPath之前需要先得到最大的dp[i],然后将i作为路径起点传入

void printPath(int i)
{
    printf("%d",i);
    while(choice[i]!=-1)
    {
        i=choice[i];
        printf("->%d",i);
    }
}

//固定终点,求DAG的最长路径长度
bool vis[maxn];
int DP(int i)
{
    if(vis[i]) return dp[i];
    vis[i]=true;
    for(int j=0;j<n;j++)
    {
        if(G[i][j]!=INF)
        {
            dp[i]=max(dp[i],DP(j)+G[i][j]);
        }
    }

    return dp[i];
}
内容概要:本文详细介绍了利用Norrbin/Fossen模型和反步法(PID)控制算法实现无人船(USV)路径跟踪的方法,特别是在存在洋流扰动的情况下。首先,文章解释了Norrbin模型用于描述船舶低频运动特性的优势,并展示了如何将洋流速度从惯性坐标系转换到船体坐标系。接着,讨论了LOS制导算法的改进,使其能够适应洋流影响。然后,阐述了反步法控制律的设计及其与PID控制的结合,强调了虚拟控制量的设计和参数调整的重要性。最后,通过Matlab/Simulink进行建模和仿真实验,验证了该方法的有效性,并提供了详细的代码实现和可视化结果。 适合人群:从事无人船控制系统研究和开发的技术人员,尤其是对路径跟踪和海洋环境扰动补偿感兴趣的工程师和研究人员。 使用场景及目标:适用于需要提高无人船在复杂海况下路径跟踪精度的应用场合,如海洋测绘、环境监测等。目标是通过引入先进的控制算法,减少洋流等外界因素对路径跟踪性能的影响,从而提高系统的鲁棒性和可靠性。 其他说明:文中提供的代码片段和仿真结果有助于读者理解和复现实验过程。同时,作者分享了许多实践经验,如参数调整技巧和常见错误避免,对于初学者非常有帮助。此外,文章还提出了未来的研究方向,如加入自适应观测器以进一步改善抗扰动性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值