BFS算法使用Lua,可以找到2个节点之间的最短路径。
2017-2-14 18:44:42
收藏:0
阅读:76
评论:1
我成功编写了一段代码,使用 BFS 算法迭代了所有图节点。这是简单的一部分 :)
现在我卡在了获取两个节点之间的最短路径上。
这是我目前的代码:
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
queue = {stack={}}
function queue:en(e)
table.insert(self.stack, e)
end
function queue:de()
local e = self.stack[1]
self.stack[1] = nil
local new_stack = {}
for _, v in pairs(self.stack) do
table.insert(new_stack, v)
end
self.stack = new_stack
return e
end
function queue:count()
return #self.stack
end
function bfs()
start = 1
goal = 10
visited = {}
path = {}
graph = {}
graph[1] = {2,3,4}
graph[2] = {6,5,1}
graph[3] = {1}
graph[4] = {7,8}
graph[5] = {9,10,2}
graph[6] = {2}
graph[7] = {4,11,12}
graph[8] = {4}
graph[9] = {5}
graph[10] = {5}
graph[11] = {7}
graph[12] = {7}
queue:en(start)
table.insert(visited, start)
depth = 1
while queue:count() > 0 do
node = queue:de()
for _, exit in pairs(graph[node]) do
if not table.contains(visited, exit) then
table.insert(visited, exit)
if exit == goal then
print("GOAL : " .. exit)
do return end
end
if graph[exit] then
print("Node: " .. exit .. ", Depth: " .. depth)
queue:en(exit)
end
end
end
depth = depth + 1
end
end
bfs()
运行后输出:
Node: 2, Depth: 1
Node: 3, Depth: 1
Node: 4, Depth: 1
Node: 6, Depth: 2
Node: 5, Depth: 2
Node: 7, Depth: 4
Node: 8, Depth: 4
Node: 9, Depth: 6
GOAL : 10
在当前示例中,我需要从 1 到 10 获取最短路径。
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
这是如何实现的。
queue.lualocal queue = {} function queue:init() local q = {} q.stack = {} function q:push(e) table.insert(self.stack, e) end function q:pull() local e = self.stack[1] table.remove(self.stack, 1) return e end function q:count() return #self.stack end return q end return queuetbl.luafunction table.contains(tbl, e) for _, v in pairs(tbl) do if v == e then return true end end return false end function table.copy(tbl) local t = {} for _, v in pairs(tbl) do table.insert(t, v) end return t endbfs.luarequire "tbl" -- table.contains and table.copy queue = require "queue" local function bfs(graph, start, goal) if not graph[start] then return false end local visited = {} local queue = queue:init() queue:push({start}) table.insert(visited, start) while queue:count() > 0 do local path = queue:pull() local node = path[#path] if node == goal then return path end for _, exit in pairs(graph[node]) do if not table.contains(visited, exit) then table.insert(visited, exit) if graph[exit] then local new = table.copy(path) table.insert(new, exit) queue:push(new) end end end end return false end return bfs用法:
将这三个文件保存到您的 Lua 路径。
bfs = require "bfs" path = bfs(grap, start, goal)