This project demonstrates the generation of random graphs, the application of graph coloring algorithms, visualization of the results, and saving the graph data in a compressed .gz format using Python. It uses the networkx library for graph operations and matplotlib for visualization.
- β Generate random undirected graphs using the ErdΕsβRΓ©nyi model
- π¨ Color graphs using a greedy algorithm (
largest_firststrategy) - πΌοΈ Visualize colored graphs using Matplotlib
- π Save the graph and its coloring in a compressed
.gzformat
- NetworkX - Graph generation and algorithms
- Matplotlib - Visualization
- NumPy - Math utility
- Gzip - Compression
- Pickle - Object serialization
Make sure you have Python 3.x installed. Then install the required libraries:
pip install networkx matplotlib numpyThis project is implemented in a Jupyter Notebook named Generating_Random_Graph.ipynb. Open the notebook and run the cells sequentially to:
- Generate a random graph with
num_nodesandnum_edges - Apply a greedy coloring algorithm
- Display the graph with nodes colored by their assigned color
- Save the graph and its coloring to
graph_coloring_data.gz
| File | Description |
|---|---|
Generating_Random_Graph.ipynb |
Jupyter Notebook to generate, color, visualize, and save a graph |
graph_coloring_data.gz |
Compressed file containing the graph object and coloring dict |
README.md |
Youβre reading it! |
Graph coloring is the assignment of labels (colors) to vertices of a graph so that no two adjacent vertices share the same color. It's used in:
- Scheduling problems
- Map coloring
- Register allocation in compilers
- Frequency assignment
This project uses the greedy coloring algorithm with the largest_first strategy, which colors the nodes in descending order of their degrees.
After execution, a file named graph_coloring_data.gz will be created, containing:
{
'graph': <networkx.classes.graph.Graph>,
'coloring': {0: 0, 1: 1, 2: 0, ...}
}To load this file later:
import gzip, pickle
with gzip.open('graph_coloring_data.gz', 'rb') as f:
data = pickle.load(f)
G = data['graph']
coloring = data['coloring']Contributions, issues, and feature requests are welcome!
- Fork the repository
- Create your branch:
git checkout -b feature/your-feature - Commit your changes:
git commit -m 'Add your message' - Push to the branch:
git push origin feature/your-feature - Open a pull request
This project is licensed under the MIT License.
If you found this project useful, please βοΈ the repository to help others discover it!
