Major error is in the line of code: `| otherwise = dfs' g y ( v : vis )`
Hello allI am trying to implement depth search in Haskell. My issue here is kind of backtracking. Haskell code for depth first searchimport Data.Listimport Data.Arrayimport Control.Monadtype Node = Inttype Graph = Array Int [ Node ]dfs' :: Graph -> Node -> [ Node ] -> [ Node ]dfs' g v vis = dfsfun lst wherelst = g ! vdfsfun [] = visdfsfun ( x : xs )| elem x vis = dfsfun xs| otherwise = dfs' g y ( v : vis )
--set the flag true if the graph is direct otherwise falsebuildGraph :: ( Int , Int ) -> [ ( Node , Node ) ] -> Bool -> GraphbuildGraph bnds xs f| f = accumArray ( flip (:) ) [] bnds xs --direct graph a->b| otherwise = accumArray ( flip (:) ) [] bnds xss where --indirect a -> b and b -> axss = foldr ( \ ( x , y ) b -> ( y , x ) : b ) xs xsdfsSearch :: Graph -> Int -> [ Node ]dfsSearch g v = dfs' g v []Lets create a indirect graphghci>let g = buildGraph ( 0 , 5 ) [ ( 0 , 1 ) , ( 0 , 2 ) , ( 0 , 3 ) , ( 0 , 4 ) ] Falseghci>garray (0,5) [(0,[4,3,2,1]),(1,[0]),(2,[0]),(3,[0]),(4,[0]),(5,[])]ghci>dfsSearch g 0[0]Here I am getting only 0 but it should return [ 0 , 1 , 2 , 3 , 4 ] . What is happening here when i am passing 0 as root node to function , at first level i getlst = [ 4 , 3, 2, 1 ] . First element of this list is 4 so 0 is added to vis list and now 4 is passed to dfs' as parent. When it goes down , we get lst = [0] and since 0 is member of vis list so it returns the vis as result . When we write dfs in c/c++ then it returns to calling function and again loops through remaining element which i am not able visualize in Haskell. Could some one please help me how to solve this issue .RegardsMukesh Tiwari
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe