我如何用Haskell求图中不同路径的和 问题

我如何用Haskell求图中不同路径的和 问题,haskell,graph,functional-programming,Haskell,Graph,Functional Programming,相关问题以最短的阅读时间计算问题 时间给定问题的数量,每个问题的数组 阅读时间,以及问题之间的一系列边缘 questions = 5 readingTimes = [2, 1, 13, 1, 12] edges = [[3,0], [3,2], [4,1], [3,1]] answer: 3 要计算问题的“最短阅读时间”,您需要 取子节点创建的每个路径的读取时间之和,除以问题的直接子节点数(问题 是当前节点)。然后将此数字添加到当前值 阅读时间 使用上述输入: (paths)

相关问题
以最短的阅读时间计算问题 时间给定问题的数量,每个问题的数组 阅读时间,以及问题之间的一系列边缘

questions = 5
readingTimes = [2, 1, 13, 1, 12]
edges = [[3,0], [3,2], [4,1], [3,1]]

answer: 3
要计算问题的“最短阅读时间”,您需要 取子节点创建的每个路径的读取时间之和,除以问题的直接子节点数(问题 是当前节点)。然后将此数字添加到当前值 阅读时间

使用上述输入:

      (paths)
 current question          children       #direct    #readingTime
 0 -> 3 -> 1 -> 4      2   + (1 + 1 + 12) / 1        = 30 
        -> 2               + (1 + 13)     / 1          

 1 -> 4                 1  + (12)         / 1        = 13

 2 -> 3 -> 0           13  + (1 + 2)      / 1        = 28
        -> 1 -> 4          + (1 + 12)     / 1

 3 -> 0                 1  + (2)          / 3        = 10.3 
   -> 1 -> 4               + (1 + 12)     / 3
   -> 2                    + (13)         / 3
发布和请求 我的代码适用于下面给定的测试用例,但其他测试用例可能会失败。我的代码依赖于数据。Graph的可达性可以获取当前节点可以到达的所有节点,并将其所有子节点的总和除以总的直接子节点,但我需要将每个单独的子节点树总和除以总的直接子节点。所以我要做的是取所有路径的和除以直接子代的数量,当我应该除以每个路径和的时候

我想使用Data.Graph的组件函数来相应地折叠树并累加最小的和。我在使用
森林顶点
类型时遇到了问题,但是使用
序列
我得到了一种
树[顶点]
类型,这使我的工作变得更容易,无论我是否能够集中精力进行必要的递归/遍历

 *Main> :t graph1
 graph1 :: Graph
 *Main> :t components graph1
 components graph1 :: Forest Vertex
 *Main> :t sequence . components $ graph1
 sequence . components $ graph1 :: Tree [Vertex]
 *Main> sequence . components $ graph1
 Node {rootLabel = [0], subForest = [Node {rootLabel = [3], subForest= [Node {rootLabel = [2], subForest = []},Node {rootLabel = [1], subForest = [Node {rootLabel = [4], subForest = []}]}]}]}
如果您能帮助我优化当前代码,使其成为完整的解决方案,或者解释如何使用
Data.Graph
的组件函数折叠/遍历
[森林顶点]
,我将不胜感激

import Data.Graph
import Data.List
import qualified Data.Vector as V

relatedQuestions :: Int -> [Int] -> [[Int]] -> Int
relatedQuestions n t edges = snd smallest
     where 
           smallest = minimum [(price (snd q) (related (snd q) edges) (fst q) , snd q) | q <- zip t (vertices graph)] 
           price q r i = (fromIntegral (reach q) / fromIntegral r) + fromIntegral i
           reach q = V.sum . V.tail $ V.fromList [ (V.fromList t) V.! x | x <- reachable graph q]
           graph = createGraph n createEdges
           createEdges = concat [permutations x | x <- edges] -- makes sure edge [1,0] has edge [0,1]


-- | helper functions 

-- | number of children at node   
related :: Int -> [[Int]] -> Int
related n = length . filter (==n) . concat

createGraph :: Int -> [[Int]] -> Graph
createGraph e = buildG (0, e-1) . map (\[x,y] -> (x,y))

“…因为我在组件类型Forest Vertex vs Tree Int’s中遇到类型错误。”-包括此代码和您遇到的类型错误。我仍然无法编写任何代码,但我包含了到目前为止我一直在查看的内容。“…因为我在组件类型Forest Vertex vs Tree Int’s中遇到类型错误。”-包括此代码和您得到的类型错误。我仍然无法编写任何代码,但我包括了我目前正在查看的内容。
-- | test cases 
main :: IO ()
main = hspec $ do
   describe "relatedQuestions" $ do 
         it "test case 1" $ do 
       relatedQuestions n1 t1 edges1 `shouldBe` (3 :: Int)  
         it "test case 2" $ do 
       relatedQuestions n2 t2 edges2 `shouldBe` (4 :: Int)  
         it "test case 3" $ do 
       relatedQuestions n3 t3 edges3 `shouldBe` (2 :: Int)  

n1 :: Int   
n1 =  5

t1 :: [Int]
t1 = [2, 1, 13, 1, 12]

edges1 :: [[Int]]
edges1 = [[3,0], [3,2], [4,1], [3,1]]

n2 :: Int
n2 = 5

t2 :: [Int]
t2 = [3, 11, 3, 18, 3]

edges2 :: [[Int]]
edges2 = [[3,1], [4,2], [0,3], [4,1]]

n3 :: Int
n3 = 10

t3 :: [Int]
t3 = [9, 2, 7, 14, 4, 26, 21, 18, 39, 33]

edges3 :: [[Int]]
edges3 = [[2,7], [0,9], [3,5], [4,7], [0,2], [8,5], [3,6], [2,1], [5,0]]