File: PageRank.cpp

package info (click to toggle)
tulip 4.6.0dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 139,284 kB
  • ctags: 35,942
  • sloc: cpp: 289,758; ansic: 27,264; python: 1,256; sh: 923; yacc: 522; xml: 337; makefile: 258; php: 66; lex: 55
file content (157 lines) | stat: -rw-r--r-- 4,343 bytes parent folder | download
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
 *
 * This file is part of Tulip (www.tulip-software.org)
 *
 * Authors: David Auber and the Tulip development Team
 * from LaBRI, University of Bordeaux
 *
 * Tulip is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * Tulip is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 */
#include <tulip/ForEach.h>
#include <tulip/tulipconf.h>
#include <tulip/TulipPluginHeaders.h>
#include <math.h>

using namespace std;
using namespace tlp;


static const char * paramHelp[] = {
  HTML_HELP_OPEN() \
  HTML_HELP_DEF( "type", "double" ) \
  HTML_HELP_BODY() \
  "Choose a damping factor in ]0,1[" \
  HTML_HELP_CLOSE(),
  HTML_HELP_OPEN() \
  HTML_HELP_DEF( "type", "bool" ) \
  HTML_HELP_DEF( "default", "true" )  \
  HTML_HELP_BODY() \
  "indicate if the graph should be considered as directed or not" \
  HTML_HELP_CLOSE(),
};

/*@{*/
/** \file
 *  \brief  An implementation of the PageRank metric
 *
 * This plugin is an implementation of the PageRank metric.
 * First designed by Larry Page and Sergey Brin, it is a link analysis
 * algorithm that assigns a measure to each node of an 'hyperlinked' graph.
 *
 * (see http://en.wikipedia.org/wiki/PageRank for more details)
 *
 *  <b>HISTORY</b>
 *
 *  - 2007 Version 1.0: Initial release
 *  by Mohamed Bouklit, LaBRI, University Bordeaux I, France
 *  - 2010 Version 1.1: rewrite to work properly with subgraph
 *  by David Auber, LaBRI, University Bordeaux I, France
 *  - 2011 Version 2.0: fix computation for undirected graph
 *  by François Queyroi, LaBRI, University Bordeaux I, France
 *
 *  <b>LICENCE</b>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
*/
/*@}*/
struct PageRank : public DoubleAlgorithm {

  PLUGININFORMATION("Page Rank","Mohamed Bouklit & David Auber","16/12/10", "Nodes measure<br/>used for links analysis.","2.0","Graph")

  PageRank(const PluginContext *context) : DoubleAlgorithm(context) {
    addInParameter<double>("d", paramHelp[0], "0.85");
    addInParameter<bool>("directed", paramHelp[1], "true");
  }

  bool run() {
    double d = 0.85;
    bool directed = true;

    if ( dataSet!=0 ) {
      dataSet->get("d",d);
      dataSet->get("directed",directed);
    }

    if(d <= 0 || d >= 1) return false;


    double nbNodes = graph->numberOfNodes();

    // Initialize the PageRank
    MutableContainer<double> R;
    MutableContainer<double> R2;
    R.setAll( 1. / nbNodes);


    for(unsigned int k=0; k < 15*log(nbNodes); ++k) {

      //cout<<"[PageRank iteration "<<k<<" ] "<<endl;
      R2.setAll(0.);

      edge e;
      forEach(e, graph->getEdges()) {
        const std::pair<node, node> eEnds = graph->ends(e);
        node src = eEnds.first;

        if(directed) {
          node tgt = eEnds.second;
          double prev = R2.get(tgt);
          R2.set(tgt, prev + R.get(src) / double(graph->outdeg(src)));
        }
        else {
          node tgt = eEnds.second;
          double prev = R2.get(tgt);
          R2.set(tgt, prev + R.get(src) / double(graph->deg(src)));

          prev = R2.get(src);
          R2.set(src, prev + R.get(tgt) / double(graph->deg(tgt)));
        }
      }

      node n;
      forEach(n, graph->getNodes())
      R2.set(n, d * R2.get(n));

      double mu = 0.0;
      forEach(n, graph->getNodes())
      mu += R.get(n) - R2.get(n);

      forEach(n, graph->getNodes())
      R2.set(n, R2.get(n) + mu*1.0/nbNodes);

      /*double delta = 0.0;
      forEach(n, graph->getNodes())

      if (R.get(n) > R2.get(n))
        delta += R.get(n) - R2.get(n);
      else
      delta += R2.get(n) - R.get(n);*/

      forEach(n, graph->getNodes())
      R.set(n, R2.get(n));

    }

    node n;
    forEach(n, graph->getNodes()) {
      result->setNodeValue(n, R.get(n));
    }

    return true;
  }
};

PLUGIN(PageRank)