neurographdb / src /graph.cpp
goethe0101's picture
Upload folder using huggingface_hub
9422a5b verified
Raw
History Blame Contribute Delete
7.27 kB
#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]) {
/* ์ˆ ์–ด ํ˜ธํ™˜ โ€” ๋ฌดํƒ€์ž…(-1)์€ ์–ธ์ œ๋‚˜ ํ†ต๊ณผํ•œ๋‹ค. ์งˆ์˜ ์ˆ ์–ด๊ฐ€ ์—†์–ด๋„ ํ†ต๊ณผํ•œ๋‹ค.
* ๊ทธ๋ž˜์„œ ๋ช…์ œ์ธต์„ ๋„๋ฉด ์•„๋ž˜ ๊ณฑ์…ˆ์ด ์ „๋ถ€ 1.0์ด ๋˜์–ด ์˜ˆ์ „ ์ฝ”๋“œ์™€ ๊ฐ™์•„์ง„๋‹ค. */
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 {
/* ๋น” ์•ˆ์˜ ํ•œ ๊ฒฝ๋กœ. ๊นŠ์ด๊ฐ€ 3์ด๋ผ ๋…ธ๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ๋“ค๊ณ  ๋‹ค๋…€๋„ ์‹ธ๋‹ค.
* ๋ฐฉ๋ฌธ ๋…ธ๋“œ๋ฅผ ๋‹ด๋Š” ์ด์œ ๋Š” ์ˆœํ™˜์„ ๋ง‰๊ธฐ ์œ„ํ•ด์„œ๋‹ค โ€” ๊ฐ™์€ ๋ฌธ๋‹จ์„ ๋‘ ๋ฒˆ ์ง€๋‚˜๋Š” ๊ฒฝ๋กœ๋Š”
* ์ƒˆ ์ •๋ณด๋ฅผ ์ฃผ์ง€ ์•Š์œผ๋ฉด์„œ ํ‰๊ท  ์ ์ˆ˜๋งŒ ์˜ฌ๋ฆฐ๋‹ค. */
struct Path {
int32_t last;
float sum; /* ๊ฒฝ๋กœ ์œ„ qsim ํ•ฉ */
float worst; /* ๊ฒฝ๋กœ ์œ„ ์ตœ์†Œ qsim */
float tail; /* ๋„์ฐฉ ๋…ธ๋“œ qsim */
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);
}
};
} // namespace
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;
/* seed๋งˆ๋‹ค ๋…๋ฆฝ์ ์œผ๋กœ ๋น”์„ ๊ตด๋ฆฐ๋‹ค. ์—ฌ๊ธฐ์„œ ํ•ฉ์น˜๋ฉด ํฌ์†Œ seed๊ฐ€ ๋ฌด์˜๋ฏธํ•ด์ง„๋‹ค. */
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;
}