找到图中所有封闭游走的算法,适用于给定的顶点
今日不肯埋头,明日何以抬头!每日一句努力自己的话哈哈~哈喽,今天我将给大家带来一篇《找到图中所有封闭游走的算法,适用于给定的顶点》,主要内容是讲解等等,感兴趣的朋友可以收藏或者有更好的建议在评论提出,我都会认真看的!大家一起进步,一起学习!
我有一个无向图,没有多个/平行边,并且每个边都用距离加权。我希望在图表中找到某个最小和最大总距离之间的封闭行走。我还希望能够对步行中重复距离的数量设置限制,例如,如果将 10 的边遍历两次,则重复距离为 10。将重复距离设置为 0 应该会发现闭合图中的路径(与行走相对)。最后,虽然找到所有封闭步道会很好,但我不确定这对于存在数千/数百万条潜在路线的较大图表是否可行,因为我相信这将花费指数时间。因此,我目前正在尝试根据启发式函数找到一些最佳的封闭行走,以便算法在合理的时间内完成。
编辑:大致性能要求 - 我用来测试的图当前有 17,070 个节点和 23,026 个边。生成的循环包含大约 50 个边,查找时间约为 20 秒,但是我希望找到更长距离的循环(~100 个边),理想情况下也在 <1 分钟的时间范围内。
这是我当前的方法:
graph.go:
package graph
import (
"fmt"
"github.com/bgadrian/data-structures/priorityqueue"
)
// a graph struct is a collection of nodes and edges represented as an adjacency list.
// each edge has a weight (float64).
type edge struct {
heuristicvalue int // lower heuristic = higher priority
distance float64
}
type graph struct {
edges map[int]map[int]edge // adjacency list, accessed like edges[fromid][toid]
}
func (g *graph) findallloops(start int, mindistance float64, maxdistance float64, maxrepeated float64, routescap int) []route {
// find all loops that start and end at node start.
// a loop is a closed walk through the graph.
loops := []route{}
loopcount := 0
queue, _ := priorityqueue.newhierarchicalheap(100, 0, 100, false)
queue.enqueue(*newroute(start), 0)
size := 1
distancetostart := getdistancetostart(g, start)
for size > 0 {
// pop the last element from the stack
temp, _ := queue.dequeue()
size--
route := temp.(route)
// get the last node from the route
lastnode := route.lastnode()
// if the route is too long or has too much repeated distance,
// don't bother exploring it further
if route.distance + distancetostart[route.lastnode()] > maxdistance || route.repeateddistance > maxrepeated {
continue
}
// now we need to efficiently check if the total repeated distance is too long
// if the last node is the start node and the route is long enough, add it to the list of loops
if lastnode == start && route.distance >= mindistance && route.distance <= maxdistance {
loops = append(loops, route)
loopcount++
if loopcount >= routescap {
return loops
}
}
// add all the neighbours of the last node to the stack
for neighbour := range g.edges[lastnode] {
// newroute will be a copy of the current route, but with the new node added
newroute := route.withaddednode(neighbour, g.edges[lastnode][neighbour])
queue.enqueue(newroute, newroute.heuristic())
size++
}
}
return loops
}
路线.go:
package graph
import (
"fmt"
)
// A route is a lightweight struct describing a route
// It is stored as a sequence of nodes (ints, which are the node ids)
// and a distance (float64)
// It also counts the total distance that is repeated (going over the same
// edge twice)
// To do this, it stores a list of edges that have been traversed before
type Route struct {
Nodes []int
Distance float64
Repeated []IntPair // two ints corresponding to the nodes, orderedd by (smallest, largest)
RepeatedDistance float64
Heuristic int // A heuristic for the type of Routes we would like to generate
LastWay int
}
type IntPair struct {
A int
B int
}
// WithAddedNode adds a node to the route
func (r *Route) WithAddedNode(node int, edge Edge) Route {
newRoute := Route{Nodes: make([]int, len(r.Nodes) + 1), Distance: r.Distance, Repeated: make([]IntPair, len(r.Repeated), len(r.Repeated) + 1), RepeatedDistance: r.RepeatedDistance, Ways: r.Ways, LastWay: r.LastWay}
copy(newRoute.Nodes, r.Nodes)
copy(newRoute.Repeated, r.Repeated)
newRoute.Nodes[len(r.Nodes)] = node
newRoute.Distance += edge.Distance
// Get an IntPair where A is the smaller node id and B is the larger
var pair IntPair
if newRoute.Nodes[len(newRoute.Nodes)-2] < node {
pair = IntPair{newRoute.Nodes[len(newRoute.Nodes)-2], node}
} else {
pair = IntPair{node, newRoute.Nodes[len(newRoute.Nodes)-2]}
}
// Check if the edge has been traversed before
found := false
for _, p := range newRoute.Repeated {
if p == pair {
newRoute.RepeatedDistance += edge.Distance
found = true
break
}
}
if !found {
newRoute.Repeated = append(newRoute.Repeated, pair)
}
// Current heuristic sums heuristics of edges but this may change in the future
newRoute.Heuristic += edge.HeuristicValue
return newRoute
}
func (r *Route) Heuristic() int {
return r.Heuristic
}
func (r *Route) LastNode() int {
return r.Nodes[len(r.Nodes)-1]
}
以下是我目前为加速算法所做的事情:
- 不是查找所有循环,而是根据给定的启发式查找前几个循环。
- 修剪图表,尽可能删除几乎所有 2 阶节点,并用权重 = 原始两条边之和的单条边替换它们。
- 使用切片来存储重复的边,而不是使用地图。
- 使用 dijkstra 算法找到从起始节点到每个其他节点的最短距离。如果一条路线无法在这段距离内返回,请立即丢弃它。 (算法实现的代码未包含在帖子中,因为它只占运行时间的约 4%)
根据 go 分析器,route.go 中的 withaddednode 方法占运行时的 68%,该方法的时间大致均匀地分布在 runtime.memmove 和 runtime.makeslice (调用 runtime.mallocgc)之间。本质上,我相信大部分时间都花在复制路由上。我将非常感谢任何有关如何改进该算法的运行时间或任何可能的替代方法的反馈。如果我可以提供任何其他信息,请告诉我。非常感谢!
正确答案
查找两个顶点之间的所有路径的标准算法是:
- 应用 dijkstra 寻找最便宜的路径
- 增加路径中链接的成本
- 重复直到找不到新路径
在您的情况下,您希望路径返回到起始顶点。可以轻松修改算法来做到这一点
- 将起始顶点 s 分成两部分:s1 和 s2。
- 在连接到 s 的顶点上循环 v。
- 在 s1 和 v 之间添加零成本边
- 在 s2 和 v 之间添加零成本边
- 删除 s 及其边缘。
- 运行标准算法来查找 s1 和 s2 之间的所有路径
您需要修改 dijkstra 代码以考虑其他限制 - 最大长度和“启发式”
性能
尽管您说您担心大型图表的性能,但您没有给出所需的性能。我会尝试提供一个估计。
我对 go 的性能一无所知,所以我假设它与我熟悉的 c++ 相同。
以下是 c++ 的一些性能测量:
运行 dijkstra 算法的运行时间,以在从 https://dyngraphlab.github.io/。使用 graphex 应用程序运行 3 次的最长结果。
| Vertex Count | Edge Count | Run time ( secs ) |
|---|---|---|
| 10,000 | 50,000 | 0.3 |
| 145,000 | 2,200,000 | 40 |
今天关于《找到图中所有封闭游走的算法,适用于给定的顶点》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
我试图终止正在运行的 golang 脚本,但是它继续运行尽管已被杀死
- 上一篇
- 我试图终止正在运行的 golang 脚本,但是它继续运行尽管已被杀死
- 下一篇
- 比较和更新两个不同映射字符串接口的键在Golang中的方法
-
- Golang · Go问答 | 1年前 |
- 在读取缓冲通道中的内容之前退出
- 139浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 戈兰岛的全球 GOPRIVATE 设置
- 204浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何将结构作为参数传递给 xml-rpc
- 325浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何用golang获得小数点以下两位长度?
- 478浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何通过 client-go 和 golang 检索 Kubernetes 指标
- 486浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 将多个“参数”映射到单个可变参数的习惯用法
- 439浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 将 HTTP 响应正文写入文件后出现 EOF 错误
- 357浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 结构中映射的匿名列表的“复合文字中缺少类型”
- 352浏览 收藏
-
- Golang · Go问答 | 1年前 |
- NATS Jetstream 的性能
- 101浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何将复杂的字符串输入转换为mapstring?
- 440浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 相当于GoLang中Java将Object作为方法参数传递
- 212浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何确保所有 goroutine 在没有 time.Sleep 的情况下终止?
- 143浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- ChatExcel酷表
- ChatExcel酷表是由北京大学团队打造的Excel聊天机器人,用自然语言操控表格,简化数据处理,告别繁琐操作,提升工作效率!适用于学生、上班族及政府人员。
- 3193次使用
-
- Any绘本
- 探索Any绘本(anypicturebook.com/zh),一款开源免费的AI绘本创作工具,基于Google Gemini与Flux AI模型,让您轻松创作个性化绘本。适用于家庭、教育、创作等多种场景,零门槛,高自由度,技术透明,本地可控。
- 3405次使用
-
- 可赞AI
- 可赞AI,AI驱动的办公可视化智能工具,助您轻松实现文本与可视化元素高效转化。无论是智能文档生成、多格式文本解析,还是一键生成专业图表、脑图、知识卡片,可赞AI都能让信息处理更清晰高效。覆盖数据汇报、会议纪要、内容营销等全场景,大幅提升办公效率,降低专业门槛,是您提升工作效率的得力助手。
- 3436次使用
-
- 星月写作
- 星月写作是国内首款聚焦中文网络小说创作的AI辅助工具,解决网文作者从构思到变现的全流程痛点。AI扫榜、专属模板、全链路适配,助力新人快速上手,资深作者效率倍增。
- 4543次使用
-
- MagicLight
- MagicLight.ai是全球首款叙事驱动型AI动画视频创作平台,专注于解决从故事想法到完整动画的全流程痛点。它通过自研AI模型,保障角色、风格、场景高度一致性,让零动画经验者也能高效产出专业级叙事内容。广泛适用于独立创作者、动画工作室、教育机构及企业营销,助您轻松实现创意落地与商业化。
- 3814次使用
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- Golang取得代码运行时间的问题
- 2023-02-24 501浏览
-
- 请问 go 代码如何实现在代码改动后不需要Ctrl+c,然后重新 go run *.go 文件?
- 2023-01-08 501浏览
-
- 如何从同一个 io.Reader 读取多次
- 2023-04-11 501浏览

