[Java]SimRank
有关SimRank请参看文章http://www.cnblogs.com/youwang/archive/2010/02/06/1665105.html,或者去看相关论文。下面是Java代码实现,用到的是webgraph库。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package cn.edu.dlut.wisdom; import it.unimi.dsi.fastutil.ints.*; import it.unimi.dsi.webgraph.*; /** * * @author You Wang */ public class SimRank { private ImmutableGraph graph; private ImmutableGraph igraph; private double dampening = 0.85 ; private Int2ObjectArrayMap<Int2DoubleArrayMap> scores; public double getDampening() { return dampening; } public void setDampening( double dampening) { this .dampening = dampening; } public SimRank(ImmutableGraph graph) { this .graph = graph; this .igraph = Transform.transpose(graph); } public SimRank(ImmutableGraph graph, ImmutableGraph igraph) { this .graph = graph; this .igraph = igraph; } public void computeSimRank() { int n = graph.numNodes(); int iter = (( int ) Math.abs(Math.log(( double ) n) / Math.log(( double ) 10 ))) + 1 ; computeSimRank(iter); } public Int2DoubleArrayMap getSimRank( int i) { return scores.get(i); } public Int2ObjectArrayMap getSimRank() { return scores; } public double getSimRank( int i, int j) { return scores.get(i).get(j); } public void computeSimRank( int iter) { // 初始化 int numNodes = graph.numNodes(); this .scores = new Int2ObjectArrayMap<Int2DoubleArrayMap>(numNodes); for ( int i = 0 ; i < numNodes; i++) { Int2DoubleArrayMap aux = new Int2DoubleArrayMap(numNodes); for ( int j = 0 ; j < numNodes; j++) { if (i == j) aux.put(j, 1 ); else aux.put(j, 0 ); } scores.put(i, aux); } // 迭代计算 while (iter-- > 0 ) { for ( int i = 0 ; i < numNodes; i++) { int numInNeighborsi = igraph.outdegree(i); if (numInNeighborsi == 0 ) continue ; int [] inNeighborsi = igraph.successorArray(i); Int2DoubleArrayMap rimap = scores.get(i); for ( int j = 0 ; j < numNodes; j++) { int numInNeighborsj = igraph.outdegree(j); if (numInNeighborsj == 0 ) continue ; int [] inNeighborsj = igraph.successorArray(j); double rab = 0 ; for ( int a : inNeighborsi) { Int2DoubleArrayMap aux = scores.get(a); for ( int b : inNeighborsj) { rab += aux.get(b); } } rab *= dampening / (numInNeighborsi * numInNeighborsj); rimap.put(j, rab); } } } } } |

本文基于署名 2.5 中国大陆许可协议发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名小橋流水(包含链接)。如您有任何疑问或者授权方面的协商,请给我发邮件。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述