Skip to content

Commit

Permalink
fix Dijkstra
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetbout committed Apr 12, 2024
1 parent edbeee8 commit 67aed80
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
32 changes: 27 additions & 5 deletions src/searching/src/dijkstra.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,30 @@ use core::nullable::{FromNullableResult, match_nullable};

#[derive(Copy, Drop)]
pub struct Node {
pub source: u32,
pub dest: u32,
pub weight: u128
source: u32,
dest: u32,
weight: u128
}

#[generate_trait]
pub impl NodeGetters of NodeGettersTrait {
fn weight(self: @Node) -> @u128 {
self.weight
}

fn dest(self: @Node) -> @u32 {
self.dest
}

fn source(self: @Node) -> @u32 {
self.source
}
}

/// Graph representation.
pub struct Graph<T> {
pub nodes: Array<Node>,
pub adj_nodes: Felt252Dict<T>,
pub(crate) nodes: Array<Node>,
adj_nodes: Felt252Dict<T>,
}

/// Graph trait.
Expand All @@ -22,6 +37,8 @@ pub trait GraphTrait {
fn add_edge(ref self: Graph<Nullable<Span<Node>>>, source: u32, dest: u32, weight: u128);
/// return shortest path from s
fn shortest_path(ref self: Graph<Nullable<Span<Node>>>, source: u32) -> Felt252Dict<u128>;
/// return shortest path from s
fn adj_nodes(ref self: Graph<Nullable<Span<Node>>>, source: felt252) -> Nullable<Span<Node>>;
}

impl DestructGraph<T, +Drop<T>, +Felt252DictValue<T>> of Destruct<Graph<T>> {
Expand Down Expand Up @@ -66,6 +83,11 @@ impl GraphImpl of GraphTrait {
fn shortest_path(ref self: Graph<Nullable<Span<Node>>>, source: u32) -> Felt252Dict<u128> {
dijkstra(ref self, source)
}


fn adj_nodes(ref self: Graph<Nullable<Span<Node>>>, source: felt252) -> Nullable<Span<Node>> {
self.adj_nodes.get(source)
}
}

pub fn dijkstra(ref self: Graph<Nullable<Span<Node>>>, source: u32) -> Felt252Dict<u128> {
Expand Down
14 changes: 7 additions & 7 deletions src/searching/src/tests/dijkstra_test.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alexandria_searching::dijkstra::{Graph, Node, GraphTrait};
use alexandria_searching::dijkstra::{Graph, Node, GraphTrait, NodeGetters};
use core::nullable::{FromNullableResult, match_nullable};


Expand All @@ -17,7 +17,7 @@ fn add_edge() {
GraphTrait::add_edge(ref graph, 2, 3, 3);

assert_eq!(graph.nodes.len(), 6, "wrong node number");
let val = graph.adj_nodes.get(source.into());
let val = graph.adj_nodes(source.into());

let span = match match_nullable(val) {
FromNullableResult::Null => { panic!("No value found") },
Expand All @@ -27,14 +27,14 @@ fn add_edge() {
assert_eq!(span.len(), 4, "wrong nb of adj edge for node 0");

let new_node = *span.get(1).unwrap().unbox();
assert_eq!(new_node.dest, dest + 1, "Wrong dest in adj edge");
assert_eq!(new_node.weight, weight + 1, "Wrong weight in adj edge");
assert_eq!(*new_node.dest(), dest + 1, "Wrong dest in adj edge");
assert_eq!(*new_node.weight(), weight + 1, "Wrong weight in adj edge");

let new_node = *span.get(3).unwrap().unbox();
assert_eq!(new_node.dest, dest + 3, "Wrong dest in adj edge");
assert_eq!(new_node.weight, weight + 3, "Wrong weight in adj edge");
assert_eq!(*new_node.dest(), dest + 3, "Wrong dest in adj edge");
assert_eq!(*new_node.weight(), weight + 3, "Wrong weight in adj edge");

let val = graph.adj_nodes.get(2.into());
let val = graph.adj_nodes(2.into());

let span = match match_nullable(val) {
FromNullableResult::Null => { panic!("No value found") },
Expand Down

0 comments on commit 67aed80

Please sign in to comment.