[Math] Coloring problem on directed graph

algorithmscomputer sciencegraph theory

Let $D=(V,E)$ a directed graph.

How to color nodes of $D$ in white and black such that:

  1. No two white colors are adjacent, and
  2. For each black node there exists at least one white node which is adjacent to it or between some white node and the black node there exists one directed path of length at most $2$.

It can be done using $DFS$, but how to start? It sound like bipartite/tripartite directed graphs..

Best Answer

This problem is equivalent to finding a maximal independent set and colouring it white. To solve it, depth-first search on the graph, and colour the source node white.

If the graph is directed, change it to an undirected graph by replacing every edge with an undirected edge.

To clarify the terminology, "no two white colors are adjacent" means if 2 white vertices are connected by a directed edge, it violates this condition, so here the edge behaves as it is undirected. Since "between the white node and the black node there exists one directed path of length at most 2", so if there is an edge from the white node to the black node, it satisfies this criteria. Since "for each black node there exist at least one white node which is adjacent to it", if there is an edge from the black node to the white node, this criteria is also satisfied.

Let $x$ be the current node in the depth-first search.

If any of $x$'s neighbours has been coloured white, colour $x$ black. Otherwise, colour $x$ white.

Checking $x$'s neighbours can be done in $O(E)$ time for the entire graph over the depth first search, hence the entire algorithm is $O(V+E)$ time if the graph is stored as an adjacency list.

This algorithm terminates. We now show it works. If a node is coloured black, then some neighbour has to be white so that it can be coloured black.

Otherwise, a node is coloured white. The visited neighbours of the node (when the node is visited) are black. The subsequent unvisited neighbours of the nodes will not be coloured white as they are adjacent to a node that is coloured white. Hence all neighbours will be coloured black.

Hence, this satisfies your condition.

Related Question