Debugging 调试一个简单的haskell函数以将颜色放置到节点;graphColor";
因此,我在haskell有一个任务,我一直坚持我的逻辑,这就是给我的问题: graphColor查找图形的所有可能着色 着色是将节点分配给颜色 这样,如果两个节点通过一个节点连接,则它们必须具有不同的颜色 边缘 graphColor采用以下参数Debugging 调试一个简单的haskell函数以将颜色放置到节点;graphColor";,debugging,haskell,Debugging,Haskell,因此,我在haskell有一个任务,我一直坚持我的逻辑,这就是给我的问题: graphColor查找图形的所有可能着色 着色是将节点分配给颜色 这样,如果两个节点通过一个节点连接,则它们必须具有不同的颜色 边缘 graphColor采用以下参数 颜色列表 节点列表 边列表(即节点对) 部分着色(即节点和颜色对的列表) 边的示例列表 e1 :: [Edge] e1 = [(1,3),(2,1),(2,5),(3,1),(3,2),(3,3),(3,4),(3,5),(5,6),(2,6)] e2
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:
例2:
这是迄今为止我的代码,它只返回空列表,不管发生什么
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]
graphColor::[Color]->[Node]->[Edge]->Coloring->[Coloring]
--在这里填写您的代码
graphColor节点边解决方案
|长度节点==长度解决方案=[解决方案]
|否则=concat[图形颜色节点边(newSol:解决方案)|
x颜色->布尔
不重复解决方案边x y
|x`elem`[z |(z,|)
> 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]