This project provides a sophisticated solution to the graph coloring problem, incorporating a range of constraints that add complexity and realism to the challenge. The goal is to color the vertices of a graph such that no two adjacent vertices share the same color, all while adhering to additional constraints such as minimizing the number of colors used, respecting pre-assigned colors, handling color exclusions, and dynamically adapting to graph changes.
- Minimum Colors Constraint: Ensures the graph is colored using the minimum number of colors possible.
- Pre-Assigned Colors: Accommodates vertices that have been pre-colored, which cannot be changed during the process.
- Color Exclusion: Efficiently handles situations where certain colors are not allowed on specific vertices.
- Dynamic Graph Changes: Adapts to modifications in the graph (e.g., adding/removing vertices or edges) without the need to completely redo the coloring.
- Heuristic Optimization: Utilizes heuristic approaches to improve performance, such as greedy algorithms and metaheuristic methods.
- Performance-Optimized: Designed to efficiently handle large graphs with up to 10,000 vertices and 50,000 edges.
- Purpose: Manages the graph structure, including adding or removing vertices and edges.
- Key Functions:
add_edge(u, v)
: Connects verticesu
andv
.remove_edge(u, v)
: Removes the connection betweenu
andv
.set_pre_assigned_color(vertex, color)
: Sets a fixed color for a vertex.set_color_exclusion(vertex, color)
: Prevents a vertex from using a specific color.
- Purpose: Handles the graph coloring using the DSATUR algorithm.
- Key Functions:
dsatur()
: Colors the graph while meeting all constraints.apply_color(vertex, color)
: Colors a vertex with the specified color.
- Purpose: Manages and applies constraints during the coloring process.
- Key Function:
enforce_constraints()
: Ensures all constraints are applied during coloring.
- Time Complexity: DSATUR algorithm is efficient, with a complexity of O(V^2 log V), where V is the number of vertices.
- Space Complexity: Uses O(V + E) space, where E is the number of edges.
- Practical Performance: Handles large graphs efficiently and remains responsive with up to 10,000 vertices and 50,000 edges.
- Modular Design: Clear separation of different components.
- Documentation: Comments and explanations are provided for clarity.
- Best Practices: Follows Python conventions for maintainability.
- Clone the Repository:
git clone https://github.com/your-repo/graph_coloring.git cd graph_coloring
- Install Dependencies: No extra packages needed, just Python 3.x.
- Import the classes from
graph_coloring.py
in your project. - Use them as shown in
test_graph_coloring.py
.
To test the solution:
python test_graph_coloring.py
The tests cover:
- Basic Coloring: Simple graph tests.
- Pre-Assigned Colors: Checks respect for pre-colored vertices.
- Color Exclusion: Tests color restrictions.
- Dynamic Changes: Evaluates adaptability to graph modifications.
- Large Graphs: Performance and correctness on big graphs.