schochastics / netrankr
1
#include <Rcpp.h>
2
// #include <cstddef>
3
using namespace Rcpp;
4

5
// typedef int_least64_t linex;
6

7 2
double AssignTopDown(int v, 
8
                  NumericVector &lef, 
9
                  IntegerVector &visited,
10
                  std::vector<std::vector<int> > &ImSucc
11
                  ){
12 2
  visited[v]=1;
13 2
  double e=0;
14 2
  for(std::vector<int>::size_type i = 0; i!=ImSucc[v].size(); ++i){
15
  // for(int i=0; i<ImSucc[v].size(); ++i){
16 2
    int vPrime=ImSucc[v][i];
17 2
    if(vPrime==0){
18 2
      e+=1;
19 2
      lef[vPrime]=1;
20
    }
21
    else{
22 2
      if(visited[vPrime]==0){
23 2
        e=e+AssignTopDown(vPrime,lef,visited,ImSucc);
24
      }
25
      else{
26 2
        e=e+lef[vPrime];
27
      }
28
    }
29
  }
30 2
  lef[v]=e;
31 2
  return e;
32
}
33

34 2
void AssignBottomUp(int nElem,
35
                    NumericVector &lei, 
36
                    IntegerVector &visited,
37
                    std::vector<std::vector<int> > &ImSucc){
38 2
  std::vector<int> Q;
39

40 2
  lei(nElem)=1;
41

42 2
  for(std::vector<int>::size_type i = 0; i!=ImSucc[nElem].size();++i){
43
  // for(int i=0; i<ImSucc[nElem].size();++i){
44 2
    int vPrime=ImSucc[nElem][i];
45 2
    Q.push_back(vPrime);
46 2
    lei[vPrime]=1;
47
  }
48 2
  while(Q.size()!=0){
49 2
    Rcpp::checkUserInterrupt();
50 2
    int v=Q[0];
51 2
    Q.erase(Q.begin());
52

53 2
    for(std::vector<int>::size_type j = 0; j!=ImSucc[v].size();++j){
54
    // for(int j=0;j<ImSucc[v].size();++j){
55 2
      int vPrime=ImSucc[v][j];
56 2
      lei[vPrime]+=lei[v];
57 2
      if(visited[vPrime]==0){
58 2
        Q.push_back(vPrime);
59 2
        visited[vPrime]=1;
60
      }
61
    }
62
  }
63
}
64

65 2
void ComputeRankProb(int v,int h, NumericMatrix &rp,
66
                     std::vector<std::vector<int> > &ImSucc,
67
                     std::vector<std::vector<int> > &ideals,
68
                     IntegerVector &visited,
69
                     NumericVector &lei,
70
                     NumericVector &lef,
71
                     double &e){
72 2
  visited[v]=1;
73
  // std::vector<int>::size_type i = 0; i!=child[0].size(); ++i
74 2
  for(std::vector<int>::size_type j = 0;j!=ImSucc[v].size();++j){
75
    // for(int j=0;j<ImSucc[v].size();++j){
76 2
    int vPrime=ImSucc[v][j];
77
    int x;
78 2
    std::set_difference(ideals[vPrime].begin(), ideals[vPrime].end(),
79 2
                        ideals[v].begin(), ideals[v].end(), &x);
80 2
    rp(x,h)=rp(x,h)+double(lei[v])*double(lef[vPrime])/double(e);
81 2
    if((vPrime!=0) & (visited[vPrime]==0)){
82 2
      ComputeRankProb(vPrime,h+1,rp,ImSucc,ideals,visited,lei,lef,e);
83
    }
84
  }
85
  
86
}
87

88 2
void ComputeMutualRankProb(int v,int h, int &nElem,
89
                           NumericMatrix &mrp,
90
                           std::vector<std::vector<int> > &ImSucc,
91
                           std::vector<std::vector<int> > &ideals,
92
                           IntegerVector &visited,
93
                           IntegerVector &visitedElem,
94
                           NumericVector &lei,
95
                           NumericVector &lef,
96
                           double &e){
97 2
  visited[v]=1;
98 2
  for(std::vector<int>::size_type j = 0;j!=ImSucc[v].size();++j){
99
  // for(int j=0;j<ImSucc[v].size();++j){
100 2
    int vPrime=ImSucc[v][j];
101 2
    for(int y=0;y<nElem; ++y){
102 2
      if(visitedElem[y]==1){
103
        int x;
104 2
        std::set_difference(ideals[vPrime].begin(), ideals[vPrime].end(),
105 2
                            ideals[v].begin(), ideals[v].end(), &x);
106 2
        mrp(x,y)=mrp(x,y)+double(lei[v])*double(lef[vPrime])/double(e);
107
      }
108 2
      if((vPrime!=0) & (visited[vPrime]==0)){
109
        int x;
110 2
        std::set_difference(ideals[vPrime].begin(), ideals[vPrime].end(),
111 2
                            ideals[v].begin(), ideals[v].end(), &x);
112 2
        visitedElem[x]=1;
113 2
        ComputeMutualRankProb(vPrime,h+1,nElem,mrp,ImSucc,ideals,visited,visitedElem,lei,lef,e);
114 2
        visitedElem[x]=0;
115
      }
116
        
117
    }
118
  }
119
  
120
  
121
}
122

123
// [[Rcpp::export]]
124 2
Rcpp::List rankprobs(std::vector<std::vector<int> > ImPred,
125
                     std::vector<std::vector<int> > ideals,
126
                     int nElem,
127
                     int nIdeals){
128
  double e;
129 2
  NumericVector lei(nIdeals);
130 2
  NumericVector lef(nIdeals);
131 2
  IntegerVector visited(nIdeals);
132 2
  IntegerVector visitedElem(nElem);
133
  
134
  /* Turn ImPred to ImSucc*/
135 2
  std::vector<std::vector<int> > ImSucc(nIdeals);
136 2
  for(int i=0; i<nIdeals;++i){
137 2
    for(std::vector<int>::size_type j=0;j!=ImPred[i].size();++j){
138
    // for(int j=0;j<ImPred[i].size();++j){
139 2
      int idx=ImPred[i][j];
140 2
      ImSucc[idx].push_back(i);
141
    }
142
  }
143
  /* Sort increasingly*/
144 2
  for(int i=0;i<nIdeals;++i){
145 2
    std::sort(ImSucc[i].begin(), ImSucc[i].end());
146
  }
147
  /*calculate number of path*/
148 2
  e=AssignTopDown(nElem, lef,visited,ImSucc);
149 2
  std::fill(visited.begin(), visited.end(), 0);
150 2
  AssignBottomUp(nElem,lei,visited,ImSucc);
151

152
  /*rank probabilities*/
153 2
  std::fill(visited.begin(), visited.end(), 0);
154 2
  NumericMatrix rp(nElem,nElem);
155 2
  ComputeRankProb(nElem,0,rp,ImSucc,ideals,visited,lei,lef,e);
156
  
157
  /*mutual rank probabilities*/
158 2
  std::fill(visited.begin(), visited.end(), 0);
159 2
  NumericMatrix mrp(nElem,nElem);
160 2
  ComputeMutualRankProb(nElem,1,nElem,mrp,ImSucc,ideals,visited,visitedElem,lei,lef,e);
161 2
  return Rcpp::List::create(Rcpp::Named("linext") = e, 
162 2
                            Rcpp::Named("rp")=rp,
163 2
                            Rcpp::Named("mrp")=mrp);
164
}
165

166

Read our documentation on viewing source code .

Loading