连接的元组对之间的最短路径

编程入门 行业动态 更新时间:2024-10-24 14:27:44
本文介绍了连接的元组对之间的最短路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是新来的,对于任何类型的帮助,我都非常乐意……我有一个元组列表,我必须找到连接的元组对之间的最短路径.例如,我有一个名为pairs = [(1,2),(2,4),(1,3),(3,1),(4,3)]的列表,我必须找到最短路径:

I'm new at this and i would be really greatfull for any kind of help...I have a list of tuples and I have to find shortest path between connected pairs of tuples. For example, I have list called pairs = [(1,2),(2,4),(1,3),(3,1),(4,3)] and i have to find shortest path for:

1 to 2, 1 to 3 and 1 to 4 2 to 1, 2 to 3 and 2 to 4 3 to 1, 3 to 2 and 3 to 4

在这个列表对中,如果我搜索 1 到 3 之间的联系,我必须得到可能的结果:

In this list pairs, if I search for connection between 1 to 3, I have to possible outcomes:

1) (1,3) 2) (1,2)-(2,4)-(4,3)

当然最短的是第一个 - (1,3)

and of course the shortest one is the first one - (1,3)

谢谢...

推荐答案

如果你只搜索两个数字之间的最短路径(我们称之为节点)并且它们之间的边长为 1,你可以使用 BFS,如果有其他距离,可以使用Dijkstra.由于两者都是图算法,您可能需要对它们稍作更改,因为您只有一个边列表,而不是图结构.

If you only search for the shortest path between two numbers (lets call them nodes) and the length of the edges between them is 1, you can use BFS, if they have some other distance, you can use Dijkstra. Since both are graph algorithms, you may need to change them a little since you have only a list of edges, not graph structure.

更多推荐

连接的元组对之间的最短路径

本文发布于:2023-11-29 23:22:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647943.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最短   路径

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!