Take Me Back To Kindergarten

Given a graph with n n vertices and two colors, we want to determine if we can color each vertex such that no two neighboring vertices have the same color. Which way of traversing the graph makes sense for solving this problem?

Relevant graph algorithms :

Breadth First Search Depth First Search Randomly pick each vertex between 1 1 and n n None of the answers are technicaly correct

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Christopher Boo
Aug 10, 2016

"Makes sense" might sounds ambiguous as both approach is valid. But let's say if you are solving this problem on paper, and you want to be systematic , you should apply Breadth-First Search . In other words, you will first select an arbitrary node and color it in 1. Then, you expand to its neighbor and color all of them with 2. If throughout the progress you found that your neighbor has been colored and is the same as the current node, then the colored graph cannot be formed.

If you perform Depth-First Search , you will find yourself confused or missed out some same-colored neighbor as you don't check all your neighbors but keep exploring until the end. Breadth-First Search check for validity while coloring.

I don't think that a graph be colored with just 2 colors. For example how would we color this graph with 2 colors?

Lokesh Sharma - 4 years, 10 months ago

Log in to reply

You are right. Not all graph can be colored with only 2 colors and at the same time no neighboring pairs share the same color. A graph that can be colored this way is called a bipartite graph .

Christopher Boo - 4 years, 10 months ago

im ethiopian too im 18 years old

Nahom Assefa - 1 year, 9 months ago
Mayank Mittal
Dec 6, 2020

The solution can be done by using both the algorithms BFS and DFS. But here BFS make more sense because , BFS will move to the next distance only after traversing all the nodes at current distance.

Formally the BFS algorithm visits all vertices in a graph GGG, that are kkk edges away from the source vertex sss before visiting any vertex k+1k+1k+1 edges away

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...