#ifndef NK_LINK_PREDICTION_H #define NK_LINK_PREDICTION_H #include "rust/cxx.h" #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 NewAdamicAdarIndex(const Graph &G) { return make_unique(G); } inline void AdamicAdarIndexRunOn(AdamicAdarIndex &algo, rust::Slice src, rust::Slice dst, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { vector> pairs{}; for (size_t i = 0; i < src.length(); ++i) { pairs.emplace_back(pair(src[i], dst[i])); } for (auto &&res : algo.runOn(pairs)) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline void AdamicAdarIndexRunAll(AdamicAdarIndex &algo, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { for (auto &&res : algo.runAll()) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline unique_ptr NewAdjustedRandIndex(const Graph &G) { return make_unique(G); } inline void AdjustedRandIndexRunOn(AdjustedRandIndex &algo, rust::Slice src, rust::Slice dst, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { vector> pairs{}; for (size_t i = 0; i < src.length(); ++i) { pairs.emplace_back(pair(src[i], dst[i])); } for (auto &&res : algo.runOn(pairs)) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline void AdjustedRandIndexRunAll(AdjustedRandIndex &algo, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { for (auto &&res : algo.runAll()) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline unique_ptr NewAlgebraicDistanceIndex(const Graph &G, count numberSystems, count numberIterations, double omega = 0.5, index norm = 2) { return make_unique(G, numberSystems, numberIterations, omega, norm); } inline void AlgebraicDistanceIndexRunOn(AlgebraicDistanceIndex &algo, rust::Slice src, rust::Slice dst, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { vector> pairs{}; for (size_t i = 0; i < src.length(); ++i) { pairs.emplace_back(pair(src[i], dst[i])); } for (auto &&res : algo.runOn(pairs)) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline void AlgebraicDistanceIndexRunAll(AlgebraicDistanceIndex &algo, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { for (auto &&res : algo.runAll()) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline unique_ptr NewCommonNeighborsIndex(const Graph &G) { return make_unique(G); } inline void CommonNeighborsIndexRunOn(CommonNeighborsIndex &algo, rust::Slice src, rust::Slice dst, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { vector> pairs{}; for (size_t i = 0; i < src.length(); ++i) { pairs.emplace_back(pair(src[i], dst[i])); } for (auto &&res : algo.runOn(pairs)) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline void CommonNeighborsIndexRunAll(CommonNeighborsIndex &algo, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { for (auto &&res : algo.runAll()) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline unique_ptr NewPrecisionRecallMetric(const Graph &G) { return make_unique(G); } inline void PrecisionRecallMetricGetCurve(PrecisionRecallMetric &algo, rust::Slice us, rust::Slice vs, rust::Slice ws, count numThresholds, rust::Vec &xs, rust::Vec &ys) { vector predictions{}; for (size_t i = 0; i < us.length(); ++i) { predictions.emplace_back(pair(pair(us[i], vs[i]), ws[i])); } auto res = algo.getCurve(predictions, numThresholds); for (size_t i = 0; i < res.first.size(); ++i) { xs.push_back(res.first[i]); ys.push_back(res.second[i]); } } inline double PrecisionRecallMetricGetAreaUnderCurve(const PrecisionRecallMetric &algo, rust::Slice xs, rust::Slice ys) { vector xs_{xs.begin(), xs.end()}; vector ys_{xs.begin(), ys.end()}; return algo.getAreaUnderCurve(pair(xs_, ys_)); } inline unique_ptr NewROCMetric(const Graph &G) { return make_unique(G); } inline void ROCMetricGetCurve(ROCMetric &algo, rust::Slice us, rust::Slice vs, rust::Slice ws, count numThresholds, rust::Vec &xs, rust::Vec &ys) { vector predictions{}; for (size_t i = 0; i < us.length(); ++i) { predictions.emplace_back(pair(pair(us[i], vs[i]), ws[i])); } auto res = algo.getCurve(predictions, numThresholds); for (size_t i = 0; i < res.first.size(); ++i) { xs.push_back(res.first[i]); ys.push_back(res.second[i]); } } inline double ROCMetricGetAreaUnderCurve(const ROCMetric &algo, rust::Slice xs, rust::Slice ys) { vector xs_{xs.begin(), xs.end()}; vector ys_{xs.begin(), ys.end()}; return algo.getAreaUnderCurve(pair(xs_, ys_)); } inline unique_ptr NewJaccardIndex(const Graph &G) { return make_unique(G); } inline void JaccardIndexRunOn(JaccardIndex &algo, rust::Slice src, rust::Slice dst, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { vector> pairs{}; for (size_t i = 0; i < src.length(); ++i) { pairs.emplace_back(pair(src[i], dst[i])); } for (auto &&res : algo.runOn(pairs)) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline void JaccardIndexRunAll(JaccardIndex &algo, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { for (auto &&res : algo.runAll()) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline unique_ptr NewKatzIndex(const Graph &G, count maxPathLength = 5, double dampingValue = 0.005) { return make_unique(G, maxPathLength, dampingValue); } inline void KatzIndexRunOn(KatzIndex &algo, rust::Slice src, rust::Slice dst, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { vector> pairs{}; for (size_t i = 0; i < src.length(); ++i) { pairs.emplace_back(pair(src[i], dst[i])); } for (auto &&res : algo.runOn(pairs)) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline void KatzIndexRunAll(KatzIndex &algo, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { for (auto &&res : algo.runAll()) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline void LinkThresholderByCount(rust::Slice us, rust::Slice vs, rust::Slice ws, count numLinks, rust::Vec &src, rust::Vec &dst) { vector predictions{}; for (size_t i = 0; i < us.length(); ++i) { predictions.emplace_back(pair(pair(us[i], vs[i]), ws[i])); } for (auto &&p : LinkThresholder::byCount(predictions, numLinks)) { src.push_back(p.first); dst.push_back(p.second); } } inline void LinkThresholderByPercentage(rust::Slice us, rust::Slice vs, rust::Slice ws, double percentageLinks, rust::Vec &src, rust::Vec &dst) { vector predictions{}; for (size_t i = 0; i < us.length(); ++i) { predictions.emplace_back(pair(pair(us[i], vs[i]), ws[i])); } for (auto &&p : LinkThresholder::byPercentage(predictions, percentageLinks)) { src.push_back(p.first); dst.push_back(p.second); } } inline void LinkThresholderByScore(rust::Slice us, rust::Slice vs, rust::Slice ws, double minScore, rust::Vec &src, rust::Vec &dst) { vector predictions{}; for (size_t i = 0; i < us.length(); ++i) { predictions.emplace_back(pair(pair(us[i], vs[i]), ws[i])); } for (auto &&p : LinkThresholder::byScore(predictions, minScore)) { src.push_back(p.first); dst.push_back(p.second); } } inline unique_ptr NewMissingLinksFinder(const Graph &G) { return make_unique(G); } inline void MissingLinksFinderFindAtDistance(MissingLinksFinder &algo, count k, rust::Vec &src, rust::Vec &dst) { for (auto &&p : algo.findAtDistance(k)) { src.push_back(p.first); dst.push_back(p.second); } } inline void MissingLinksFinderFindFromNode(MissingLinksFinder &algo, node u, count k, rust::Vec &src, rust::Vec &dst) { for (auto &&p : algo.findFromNode(u, k)) { src.push_back(p.first); dst.push_back(p.second); } } inline unique_ptr NewNeighborhoodDistanceIndex(const Graph &G) { return make_unique(G); } inline void NeighborhoodDistanceIndexRunOn(NeighborhoodDistanceIndex &algo, rust::Slice src, rust::Slice dst, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { vector> pairs{}; for (size_t i = 0; i < src.length(); ++i) { pairs.emplace_back(pair(src[i], dst[i])); } for (auto &&res : algo.runOn(pairs)) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline void NeighborhoodDistanceIndexRunAll(NeighborhoodDistanceIndex &algo, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { for (auto &&res : algo.runAll()) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline unique_ptr> NeighborhoodUtilityGetNeighborsUnion(const Graph &G, node u, node v) { return make_unique>(NeighborhoodUtility::getNeighborsUnion(G, u, v)); } inline unique_ptr> NeighborhoodUtilityGetCommonNeighbors(const Graph &G, node u, node v) { return make_unique>(NeighborhoodUtility::getCommonNeighbors(G, u, v)); } inline unique_ptr NewNeighborsMeasureIndex(const Graph &G) { return make_unique(G); } inline void NeighborsMeasureIndexRunOn(NeighborsMeasureIndex &algo, rust::Slice src, rust::Slice dst, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { vector> pairs{}; for (size_t i = 0; i < src.length(); ++i) { pairs.emplace_back(pair(src[i], dst[i])); } for (auto &&res : algo.runOn(pairs)) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline void NeighborsMeasureIndexRunAll(NeighborsMeasureIndex &algo, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { for (auto &&res : algo.runAll()) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline unique_ptr NewPreferentialAttachmentIndex(const Graph &G) { return make_unique(G); } inline void PreferentialAttachmentIndexRunOn(PreferentialAttachmentIndex &algo, rust::Slice src, rust::Slice dst, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { vector> pairs{}; for (size_t i = 0; i < src.length(); ++i) { pairs.emplace_back(pair(src[i], dst[i])); } for (auto &&res : algo.runOn(pairs)) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline void PreferentialAttachmentIndexRunAll(PreferentialAttachmentIndex &algo, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { for (auto &&res : algo.runAll()) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline unique_ptr RandomLinkSamplerByCount(const Graph &G, count numLinks) { return make_unique(RandomLinkSampler::byCount(G, numLinks)); } inline unique_ptr RandomLinkSamplerByPercentage(const Graph &G, double percentage) { return make_unique(RandomLinkSampler::byPercentage(G, percentage)); } inline unique_ptr NewResourceAllocationIndex(const Graph &G) { return make_unique(G); } inline void ResourceAllocationIndexRunOn(ResourceAllocationIndex &algo, rust::Slice src, rust::Slice dst, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { vector> pairs{}; for (size_t i = 0; i < src.length(); ++i) { pairs.emplace_back(pair(src[i], dst[i])); } for (auto &&res : algo.runOn(pairs)) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline void ResourceAllocationIndexRunAll(ResourceAllocationIndex &algo, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { for (auto &&res : algo.runAll()) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline unique_ptr NewSameCommunityIndex(const Graph &G) { return make_unique(G); } inline void SameCommunityIndexRunOn(SameCommunityIndex &algo, rust::Slice src, rust::Slice dst, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { vector> pairs{}; for (size_t i = 0; i < src.length(); ++i) { pairs.emplace_back(pair(src[i], dst[i])); } for (auto &&res : algo.runOn(pairs)) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline void SameCommunityIndexRunAll(SameCommunityIndex &algo, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { for (auto &&res : algo.runAll()) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline unique_ptr NewTotalNeighborsIndex(const Graph &G) { return make_unique(G); } inline void TotalNeighborsIndexRunOn(TotalNeighborsIndex &algo, rust::Slice src, rust::Slice dst, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { vector> pairs{}; for (size_t i = 0; i < src.length(); ++i) { pairs.emplace_back(pair(src[i], dst[i])); } for (auto &&res : algo.runOn(pairs)) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline void TotalNeighborsIndexRunAll(TotalNeighborsIndex &algo, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { for (auto &&res : algo.runAll()) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline unique_ptr NewUDegreeIndex(const Graph &G) { return make_unique(G); } inline void UDegreeIndexRunOn(UDegreeIndex &algo, rust::Slice src, rust::Slice dst, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { vector> pairs{}; for (size_t i = 0; i < src.length(); ++i) { pairs.emplace_back(pair(src[i], dst[i])); } for (auto &&res : algo.runOn(pairs)) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline void UDegreeIndexRunAll(UDegreeIndex &algo, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { for (auto &&res : algo.runAll()) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline unique_ptr NewVDegreeIndex(const Graph &G) { return make_unique(G); } inline void VDegreeIndexRunOn(VDegreeIndex &algo, rust::Slice src, rust::Slice dst, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { vector> pairs{}; for (size_t i = 0; i < src.length(); ++i) { pairs.emplace_back(pair(src[i], dst[i])); } for (auto &&res : algo.runOn(pairs)) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } inline void VDegreeIndexRunAll(VDegreeIndex &algo, rust::Vec &ks, rust::Vec &vs, rust::Vec &ws) { for (auto &&res : algo.runAll()) { ks.push_back(res.first.first); vs.push_back(res.first.second); ws.push_back(res.second); } } } #endif // NK_LINK_PREDICTION_H