Skip to content

Instantly share code, notes, and snippets.

@jy95
Created March 5, 2015 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jy95/7b138f1257bfc4b17ffe to your computer and use it in GitHub Desktop.
Save jy95/7b138f1257bfc4b17ffe to your computer and use it in GitHub Desktop.
Methode PageRank
public Matrice pageRank() {
// 0. Virer si pas carré
if (!this.carree())
throw new MathException("Pas carrée");
// 1. vérification matrice avec que des 0 et des 1
for (int i = 0 ; i < nbLignes ; i++) {
for (int j= 0 ; j < nbColonnes ; j++) {
if (data[i][j] != 0.0 && data[i][j] != 1.0 )
throw new MathException("Pas que des 0 et des 1");
}
}
// 2. Creer matrice transistion (sans oublier trous noir)
Matrice transistion = new Matrice(this);
Matrice trousNoirs = new Matrice(transistion.nbLignes,transistion.nbColonnes);
for (int i = 0 ; i < transistion.nbColonnes; i++) {
double sommeColonne = 0;
for (int j= 0 ; j < transistion.nbLignes ; j++) {
sommeColonne += transistion.data[j][i];
trousNoirs.data[j][i] = 1.0/nbLignes;
}
if (sommeColonne == 0){
for (int k = 0 ; k < transistion.nbColonnes ; k++) {
transistion.data[k][i] = 1.0/nbLignes;
}
} else {
for (int k = 0 ; k < transistion.nbColonnes ; k++) {
transistion.data[k][i] /= sommeColonne;
}
}
}
// 3. Créer matrice google
Matrice google = transistion.produitParScalaire(0.85).somme(trousNoirs.produitParScalaire(0.15));
// 4. Association matrice initial v0
Matrice v0 = new Matrice(nbColonnes,1);
v0.data[0][0] = 1;
// 5. Calculer état stable
for (int i= 0 ; i <= 85 ; i++){
Matrice temporaire = google.produitAGauche(v0);
// normaliser matrice
double sommeColonne = 0;
for (int j = 0 ; j < temporaire.nbLignes ; j++) {
sommeColonne += temporaire.data[j][0];
}
if (sommeColonne != 0.0) {
for (int j = 0 ; j < temporaire.nbLignes ; j++) {
temporaire.data[j][0] = temporaire.data[j][0] / sommeColonne;
}
}
v0 = temporaire;
}
return v0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment