用递归的方法找到图中两个顶点之间的最短路径和最短距离

前情

学习数据结构都会学习到图,会求两个点之间的最短距离和最短路径。这时候你会学到迪杰斯特拉 (dijkstra)和弗洛伊德算法(Floyd)。学到这个的时候,我很难理解。然后我试着用更简单递归的方法写一个求两个顶点之间的最短距离和最短路径。

推演

我选用的图的结构如下图

用邻接矩阵表示如下图代码

char graph[5][5] = {
        {0,1,-1,4,2},
        {1,0,8,3,3},
        {-1,8,0,-1,7},
        {4,3,-1,0,5},
        {2,3,7,5,0}
    };

然后用思维导图将整个过程进行推导,找到递归过程中,结束点。推导的思维导图如下

初始问题是是找0点到2点的最短距离。然后0点与1点、3点和4点相连,于是问题可以分解为该点的距离与0点的距离加上该点与目标点2的最短距离,再然后将距离比较得到最短的那个距离,就是0到2的最短距离。这个第二层的问题又可以展开为其他的相同问题。直到f(2,2)得到的距离就是0,于是就可以递归返回了。

值得注意是递归的过程需要记录自己走过的点,如果没有话,就会出现一直绕圈子,走不出圈子。一直递归,导致堆栈溢出。还有一个问题就是会出现自己的路径到达不了终点,这种情况也需要判断。

代码如下

#include <iostream>
#include <array>
#include <list>
#include <algorithm>
#include <map>

using namespace std;
using std::array;

void printf_list(const list<int> & l)
{
    for(auto it = l.begin(); it != l.end(); it++)
    {
        cout << *it << "<";
    }
    cout << endl;
}

struct Distance
{
    int len;
    Distance(int n)
    {
        len = n;
    }
    bool operator>(const Distance & dist)
    {
        if(this->len == -1)
        {
            return true;
        }
        else if(dist.len == -1)
        {
            return false;
        }
        else
        {
            return this->len > dist.len;
        }
    }
    Distance  operator+(const Distance & dist)
    {
        if(this->len == -1 || dist.len == -1)
        {
            return Distance(-1);
        }
        else
        {
            return Distance(this->len + dist.len);
        }
    }
    friend std::ostream & operator<<(std::ostream & out, Distance dist)
    {
        if (dist.len == -1)
        {
            out << "len:" << "不可达";
        }
        else
        {
            out << "len:" <<dist.len;
        }
        
    }
};


Distance MinDistance(const char (*graph)[5], int W, int L, int src, int dest,list<int> & route)
{
    if(src == dest)
    {
        route.push_back(dest);//记录已经走过的点
        printf_list(route);
        return Distance(0);
    }
    else
    {
        route.push_back(src);//记录已经走过的点
       list<int> list_min;
       Distance len_min = 0;
       int flag = 0;
       for(int i=0; i < W; i++)
       {
         if(graph[src][i] != -1)//判断src点是否能够到达i点
         {
            list<int> list_temp = route;
            if(find(route.begin(), route.end(), i) == route.end())//判断是否已经走过了
            {
                /*
                    下方if esle 实现的功能是min函数的功能
                */
                if(flag == 0)
                {
                    len_min = MinDistance(graph, 5, 5,i, dest, list_temp) + graph[src][i];
                    list_min = list_temp;
                    flag = 1;
                    cout << len_min << endl;
                }
                else
                {
                    Distance t = MinDistance(graph, 5, 5, i, dest, list_temp) + graph[src][i];
                    if(len_min > t)
                    {
                        len_min = t;
                        list_min = list_temp;
                    }
                    cout << t << endl;
                }
            }
         }
       } 
        //避免前方无路,返回Distance(-1),表示前面不可到达
       if(flag == 0)
       {
            return Distance(-1);
       }
       else
       {
            cout << len_min << endl;
            printf_list(list_min);
            route = list_min;
            return len_min;
       }
       
    }
}
int main()
{
    //不可达设置为-1
    char graph[5][5] = {
        {0,1,-1,4,2},
        {1,0,8,3,3},
        {-1,8,0,-1,7},
        {4,3,-1,0,5},
        {2,3,7,5,0}
    };
    list<int> route;
    Distance lenth = MinDistance(graph, 5, 5, 0, 2,route);
    cout << lenth << endl;
    printf_list(route);
    return 0;
    
}

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值