Skip to content

Added graph_coloring algorithm to backtracking #759

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

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Src
* Backtracking
* [All Combination Of Size K](https://github.com/TheAlgorithms/Rust/blob/master/src/backtracking/all_combination_of_size_k.rs)
* [Graph Coloring](https://github.com/TheAlgorithms/Rust/blob/master/src/backtracking/graph_coloring.rs)
* [Hamiltonian Cycle](https://github.com/TheAlgorithms/Rust/blob/master/src/backtracking/hamiltonian_cycle.rs)
* [Knight Tour](https://github.com/TheAlgorithms/Rust/blob/master/src/backtracking/knight_tour.rs)
* [N Queens](https://github.com/TheAlgorithms/Rust/blob/master/src/backtracking/n_queens.rs)
Expand Down
127 changes: 127 additions & 0 deletions src/backtracking/graph_coloring.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//graph-colouring uses backtracking algorithm
//it colors the graph in such a way that no two adjacent vertices are of same color
//if two adjacent vertices are found at same color it aims to solve that using backtracking

use std::fmt::Write;
// Checks whether no two adjacent vertices are of the same color
fn is_safe(g: &[Vec<i32>], color: &[i32], v: usize, c: i32) -> bool {
for (i, &val) in g[v].iter().enumerate() {
if val == 1 && color[i] == c {
return false;
}
}
true
}

// Displays the color assigned to each vertex
fn display(color: &[i32], solution_number: &mut i32, output: &mut String) {
writeln!(output, "Solution {solution_number}:").expect("Failed to write to output");

println!("{output}");
*solution_number += 1;
for (i, &c) in color.iter().enumerate() {
writeln!(output, "Vertex {} -> Color {}", i + 1, c).expect("Failed to write to output");

println!("{output}");
}
output.push('\n');
}

// Solves the graph coloring problem using backtracking
pub fn graph_coloring(
g: &[Vec<i32>],
color: &mut [i32],
v: usize,
m: i32,
solution_number: &mut i32,
output: &mut String,
) -> bool {
if v == g.len() {
display(color, solution_number, output);
return true;
}
let mut res = false;
for i in 1..=m {
if is_safe(g, color, v, i) {
color[v] = i;
res = graph_coloring(g, color, v + 1, m, solution_number, output) || res;
color[v] = 0;
}
}
res
}

// Test function
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn graph_coloring_check_3() {
let g = vec![
vec![0, 1, 1, 1],
vec![1, 0, 1, 0],
vec![1, 1, 0, 1],
vec![1, 0, 1, 0],
];
let mut color = vec![0; 4];
let mut solution_number = 1;
let mut output = String::new();
let expected_output = "Solution 1:
Vertex 1 -> Color 1
Vertex 2 -> Color 2
Vertex 3 -> Color 3
Vertex 4 -> Color 2

Solution 2:
Vertex 1 -> Color 1
Vertex 2 -> Color 3
Vertex 3 -> Color 2
Vertex 4 -> Color 3

Solution 3:
Vertex 1 -> Color 2
Vertex 2 -> Color 1
Vertex 3 -> Color 3
Vertex 4 -> Color 1

Solution 4:
Vertex 1 -> Color 2
Vertex 2 -> Color 3
Vertex 3 -> Color 1
Vertex 4 -> Color 3

Solution 5:
Vertex 1 -> Color 3
Vertex 2 -> Color 1
Vertex 3 -> Color 2
Vertex 4 -> Color 1

Solution 6:
Vertex 1 -> Color 3
Vertex 2 -> Color 2
Vertex 3 -> Color 1
Vertex 4 -> Color 2

";

graph_coloring(&g, &mut color, 0, 3, &mut solution_number, &mut output);
assert_eq!(expected_output, output);
}
#[test]
fn graph_coloring_check_2() {
let g = vec![
vec![0, 1, 1, 1],
vec![1, 0, 1, 0],
vec![1, 1, 0, 1],
vec![1, 0, 1, 0],
];
let mut color = vec![0; 4];
let mut solution_number = 1;
let mut output = String::new();
let expected_output = "";

graph_coloring(&g, &mut color, 0, 2, &mut solution_number, &mut output);
assert_eq!(expected_output, output);
}
}
2 changes: 2 additions & 0 deletions src/backtracking/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod all_combination_of_size_k;
mod graph_coloring;
mod hamiltonian_cycle;
mod knight_tour;
mod n_queens;
Expand All @@ -8,6 +9,7 @@ mod rat_in_maze;
mod sudoku;

pub use all_combination_of_size_k::generate_all_combinations;
pub use graph_coloring::graph_coloring;
pub use hamiltonian_cycle::find_hamiltonian_cycle;
pub use knight_tour::find_knight_tour;
pub use n_queens::n_queens_solver;
Expand Down