Skip to content
Snippets Groups Projects
Commit faef859e authored by Guido Trotter's avatar Guido Trotter
Browse files

Ganeti/HTools/Graph Add isColorable


Check whether coloring on a given graph makes sense. This is the case
only if there are no loops and the graph is undirected.

Signed-off-by: default avatarGuido Trotter <ultrotter@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 742bd043
No related branches found
No related tags found
No related merge requests found
...@@ -89,6 +89,14 @@ case_emptyVertColorMapEmpty :: Assertion ...@@ -89,6 +89,14 @@ case_emptyVertColorMapEmpty :: Assertion
case_emptyVertColorMapEmpty = case_emptyVertColorMapEmpty =
assertEqual "" 0 $ IntMap.size emptyVertColorMap assertEqual "" 0 $ IntMap.size emptyVertColorMap
-- | Check that our generated graphs are colorable
prop_isColorableTestableGraph :: TestableGraph -> Property
prop_isColorableTestableGraph (TestableGraph g) = isColorable g ==? True
-- | Check that our generated graphs are colorable
prop_isColorableTestableClique :: TestableClique -> Property
prop_isColorableTestableClique (TestableClique g) = isColorable g ==? True
-- | Check that the given algorithm colors a clique with the same number of -- | Check that the given algorithm colors a clique with the same number of
-- colors as the vertices number. -- colors as the vertices number.
prop_colorClique :: (Graph.Graph -> ColorVertMap) -> TestableClique -> Property prop_colorClique :: (Graph.Graph -> ColorVertMap) -> TestableClique -> Property
...@@ -128,4 +136,6 @@ testSuite "HTools/Graph" ...@@ -128,4 +136,6 @@ testSuite "HTools/Graph"
, 'prop_colorDsaturClique , 'prop_colorDsaturClique
, 'prop_colorLFAllNodes , 'prop_colorLFAllNodes
, 'prop_colorDsaturAllNodes , 'prop_colorDsaturAllNodes
, 'prop_isColorableTestableGraph
, 'prop_isColorableTestableClique
] ]
...@@ -58,11 +58,15 @@ module Ganeti.HTools.Graph ...@@ -58,11 +58,15 @@ module Ganeti.HTools.Graph
, colorInOrder , colorInOrder
, colorLF , colorLF
, colorDsatur , colorDsatur
, isColorable
-- * Color map transformations -- * Color map transformations
, colorVertMap , colorVertMap
-- * Vertex sorting -- * Vertex characteristics
, verticesByDegreeDesc , verticesByDegreeDesc
, verticesByDegreeAsc , verticesByDegreeAsc
, neighbors
, hasLoop
, isUndirected
) where ) where
import Data.Maybe import Data.Maybe
...@@ -106,12 +110,27 @@ verticesByDegreeAsc g = map fst . sortBy (comparing snd) $ verticesDegree g ...@@ -106,12 +110,27 @@ verticesByDegreeAsc g = map fst . sortBy (comparing snd) $ verticesDegree g
neighbors :: Graph.Graph -> Graph.Vertex -> [Graph.Vertex] neighbors :: Graph.Graph -> Graph.Vertex -> [Graph.Vertex]
neighbors g v = g Array.! v neighbors g v = g Array.! v
-- | Check whether a graph has no loops.
-- (vertices connected to themselves)
hasLoop :: Graph.Graph -> Bool
hasLoop g = any vLoops $ Graph.vertices g
where vLoops v = v `elem` neighbors g v
-- | Check whether a graph is undirected
isUndirected :: Graph.Graph -> Bool
isUndirected g =
(sort . Graph.edges) g == (sort . Graph.edges . Graph.transposeG) g
-- * Coloring -- * Coloring
-- | Empty color map. -- | Empty color map.
emptyVertColorMap :: VertColorMap emptyVertColorMap :: VertColorMap
emptyVertColorMap = IntMap.empty emptyVertColorMap = IntMap.empty
-- | Check whether a graph is colorable.
isColorable :: Graph.Graph -> Bool
isColorable g = isUndirected g && not (hasLoop g)
-- | Get the colors of a list of vertices. -- | Get the colors of a list of vertices.
-- Any uncolored vertices are ignored. -- Any uncolored vertices are ignored.
listColors :: VertColorMap -> [Graph.Vertex] -> [Color] listColors :: VertColorMap -> [Graph.Vertex] -> [Color]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment