如何测试Lua中的关系是否可传递?

如何测试Lua中的关系是否可传递?,lua,relation,Lua,Relation,我需要做一个程序来测试给定的关系是否是可传递的 我想用链表来做这个,但我不知道怎么做。我看到过类似问题的答案,但没有一个是针对Lua的,因为我只是在学习编码,我不知道如何让这些答案对我有用 这是我目前的尝试: relation={{1,1}, {1,2}, {2,1}} n=3 for i=1,n do if relation[i][1] == relation[i][2] then print("transitive") else if relation[i][1] ~= relation[i

我需要做一个程序来测试给定的关系是否是可传递的

我想用链表来做这个,但我不知道怎么做。我看到过类似问题的答案,但没有一个是针对Lua的,因为我只是在学习编码,我不知道如何让这些答案对我有用

这是我目前的尝试:

relation={{1,1}, {1,2}, {2,1}}
n=3
for i=1,n do
if relation[i][1] == relation[i][2] then
print("transitive")
else if relation[i][1] ~= relation[i][2] then
print("checking")
for q=1,n do
_G["x"..i]=relation[q][1]
_G["y"..i]=relation[q][2]
end
if _G["x"..i] == nil then
print("transitive")
end
for w=1,3 do
if
_G["y"..i] == relation[w][1] then
relation[w][2] = z
for e=1,3 do
if 
z == relation[e][2] and e ~= w then
if relation[e][2] == _G["x"..i] then
print("transitive")
end
else print("not transitive")
end
end
end
end
end
end
end
print("end loop 1")
for i=n,1,-1 do
if relation[i][1] == relation[i][2] then
print("transitive")
else if relation[i][1] ~= relation[i][2] then
print("checking")
for q=n,1,-1 do
_G["x"..i]=relation[q][1]
_G["y"..i]=relation[q][2]
end
if _G["x"..i] == nil then
print("transitive")
end
for w=n,1,-1 do
if
_G["y"..i] == relation[w][1] then
relation[w][2] = z
for e=n,1,-1 do
if 
z == relation[e][2] and e ~= w then
if relation[e][2] == _G["x"..i] then
print("transitive")
end
else print("not transitive")
end
end
end
end
end
end
end

我总算让它工作了!我不知道怎么做,但这很重要

relation={{1,2}, {2,1}, {2,2}}
n=3
for i=1,n do
if relation[i][1] == relation[i][2] then
print("transitive")
else if relation[i][1] ~= relation[i][2] then
print("checking")
for q=1,n do
_G["x"..i]=relation[i][1]
_G["y"..i]=relation[i][2]
end
if _G["x"..i] == nil then
print("transitive")
end
for w=1,3 do
if
_G["y"..i] == relation[w][1] and w ~= i then
relation[w][2] = z
for e=1,3 do
if 
z == relation[e][2] and e ~= w then
if relation[e][2] == _G["x"..i] then
print("transitive")
end
else print("not transitive")
end
end
end
end
end
end
end
print("end loop 1")
for i=n,1,-1 do
if relation[i][1] == relation[i][2] then
print("transitive")
else if relation[i][1] ~= relation[i][2] then
print("checking")
for q=n,1,-1 do
_G["x"..i]=relation[i][1]
_G["y"..i]=relation[i][2]
end
if _G["x"..i] == nil then
print("transitive")
end
for w=n,1,-1 do
if
_G["y"..i] == relation[w][1] and w ~= i then
relation[w][2] = z
for e=n,1,-1 do
if 
z == relation[e][2] and e ~= w then
if relation[e][2] == _G["x"..i] then
print("transitive")
end
else print("not transitive")
end
end
end
end
end
end
end

您好,您能提供有关您的具体应用程序的更多信息吗?您是否有尝试使用链表解决此问题的代码?为什么你认为链表是一个好的解决方案?你能把你不确定如何翻译成lua的其他答案的链接包括进来吗?我尝试过使用链接列表,但失败了。我不知道如何翻译成Lua的一个例子是:我一直在编写一些代码,但我甚至还没有接近完成它。不幸的是,链接的问题也只是代码,很难理解所要求的内容。你能用文字描述一下你试图实现的算法吗?理解非格式化和非惯用代码的工作原理并不容易。很难说为什么它不起作用。(好吧,对于特定的代码片段,我可以猜测它不起作用,因为e=1,3时的行
,但这可能不会帮助您解决实际问题)