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
impl Graph
Sourcepub fn new(n: u32, directed: bool) -> IgraphResult<Self>
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.
Sourcepub fn with_vertices(n: u32) -> Self
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.
Sourcepub fn is_directed(&self) -> bool
pub fn is_directed(&self) -> bool
true if the graph is directed. Counterpart of igraph_is_directed().
Sourcepub fn add_vertices(&mut self, nv: u32) -> IgraphResult<(VertexId, VertexId)>
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().
Sourcepub fn add_edge(&mut self, u: VertexId, v: VertexId) -> IgraphResult<()>
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.
Sourcepub fn add_edges<I>(&mut self, edges: I) -> IgraphResult<()>
pub fn add_edges<I>(&mut self, edges: I) -> IgraphResult<()>
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).
Sourcepub fn neighbors(&self, v: VertexId) -> IgraphResult<Vec<VertexId>>
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, ...).
Sourcepub fn degree(&self, v: VertexId) -> IgraphResult<usize>
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).
Sourcepub fn edge_source(&self, eid: EdgeId) -> IgraphResult<VertexId>
pub fn edge_source(&self, eid: EdgeId) -> IgraphResult<VertexId>
Source endpoint of edge eid. Counterpart of IGRAPH_FROM
(igraph_interface.h:115).
Sourcepub fn edge_target(&self, eid: EdgeId) -> IgraphResult<VertexId>
pub fn edge_target(&self, eid: EdgeId) -> IgraphResult<VertexId>
Target endpoint of edge eid. Counterpart of IGRAPH_TO
(igraph_interface.h:128).
Sourcepub fn edge(&self, eid: EdgeId) -> IgraphResult<(VertexId, VertexId)>
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).
Sourcepub fn edge_other(&self, eid: EdgeId, vid: VertexId) -> IgraphResult<VertexId>
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.
Sourcepub fn incident(&self, v: VertexId) -> IgraphResult<Vec<EdgeId>>
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.
Sourcepub fn get_eid(&self, from: VertexId, to: VertexId) -> IgraphResult<EdgeId>
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.
Sourcepub fn find_eid(
&self,
from: VertexId,
to: VertexId,
) -> IgraphResult<Option<EdgeId>>
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).
Sourcepub fn get_all_eids_between(
&self,
from: VertexId,
to: VertexId,
) -> IgraphResult<Vec<EdgeId>>
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.
Sourcepub fn delete_edges(&mut self, edges: &[EdgeId]) -> IgraphResult<()>
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).
Sourcepub fn delete_vertices(&mut self, vertices: &[VertexId]) -> IgraphResult<()>
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).
Sourcepub fn delete_vertices_map(
&mut self,
vertices: &[VertexId],
) -> IgraphResult<(Vec<Option<VertexId>>, Vec<VertexId>)>
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, elseNone. 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).