Skip to main content

Graph

Struct Graph 

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

Counterpart of igraph_t (see references/igraph/include/igraph_datatype.h).

Phase-0 callers (bfs, read_edgelist, oracle tests) only depended on with_vertices, add_edge, add_edges, vcount, ecount, neighbors, degree — those signatures are preserved here, so existing call sites compile unchanged. New for Phase 1: new (with directed flag), is_directed.

Implementations§

Source§

impl Graph

Source

pub fn new(n: u32, directed: bool) -> IgraphResult<Self>

Construct an empty graph on n vertices.

Counterpart of igraph_empty(); directed defaults to false if you use Graph::with_vertices instead.

Source

pub fn with_vertices(n: u32) -> Self

Construct an empty undirected graph on n vertices.

Equivalent to Graph::new(n, false).unwrap() for the success path. Phase-0 compatibility shim — exact signature preserved.

Source

pub fn vcount(&self) -> u32

Number of vertices. Counterpart of igraph_vcount().

Source

pub fn ecount(&self) -> usize

Number of edges. Counterpart of igraph_ecount().

Source

pub fn is_directed(&self) -> bool

true if the graph is directed. Counterpart of igraph_is_directed().

Source

pub fn add_vertices(&mut self, nv: u32) -> IgraphResult<(VertexId, VertexId)>

Append nv isolated vertices, returning the inclusive id range (first, last) of the new vertices. If nv == 0 returns (self.n, self.n) and does nothing.

Counterpart of igraph_add_vertices().

Source

pub fn add_edge(&mut self, u: VertexId, v: VertexId) -> IgraphResult<()>

Add a single edge from u to v.

Self-loops and parallel edges are allowed. For undirected graphs the edge is canonicalised so the stored from <= to.

Source

pub fn add_edges<I>(&mut self, edges: I) -> IgraphResult<()>
where I: IntoIterator<Item = (VertexId, VertexId)>,

Add a sequence of edges. After all edges are appended, the indexes (oi / ii / os / is) are rebuilt in one pass — counterpart of igraph_add_edges (type_indexededgelist.c:254-367).

Source

pub fn neighbors(&self, v: VertexId) -> IgraphResult<Vec<VertexId>>

Out-edge neighbour iterator for vertex v.

For undirected graphs this returns all neighbours (since the indexing tracks both endpoints symmetrically). Order is the upstream igraph order — edges are visited in oi order, then ii order, with duplicates suppressed when the same edge is incident on both.

Counterpart of igraph_neighbors(graph, _, vid, IGRAPH_ALL, ...).

Source

pub fn degree(&self, v: VertexId) -> IgraphResult<usize>

Degree of vertex v — number of edges incident to it.

On undirected graphs every edge counts once except a self-loop which counts twice (matches upstream igraph’s IGRAPH_LOOPS = TWICE default at type_indexededgelist.c:1162).

Counterpart of igraph_degree_1(_, _, _, IGRAPH_ALL, IGRAPH_LOOPS_TWICE).

Source

pub fn edge_source(&self, eid: EdgeId) -> IgraphResult<VertexId>

Source endpoint of edge eid. Counterpart of IGRAPH_FROM (igraph_interface.h:115).

Source

pub fn edge_target(&self, eid: EdgeId) -> IgraphResult<VertexId>

Target endpoint of edge eid. Counterpart of IGRAPH_TO (igraph_interface.h:128).

Source

pub fn edge(&self, eid: EdgeId) -> IgraphResult<(VertexId, VertexId)>

Both endpoints of edge eid, ordered as (from, to). Counterpart of igraph_edge (igraph_interface.h:71).

Source

pub fn edge_other(&self, eid: EdgeId, vid: VertexId) -> IgraphResult<VertexId>

The other endpoint of eid given one endpoint vid. Counterpart of IGRAPH_OTHER (igraph_interface.h:145). Errors if vid is not actually an endpoint of eid.

Source

pub fn incident(&self, v: VertexId) -> IgraphResult<Vec<EdgeId>>

Edge ids incident to vertex v, in the same iteration order as Graph::neighbors.

For undirected graphs returns the union of out-side (oi) and in-side (ii) edges — every edge incident to v once, except self-loops which appear twice (matching igraph_neighbors / igraph_degree’s IGRAPH_LOOPS_TWICE default at type_indexededgelist.c:1162).

For directed graphs returns out-edges only, mirroring this AWU’s neighbors() choice. (The full mode-aware variant lands later alongside igraph_neighbors(mode = IN/OUT/ALL).)

Counterpart of igraph_incident(_, _, v, IGRAPH_ALL, IGRAPH_LOOPS_TWICE) for undirected; IGRAPH_OUT mode for directed.

Source

pub fn get_eid(&self, from: VertexId, to: VertexId) -> IgraphResult<EdgeId>

Edge id between from and to, if any.

On undirected graphs (u, v) and (v, u) are equivalent. On directed graphs the search follows the edge direction from -> to. Returns crate::IgraphError::InvalidArgument when no such edge exists; for the “no error, return None” variant use Self::find_eid.

Counterpart of igraph_get_eid(_, _, from, to, /*directed=*/true, /*error=*/true) from references/igraph/src/graph/type_indexededgelist.c:1522-1555. Phase-1 minimal slice: linear scan across the from-bucket; the upstream binary-search optimisation lands in a perf pass.

Source

pub fn find_eid( &self, from: VertexId, to: VertexId, ) -> IgraphResult<Option<EdgeId>>

Edge id between from and to, or None if not connected.

Same semantics as Self::get_eid but no-error variant matching upstream’s error=false mode. When parallel edges exist, returns the lowest edge id (matching upstream’s “always returns the same edge ID” guarantee).

Source

pub fn get_all_eids_between( &self, from: VertexId, to: VertexId, ) -> IgraphResult<Vec<EdgeId>>

All edge ids between from and to, including parallel edges and (for self-loops) the loop edge once.

Counterpart of igraph_get_all_eids_between() from references/igraph/src/graph/type_indexededgelist.c:~1700. On undirected graphs (u, v) and (v, u) are equivalent. The returned vector is sorted ascending by edge id.

Source

pub fn delete_edges(&mut self, edges: &[EdgeId]) -> IgraphResult<()>

Remove the given edges from the graph.

edges may contain the same id more than once — the second and later occurrences are no-ops. Remaining edges keep their pairwise relative order but are renumbered so edge ids stay contiguous starting at 0. Returns IgraphError::EdgeOutOfRange if any id is >= ecount(); on error the graph is left unchanged.

Counterpart of igraph_delete_edges (references/igraph/src/graph/type_indexededgelist.c:500).

Source

pub fn delete_vertices(&mut self, vertices: &[VertexId]) -> IgraphResult<()>

Remove the given vertices and all their incident edges.

vertices may repeat ids freely. Surviving vertices get renumbered so the new id space is 0..new_vcount in their previous relative order. Returns IgraphError::VertexOutOfRange if any id is >= vcount(); on error the graph is left unchanged.

Counterpart of igraph_delete_vertices (references/igraph/src/graph/type_indexededgelist.c:540).

Source

pub fn delete_vertices_map( &mut self, vertices: &[VertexId], ) -> IgraphResult<(Vec<Option<VertexId>>, Vec<VertexId>)>

Like delete_vertices, but also returns the old↔new vertex id mappings.

Returns (map, invmap) where:

  • map[old_id] == Some(new_id) if the vertex was retained, else None. Length is the original vertex count.
  • invmap[new_id] == old_id. Length is the new vertex count.

Counterpart of igraph_delete_vertices_map (references/igraph/src/graph/type_indexededgelist.c:645).

Source

pub fn cache_get(&self, prop: CachedProperty) -> Option<bool>

Look up a cached boolean property without computing it.

Returns None if the property has not been cached yet. Pair with Self::cache_set in compute functions:

if let Some(v) = g.cache_get(CachedProperty::IsDag) { return v; }
let v = compute_is_dag(g);
g.cache_set(CachedProperty::IsDag, v);
v

Counterpart of igraph_i_property_cache_has + _get_bool from references/igraph/src/graph/caching.c.

Source

pub fn cache_set(&self, prop: CachedProperty, value: bool)

Store the value of a cached boolean property.

Takes &self (interior mutability via Cell) — populating the cache from a compute function is not considered a mutation of the graph, matching igraph C semantics where compute helpers take const igraph_t * and still write to the cache.

Counterpart of igraph_i_property_cache_set_bool.

Source

pub fn cache_invalidate(&self, prop: CachedProperty)

Drop the cached value of a single property (no-op if not cached).

Use this if you change the graph via a private path that doesn’t go through add_edges / delete_*.

Counterpart of igraph_i_property_cache_invalidate.

Source

pub fn cache_invalidate_all(&self)

Drop every cached boolean property.

Counterpart of igraph_i_property_cache_invalidate_all.

Trait Implementations§

Source§

impl Clone for Graph

Source§

fn clone(&self) -> Graph

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 Default for Graph

Source§

fn default() -> Graph

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

Auto Trait Implementations§

§

impl !Freeze 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> 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.