change algo casings; start algo docs

main
Ziyang Hu 2 years ago
parent c1f80103bd
commit 5c24d4baa2

@ -0,0 +1,21 @@
==============================
Algorithms
==============================
.. module:: Algo
.. function:: reorder_sort(rel[...], out: [...], sort_by: [...], descending: false, break_ties: false, skip: 0, take: 0)
Sort and then extract new columns of the passed in relation ``rel``.
:param required out: A list of expressions which will be used to produce the output relation. Any bindings in the expressions will be bound to the named positions in ``rel``.
:param sort_by: A list of expressions which will be used to produce the sort keys. Any bindings in the expressions will be bound to the named positions in ``rel``.
:param descending: Whether the sorting process should be done in descending order. Defaults to ``false``.
:param break_ties: Whether ties should be broken, e.g. whether the first two rows with *identical sort keys* should be given ordering numbers ``1`` and ``2`` instead of ``1`` and ``1``. Defaults to false.
:param skip: How many rows to skip before producing rows. Defaults to zero.
:param take: How many rows at most to produce. Zero means no limit. Defaults to zero.
:return: The returned relation, in addition to the rows specified in the parameter ``out``, will have the ordering prepended. The ordering starts at ``1``.
.. TIP::
This algorithm serves a similar purpose to the global ``:order``, ``:limit`` and ``:offset`` options, but can be applied to intermediate results. Prefer the global options if it is applied to the final output.

@ -13,6 +13,7 @@ Welcome to Cozo's documentation!
datatypes
functions
aggregations
algorithms
Indices and tables

@ -81,25 +81,25 @@ impl AlgoHandle {
opts: &BTreeMap<SmartString<LazyCompact>, Expr>,
) -> Option<usize> {
Some(match &self.name.name as &str {
"clustering_coefficients" => 4,
"degree_centrality" => 4,
"closeness_centrality" => 2,
"betweenness_centrality" => 2,
"depth_first_search" | "dfs" => 1,
"breadth_first_search" | "bfs" => 1,
"shortest_path_dijkstra" => 4,
"shortest_path_astar" => 4,
"k_shortest_path_yen" => 4,
"minimum_spanning_tree_prim" => 3,
"minimum_spanning_tree_kruskal" => 3,
"top_sort" => 2,
"connected_components" => 2,
"strongly_connected_components" | "scc" => 2,
"pagerank" => 2,
"community_detection_louvain" => 2,
"label_propagation" => 2,
"random_walk" => 3,
"reorder_sort" => {
"ClusteringCoefficients" => 4,
"DegreeCentrality" => 4,
"ClosenessCentrality" => 2,
"BetweennessCentrality" => 2,
"DepthFirstSearch" | "DFS" => 1,
"BreadthFirstSearch" | "BFS" => 1,
"ShortestPathDijkstra" => 4,
"ShortestPathAStar" => 4,
"KShortestPathYen" => 4,
"MinimumSpanningTreePrim" => 3,
"MinimumSpanningTreeKruskal" => 3,
"TopSort" => 2,
"ConnectedComponents" => 2,
"StronglyConnectedComponents" | "SCC" => 2,
"PageRank" => 2,
"CommunityDetectionLouvain" => 2,
"LabelPropagation" => 2,
"RandomWalk" => 3,
"ReorderSort" => {
let out_opts = opts.get("out")?;
match out_opts {
Expr::Const {
@ -116,27 +116,27 @@ impl AlgoHandle {
pub(crate) fn get_impl(&self) -> Result<Box<dyn AlgoImpl>> {
Ok(match &self.name.name as &str {
"clustering_coefficients" => Box::new(ClusteringCoefficients),
"degree_centrality" => Box::new(DegreeCentrality),
"closeness_centrality" => Box::new(ClosenessCentrality),
"betweenness_centrality" => Box::new(BetweennessCentrality),
"depth_first_search" | "dfs" => Box::new(Dfs),
"breadth_first_search" | "bfs" => Box::new(Bfs),
"shortest_path_dijkstra" => Box::new(ShortestPathDijkstra),
"shortest_path_astar" => Box::new(ShortestPathAStar),
"k_shortest_path_yen" => Box::new(KShortestPathYen),
"minimum_spanning_tree_prim" => Box::new(MinimumSpanningTreePrim),
"minimum_spanning_tree_kruskal" => Box::new(MinimumSpanningTreeKruskal),
"top_sort" => Box::new(TopSort),
"connected_components" => Box::new(StronglyConnectedComponent::new(false)),
"strongly_connected_components" | "scc" => {
"ClusteringCoefficients" => Box::new(ClusteringCoefficients),
"DegreeCentrality" => Box::new(DegreeCentrality),
"ClosenessCentrality" => Box::new(ClosenessCentrality),
"BetweennessCentrality" => Box::new(BetweennessCentrality),
"DepthFirstSearch" | "DFS" => Box::new(Dfs),
"BreadthFirstSearch" | "BFS" => Box::new(Bfs),
"ShortestPathDijkstra" => Box::new(ShortestPathDijkstra),
"ShortestPathAStar" => Box::new(ShortestPathAStar),
"KShortestPathYen" => Box::new(KShortestPathYen),
"MinimumSpanningTreePrim" => Box::new(MinimumSpanningTreePrim),
"MinimumSpanningTreeKruskal" => Box::new(MinimumSpanningTreeKruskal),
"TopSort" => Box::new(TopSort),
"ConnectedComponents" => Box::new(StronglyConnectedComponent::new(false)),
"StronglyConnectedComponents" | "SCC" => {
Box::new(StronglyConnectedComponent::new(true))
}
"pagerank" => Box::new(PageRank),
"community_detection_louvain" => Box::new(CommunityDetectionLouvain),
"label_propagation" => Box::new(LabelPropagation),
"random_walk" => Box::new(RandomWalk),
"reorder_sort" => Box::new(ReorderSort),
"PageRank" => Box::new(PageRank),
"CommunityDetectionLouvain" => Box::new(CommunityDetectionLouvain),
"LabelPropagation" => Box::new(LabelPropagation),
"RandomWalk" => Box::new(RandomWalk),
"ReorderSort" => Box::new(ReorderSort),
name => bail!(AlgoNotFoundError(name.to_string(), self.name.span)),
})
}

@ -117,7 +117,7 @@ fn air_routes() -> Result<()> {
let dfs_time = Instant::now();
let res = db.run_script(r#"
starting[] <- [['PEK']];
?[] <~ dfs(:flies_to_code[], [id <airport.iata code], starting[], condition: (code == 'LHR'));
?[] <~ DFS(:flies_to_code[], [id <airport.iata code], starting[], condition: (code == 'LHR'));
"#, &params,
)?;
dbg!(dfs_time.elapsed());
@ -127,7 +127,7 @@ fn air_routes() -> Result<()> {
let res = db.run_script(
r#"
starting[] <- [['PEK']];
?[] <~ bfs(:flies_to_code[], [id <airport.iata code], starting[], condition: code == 'SOU');
?[] <~ BFS(:flies_to_code[], [id <airport.iata code], starting[], condition: code == 'SOU');
"#,
&params,
)?;
@ -137,7 +137,7 @@ fn air_routes() -> Result<()> {
let scc_time = Instant::now();
let res = db.run_script(
r#"
res[] <~ strongly_connected_components(:flies_to_code[], [id <airport.iata code]);
res[] <~ StronglyConnectedComponents(:flies_to_code[], [id <airport.iata code]);
?[grp, code] := res[code, grp], grp != 0;
"#,
&params,
@ -148,7 +148,7 @@ fn air_routes() -> Result<()> {
let cc_time = Instant::now();
let res = db.run_script(
r#"
res[] <~ connected_components(:flies_to_code[], [id <airport.iata code]);
res[] <~ ConnectedComponents(:flies_to_code[], [id <airport.iata code]);
?[grp, code] := res[code, grp], grp != 0;
"#,
&params,
@ -160,7 +160,7 @@ fn air_routes() -> Result<()> {
let res = db.run_script(r#"
starting[code, lat, lon] := code <- 'HFE', :code_lat_lon[code, lat, lon];
goal[code, lat, lon] := code <- 'LHR', :code_lat_lon[code, lat, lon];
?[] <~ shortest_path_astar(:flies_to_code[], :code_lat_lon[node, lat1, lon1], starting[], goal[goal, lat2, lon2], heuristic: haversine_deg_input(lat1, lon1, lat2, lon2) * 3963);
?[] <~ ShortestPathAStar(:flies_to_code[], :code_lat_lon[node, lat1, lon1], starting[], goal[goal, lat2, lon2], heuristic: haversine_deg_input(lat1, lon1, lat2, lon2) * 3963);
"#, &params,
)?;
println!("{}", res);
@ -169,7 +169,7 @@ fn air_routes() -> Result<()> {
let deg_centrality_time = Instant::now();
let res = db.run_script(
r#"
deg_centrality[] <~ degree_centrality(:flies_to[a, b]);
deg_centrality[] <~ DegreeCentrality(:flies_to[a, b]);
?[total, out, in] := deg_centrality[node, total, out, in];
:order -total;
:limit 10;
@ -193,7 +193,7 @@ fn air_routes() -> Result<()> {
r#"
flies_to[a, b] := [r route.src ac], [r route.dst bc],
[ac airport.iata a], [bc airport.iata b];
deg_centrality[] <~ degree_centrality(flies_to[a, b]);
deg_centrality[] <~ DegreeCentrality(flies_to[a, b]);
?[node, total, out, in] := deg_centrality[node, total, out, in];
:order -total;
:limit 10;
@ -219,7 +219,7 @@ fn air_routes() -> Result<()> {
r#"
starting[] <- [['JFK']];
ending[] <- [['KUL']];
res[] <~ shortest_path_dijkstra(:flies_to_code[], starting[], ending[]);
res[] <~ ShortestPathDijkstra(:flies_to_code[], starting[], ending[]);
?[path] := res[src, dst, cost, path];
"#,
&params,
@ -233,7 +233,7 @@ fn air_routes() -> Result<()> {
r#"
starting[] <- [['PEK']];
ending[] <- [['SIN']];
?[] <~ k_shortest_path_yen(:flies_to_code[], starting[], ending[], k: 5);
?[] <~ KShortestPathYen(:flies_to_code[], starting[], ending[], k: 5);
"#,
&params,
)?;

Loading…
Cancel
Save