Skip to content

A 2-coloring problem solving using bfs to find a path between each adjacent node.

Notifications You must be signed in to change notification settings

developer1231/2-coloring

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

2-coloring

The algorithm i've created is a modified version of the Breadth-First Search (BFS) algorithm tailored specifically for coloring vertices in an undirected graph with two colors, RED and BLUE, ensuring that no two adjacent vertices have the same color.

Here's a description of the algorithm:

Objective:

To traverse an undirected graph using BFS and assign colors (RED and BLUE) to its vertices in such a way that no adjacent vertices share the same color. Algorithm Steps: Initialization: Initialize arrays to track the state of vertices, predecessor information, explored status, and colors for each vertex. Start BFS traversal from a given source vertex (in this case, source = 0).

BFS Traversal:

Initialize a queue (frontier) to store vertices to be visited. Enqueue the source vertex into the frontier queue and mark it as discovered. While the frontier queue is not empty: Dequeue a vertex from the frontier queue (CurrentElement). Explore its neighbors: Check if there is an edge between the CurrentElement and its neighbors. If an edge exists and the neighbor vertex has not been discovered: Mark the neighbor vertex as discovered. Enqueue the neighbor vertex into the frontier queue. Assign a color (alternating between RED and BLUE) to the neighbor vertex such that no adjacent vertices share the same color.

Result:

After the BFS traversal completes, the algorithm assigns colors (RED or BLUE) to each vertex in the graph in such a way that no two adjacent vertices share the same color. The result printed will show the chosen colors for each vertex based on the BFS traversal.

Key Points:

The algorithm utilizes BFS to traverse the graph, maintaining the properties of exploration and checking for neighboring vertices' colors to prevent adjacent vertices from having the same color. It ensures that the graph can be 2-colored using RED and BLUE without violating the constraint where adjacent vertices must have different colors. The output represents the chosen colors for each vertex, demonstrating a proper 2-coloring of the graph. This algorithm provides an efficient way to perform a 2-coloring of an undirected graph using BFS traversal, making it a fundamental approach to graph coloring problems where vertices need to be colored with a limited set of colors while adhering to specific constraints.

Releases

No releases published

Packages

No packages published

Languages