我选择用节点列表(例如n=[1,2,3,4]
)和表示边缘的对列表(示例m=[(1,2), (2,3)]
)在Haskell中表示图形。现在我必须看看图表是否强连接。Haskell中的图形表示
我的主要问题是如何找到图中2个节点之间是否有方法。我写了这样的事情:
-- sees if 2 nodes are adjacent
adjacent x y [] = False
adjacent x y (mu:m) =
if(x== (fst mu) && y==(snd mu)) then True
else adjacent x y m
-- the successor of a node, ex for the edge (1,2) the succ of 1 is 2
suc x [] = 0
suc x (l:list) =
if(x==(fst l)) then snd l
else suc x list
-- my main function
way 0 y list = False
way x y (mu:m)
| x==y = True
| (adjacent x y (mu:m)) == True = True
| otherwise =
if ((way (suc x (mu:m)) y (mu:m))==False) then way (suc x m) y m
else True
它工作时,我有1度节点,但对于具有更大程度的节点并不总是工作。你能给我一些线索吗?
+1为是明确它是家庭作业!让我们知道如何接近帮助。 – MtnViewMark 2010-05-05 20:28:59
一旦某个特定答案帮助您成功解决了您的问题,选择它就很正常(点击投票计数下的 复选标记),以便知道问题已解决。向成员表明他们现在可能想花时间处理其他问题。 – MtnViewMark 2010-05-06 13:13:18