Skip to main content

DirectedGraph

Struct DirectedGraph 

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

有向图

使用邻接表表示,支持带权边。

Implementations§

Source§

impl DirectedGraph

Source

pub fn new() -> Self

创建空图

Source

pub fn add_node(&mut self, node: NodeId)

添加节点

Source

pub fn add_edge(&mut self, from: NodeId, to: NodeId, weight: Weight)

添加带权有向边

Source

pub fn add_edge_unweighted(&mut self, from: NodeId, to: NodeId)

添加无权有向边(权重=1.0)

Source

pub fn neighbors(&self, node: NodeId) -> Option<&Vec<(NodeId, Weight)>>

获取节点的邻居

Source

pub fn nodes(&self) -> impl Iterator<Item = NodeId> + '_

所有节点

Source

pub fn node_count(&self) -> usize

节点数

Source

pub fn edge_count(&self) -> usize

边数

Source

pub fn has_node(&self, node: NodeId) -> bool

是否包含节点

Source

pub fn has_edge(&self, from: NodeId, to: NodeId) -> bool

是否包含边

Source

pub fn edge_weight(&self, from: NodeId, to: NodeId) -> Option<Weight>

获取边权重

Source

pub fn bfs(&self, start: NodeId) -> Vec<NodeId>

广度优先搜索(BFS)

start 出发,返回按 BFS 顺序访问的节点列表。

Source

pub fn dfs(&self, start: NodeId) -> Vec<NodeId>

深度优先搜索(DFS)

start 出发,返回按 DFS 顺序访问的节点列表。

Source

pub fn dijkstra( &self, start: NodeId, end: NodeId, ) -> Option<(Vec<NodeId>, Weight)>

Dijkstra 最短路径算法

返回从 startend 的最短路径和总距离。 如果不可达返回 None。

Source

pub fn topological_sort(&self) -> Option<Vec<NodeId>>

拓扑排序(Kahn 算法)

返回拓扑排序结果。如果图有环返回 None。

Source

pub fn has_cycle(&self) -> bool

环检测(DFS)

检测图中是否存在环。

Source

pub fn connected_components(&self) -> Vec<Vec<NodeId>>

连通分量(弱连通)

返回每个连通分量的节点列表。

Source

pub fn reverse(&self) -> DirectedGraph

反转图(所有边方向取反)

Source

pub fn in_degree(&self, node: NodeId) -> usize

节点的入度

Source

pub fn out_degree(&self, node: NodeId) -> usize

节点的出度

Trait Implementations§

Source§

impl Clone for DirectedGraph

Source§

fn clone(&self) -> DirectedGraph

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 DirectedGraph

Source§

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

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

impl Default for DirectedGraph

Source§

fn default() -> DirectedGraph

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

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.