调试一个简单的haskell函数,将颜色放置到节点" graphColor"

时间:2017-10-04 22:15:31

标签: debugging haskell

所以我在haskell有一个任务,我坚持我的逻辑,这就是我给出的问题:

graphColor查找图形的所有可能的颜色   着色是节点到颜色的分配     这样两个节点如果用一个连接就必须有不同的颜色 边缘

graphColor采用以下参数

  1. 颜色列表
  2. 节点列表
  3. 边缘列表(即节点对)
  4. 部分着色(即,节点和颜色对的列表)
  5. 边缘示例

    e1 :: [Edge]
    e1 = [(1,3),(2,1),(2,5),(3,1),(3,2),(3,3),(3,4),(3,5),(5,6),(2,6)]
    
    e2 :: [Edge]
    e2 = [(1,2),(2,3),(3,4),(1,3),(2,4),(1,4)]
    
    type Code = [(Char,Char)]
    
    -- domain of our code
    
    domain1 :: [Char]
    domain1 = ['a'..'z']
    
    -- associated range
    
    range1 :: [Char]
    range1 = ['q'..'z'] ++ ['a'..'p']
    
    -- create a code out of our domain and range
    code1 :: Code
    code1 = zip domain1 range1
    

    graphColor返回图表的所有完整色彩,如果有多个解决方案,则列表不需要包含所有这些,只要它包含至少一个。

    练习1:

    > take 1 (graphColor "abc" [1..6] e1 [])
    [[(6,'c'),(5,'a'),(4,'a'),(3,'c'),(2,'b'),(1,'a')]]
    

    练习2:

    > take 1 (graphColor "abc" [1..4] e2 [])
    []
    

    这是我到目前为止的代码,无论如何都会返回空列表。

    graphColor :: [Color] -> [Node] -> [Edge] -> Coloring -> [Coloring]
    -- Fill in your code here
    graphColor colors nodes edges solution
      | length nodes == length solution = [solution]
      | otherwise = concat [graphColor colors nodes edges (newSol : solution) |
                                 x <- nodes, y <- colors,
                                 newSol <- [(x,y)],
                                 notRepeat solution edges x y,
                                 notElem newSol solution]
    notRepeat :: Coloring -> [Edge] -> Node -> Color -> Bool
    notRepeat solution edges x y 
      | x `elem` [z | (z,_) <- solution] = False
      |otherwise = helpHelper edges [z | (z,y) <- solution] x
    
    helpHelper edges matchedNodes x
       = x `elem` [z | y <- matchedNodes, (z,y) <- edges] ++
         [z | y <- matchedNodes, (y,z) <- edges]
    

0 个答案:

没有答案