我选择用Haskell中的节点列表(例如
我主要问题是如何找到图中两个节点之间的路径。我写了类似这样的代码:
n=[1,2,3,4]
)和代表边缘的一对列表(例如m=[(1,2),(2,3)]
)来表示图。现在我需要确定图是否强连通。我主要问题是如何找到图中两个节点之间的路径。我写了类似这样的代码:
-- 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时,它是有效的,但对于度数更大的节点,它并不总是有效。你能给我一些线索吗?