From 4c35c36b89ec6c53f4adaebe3fd08150c8f2e8f8 Mon Sep 17 00:00:00 2001 From: yashksaini-coder Date: Mon, 28 Oct 2024 18:57:08 +0530 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Add=20Graph-coloring?= =?UTF-8?q?=20algorithm?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✅ Closes: #1335 --- Graph-coloring/README.md | 158 +++++++++++++++++++++++++++++++++++++++ Graph-coloring/program.c | 65 ++++++++++++++++ 2 files changed, 223 insertions(+) create mode 100644 Graph-coloring/README.md create mode 100644 Graph-coloring/program.c diff --git a/Graph-coloring/README.md b/Graph-coloring/README.md new file mode 100644 index 00000000..36bcce71 --- /dev/null +++ b/Graph-coloring/README.md @@ -0,0 +1,158 @@ +### C Code: Graph Coloring using Greedy Algorithm + +```c +#include +#include + +#define V 4 + +// Function to check if the color assignment is valid for the given vertex +bool isSafe(int v, int graph[V][V], int color[], int c) { + for (int i = 0; i < V; i++) + if (graph[v][i] && color[i] == c) + return false; + return true; +} + +// Recursive function to assign colors to vertices +bool graphColoringUtil(int graph[V][V], int m, int color[], int v) { + if (v == V) + return true; + + for (int c = 1; c <= m; c++) { + if (isSafe(v, graph, color, c)) { + color[v] = c; + + if (graphColoringUtil(graph, m, color, v + 1)) + return true; + + color[v] = 0; + } + } + + return false; +} + +// Main function to solve the m-coloring problem +bool graphColoring(int graph[V][V], int m) { + int color[V] = {0}; + + if (!graphColoringUtil(graph, m, color, 0)) { + printf("Solution does not exist\n"); + return false; + } + + printf("Solution exists with the following color assignment:\n"); + for (int i = 0; i < V; i++) + printf("Vertex %d ---> Color %d\n", i, color[i]); + + return true; +} + +int main() { + int graph[V][V] = { + {0, 1, 1, 1}, + {1, 0, 1, 0}, + {1, 1, 0, 1}, + {1, 0, 1, 0} + }; + + int m = 3; + graphColoring(graph, m); + + return 0; +} +``` + +--- + +### README Document for Graph Coloring Algorithm + +--- + +# Graph Coloring Algorithm in C + +This project demonstrates the Graph Coloring algorithm using a greedy approach implemented in C. The algorithm tries to color the vertices of a graph such that no two adjacent vertices share the same color. + +## Table of Contents +- [Description](#description) +- [Algorithm](#algorithm) +- [Usage](#usage) +- [Example](#example) +- [Limitations](#limitations) + +## Description + +Graph coloring is a way of coloring the vertices of a graph such that no two adjacent vertices have the same color. This problem is widely used in scheduling, register allocation in compilers, and many other optimization areas. + +### Problem Statement + +Given a graph represented by an adjacency matrix, and an integer `m` representing the number of colors, assign a color to each vertex such that: +- No two adjacent vertices have the same color. +- The solution uses at most `m` colors. + +### Solution Approach + +This implementation uses a backtracking approach: +1. **Color Assignment**: It attempts to color each vertex starting from vertex 0. +2. **Backtracking**: If the current color assignment leads to a conflict, it backtracks and tries another color. +3. **Stopping Condition**: The algorithm stops if all vertices are successfully colored or if no solution exists. + +## Usage + +### Requirements +- C compiler (like GCC). + +### Running the Program + +1. **Compile** the code using the following command: + ```bash + gcc graph_coloring.c -o graph_coloring + ``` +2. **Execute** the program: + ```bash + ./graph_coloring + ``` + +The program will output a possible coloring for the graph if a solution exists with the given number of colors, or it will indicate that no solution is possible. + +### Code Structure + +- **isSafe**: Checks if it’s safe to color a vertex with a particular color. +- **graphColoringUtil**: Tries to color each vertex and recursively backtracks if needed. +- **graphColoring**: The main function that initializes color assignments and starts the recursive coloring. + +## Example + +Using the adjacency matrix: +``` +graph[V][V] = { + {0, 1, 1, 1}, + {1, 0, 1, 0}, + {1, 1, 0, 1}, + {1, 0, 1, 0} +}; +``` + +With `m = 3` colors, a possible output is: +``` +Solution exists with the following color assignment: +Vertex 0 ---> Color 1 +Vertex 1 ---> Color 2 +Vertex 2 ---> Color 3 +Vertex 3 ---> Color 1 +``` + +This result assigns colors such that no two adjacent vertices share the same color. + +## Limitations + +- This approach is not efficient for large graphs, as it uses a backtracking technique that has exponential time complexity in the worst case. +- The solution may not be optimal for large and complex graphs. + +--- + +### Notes + +- Adjust the adjacency matrix and `m` (number of colors) as needed to test different graphs. +- This code can be modified to use a non-greedy approach for graphs where a minimal coloring is essential. diff --git a/Graph-coloring/program.c b/Graph-coloring/program.c new file mode 100644 index 00000000..59c6684e --- /dev/null +++ b/Graph-coloring/program.c @@ -0,0 +1,65 @@ +#include +#include + +#define V 4 // Number of vertices in the graph + +// Function to check if the color assignment is valid for the given vertex +bool isSafe(int v, int graph[V][V], int color[], int c) { + for (int i = 0; i < V; i++) + if (graph[v][i] && color[i] == c) // Check if adjacent vertices have the same color + return false; + return true; +} + +// Recursive function to assign colors to vertices +bool graphColoringUtil(int graph[V][V], int m, int color[], int v) { + if (v == V) // All vertices are assigned a color + return true; + + for (int c = 1; c <= m; c++) { + if (isSafe(v, graph, color, c)) { + color[v] = c; + + // Recur to assign colors to the rest of the vertices + if (graphColoringUtil(graph, m, color, v + 1)) + return true; + + color[v] = 0; // Backtrack + } + } + + return false; +} + +// Main function to solve the m-coloring problem +bool graphColoring(int graph[V][V], int m) { + int color[V] = {0}; // Initialize all vertices as unassigned (0) + + if (!graphColoringUtil(graph, m, color, 0)) { + printf("Solution does not exist\n"); + return false; + } + + // Print the color assignment + printf("Solution exists with the following color assignment:\n"); + for (int i = 0; i < V; i++) + printf("Vertex %d ---> Color %d\n", i, color[i]); + + return true; +} + +// Main function +int main() { + // Example adjacency matrix for a graph + int graph[V][V] = { + {0, 1, 1, 1}, + {1, 0, 1, 0}, + {1, 1, 0, 1}, + {1, 0, 1, 0} + }; + + int m = 3; // Number of colors + graphColoring(graph, m); + + return 0; +} From b52165ca2dd0a44e9fddb199084a3931c78273c3 Mon Sep 17 00:00:00 2001 From: yashksaini-coder Date: Tue, 29 Oct 2024 16:58:46 +0530 Subject: [PATCH 2/2] Removed the file to graph folder --- {Graph-coloring => Graph Algorithms/Graph-coloring}/README.md | 0 {Graph-coloring => Graph Algorithms/Graph-coloring}/program.c | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {Graph-coloring => Graph Algorithms/Graph-coloring}/README.md (100%) rename {Graph-coloring => Graph Algorithms/Graph-coloring}/program.c (100%) diff --git a/Graph-coloring/README.md b/Graph Algorithms/Graph-coloring/README.md similarity index 100% rename from Graph-coloring/README.md rename to Graph Algorithms/Graph-coloring/README.md diff --git a/Graph-coloring/program.c b/Graph Algorithms/Graph-coloring/program.c similarity index 100% rename from Graph-coloring/program.c rename to Graph Algorithms/Graph-coloring/program.c