Skip to main content

Graph

Struct Graph 

Source
pub struct Graph { /* private fields */ }
Expand description

An undirected graph without self-loops or multiple edges.

Implementations§

Source§

impl Graph

Source

pub fn from_vectors( vectors: &[i64], dim: usize, allowed: &[i64], ) -> Result<Graph, Error>

The graph on integer vectors of length dim, held one after another in vectors, with two joined when their inner product is one of allowed.

Sets of vectors with prescribed inner products are then cliques: rows of a Hadamard matrix are pairwise orthogonal (allowed = [0]), equiangular lines have inner products ±c. The inner products are computed in parallel straight into the adjacency matrix, so no edge list is built, which matters when there are hundreds of millions of edges. Entries must be small enough that inner products fit an i64.

Source

pub fn from_words(words: &[u64], distances: &[u32]) -> Graph

The graph on binary words of up to 64 bits, each given as an integer whose bit i is the word’s i-th entry, with two joined when their Hamming distance (the number of places in which they differ) is one of distances.

A binary code of minimum distance d is then a clique when distances is d..=len, and an independent set in the graph with distances 1..d. Distances are computed in parallel straight into the adjacency matrix, each as one exclusive or and a bit count.

Examples found in repository?
examples/popcount_bench.rs (line 52)
38fn main() {
39    let rounds: usize = std::env::args()
40        .nth(1)
41        .and_then(|a| a.parse().ok())
42        .unwrap_or(5);
43    let all15: Vec<u64> = (0..1u64 << 15).collect();
44    let all16: Vec<u64> = (0..1u64 << 16).collect();
45    let weight5: Vec<u64> = (0..1u64 << 18).filter(|w| w.count_ones() == 5).collect();
46    let workloads: [(&str, &[u64], &[u32]); 3] = [
47        ("from_words_all15_d1-3", &all15, &[1, 2, 3]),
48        ("from_words_weight5_len18_d1-5", &weight5, &[1, 2, 3, 4, 5]),
49        ("from_words_all16_d1-3", &all16, &[1, 2, 3]),
50    ];
51    for (name, words, distances) in workloads {
52        let t = time(rounds, || Graph::from_words(words, distances).num_edges());
53        println!("RESULT {name} {t:.4}");
54    }
55    // the control: a search, which counts no bits in its hot loop
56    let g = generate(Model::Exponential, 1000, 500, &mut Xoshiro::new(1)).expect("a random graph");
57    let mut p = SearchParams::new(100, 4950); // a 100-clique: unreachable, so every experiment makes all its moves
58    p.experiments = 32;
59    p.max_moves = 2000;
60    let t = time(rounds, || search(&g, &p).expect("a search").successes());
61    println!("RESULT search_control {t:.4}");
62}
Source

pub fn from_edges<I>(n: usize, edges: I) -> Result<Graph, Error>
where I: IntoIterator<Item = (usize, usize)>,

Builds a graph on n vertices from an edge list.

Source

pub fn empty(n: usize) -> Graph

The empty graph on n vertices.

Source

pub fn num_vertices(&self) -> usize

Number of vertices.

Source

pub fn num_edges(&self) -> usize

Number of edges.

Examples found in repository?
examples/popcount_bench.rs (line 52)
38fn main() {
39    let rounds: usize = std::env::args()
40        .nth(1)
41        .and_then(|a| a.parse().ok())
42        .unwrap_or(5);
43    let all15: Vec<u64> = (0..1u64 << 15).collect();
44    let all16: Vec<u64> = (0..1u64 << 16).collect();
45    let weight5: Vec<u64> = (0..1u64 << 18).filter(|w| w.count_ones() == 5).collect();
46    let workloads: [(&str, &[u64], &[u32]); 3] = [
47        ("from_words_all15_d1-3", &all15, &[1, 2, 3]),
48        ("from_words_weight5_len18_d1-5", &weight5, &[1, 2, 3, 4, 5]),
49        ("from_words_all16_d1-3", &all16, &[1, 2, 3]),
50    ];
51    for (name, words, distances) in workloads {
52        let t = time(rounds, || Graph::from_words(words, distances).num_edges());
53        println!("RESULT {name} {t:.4}");
54    }
55    // the control: a search, which counts no bits in its hot loop
56    let g = generate(Model::Exponential, 1000, 500, &mut Xoshiro::new(1)).expect("a random graph");
57    let mut p = SearchParams::new(100, 4950); // a 100-clique: unreachable, so every experiment makes all its moves
58    p.experiments = 32;
59    p.max_moves = 2000;
60    let t = time(rounds, || search(&g, &p).expect("a search").successes());
61    println!("RESULT search_control {t:.4}");
62}
Source

pub fn has_edge(&self, u: usize, v: usize) -> bool

Whether {u, v} is an edge. Panics if either vertex is out of range.

Source

pub fn row(&self, u: usize) -> &[u64]

Row u of the adjacency matrix as a bitset: bit v % 64 of word v / 64 is set iff {u, v} is an edge. Test bits with bit.

Source

pub fn neighbors(&self, u: usize) -> &[u32]

The neighbors of u, in ascending order.

Source

pub fn degree(&self, u: usize) -> usize

The degree of u.

Source

pub fn degrees(&self) -> Vec<usize>

All degrees, indexed by vertex.

Source

pub fn edges(&self) -> impl Iterator<Item = (usize, usize)> + '_

Edges (u, v) with u < v, in lexicographic order.

Source

pub fn density(&self) -> f64

Edge density m / (n choose 2), or 0 for graphs with fewer than two vertices.

Source

pub fn into_builder(self) -> GraphBuilder

Converts back into a builder, e.g. to modify the graph.

Source

pub fn induced_edge_count(&self, vertices: &[usize]) -> usize

Number of edges among vertices, which must be distinct.

Source

pub fn components(&self) -> (Vec<usize>, usize)

Connected components, as a component index per vertex plus the number of components.

Trait Implementations§

Source§

impl Clone for Graph

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Graph

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Graph

Source§

impl PartialEq for Graph

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl SearchGraph for Graph

Source§

type Row<'a> = &'a [u64]

A row of the adjacency (or weight) matrix.
Source§

fn num_vertices(&self) -> usize

Source§

fn neighbors(&self, u: usize) -> &[u32]

Neighbors of u, ascending.
Source§

fn adjacency_row(&self, u: usize) -> &[u64]

Source§

fn edge_value(&self, u: usize, v: usize) -> i64

What the edge {u, v} contributes to the subgraph’s value: its weight, or 0 or 1.
Source§

fn neighbor_value(&self, _u: usize, _v: usize) -> i64

SearchGraph::edge_value for a known neighbor v of u.
Source§

fn objective_bound(&self) -> i64

The initial “best value so far” of an experiment: the sum of the degrees in sub_search, the total weight in wsub_search.
Source§

fn max_row_value(&self, _u: usize) -> i64

An upper bound on AdjacencyRow::value in u’s row: 1 in an unweighted graph, the largest weight at u in a weighted one. The search uses it to skip rows that cannot hold a best move; the default, a bound too large to skip any, is always correct.
Source§

fn as_graph(&self) -> Option<&Graph>

The graph as an unweighted Graph, if it is one (regular searches need one).
Source§

impl StructuralPartialEq for Graph

Auto Trait Implementations§

§

impl Forget for Graph

§

impl Freeze for Graph

§

impl Move for Graph

§

impl RefUnwindSafe for Graph

§

impl Send for Graph

§

impl Sync for Graph

§

impl Unpin for Graph

§

impl UnsafeUnpin for Graph

§

impl UnwindSafe for Graph

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.