Skip to content

New addition: Chromatic numbers! #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Graph-Coloring/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# Problem: Graph Coloring
# Problem: Graph Coloring/Colorability

## Description

Given a graph *G=(V, E)*, where *V* is a set of vertices and *E* is a set of edges and a set of colors *C*, find an assignment of colors to vertices such that no two adjacent vertices share an edge.

Two problems related to the *graph coloring* problem are:
* **k-coloring** - Is there a coloring with *k* colors? E.g., 3-colorability.
* **chromatic numbers** - Find the *minimum* number of colors for a given graph.

## Example
A graph with 10 vertices and 3 colors.

Expand Down
37 changes: 37 additions & 0 deletions Graph-Coloring/chromatic_number.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
%*
* File: chromatic_number.lp
* Author: David Gonzalez, Joshua T. Guerin
*
* Description: Given a graph, find the minimal set of colors s.t. no
adjacent veritices share a color.
* Use: clingo chromatic_number.lp instance.lp
*%

% Count the number of nodes/max colors.
count(N) :- N = #count{X : node(X)}.

% Set of available colors (max=number of nodes).
color(1..N) :- count(N).

% Edges are symmetric.
% Assumes potentially directed description of an undirected graph.
edge(M, N) :- edge(N, M).

% Each vertex is assigned a single color.
1 { color(N, C) : color(C) } 1 :- node(N).

% No two adjecent vertices can have the same color.
:- edge(N, M), color(N, C), color(M, C).

% Count the number of colors.
number(X) :- X = #count{ C : color(N, C) }.

% Minimize the number of colors used.
#minimize { X : number(X) }.

% PRINT
% Optional--display of color information is likely useful.
#show color/2.

% The chromatic number.
#show number/1.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ You are absolutely correct! The term really doesn't apply based on the definitio
## Organization
While I am opting for a flat directory organization at the moment for ease of navigation, one possible organization of the problems for the reader could be:

| First Header | Second Header |
| Category | Problems |
| ------------- | ------------- |
| Number Theory | [Composite Numbers](https://github.com/joshuaguerin/Answer-Set-Programming-Algorithms/tree/master/Prime-Sieve) |
| | [Prime Sieve](https://github.com/joshuaguerin/Answer-Set-Programming-Algorithms/tree/master/Prime-Sieve) |
Expand All @@ -26,6 +26,7 @@ While I am opting for a flat directory organization at the moment for ease of na
| Puzzles/Games | [N-Queens](https://github.com/joshuaguerin/Answer-Set-Programming-Algorithms/tree/master/N-Queens) |
| | [Sudoku](https://github.com/joshuaguerin/Answer-Set-Programming-Algorithms/tree/master/Sudoku) |
| Graphs | [Graph Coloring](https://github.com/joshuaguerin/Answer-Set-Programming-Algorithms/tree/master/Graph-Coloring)
| | [Chromatic Numbers](https://github.com/joshuaguerin/Answer-Set-Programming-Algorithms/tree/master/Graph-Coloring)
| | [Clique](https://github.com/joshuaguerin/Answer-Set-Programming-Algorithms/tree/master/Clique) |
| | [Dominating Set](https://github.com/joshuaguerin/Answer-Set-Programming-Algorithms/tree/master/Dominating-Set) |
| | [Independent Set](https://github.com/joshuaguerin/Answer-Set-Programming-Algorithms/tree/master/Independent_Set) |
Expand Down