Skip to main content

UndirectedGraph

Struct UndirectedGraph 

Source
pub struct UndirectedGraph {
    pub n: usize,
    pub adj: Vec<HashSet<usize>>,
}
Expand description

An undirected simple graph (represented as adjacency sets for simplicity).

Fields§

§n: usize

Number of vertices.

§adj: Vec<HashSet<usize>>

Adjacency sets: adj[u] = set of neighbors

Implementations§

Source§

impl UndirectedGraph

Source

pub fn new(n: usize) -> Self

Create with n vertices.

Source

pub fn add_edge(&mut self, u: usize, v: usize)

Add undirected edge.

Source

pub fn bfs(&self, s: usize) -> Vec<usize>

BFS from source s.

Source

pub fn is_connected(&self) -> bool

Is the graph connected?

Source

pub fn num_components(&self) -> usize

Number of connected components.

Source

pub fn bipartite_coloring(&self) -> Option<Vec<usize>>

Is the graph bipartite? Returns Some(coloring) or None.

Source

pub fn is_bipartite(&self) -> bool

Is the graph bipartite?

Source

pub fn all_degrees_even(&self) -> bool

Check if all vertex degrees are even (necessary for Eulerian circuit).

Source

pub fn has_eulerian_circuit(&self) -> bool

Check if graph has an Eulerian circuit (connected + all even degrees).

Source

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

Greedy graph coloring (returns number of colors used and coloring).

Source

pub fn is_proper_coloring(&self, coloring: &[usize]) -> bool

Check if a given coloring is proper.

Source

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

Degree of vertex u.

Source

pub fn edge_count(&self) -> usize

Number of edges.

Source

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

Minimum spanning tree (Prim’s algorithm, unweighted → any spanning tree). Returns edges of the spanning tree.

Source

pub fn complete(n: usize) -> Self

Create complete graph K_n.

Source

pub fn path(n: usize) -> Self

Create path graph P_n (0-1-2-…-n-1).

Source

pub fn cycle(n: usize) -> Self

Create cycle graph C_n.

Source

pub fn complete_bipartite(m: usize, n: usize) -> Self

Create complete bipartite graph K_{m,n}.

Trait Implementations§

Source§

impl Clone for UndirectedGraph

Source§

fn clone(&self) -> UndirectedGraph

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for UndirectedGraph

Source§

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

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

impl Default for UndirectedGraph

Source§

fn default() -> UndirectedGraph

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.