hashing, modifying pq

main
Ziyang Hu 2 years ago
parent 933de885cd
commit 433cbc349a

@ -26,6 +26,8 @@ rmp-serde = "1.1.0"
rmpv = "1.0.0" rmpv = "1.0.0"
base64 = "0.13.0" base64 = "0.13.0"
chrono = "0.4.19" chrono = "0.4.19"
priority-queue = "1.2.3"
ordered-float = "3.0.0"
num-traits = "0.2.15" num-traits = "0.2.15"
itertools = "0.10.3" itertools = "0.10.3"
regex = "1.6.0" regex = "1.6.0"

@ -7,7 +7,7 @@
* [x] dfs * [x] dfs
* [x] shortest path * [x] shortest path
* [ ] A* * [ ] A*
* [ ] Yen's k-shortest * [x] Yen's k-shortest
* [ ] all-pairs shortest path * [ ] all-pairs shortest path
* [x] single-source shortest path * [x] single-source shortest path
* [ ] minimum spanning tree * [ ] minimum spanning tree

@ -27,6 +27,7 @@ pub(crate) mod shortest_path_dijkstra;
pub(crate) mod strongly_connected_components; pub(crate) mod strongly_connected_components;
pub(crate) mod top_sort; pub(crate) mod top_sort;
pub(crate) mod yen; pub(crate) mod yen;
pub(crate) mod triangles;
pub(crate) trait AlgoImpl { pub(crate) trait AlgoImpl {
fn run( fn run(

@ -1,10 +1,13 @@
use std::cmp::Ordering; use std::cmp::{Ordering, Reverse};
use std::collections::{BTreeMap, BTreeSet, BinaryHeap}; use std::collections::{BTreeMap, BTreeSet};
use std::iter; use std::iter;
use anyhow::{anyhow, bail, Result}; use anyhow::{anyhow, bail, Result};
use itertools::Itertools; use itertools::Itertools;
use ordered_float::OrderedFloat;
use priority_queue::PriorityQueue;
use crate::algo::AlgoImpl; use crate::algo::AlgoImpl;
use crate::data::expr::Expr; use crate::data::expr::Expr;
use crate::data::program::{MagicAlgoRuleArg, MagicSymbol}; use crate::data::program::{MagicAlgoRuleArg, MagicSymbol};
@ -216,39 +219,33 @@ pub(crate) fn dijkstra<FE: ForbiddenEdge, FN: ForbiddenNode, G: Goal + Clone>(
forbidden_nodes: &FN, forbidden_nodes: &FN,
) -> Vec<(usize, f64, Vec<usize>)> { ) -> Vec<(usize, f64, Vec<usize>)> {
let mut distance = vec![f64::INFINITY; edges.len()]; let mut distance = vec![f64::INFINITY; edges.len()];
let mut heap = BinaryHeap::new(); let mut pq = PriorityQueue::new();
let mut back_pointers = vec![usize::MAX; edges.len()]; let mut back_pointers = vec![usize::MAX; edges.len()];
distance[start] = 0.; distance[start] = 0.;
heap.push(HeapState { pq.push(start, Reverse(OrderedFloat(0.)));
cost: 0.,
node: start,
});
let mut goals_remaining = goals.clone(); let mut goals_remaining = goals.clone();
while let Some(state) = heap.pop() { while let Some((node, Reverse(OrderedFloat(cost)))) = pq.pop() {
if state.cost > distance[state.node] { if cost > distance[node] {
continue; continue;
} }
for (nxt_node, path_weight) in &edges[state.node] { for (nxt_node, path_weight) in &edges[node] {
if forbidden_nodes.is_forbidden(*nxt_node) { if forbidden_nodes.is_forbidden(*nxt_node) {
continue; continue;
} }
if forbidden_edges.is_forbidden(state.node, *nxt_node) { if forbidden_edges.is_forbidden(node, *nxt_node) {
continue; continue;
} }
let nxt_cost = state.cost + *path_weight; let nxt_cost = cost + *path_weight;
if nxt_cost < distance[*nxt_node] { if nxt_cost < distance[*nxt_node] {
heap.push(HeapState { pq.push_increase(*nxt_node, Reverse(OrderedFloat(nxt_cost)));
cost: nxt_cost,
node: *nxt_node,
});
distance[*nxt_node] = nxt_cost; distance[*nxt_node] = nxt_cost;
back_pointers[*nxt_node] = state.node; back_pointers[*nxt_node] = node;
} }
} }
goals_remaining.visit(state.node); goals_remaining.visit(node);
if goals_remaining.is_exhausted() { if goals_remaining.is_exhausted() {
break; break;
} }

@ -0,0 +1,40 @@
use std::collections::BTreeMap;
use anyhow::Result;
use crate::algo::AlgoImpl;
use crate::data::expr::Expr;
use crate::data::program::{MagicAlgoRuleArg, MagicSymbol};
use crate::data::symb::Symbol;
use crate::runtime::derived::DerivedRelStore;
use crate::runtime::transact::SessionTx;
pub(crate) struct ClusteringCoefficient;
impl AlgoImpl for ClusteringCoefficient {
fn run(
&mut self,
tx: &mut SessionTx,
rels: &[MagicAlgoRuleArg],
opts: &BTreeMap<Symbol, Expr>,
stores: &BTreeMap<MagicSymbol, DerivedRelStore>,
out: &DerivedRelStore,
) -> Result<()> {
todo!()
}
}
pub(crate) struct Triangles;
impl AlgoImpl for Triangles {
fn run(
&mut self,
tx: &mut SessionTx,
rels: &[MagicAlgoRuleArg],
opts: &BTreeMap<Symbol, Expr>,
stores: &BTreeMap<MagicSymbol, DerivedRelStore>,
out: &DerivedRelStore,
) -> Result<()> {
todo!()
}
}

@ -1,6 +1,7 @@
use std::cmp::{Ordering, Reverse}; use std::cmp::{Ordering, Reverse};
use std::collections::BTreeSet; use std::collections::BTreeSet;
use std::fmt::{Debug, Display, Formatter}; use std::fmt::{Debug, Display, Formatter};
use std::hash::{Hash, Hasher};
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use regex::Regex; use regex::Regex;
@ -9,6 +10,8 @@ use serde::{Deserialize, Deserializer, Serialize};
use smallvec::SmallVec; use smallvec::SmallVec;
use smartstring::{LazyCompact, SmartString}; use smartstring::{LazyCompact, SmartString};
use ordered_float::OrderedFloat;
use crate::data::encode::EncodedVec; use crate::data::encode::EncodedVec;
use crate::data::id::{EntityId, TxId}; use crate::data::id::{EntityId, TxId};
use crate::data::triple::StoreOp; use crate::data::triple::StoreOp;
@ -16,6 +19,12 @@ use crate::data::triple::StoreOp;
#[derive(Clone)] #[derive(Clone)]
pub(crate) struct RegexWrapper(pub(crate) Regex); pub(crate) struct RegexWrapper(pub(crate) Regex);
impl Hash for RegexWrapper {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.as_str().hash(state)
}
}
impl Serialize for RegexWrapper { impl Serialize for RegexWrapper {
fn serialize<S>(&self, _serializer: S) -> std::result::Result<S::Ok, S::Error> fn serialize<S>(&self, _serializer: S) -> std::result::Result<S::Ok, S::Error>
where where
@ -55,7 +64,7 @@ impl PartialOrd for RegexWrapper {
} }
#[derive( #[derive(
Clone, PartialEq, Eq, PartialOrd, Ord, serde_derive::Deserialize, serde_derive::Serialize, Clone, PartialEq, Eq, PartialOrd, Ord, serde_derive::Deserialize, serde_derive::Serialize, Hash,
)] )]
pub(crate) enum DataValue { pub(crate) enum DataValue {
#[serde(rename = "n")] #[serde(rename = "n")]
@ -102,6 +111,15 @@ pub(crate) enum Number {
Float(f64), Float(f64),
} }
impl Hash for Number {
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
Number::Int(i) => i.hash(state),
Number::Float(f) => OrderedFloat(*f).hash(state),
}
}
}
impl Number { impl Number {
pub(crate) fn get_int(&self) -> Option<i64> { pub(crate) fn get_int(&self) -> Option<i64> {
match self { match self {

@ -197,7 +197,6 @@ fn air_routes() -> Result<()> {
dbg!(yen_time.elapsed()); dbg!(yen_time.elapsed());
println!("{}", res); println!("{}", res);
return Ok(());
let starts_with_time = Instant::now(); let starts_with_time = Instant::now();
let res = db.run_script( let res = db.run_script(

Loading…
Cancel
Save