-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added - Add property test `biclique_complement_size` for `adjacency_list`. - Add property test `biclique_complement_size` for `adjacency_matrix`. - Add property test `complete_complement_eq_empty` for `adjacency_list`. - Add property test `complete_complement_eq_empty` for `adjacency_matrix`. - Add property test `cycle_complement_size` for `adjacency_list`. - Add property test `cycle_complement_size` for `adjacency_matrix`. - Add property test `empty_complement_eq_complete` for `adjacency_list`. - Add property test `empty_complement_eq_complete` for `adjacency_matrix`. - Add property test `star_complement_size` for `adjacency_list`. - Add property test `star_complement_size` for `adjacency_matrix`. - Add the `Complement` trait.
- Loading branch information
1 parent
3461b87
commit 8488f55
Showing
11 changed files
with
281 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
//! Generate the complement of a digraph. | ||
//! | ||
//! The complement of a digraph contains all arcs not present in the original | ||
//! digraph. | ||
//! | ||
//! # Examples | ||
//! | ||
//! ``` | ||
//! use graaf::{ | ||
//! adjacency_list::Digraph, | ||
//! gen::Cycle, | ||
//! op::{ | ||
//! AddArc, | ||
//! Arcs, | ||
//! Complement, | ||
//! }, | ||
//! }; | ||
//! | ||
//! // 0 -> {1} | ||
//! // 1 -> {2} | ||
//! // 2 -> {3} | ||
//! // 3 -> {0} | ||
//! | ||
//! let digraph = Digraph::cycle(4); | ||
//! | ||
//! // 0 -> {2, 3} | ||
//! // 1 -> {0, 3} | ||
//! // 2 -> {0, 1} | ||
//! // 3 -> {1, 2} | ||
//! | ||
//! let converse = digraph.complement(); | ||
//! | ||
//! assert!(converse.arcs().eq([ | ||
//! (0, 2), | ||
//! (0, 3), | ||
//! (1, 0), | ||
//! (1, 3), | ||
//! (2, 0), | ||
//! (2, 1), | ||
//! (3, 1), | ||
//! (3, 2) | ||
//! ])); | ||
//! ``` | ||
use crate::gen::Empty; | ||
|
||
use super::{ | ||
AddArc, | ||
HasArc, | ||
Order, | ||
}; | ||
|
||
/// Generate the complement of a digraph. | ||
/// | ||
/// # How do I implement `Complement`? | ||
/// | ||
/// Provide an implementation of `complement` that returns a digraph with all | ||
/// arcs not present in the original digraph. | ||
/// | ||
/// ``` | ||
/// use { | ||
/// graaf::op::Complement, | ||
/// std::collections::BTreeSet, | ||
/// }; | ||
/// | ||
/// struct Digraph { | ||
/// arcs: Vec<BTreeSet<usize>>, | ||
/// } | ||
/// | ||
/// impl Complement for Digraph { | ||
/// fn complement(&self) -> Self { | ||
/// let order = self.arcs.len(); | ||
/// let mut arcs = vec![BTreeSet::<usize>::new(); order]; | ||
/// | ||
/// for u in 0..order { | ||
/// for v in u + 1..order { | ||
/// if !self.arcs[u].contains(&v) { | ||
/// arcs[u].insert(v); | ||
/// } | ||
/// | ||
/// if !self.arcs[v].contains(&u) { | ||
/// arcs[v].insert(u); | ||
/// } | ||
/// } | ||
/// } | ||
/// | ||
/// Self { arcs } | ||
/// } | ||
/// } | ||
/// | ||
/// // 0 -> {1} | ||
/// // 1 -> {2} | ||
/// // 2 -> {3} | ||
/// // 3 -> {0} | ||
/// | ||
/// let digraph = Digraph { | ||
/// arcs: vec![ | ||
/// BTreeSet::from([1]), | ||
/// BTreeSet::from([2]), | ||
/// BTreeSet::from([3]), | ||
/// BTreeSet::from([0]), | ||
/// ], | ||
/// }; | ||
/// | ||
/// // 0 -> {2, 3} | ||
/// // 1 -> {0, 3} | ||
/// // 2 -> {0, 1} | ||
/// // 3 -> {1, 2} | ||
/// | ||
/// let complement = digraph.complement(); | ||
/// | ||
/// assert_eq!( | ||
/// complement.arcs, | ||
/// vec![ | ||
/// BTreeSet::from([2, 3]), | ||
/// BTreeSet::from([0, 3]), | ||
/// BTreeSet::from([0, 1]), | ||
/// BTreeSet::from([1, 2]), | ||
/// ], | ||
/// ); | ||
/// ``` | ||
pub trait Complement { | ||
/// Generates the complement of the digraph. | ||
#[must_use] | ||
fn complement(&self) -> Self; | ||
} | ||
|
||
impl<D> Complement for D | ||
where | ||
D: AddArc + Empty + HasArc + Order, | ||
{ | ||
fn complement(&self) -> Self { | ||
let order = self.order(); | ||
let mut digraph = D::empty(order); | ||
|
||
for u in 0..order { | ||
for v in u + 1..order { | ||
if !self.has_arc(u, v) { | ||
digraph.add_arc(u, v); | ||
} | ||
|
||
if !self.has_arc(v, u) { | ||
digraph.add_arc(v, u); | ||
} | ||
} | ||
} | ||
|
||
digraph | ||
} | ||
} |
Oops, something went wrong.