I decided to present the graph in Haskell using a list of nodes (e.g. n=[1,2,3,4]) and a list of pairs representing edges (example m=[(1,2), (2,3)]). Now I have to see if the graph is tightly connected.
My main problem is how to find a way between two nodes in the graph. I wrote something like this:
-- 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
It works when I have nodes of degree 1, but for nodes with a greater degree it does not always work. Can you let me know about this?
source
share