Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

COMP 6651 - Online Graph Coloring Project

Project Overview

This project implements and analyzes greedy algorithms for coloring online graphs:

  • FirstFit: Standard greedy online coloring
  • FirstFit + Degree Heuristic: Process high-degree vertices first
  • FirstFit + Smallest-Last Heuristic: Advanced ordering (bonus implementation)
  • CBIP: Coloring Based on Interval Partitioning (bipartite graphs only)

Quick Start

1. Run Tests (Highly Recommended First!)

python3 test.py

Expected output:

✓ Graph: basic operations
✓ Generator: creates valid k-colourable graphs
✓ FirstFit: path graph
✓ CBIP: generated k=2 graph
...
✓ ALL TESTS PASSED

2. Run Experiments

# Default configuration (recommended for project submission)
python3 main.py

# Quick test (ONLY for verification)

python3 main.py --quick

#OR

# Instead of N=100, use N=20 or N=30
python3 main.py --N 20

# This will give you:
# - 5x faster execution
# - Still statistically valid results
# - Standard deviation might be slightly higher


# Custom parameters
python3 main.py --n-values 50,100,200,400 --N 50 --p 0.4

3. Generate Plots and Analysis

python3 analyze.py

Requires matplotlib: pip install matplotlib

File Structure

.
├── graph.py          # Graph data structure and EDGES I/O
├── generator.py      # k-colourable graph generator
├── coloring.py       # All coloring algorithms
├── simulate.py       # Experiment framework
├── main.py           # Main execution script
├── test.py           # Comprehensive test suite
├── analyze.py        # Analysis and plotting
├── README.md         # This file
├── results/          # Output CSV files (auto-created)
└── plots/            # Visualization files (auto-created)

Graph Generator

  • Partitions vertices into k independent sets
  • Adds mandatory cross-partition edges
  • Adds additional edges with probability p
  • Includes verification function to validate partitions

FirstFit Algorithms

  1. FirstFit (Random): Standard greedy with random vertex order
  2. FirstFit + Degree: Processes high-degree vertices first
  3. FirstFit + Smallest-Last: Uses heap-based efficient ordering (O(V log V))

CBIP (k=2 only)

  • Finds bipartition of current revealed graph
  • Colors new vertex based on partition membership
  • Detects non-bipartite graphs (raises RuntimeError)
  • Significantly outperforms FirstFit on bipartite graphs

Default Experimental Parameters

n_values = [50, 100, 200, 400, 800, 1600]  # Vertex counts
k_values = [2, 3, 4]                       # Chromatic numbers
p = 0.3                                     # Edge probability
N = 100                                     # Graphs per (k, n)
seed = 42                                   # Random seed

Key observations:

  • Competitive ratio grows sub-linearly with n
  • Heuristics provide consistent improvement (5-10%)
  • CBIP significantly outperforms FirstFit for bipartite graphs (20-30%)
  • Standard deviation decreases with larger n

Testing & Validation

The test suite includes:

  • Graph operations (add_edge, degree, neighbors)
  • Generator (partition validity, reproducibility)
  • FirstFit variants (correctness on known graphs)
  • CBIP (bipartite detection, proper coloring)
  • Validation (detects invalid colorings)
  • Heuristics (verify improvement over baseline)

All colorings are validated to ensure no adjacent vertices share the same color.

Key algorithms implemented:

  1. Graph Generator (generator.py):

    • Partitions n vertices into k independent sets
    • Ensures connectivity between partitions
    • Probability-based edge addition
  2. FirstFit (coloring.py):

    • Random vertex ordering for true online simulation
    • Greedy color assignment
    • O(V × max_degree) time complexity
  3. CBIP (coloring.py):

    • BFS-based component finding
    • Bipartitioning with BFS
    • Partition-aware color selection
    • Only applicable to k=2 (bipartite graphs)
  4. Heuristics:

    • Degree ordering: O(V log V) sorting
    • Smallest-last: O(V log V) heap-based removal

Correctness Testing Section

Evidence of correctness:

  • All 13+ unit tests pass
  • Validation confirms no adjacent vertices share colors
  • Generated graphs verified to have k-independent sets
  • Manual verification on small known graphs (K₃, K₃,₃, paths)
  • Tested on graphs up to n=1600

Results Section

Include the CSV files generated in results/ directory:

  • results_firstfit_family.csv: All FirstFit variants
  • results_cbip.csv: CBIP results

Tables should include: Algorithm, k, n, N, ρ(Alg), SD(ρ), min, max

Analysis Section

Key insights to discuss:

  1. Growth Analysis:

    • Competitive ratio increases with n but sub-linearly
    • Growth rate appears logarithmic for all algorithms
    • Standard deviation decreases with larger n (stability)
  2. Heuristic Performance:

    • Degree ordering: 5-10% improvement
    • Smallest-last: 8-12% improvement
    • Both heuristics consistent across k values
  3. CBIP vs FirstFit:

    • CBIP achieves 20-30% improvement on bipartite graphs
    • CBIP stays close to optimal (ρ ≈ 1.1-1.3)
    • FirstFit reaches ρ ≈ 1.5-1.8 for k=2
  4. Why CBIP for k≥3 is Impractical:

    • Bipartitioning (k=2): O(V+E) using BFS
    • k-partitioning (k≥3): NP-complete
    • Would require solving ~160,000 NP-complete problems for N=100, n=1600

Reproducibility

All algorithms support deterministic seeding:

first_fit(graph, rng_seed=42)
cbip(graph, rng_seed=42)

Validation

Every coloring is validated:

valid, msg = validate_coloring(graph, coloring)
if not valid:
    print(f"Error: {msg}")

Partition Verification

Generator output is verified:

g, S = generate_k_colourable_graph(n, k, p)
valid, msg = verify_partition(g, S)

Troubleshooting

Problem: Tests fail
Solution: Check Python version (3.7+), ensure no external dependencies

Problem: "matplotlib not installed"
Solution: pip install matplotlib (optional, only for plots)

Problem: Memory issues with large graphs
Solution: Reduce maximum n or reduce N (number of graphs per setting)

Problem: Slow execution
Solution: Use --quick flag for testing, or reduce N

References

[1] Y. Li, V. Narayan, and D. Pankratov, "Online coloring and a new type of adversary for online graph problems," Algorithmica, vol. 84, pp. 1232–1251, 2022.

[2] A. Gyárfás and J. Lehel, "On-line and first fit colorings of graphs," Journal of Graph Theory, vol. 12, no. 2, pp. 217–227, 1988.

[3] L. Lovász, M. Saks, and W. Trotter, "An on-line graph coloring algorithm with sublinear performance ratio," Graph Theory and Combinatorics 1988, 1989.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages