August 28, 2024

P86 - Node degree and graph coloration.

a) Write a method Node.degree that determines the degree of a given node.

scala

scala> Graph.fromString("[a-b, b-c, a-c, a-d]").nodes("a").degree
res0: Int = 3

b) Write a method that lists all nodes of a graph sorted according to decreasing degree.

scala

scala> Graph.fromString("[a-b, b-c, a-c, a-d]").nodesByDegree
res1: List[Graph[String,Unit]#Node] = List(Node(a), Node(c), Node(b), Node(d))

c) Use Welsh-Powell’s algorithm to paint the nodes of a graph in such a way that adjacent nodes have different colors.  Make a method colorNodes that returns a list of tuples, each of which contains a node and an integer representing its color.

scala

scala> Graph.fromString("[a-b, b-c, a-c, a-d]").colorNodes
res2: List[(Graph[String,Unit]#Node,Int)] = List((Node(a),1), (Node(b),2), (Node(c), 3), (Node(d), 2))
Be first to comment
Leave a reply