function name2node( graph , name )
if not graph[name] then
graph[name] = { name = name , adj = {} }
print(#graph)
end
return graph[name]
end
function readgraph()
local graph = {}
for line in io.lines() do
local namefrom , nameto = string.match(line, "(%S+)%s+(%S+)" )
local from = name2node( graph , namefrom )
local to = name2node( graph , nameto )
from.adj[to] = true
end
return graph
end
function findpath( curr , to , path , visited )
path = path or {}
visited = visited or {}25
if visited[curr] then
return nil
end
visited[curr] = true
path[ #path + 1 ] = curr
if curr == to then
return path
end
for node in pairs( curr.adj ) do
local p = findpath(node , to , path , visited)
if p then return p end
path [#path] = nil
end
end
function printpath( path )
for i = 1 , #path do
print(path[i].name)
end
end
g = readgraph()
a = name2node( g , "a" )
b = name2node( g , "b" )
p = findpath( a , b )
if p then printpath(p) end