shortest path problem

带权图
1. 单源点 最短路径问题: Dijkstra,(可用 priority_queue 优化)
2. 任意两点最短路径问题:Floyd

  1. 邻接矩阵 的 Dijkstra
    **算法:
    第一步:在S-V中,选择一个d[v]最小的点v,将v加入S,d[v]为它的最终值
    第二步:在S-V中,找到一个待更新最短路径的点u(因为S中添入了V),更新u的最短路径(暂时的) 和 前置点**
#include<iostream>
using namespace std;

#define MAX 10000  //定义无穷大
#define N 100
int path[N];


struct Graph{
    int vexnum;
    int arcnum;
    int arcs[N][N];
    char vexs[N];
};

int Loc(Graph G,char a)
{
    for(int i=1;i<=G.vexnum;i++)
    {
        if(G.vexs[i] == a)
        {
            return i;
        }
    }
}

void Input(Graph &G)
{
    cin>>G.vexnum>>G.arcnum;
    for(int i=1;i<=G.vexnum;i++)
    {
        cin>>G.vexs[i];
    }
    for(int i=1;i<=G.vexnum;i++)
    {
        for(int j=1;j<=G.vexnum;j++)
        {
            G.arcs[i][j] = MAX;
        }
    }
    for(int i=1;i<=G.arcnum;i++)
    {
        int x,y,w;
        char a,b;
        cin>>a>>b>>w;
        x = Loc(G,a);
        y = Loc(G,b);
        G.arcs[x][y] = w;
    }
}


void OutPath(Graph G, int v)
{
    if(path[v] == 0)
    {
        cout<<G.vexs[v]<<" ";
        return ;
    }
    else
    {
        OutPath(G,path[v]);
        cout<<G.vexs[v]<<" ";
    }
} 

// 求a->b的最短路径
void Dijkstra(Graph &G,char a,char b)
{
    int s[N];
    int d[N];
    int v0 = Loc(G,a);
    // 初始化
    for(int i=1;i<=G.vexnum;i++)
    {
        s[i] = 0;
        d[i] = G.arcs[v0][i];
        if(G.arcs[v0][i] != MAX)
        {
            path[i] = v0;       
        }
        else
        {
            path[i] = 0;
        }
    }
    s[v0] = 1;
    d[v0] = 0;


    for(int i=1;i<=G.vexnum-1;i++)
    {
         // 在 S-V 中选择一点 u ,d[u]最小    ----> 将 u 加入s 
        int min = MAX;
        int v,j;
        for(j=1;j<=G.vexnum;j++)
        {
            if(s[j]==0)
            {
                if(d[j] < min)
                {
                    min = d[j];
                    v = j;
                }
            } 
        }
        // 得到 v 
        s[v] = 1;

     //u 为S-v 中的点,若d[u] > d[v]+arcs[v][u], 则d[u] = d[v] + arcs[v][u],path[u] = v; 
        for(j=1;j<=G.vexnum;j++)
        {
            if(s[j] == 0)
            {
                if(d[j] > d[v]+G.arcs[v][j])
                {
                    path [j] = v;
                    d[j] = d[v] + G.arcs[v][j];
                }
            }
        }
    }

    int vt = Loc(G,b);
    cout<<d[vt]<<endl;
    OutPath(G,vt);
    cout<<endl;
}


int main()
{
    Graph G;
    Input(G);
    char a,b;
     cin>>a>>b;
     Dijkstra(G,a,b);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To find the shortest path from a given starting node s to any other nodes in the (undirected) graph, we can use Breadth-First Search (BFS) algorithm. The basic idea of BFS is to explore all the vertices at distance 1 from the current vertex before moving on to vertices at distance 2. Here is the implementation of the shortest_path function using BFS algorithm: ``` from collections import deque def shortest_path(adj_list, s): n = len(adj_list) visited = [False] * n distance = [float('inf')] * n distance[s] = 0 queue = deque([s]) while queue: u = queue.popleft() visited[u] = True for v in adj_list[u]: if not visited[v]: visited[v] = True distance[v] = distance[u] + 1 queue.append(v) for i in range(n): if not visited[i]: distance[i] = float('inf') return distance ``` In the above code, we first initialize two lists: visited and distance. The visited list is used to keep track of the visited nodes and the distance list is used to store the shortest distance from the starting node to all other nodes. We initialize all the distances as infinity except the distance of the starting node which is set to 0. We then use a deque (double-ended queue) to implement the BFS algorithm. We start by adding the starting node to the queue. Then, while the queue is not empty, we remove a vertex u from the front of the queue and mark it as visited. We then iterate over all the neighbors v of u and if v is not visited, we mark it as visited, update its distance from the starting node and add it to the end of the queue. Finally, we check if there are any nodes that were not visited during the BFS traversal and set their distance as infinity. We then return the distance list. Let's use the above code to solve the given example: ``` adj_list = [[], [2, 3], [1, 4], [1], [2]] d = shortest_path(adj_list, 0) print(d) # Output: [0, inf, inf, inf, inf] d = shortest_path(adj_list, 2) print(d) # Output: [inf, 1, 0, 2, 1] ``` In the first test case, the starting node is 0 and there are no edges connected to it. Hence, the distance to all other nodes is infinity. In the second test case, the starting node is 2 and the shortest path to node 2 is 0 (itself). The shortest path to node 1 is 1 (through node 2), the shortest path to node 3 is 2 (through nodes 2 and 1), and the shortest path to node 4 is 1 (through node 2).

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值