#ifndef NK_DISTANCE_H #define NK_DISTANCE_H #include "rust/cxx.h" #include "graph_event.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace NetworKit { using namespace std; inline unique_ptr NewAPSP(const Graph &G) { return make_unique(G); } inline node APSPGetDistances(const APSP &algo, rust::Vec &ws) { auto distances = algo.getDistances(); for (auto &&edges : distances) { for (auto &&wt : edges) { ws.push_back(wt); } } return distances.size(); } inline unique_ptr NewAStar(const Graph &G, const std::vector &distanceHeu, node source, node target, bool storePred) { return make_unique(G, distanceHeu, source, target, storePred); } inline unique_ptr> AStarGetPath(const AStar &algo) { return make_unique>(algo.getPath()); } inline unique_ptr> AStarGetPredecessors(const AStar &algo) { return make_unique>(algo.getPredecessors()); } inline unique_ptr> AStarGetDistances(const AStar &algo) { return make_unique>(algo.getDistances()); } inline void AStarGetTargetIndexMap(const AStar &algo, rust::Vec &src, rust::Vec &dst) { for (auto &&pair : algo.getTargetIndexMap()) { src.push_back(pair.first); dst.push_back(pair.second); } } inline void AStarSetTargets(AStar &algo, rust::Slice targets) { algo.setTargets(targets.begin(), targets.end()); } inline unique_ptr NewAdamicAdarDistance(const Graph &G) { return make_unique(G); } inline unique_ptr> AdamicAdarDistanceGetEdgeScores(const AdamicAdarDistance &algo) { return make_unique>(algo.getEdgeScores()); } inline unique_ptr NewAlgebraicDistance(const Graph &G, count numberSystems = 10, count numberIterations = 30, double omega = 0.5, index norm = 0, bool withEdgeScores = false) { return make_unique(G, numberSystems, numberIterations, omega, norm, withEdgeScores); } inline unique_ptr> AlgebraicDistanceGetEdgeScores(const AlgebraicDistance &algo) { return make_unique>(algo.getEdgeScores()); } inline unique_ptr NewAllSimplePaths(const Graph &G, node source, node target, count cutoff = none) { return make_unique(G, source, target, cutoff); } inline void AllSimplePathsGetAllSimplePaths(AllSimplePaths &algo, rust::Vec &vs) { for (auto &&inner : algo.getAllSimplePaths()) { for (auto &&j : inner) { vs.push_back(j); } vs.push_back(none); } } inline unique_ptr NewBFS(const Graph &G, node source, bool storePaths = true, bool storeNodesSortedByDistance = false, node target = none) { return make_unique(G, source, storePaths, storeNodesSortedByDistance, target); } inline unique_ptr> BFSGetDistances(BFS &algo) { return make_unique>(algo.getDistances()); } inline unique_ptr> BFSGetPredecessors(const BFS &algo, node t) { return make_unique>(algo.getPredecessors(t)); } inline unique_ptr> BFSGetPath(const BFS &algo, node t, bool forward) { return make_unique>(algo.getPath(t, forward)); } inline void BFSGetPaths(const BFS &algo, node t, bool forward, rust::Vec &vs) { for (auto &&p : algo.getPaths(t, forward)) { vs.push_back(none); for (auto &&n : p) { vs.push_back(n); } } } inline unique_ptr> BFSGetNodeSortedByDistance(const BFS &algo) { return make_unique>(algo.getNodesSortedByDistance()); } inline unique_ptr NewBidirectionalBFS(const Graph &G, node source, node target, bool storePred) { return make_unique(G, source, target, storePred); } inline unique_ptr> BidirectionalBFSGetPath(const BidirectionalBFS &algo) { return make_unique>(algo.getPath()); } inline unique_ptr> BidirectionalBFSGetPredecessors(const BidirectionalBFS &algo) { return make_unique>(algo.getPredecessors()); } inline unique_ptr> BidirectionalBFSGetDistances(const BidirectionalBFS &algo) { return make_unique>(algo.getDistances()); } inline void BidirectionalBFSGetTargetIndexMap(const BidirectionalBFS &algo, rust::Vec &src, rust::Vec &dst) { for (auto &&pair : algo.getTargetIndexMap()) { src.push_back(pair.first); dst.push_back(pair.second); } } inline void BidirectionalBFSSetTargets(BidirectionalBFS &algo, rust::Slice targets) { algo.setTargets(targets.begin(), targets.end()); } inline unique_ptr NewBidirectionalDijkstra(const Graph &G, node source, node target, bool storePred) { return make_unique(G, source, target, storePred); } inline unique_ptr> BidirectionalDijkstraGetPath(const BidirectionalDijkstra &algo) { return make_unique>(algo.getPath()); } inline unique_ptr> BidirectionalDijkstraGetPredecessors(const BidirectionalDijkstra &algo) { return make_unique>(algo.getPredecessors()); } inline unique_ptr> BidirectionalDijkstraGetDistances(const BidirectionalDijkstra &algo) { return make_unique>(algo.getDistances()); } inline void BidirectionalDijkstraGetTargetIndexMap(const BidirectionalDijkstra &algo, rust::Vec &src, rust::Vec &dst) { for (auto &&pair : algo.getTargetIndexMap()) { src.push_back(pair.first); dst.push_back(pair.second); } } inline void BidirectionalDijkstraSetTargets(BidirectionalDijkstra &algo, rust::Slice targets) { algo.setTargets(targets.begin(), targets.end()); } inline unique_ptr NewCommuteTimeDistance(const Graph &G, double tol = 0.1) { return make_unique(G, tol); } inline unique_ptr NewDiameter(const Graph &G, uint8_t algo_e, double error = -1.f, count nSamples = 0) { DiameterAlgo algo; switch (algo_e) { case 0: algo = DiameterAlgo::AUTOMATIC; break; case 1: algo = DiameterAlgo::EXACT; break; case 2: algo = DiameterAlgo:: ESTIMATED_RANGE; break; case 3: algo = DiameterAlgo:: ESTIMATED_SAMPLES; break; case 4: algo = DiameterAlgo:: ESTIMATED_PEDANTIC; break; } return make_unique(G, algo, error, nSamples); } inline void DiameterGetDiameter(const Diameter &algo, count &lower, count &upper) { auto pair = algo.getDiameter(); lower = pair.first; upper = pair.second; } inline unique_ptr NewDijkstra(const Graph &G, node source, bool storePaths = true, bool storeNodesSortedByDistance = false, node target = none) { return make_unique(G, source, storePaths, storeNodesSortedByDistance, target); } inline unique_ptr> DijkstraGetDistances(Dijkstra &algo) { return make_unique>(algo.getDistances()); } inline unique_ptr> DijkstraGetPredecessors(const Dijkstra &algo, node t) { return make_unique>(algo.getPredecessors(t)); } inline unique_ptr> DijkstraGetPath(const Dijkstra &algo, node t, bool forward) { return make_unique>(algo.getPath(t, forward)); } inline void DijkstraGetPaths(const Dijkstra &algo, node t, bool forward, rust::Vec &vs) { for (auto &&p : algo.getPaths(t, forward)) { vs.push_back(none); for (auto &&n : p) { vs.push_back(n); } } } inline unique_ptr> DijkstraGetNodeSortedByDistance(const Dijkstra &algo) { return make_unique>(algo.getNodesSortedByDistance()); } inline unique_ptr NewDynAPSP(Graph &G) { return make_unique(G); } inline node DynAPSPGetDistances(const DynAPSP &algo, rust::Vec &ws) { auto distances = algo.getDistances(); for (auto &&edges : distances) { for (auto &&wt : edges) { ws.push_back(wt); } } return distances.size(); } inline void DynAPSPUpdate(DynAPSP &algo, uint8_t kind, node u, node v, edgeweight ew) { algo.update(toGraphEvent(kind, u, v, ew)); } inline void DynAPSPUpdateBatch(DynAPSP &algo, rust::Slice kinds, rust::Slice us, rust::Slice vs, rust::Slice ews) { vector evs; evs.reserve(kinds.length()); for (size_t i = 0; i < kinds.length(); ++i) { evs.emplace_back(toGraphEvent(kinds[i], us[i], vs[i], ews[i])); } algo.updateBatch(evs); } inline unique_ptr NewDynBFS(const Graph &G, node s, bool storePredecessors = true) { return make_unique(G, s, storePredecessors); } inline unique_ptr> DynBFSGetDistances(DynBFS &algo) { return make_unique>(algo.getDistances()); } inline unique_ptr> DynBFSGetPredecessors(const DynBFS &algo, node t) { return make_unique>(algo.getPredecessors(t)); } inline unique_ptr> DynBFSGetPath(const DynBFS &algo, node t, bool forward) { return make_unique>(algo.getPath(t, forward)); } inline void DynBFSGetPaths(const DynBFS &algo, node t, bool forward, rust::Vec &vs) { for (auto &&p : algo.getPaths(t, forward)) { vs.push_back(none); for (auto &&n : p) { vs.push_back(n); } } } inline unique_ptr> DynBFSGetNodeSortedByDistance(const DynBFS &algo) { return make_unique>(algo.getNodesSortedByDistance()); } inline void DynBFSUpdate(DynBFS &algo, uint8_t kind, node u, node v, edgeweight ew) { algo.update(toGraphEvent(kind, u, v, ew)); } inline void DynBFSUpdateBatch(DynBFS &algo, rust::Slice kinds, rust::Slice us, rust::Slice vs, rust::Slice ews) { vector evs; evs.reserve(kinds.length()); for (size_t i = 0; i < kinds.length(); ++i) { evs.emplace_back(toGraphEvent(kinds[i], us[i], vs[i], ews[i])); } algo.updateBatch(evs); } inline void EccentricityGetValue(const Graph &G, node u, node &fartherest, count &dist) { auto res = Eccentricity::getValue(G, u); fartherest = res.first; dist = res.second; } inline unique_ptr NewEffectiveDiameter(const Graph &G, double ratio) { return make_unique(G, ratio); } inline unique_ptr NewEffectiveDiameterApproximation(const Graph &G, double ratio, count k, count r) { return make_unique(G, ratio, k, r); } inline unique_ptr NewHopPlotApproximation(const Graph &G, count maxDistance, count k, count r) { return make_unique(G, maxDistance, k, r); } inline void HopPlotApproximationGetHopPlot(const HopPlotApproximation &algo, rust::Vec &ks, rust::Vec &vs) { for (auto &&pair : algo.getHopPlot()) { ks.push_back(pair.first); vs.push_back(pair.second); } } inline unique_ptr NewJaccardDistance(const Graph &G, const std::vector &triangles) { return make_unique(G, triangles); } inline unique_ptr> JaccardDistanceGetEdgeScores(const JaccardDistance &algo) { return make_unique>(algo.getEdgeScores()); } inline unique_ptr NewMultiTargetBFS(const Graph &G, node source, rust::Slice targets) { return make_unique(G, source, targets.begin(), targets.end()); } inline unique_ptr> MultiTargetBFSGetPath(const MultiTargetBFS &algo) { return make_unique>(algo.getPath()); } inline unique_ptr> MultiTargetBFSGetPredecessors(const MultiTargetBFS &algo) { return make_unique>(algo.getPredecessors()); } inline unique_ptr> MultiTargetBFSGetDistances(const MultiTargetBFS &algo) { return make_unique>(algo.getDistances()); } inline void MultiTargetBFSGetTargetIndexMap(const MultiTargetBFS &algo, rust::Vec &src, rust::Vec &dst) { for (auto &&pair : algo.getTargetIndexMap()) { src.push_back(pair.first); dst.push_back(pair.second); } } inline void MultiTargetBFSSetTargets(MultiTargetBFS &algo, rust::Slice targets) { algo.setTargets(targets.begin(), targets.end()); } inline unique_ptr NewMultiTargetDijkstra(const Graph &G, node source, rust::Slice targets) { return make_unique(G, source, targets.begin(), targets.end()); } inline unique_ptr> MultiTargetDijkstraGetPath(const MultiTargetDijkstra &algo) { return make_unique>(algo.getPath()); } inline unique_ptr> MultiTargetDijkstraGetPredecessors(const MultiTargetDijkstra &algo) { return make_unique>(algo.getPredecessors()); } inline unique_ptr> MultiTargetDijkstraGetDistances(const MultiTargetDijkstra &algo) { return make_unique>(algo.getDistances()); } inline void MultiTargetDijkstraGetTargetIndexMap(const MultiTargetDijkstra &algo, rust::Vec &src, rust::Vec &dst) { for (auto &&pair : algo.getTargetIndexMap()) { src.push_back(pair.first); dst.push_back(pair.second); } } inline void MultiTargetDijkstraSetTargets(MultiTargetDijkstra &algo, rust::Slice targets) { algo.setTargets(targets.begin(), targets.end()); } inline unique_ptr NewNeighborhoodFunction(const Graph &G) { return make_unique(G); } inline unique_ptr> NeighborhoodFunctionGetNeighborhoodFunction(const NeighborhoodFunction &algo) { return make_unique>(algo.getNeighborhoodFunction()); } inline unique_ptr NewNeighborhoodFunctionApproximation(const Graph &G, count k = 64, count r = 7) { return make_unique(G, k, r); } inline unique_ptr> NeighborhoodFunctionApproximationGetNeighborhoodFunction(const NeighborhoodFunctionApproximation &algo) { return make_unique>(algo.getNeighborhoodFunction()); } inline unique_ptr NewNeighborhoodFunctionHeuristic(const Graph &G, count n_samples, uint8_t strategy) { NeighborhoodFunctionHeuristic::SelectionStrategy s; switch (strategy) { case 0: s = NeighborhoodFunctionHeuristic::SelectionStrategy::RANDOM; break; case 1: s = NeighborhoodFunctionHeuristic::SelectionStrategy::SPLIT; break; } return make_unique(G, n_samples, s); } inline unique_ptr> NeighborhoodFunctionHeuristicGetNeighborhoodFunction(const NeighborhoodFunctionHeuristic &algo) { return make_unique>(algo.getNeighborhoodFunction()); } inline unique_ptr NewPrunedLandmarkLabeling(const Graph &G) { return make_unique(G); } inline unique_ptr NewReverseBFS(const Graph &G, node source, bool storePaths = true, bool storeNodesSortedByDistance = false, node target = none) { return make_unique(G, source, storePaths, storeNodesSortedByDistance, target); } inline unique_ptr> ReverseBFSGetDistances(ReverseBFS &algo) { return make_unique>(algo.getDistances()); } inline unique_ptr> ReverseBFSGetPredecessors(const ReverseBFS &algo, node t) { return make_unique>(algo.getPredecessors(t)); } inline unique_ptr> ReverseBFSGetPath(const ReverseBFS &algo, node t, bool forward) { return make_unique>(algo.getPath(t, forward)); } inline void ReverseBFSGetPaths(const ReverseBFS &algo, node t, bool forward, rust::Vec &vs) { for (auto &&p : algo.getPaths(t, forward)) { vs.push_back(none); for (auto &&n : p) { vs.push_back(n); } } } inline unique_ptr> ReverseBFSGetNodeSortedByDistance(const ReverseBFS &algo) { return make_unique>(algo.getNodesSortedByDistance()); } inline unique_ptr> VolumeVolumes(const Graph &G, rust::Slice rs, count samples) { vector radii{rs.begin(), rs.end()}; return make_unique>(Volume::volume(G, radii, samples)); } inline double VolumeVolume(const Graph &G, double r, count samples) { return Volume::volume(G, r, samples); } } #endif // NK_DISTANCE_H