| #include "graph.h" |
| #include <algorithm> |
| #include <queue> |
| #include <cstddef> |
|
|
| void Graph::add_edge(int32_t src, int32_t dst, float w) { |
| add_edge_typed(src, dst, w, -1, 1); |
| } |
|
|
| void Graph::add_edge_typed(int32_t src, int32_t dst, float w, int16_t type, int8_t pol) { |
| if (src < 0 || src >= n_ || dst < 0 || dst >= n_ || src == dst) return; |
| auto& e = adj_[src][dst]; |
| |
| |
| if (w > e.w) { e.w = w; e.type = type; e.pol = pol; } |
| } |
|
|
| int64_t Graph::n_edges() const { |
| int64_t c = 0; |
| for (auto& m : adj_) c += static_cast<int64_t>(m.size()); |
| return c; |
| } |
|
|
| float Graph::edge_weight(int32_t src, int32_t dst) const { |
| if (src < 0 || src >= n_) return 0.0f; |
| auto it = adj_[src].find(dst); |
| return it == adj_[src].end() ? 0.0f : it->second.w; |
| } |
|
|
| std::vector<ScoredDoc> Graph::spread(const std::vector<int32_t>& seeds, |
| const std::vector<float>& seed_acts, |
| int max_depth, |
| float depth_decay, |
| float min_act, |
| const std::vector<int16_t>& query_types, |
| float alpha, |
| float beta) const { |
| std::vector<float> act(n_, 0.0f); |
| std::vector<int> depth(n_, -1); |
|
|
| const bool typed = !query_types.empty(); |
|
|
| |
| std::queue<int32_t> frontier; |
| for (size_t i = 0; i < seeds.size(); i++) { |
| const int32_t s = seeds[i]; |
| if (s < 0 || s >= n_) continue; |
| const float a = (i < seed_acts.size()) ? seed_acts[i] : 1.0f; |
| if (a > act[s]) { act[s] = a; depth[s] = 0; frontier.push(s); } |
| } |
|
|
| while (!frontier.empty()) { |
| const int32_t u = frontier.front(); |
| frontier.pop(); |
| if (depth[u] >= max_depth) continue; |
| for (auto& [v, e] : adj_[u]) { |
| |
| |
| float f = 1.0f; |
| if (typed && e.type >= 0 && |
| std::find(query_types.begin(), query_types.end(), e.type) == query_types.end()) |
| f = alpha; |
| if (e.pol < 0) f *= beta; |
|
|
| const float cand = act[u] * e.w * f * depth_decay; |
| if (cand < min_act || cand <= act[v]) continue; |
| act[v] = cand; |
| depth[v] = depth[u] + 1; |
| frontier.push(v); |
| } |
| } |
|
|
| std::vector<ScoredDoc> out; |
| out.reserve(64); |
| for (int32_t i = 0; i < n_; i++) |
| if (act[i] > 0.0f) out.push_back({i, act[i]}); |
| std::sort(out.begin(), out.end(), |
| [](const ScoredDoc& a, const ScoredDoc& b){ return a.score > b.score; }); |
| return out; |
| } |
|
|
| namespace { |
| |
| |
| |
| struct Path { |
| int32_t last; |
| float sum; |
| float worst; |
| float tail; |
| int len; |
| std::vector<int32_t> nodes; |
| float score(int mode) const { |
| if (mode == 1) return worst; |
| if (mode == 2) return tail; |
| return sum / static_cast<float>(len); |
| } |
| }; |
| } |
|
|
| std::vector<ScoredDoc> Graph::beam_search(const std::vector<int32_t>& seeds, |
| const std::vector<float>& qsim, |
| int max_depth, |
| int beam_width, |
| float min_sim, |
| int score_mode) const { |
| std::vector<float> best(n_, -1.0f); |
|
|
| for (int32_t s : seeds) { |
| if (s < 0 || s >= n_ || s >= static_cast<int32_t>(qsim.size())) continue; |
|
|
| |
| std::vector<Path> beam; |
| beam.push_back({s, qsim[s], qsim[s], qsim[s], 1, {s}}); |
| if (qsim[s] > best[s]) best[s] = qsim[s]; |
|
|
| for (int d = 0; d < max_depth && !beam.empty(); d++) { |
| std::vector<Path> cand; |
| cand.reserve(beam.size() * 8); |
| for (const Path& p : beam) { |
| auto it = adj_.begin() + p.last; |
| for (const auto& [v, e] : *it) { |
| if (qsim[v] < min_sim) continue; |
| |
| if (std::find(p.nodes.begin(), p.nodes.end(), v) != p.nodes.end()) |
| continue; |
| Path q; |
| q.last = v; |
| q.sum = p.sum + qsim[v]; |
| q.worst = std::min(p.worst, qsim[v]); |
| q.tail = qsim[v]; |
| q.len = p.len + 1; |
| q.nodes = p.nodes; |
| q.nodes.push_back(v); |
| cand.push_back(std::move(q)); |
| } |
| } |
| if (cand.empty()) break; |
|
|
| const size_t keep = std::min<size_t>(cand.size(), |
| std::max(1, beam_width)); |
| std::partial_sort(cand.begin(), cand.begin() + keep, cand.end(), |
| [score_mode](const Path& a, const Path& b) { |
| return a.score(score_mode) > b.score(score_mode); |
| }); |
| cand.resize(keep); |
| for (const Path& p : cand) { |
| const float sc = p.score(score_mode); |
| if (sc > best[p.last]) best[p.last] = sc; |
| } |
| beam.swap(cand); |
| } |
| } |
|
|
| std::vector<ScoredDoc> out; |
| out.reserve(64); |
| for (int32_t i = 0; i < n_; i++) |
| if (best[i] >= 0.0f) out.push_back({i, best[i]}); |
| std::sort(out.begin(), out.end(), |
| [](const ScoredDoc& a, const ScoredDoc& b) { return a.score > b.score; }); |
| return out; |
| } |
|
|
| void Graph::reinforce(const std::vector<int32_t>& coactive, float delta, float w_max) { |
| for (size_t i = 0; i < coactive.size(); i++) { |
| for (size_t j = i + 1; j < coactive.size(); j++) { |
| const int32_t a = coactive[i], b = coactive[j]; |
| if (a < 0 || a >= n_ || b < 0 || b >= n_ || a == b) continue; |
| |
| EdgeAttr& ab = adj_[a][b]; |
| EdgeAttr& ba = adj_[b][a]; |
| ab.w = std::min(w_max, ab.w + delta); |
| ba.w = std::min(w_max, ba.w + delta); |
| } |
| } |
| } |
|
|
| void Graph::decay(float lambda) { |
| const float f = 1.0f - lambda; |
| for (auto& m : adj_) |
| for (auto& [v, e] : m) e.w *= f; |
| } |
|
|