[Java]SimRank

來源:互聯網
上載者:User

有關SimRank請參看文章http://www.cnblogs.com/youwang/archive/2010/02/06/1665105.html,或者去看相關論文。下面是Java代碼實現,用到的是webgraph庫。

/* * 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);                }            }        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.