pub struct TensorGraph { /* private fields */ }Expand description
A directed, weighted graph backed by a dense adjacency lattice.
Implementations§
Source§impl TensorGraph
impl TensorGraph
Sourcepub fn new(n: usize) -> Self
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}Sourcepub fn nodes(&self) -> usize
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}Sourcepub fn edge_count(&self) -> usize
pub fn edge_count(&self) -> usize
Number of edges added.
Sourcepub fn add_edge(&mut self, u: usize, v: usize, weight: f32)
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}Sourcepub fn add_edge_unweighted(&mut self, u: usize, v: usize)
pub fn add_edge_unweighted(&mut self, u: usize, v: usize)
Add an unweighted directed edge u → v (weight 1).
Sourcepub fn reachability(&self) -> PaddedTileLattice<f32>
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.
Sourcepub fn reachable(&self, u: usize, v: usize) -> bool
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}Sourcepub fn shortest_paths(&self) -> PaddedTileLattice<f32>
pub fn shortest_paths(&self) -> PaddedTileLattice<f32>
All-pairs shortest path distances, computed as tropical matrix powers.
Unreachable pairs hold +∞.
Sourcepub fn distance(&self, u: usize, v: usize) -> Option<f32>
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}Sourcepub fn walk_counts(&self, k: usize) -> PaddedTileLattice<f32>
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
impl Clone for TensorGraph
Source§fn clone(&self) -> TensorGraph
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for TensorGraph
impl RefUnwindSafe for TensorGraph
impl Send for TensorGraph
impl Sync for TensorGraph
impl Unpin for TensorGraph
impl UnsafeUnpin for TensorGraph
impl UnwindSafe for TensorGraph
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more