【图论】图的最短路径问题——无权图的单源最短路

 最短路径问题:

在网络中,求两个不同顶点之间的所有路径中,边的权值之和最小的那一条路径

第一个顶点:源点

最后一个顶点:终点

单源最短路径问题:(无权、有权)

从某固定源点出发,求其到所有其他顶点的最短路径

多源最短路径问题:

求任意两顶点间的最短路径

无权图的单源最短路

 只需要记录每个顶点最短路径上前一个顶点;

就可以递归打出每个顶点到源点的最短路径。

dist[s]需要一开始被初始化为0,其他顶点的dist则应是明显无效值

1.有没有访问过可以用dist[w]有没有被初始化同时表示;

2.无权图的收录一开始就是有确定顺序的(BFS的队列),

即使有多个上层邻接点,因为第一个被收录的上层邻接点dist最小,因此第一次更新的(dist+1)也是最小的

具有相同上层邻接点v的结点dist相同(=dist[v]+1),(如果v比其他上层邻接点先收录);

3.有权图因为受其多个上层邻接点影响,未收录前,dist确定后仍有机会改变,第一个被收录的上层邻接点dist是其所有上层邻接点中最小的,但是E<v,w>则不一定;

无权图:dist[w]=dist[v]+1;

有权图:dist[w]=min{ dist[v]+E<v,w> , dist[w] } 

#include <iostream>
#include <queue>
#define MAXN 1010
using namespace std;

int Nv,Ne;
struct LNode
{
    int data;
    LNode* next;
};

LNode* G[MAXN];

void CreateL(){
    cin>>Nv>>Ne;

    for(int i=0;i<Nv;i++){
        G[i]=new LNode;
        G[i]->data=i;
        G[i]->next=NULL;
    }

    int v1,v2;
    for(int i=0;i<Ne;i++){
        cin>>v1>>v2;
        LNode* tmp=new LNode;
        tmp->data=v2;
        tmp->next=G[v1]->next;
        G[v1]->next=tmp;

       /*LNode* tmp1=new LNode;
        tmp1->data=v1;
        tmp1->next=G[v2]->next;
        G[v2]->next=tmp1;*/
    }
}


void Unweighted(int S,int* dist,int* path){
    queue<int> q;
    q.push(S);
    dist[S]=0;

    int tmp;
    while(!q.empty()){
        tmp=q.front();
        q.pop();

        LNode* w=G[tmp]->next;
        while(w){
            if(dist[w->data]==-1){
                dist[w->data]=dist[tmp]+1;
                path[w->data]=tmp;
                q.push(w->data);
            }
            w=w->next;
        }
    }
}

void PrintL(int start,int* path){
    cout<<start;

    int tmp=path[start];
    while(tmp!=-1){
        cout<<" "<<tmp;
        tmp=path[tmp];
    }
}

int main(){
    CreateL();

    int dist[MAXN],path[MAXN];
    fill(dist,dist+Nv,-1);
    fill(path,path+Nv,-1);

    //从2出发到所有顶点的最短路径dist
    Unweighted(2,dist,path);
    
    //2到5的最短路径
    PrintL(5,path);

    return 0;
}

测试数据:

8 8
0 1
1 2
2 6
3 6
4 7
5 1
6 7
7 5

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
离字典,将起始节点的距离设为0,其他节点的距离设为无穷大 distances = {node: sys.maxsize for node in graph} distances[start] = 0 # 初始化已访问节点的集合和未访以下是使用问节点D的集ijkstra合 visited = set() unvisited算法求解最短路径的Python = set(graph) while unvisited: # 代码示例: ```python class D选择当前ijkstra距: def __init__(self, graph离最小的节点 , start, current goal): self.graph = graph # 邻接表_node = min(unvisited, key=lambda self node: distances[node]) # 更新.start = start当前节点的 # 起邻居节点点 self.goal =的距离 goal # 终点 for neighbor in graph self.open[current_node]: _list = {} if neighbor in # open 表 self.closed_list unvisited: new_distance = distances[current_node] + = {} graph[current_node][neighbor # closed 表 self.open_list[start] if new_distance] = < distances[neighbor]: 0.0 # 将 distances[neighbor] = new_distance # 将当前起点放入 open_list 中 self.parent = {节点标记start:为已访 None} 问,并从未访问集合中移除 visited.add # 存储节点的父子关系。键为(current_node) 子节点, unvisited值为父.remove(current_node) return节点。方便做最 distances def print后_path(dist路径的ances,回 start溯 self.min, end): _dis = None # 根 # 最短路径的长度 def shortest_path据距离字典和终点节点(self): while True: ,逆向 if self打印路径.open_list is path = [end None: ] print('搜索 current_node =失败 end while current_node !=, 结束!') break distance start: , min_node = for neighbor in graph min(zip[current_node]: if(self.open_list distances[current.values(), self_node] ==.open_list.keys distances[neighbor())) #] + graph 取出距[neighbor][current_node]: 离最小的节点 self path.open_list.pop.append(min_node)(neighbor) current_node = neighbor break path.reverse() # 将其从 open_list 中去除 self print.closed("_list[minShortest_node] = path from", distance # 将节点加入 closed start, "to", end,_list ":", "->".join(path)) # 示例 中 if min_node == self.goal: # 如果节点为的邻接矩阵终点 self.min_dis = distance 表示 graph shortest = { _path = [ 'Aself.goal]': {'B': # 5, 'C 记录从': 终1}, 点回溯的路径 'B

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值