Mark As Completed Discussion

One Pager Cheat Sheet

  • The Graph Coloring Problem can be solved by partitioning the elements into two different sets such that no two adjacent nodes have the same color.
  • A graph can be successfully 2-colored by visiting each node and assigning an arbitrary color to the first node, then assigning the opposite color to a node n for any of its adjacent nodes that have already been assigned the same color, or determining that it cannot be validly colored if two or more of its adjacent nodes already have opposite colors.
  • The 2-coloring problem can be solved using breadth-first search with each node being assigned the attributes assigned, red and added.
  • The entire method is illustrated in the figure below.
  • Executing the 2 coloring problem on an adjacency matrix has a time complexity of O(n^2), whereas an adjacency list would have a time complexity of O(nk) where k is the maximum degree of all nodes in the graph.

This is our final solution.

To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

You're doing a wonderful job. Keep going!

If you had any problems with this tutorial, check out the main forum thread here.