简体   繁体   English

Data.Graph 中的路径 function 在 Haskell 中如何工作?

[英]How does the path function in Data.Graph work in Haskell?

I have read the documentation that I can find on the path function ( for a directed graph ).我已经阅读了可以在路径 function 上找到的文档(对于有向图)。 There isn't much coming up in the search engine.搜索引擎中没有太多内容。

All I found basically boils down to this link .我发现的所有内容基本上都归结为这个链接

All it really says is what the path function does (which I already knew), and and example of output.它真正说的是路径 function 所做的(我已经知道),以及 output 的示例。 What I am trying to figure out, is how the path function works or how it could be implemented manually.我想弄清楚的是路径 function 是如何工作的,或者它是如何手动实现的。 So far I have gotten this far but am now stuck (or may be on the wrong path altogether, I can't be certain):到目前为止,我已经走了这么远,但现在卡住了(或者可能完全走错了路,我不能确定):

member _ [] = False
member n (x:xs)
    | x == n = True
    | otherwise = membre n xs
    
getX :: (a, b) -> a
getX (x,_) = x

getY :: (a, b) -> b
getY (_,y) = y

getAllY :: [(a , b)] -> [b]
getAllY [] = [] 
getAllY (x:xs) = (getY x):(getAllY xs)

fltrX [] _ = []
fltrX (x:xs) n 
    | (getX x) == n = x:(fltrX xs n)
    | otherwise = fltrX xs n
    
fltrY [] _ = []
fltrY (x:xs) n 
    | (getY x) == n = x:(fltrY xs n)
    | otherwise = fltrY xs n

path' :: [(a , a)] -> a -> a -> Bool
path' [] _ _ = False
path' (xs) n m 
    | (member m (getAllY (fltrX xs n))) = True
    | otherwise = *recursive statement giving me a headache*

but I am having trouble seeing how to get the recursion right.但我无法看到如何正确地进行递归。 I think I have just been staring at it too long and can't see the whole problem clearly anymore.我想我只是盯着它太久了,不能再清楚地看到整个问题。 Logic should be something like:逻辑应该是这样的:

  • it takes the y values from the tuples with x = n它从 x = n 的元组中获取 y 值
  • it finds all tuples where x in the list of Y values above它找到上面 Y 值列表中 x 的所有元组
  • if no Y values = m, do path function on new list of tuples with n = (each of the X values) and m = m如果没有 Y 值 = m,则在具有 n =(每个 X 值)和 m = m 的新元组列表上执行路径 function
  • if m is found in the Y values, return true如果在 Y 值中找到 m,则返回 true

Can anyone point me in the right direction (pun intended) preferably without typing out the code for me?任何人都可以在不为我输入代码的情况下为我指出正确的方向(双关语)吗?

Thanks!谢谢!

Thanks to commenters for providing resources that helped me write the following solution:感谢评论者提供帮助我编写以下解决方案的资源:

member _ [] = False
member n (x:xs)
    | x == n = True
    | otherwise = member n xs
    
unique [] = []
unique (x:xs)
    | member x xs == True = unique xs
    | otherwise = x:(unique xs)


findAllEndsForX :: [(Int , Int)] -> Int -> [Int]
findAllEndsForX [] _ = []
findAllEndsForX ((a,b):xs) y
  | xs == [] = a:[b]
  | a == y = a:(findAllEndsForX xs b)
  | otherwise = (findAllEndsForX xs b)

accessible :: [(Int , Int)] -> Int -> [Int]
accessible [] _ = []
accessible xs n = unique (findAllEndsForX xs n)

path' :: [(Int , Int)] -> Int -> Int -> Bool
path' [] _ _ = False
path' xs v w = member w (accessible xs v)

My ultimate goal was to mimic the functionality of the path feature, not necessarily the implementation.我的最终目标是模仿路径功能的功能,而不一定是实现。 I wanted to do the work without using any predefined functions.我想在不使用任何预定义函数的情况下完成这项工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM