Skip to main content

TensorGraph

Struct TensorGraph 

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

A directed, weighted graph backed by a dense adjacency lattice.

Implementations§

Source§

impl TensorGraph

Source

pub fn new(n: usize) -> Self

Create an n-node graph with no edges.

Examples found in repository?
examples/graph_paths.rs (line 12)
7fn main() {
8    // A small weighted DAG-ish graph.
9    //   0 →1(4) →3(1)
10    //   0 →2(1) →3(5)
11    //   2 →1(2)
12    let mut g = TensorGraph::new(4);
13    g.add_edge(0, 1, 4.0);
14    g.add_edge(0, 2, 1.0);
15    g.add_edge(2, 1, 2.0);
16    g.add_edge(1, 3, 1.0);
17    g.add_edge(2, 3, 5.0);
18
19    println!("{g:?}\n");
20
21    println!("all-pairs shortest paths (tropical matrix powers):");
22    for u in 0..g.nodes() {
23        for v in 0..g.nodes() {
24            match g.distance(u, v) {
25                Some(d) => print!("  {u}->{v}:{d:>3}"),
26                None => print!("  {u}->{v}:  ∞"),
27            }
28        }
29        println!();
30    }
31
32    // Best route 0 -> 3 is 0->2->1->3 = 1 + 2 + 1 = 4, beating 0->1->3 = 5.
33    println!(
34        "\nshortest 0 -> 3 = {:?}  (via 0->2->1->3)",
35        g.distance(0, 3)
36    );
37    assert_eq!(g.distance(0, 3), Some(4.0));
38
39    println!("reachable 0 -> 3? {}", g.reachable(0, 3));
40    println!("reachable 3 -> 0? {}", g.reachable(3, 0));
41    println!("\n✓ traversal computed as a handful of dense matmuls, no queue, no branches.");
42}
Source

pub fn nodes(&self) -> usize

Number of nodes.

Examples found in repository?
examples/graph_paths.rs (line 22)
7fn main() {
8    // A small weighted DAG-ish graph.
9    //   0 →1(4) →3(1)
10    //   0 →2(1) →3(5)
11    //   2 →1(2)
12    let mut g = TensorGraph::new(4);
13    g.add_edge(0, 1, 4.0);
14    g.add_edge(0, 2, 1.0);
15    g.add_edge(2, 1, 2.0);
16    g.add_edge(1, 3, 1.0);
17    g.add_edge(2, 3, 5.0);
18
19    println!("{g:?}\n");
20
21    println!("all-pairs shortest paths (tropical matrix powers):");
22    for u in 0..g.nodes() {
23        for v in 0..g.nodes() {
24            match g.distance(u, v) {
25                Some(d) => print!("  {u}->{v}:{d:>3}"),
26                None => print!("  {u}->{v}:  ∞"),
27            }
28        }
29        println!();
30    }
31
32    // Best route 0 -> 3 is 0->2->1->3 = 1 + 2 + 1 = 4, beating 0->1->3 = 5.
33    println!(
34        "\nshortest 0 -> 3 = {:?}  (via 0->2->1->3)",
35        g.distance(0, 3)
36    );
37    assert_eq!(g.distance(0, 3), Some(4.0));
38
39    println!("reachable 0 -> 3? {}", g.reachable(0, 3));
40    println!("reachable 3 -> 0? {}", g.reachable(3, 0));
41    println!("\n✓ traversal computed as a handful of dense matmuls, no queue, no branches.");
42}
Source

pub fn edge_count(&self) -> usize

Number of edges added.

Source

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

Add a weighted directed edge u → v.

Examples found in repository?
examples/graph_paths.rs (line 13)
7fn main() {
8    // A small weighted DAG-ish graph.
9    //   0 →1(4) →3(1)
10    //   0 →2(1) →3(5)
11    //   2 →1(2)
12    let mut g = TensorGraph::new(4);
13    g.add_edge(0, 1, 4.0);
14    g.add_edge(0, 2, 1.0);
15    g.add_edge(2, 1, 2.0);
16    g.add_edge(1, 3, 1.0);
17    g.add_edge(2, 3, 5.0);
18
19    println!("{g:?}\n");
20
21    println!("all-pairs shortest paths (tropical matrix powers):");
22    for u in 0..g.nodes() {
23        for v in 0..g.nodes() {
24            match g.distance(u, v) {
25                Some(d) => print!("  {u}->{v}:{d:>3}"),
26                None => print!("  {u}->{v}:  ∞"),
27            }
28        }
29        println!();
30    }
31
32    // Best route 0 -> 3 is 0->2->1->3 = 1 + 2 + 1 = 4, beating 0->1->3 = 5.
33    println!(
34        "\nshortest 0 -> 3 = {:?}  (via 0->2->1->3)",
35        g.distance(0, 3)
36    );
37    assert_eq!(g.distance(0, 3), Some(4.0));
38
39    println!("reachable 0 -> 3? {}", g.reachable(0, 3));
40    println!("reachable 3 -> 0? {}", g.reachable(3, 0));
41    println!("\n✓ traversal computed as a handful of dense matmuls, no queue, no branches.");
42}
Source

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

Add an unweighted directed edge u → v (weight 1).

Source

pub fn reachability(&self) -> PaddedTileLattice<f32>

The reflexive-transitive closure: reachable[u][v] is 1 iff v is reachable from u. Computed as boolean matrix powers.

Source

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

True if v is reachable from u (including u == v).

Examples found in repository?
examples/graph_paths.rs (line 39)
7fn main() {
8    // A small weighted DAG-ish graph.
9    //   0 →1(4) →3(1)
10    //   0 →2(1) →3(5)
11    //   2 →1(2)
12    let mut g = TensorGraph::new(4);
13    g.add_edge(0, 1, 4.0);
14    g.add_edge(0, 2, 1.0);
15    g.add_edge(2, 1, 2.0);
16    g.add_edge(1, 3, 1.0);
17    g.add_edge(2, 3, 5.0);
18
19    println!("{g:?}\n");
20
21    println!("all-pairs shortest paths (tropical matrix powers):");
22    for u in 0..g.nodes() {
23        for v in 0..g.nodes() {
24            match g.distance(u, v) {
25                Some(d) => print!("  {u}->{v}:{d:>3}"),
26                None => print!("  {u}->{v}:  ∞"),
27            }
28        }
29        println!();
30    }
31
32    // Best route 0 -> 3 is 0->2->1->3 = 1 + 2 + 1 = 4, beating 0->1->3 = 5.
33    println!(
34        "\nshortest 0 -> 3 = {:?}  (via 0->2->1->3)",
35        g.distance(0, 3)
36    );
37    assert_eq!(g.distance(0, 3), Some(4.0));
38
39    println!("reachable 0 -> 3? {}", g.reachable(0, 3));
40    println!("reachable 3 -> 0? {}", g.reachable(3, 0));
41    println!("\n✓ traversal computed as a handful of dense matmuls, no queue, no branches.");
42}
Source

pub fn shortest_paths(&self) -> PaddedTileLattice<f32>

All-pairs shortest path distances, computed as tropical matrix powers. Unreachable pairs hold +∞.

Source

pub fn distance(&self, u: usize, v: usize) -> Option<f32>

The shortest-path distance from u to v, or None if unreachable.

Examples found in repository?
examples/graph_paths.rs (line 24)
7fn main() {
8    // A small weighted DAG-ish graph.
9    //   0 →1(4) →3(1)
10    //   0 →2(1) →3(5)
11    //   2 →1(2)
12    let mut g = TensorGraph::new(4);
13    g.add_edge(0, 1, 4.0);
14    g.add_edge(0, 2, 1.0);
15    g.add_edge(2, 1, 2.0);
16    g.add_edge(1, 3, 1.0);
17    g.add_edge(2, 3, 5.0);
18
19    println!("{g:?}\n");
20
21    println!("all-pairs shortest paths (tropical matrix powers):");
22    for u in 0..g.nodes() {
23        for v in 0..g.nodes() {
24            match g.distance(u, v) {
25                Some(d) => print!("  {u}->{v}:{d:>3}"),
26                None => print!("  {u}->{v}:  ∞"),
27            }
28        }
29        println!();
30    }
31
32    // Best route 0 -> 3 is 0->2->1->3 = 1 + 2 + 1 = 4, beating 0->1->3 = 5.
33    println!(
34        "\nshortest 0 -> 3 = {:?}  (via 0->2->1->3)",
35        g.distance(0, 3)
36    );
37    assert_eq!(g.distance(0, 3), Some(4.0));
38
39    println!("reachable 0 -> 3? {}", g.reachable(0, 3));
40    println!("reachable 3 -> 0? {}", g.reachable(3, 0));
41    println!("\n✓ traversal computed as a handful of dense matmuls, no queue, no branches.");
42}
Source

pub fn walk_counts(&self, k: usize) -> PaddedTileLattice<f32>

The number of distinct walks of exactly k edges from u to v, as the k-th ordinary matrix power of the adjacency.

Trait Implementations§

Source§

impl Clone for TensorGraph

Source§

fn clone(&self) -> TensorGraph

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 TensorGraph

Source§

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

Formats the value using the given formatter. 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.