Php Page Rank Simulator

If you wonder how does google's page rank work, this software can give you an idea, live. For the detailed information about page rank theory click here .

Here (except for unlucky ie users) you can see the live demo of pr simulator, and download the open source software at the bottom of the page. Every single circle represents a website and the radius of the circle changes according to the pr of that site. As you can guess, the paths are the links. İf you'd like to see the source code:

 

  1. class universe{
  2.  
  3. private $number_of_sites;
  4. const TOTAL_VALUE = 100;
  5. const NUMBER_OF_ITERATIONS = 15;
  6. const D = 0.85;
  7.  
  8. function __construct($number_of_sites)
  9. {
  10. $this->number_of_sites = $number_of_sites;
  11. $this->sites = array();
  12.  
  13. $this->initial_site_value = self::TOTAL_VALUE /$this->number_of_sites;
  14. for($i=0;$i<$this->number_of_sites;$i++)
  15. {
  16. $this->add_site($i);
  17. }
  18. }
  19.  
  20. private function add_site($site)
  21. {
  22. array_push($this->sites,array(
  23. id=>$site,
  24. pr=>$this->initial_site_value,
  25. links_to=>array(),
  26. links_from=>array()
  27. ));
  28. }
  29.  
  30. public function add_connection($from,$to)
  31. {
  32. if(!in_array($from,$this->sites[$to]["links_from"]))
  33. {
  34. array_push($this->sites[$to]["links_from"],$from);
  35. array_push($this->sites[$from]["links_to"],$to);
  36. }
  37.  
  38. }
  39.  
  40. public function set_pr($site,$pr)
  41. {
  42. $this->sites[$site]["pr"] = $pr;
  43. }
  44.  
  45. public function calculate()
  46. {
  47. for($k=0;$k<self::NUMBER_OF_ITERATIONS ;$k++)
  48. {
  49. foreach ($this->sites as $site)
  50. {
  51. $this->sites[$site['id']]['pr'] += 1 - self::D;
  52. foreach ($site["links_from"] as $links_from) {
  53. $number_of_links = count($this->sites[$links_from]["links_to"]);
  54. if ($number_of_links) {
  55. $this->sites[$site['id']]['pr'] += self::D * ($this->sites[$links_from]["pr"]/$number_of_links);
  56. }
  57. }
  58. }
  59. }
  60. $this->ratio();
  61. return $this->sites;
  62. }
  63.  
  64. public function ratio()
  65. {
  66. $total_value = 0;
  67. for($i=0;$i<$this->number_of_sites;$i++)
  68. {
  69. $total_value += $this->sites[$i]['pr'];
  70. }
  71.  
  72. $universe_value_ratio = $total_value/self::TOTAL_VALUE;
  73.  
  74. if($universe_value_ratio!==1)
  75. {
  76. for($i=0;$i<$this->number_of_sites;$i++)
  77. {
  78. $this->sites[$i]['pr'] = round($this->sites[$i]['pr']/$universe_value_ratio,2);
  79. }
  80. }
  81. }
  82. }

Let's make it possible to draw it with svg:

  1. class svg{
  2.  
  3. function __construct()
  4. {
  5. $this->svg_code_start = &quot;&lt;?xml version=\&quot;1.0\&quot; standalone=\&quot;no\&quot;?&gt;&lt;!DOCTYPE svg PUBLIC \&quot;-//W3C//DTD SVG 1.1//EN\&quot; \&quot;http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\&quot;&gt;\n&quot;;
  6.  
  7. $this->svg_code_start .= &quot;&lt;svg width=\&quot;100%\&quot; height=\&quot;100%\&quot; version=\&quot;1.1\&quot; xmlns=\&quot;http://www.w3.org/2000/svg\&quot;&gt;\n&quot;;
  8. $this->svg_code_end = &quot;&lt;/svg&gt;&quot;;
  9. $this->svg_code_middle = "";
  10. }
  11.  
  12.  
  13.  
  14. private function random_color(){
  15. mt_srand((double)microtime()*1000000);
  16. $c = '';
  17. while(strlen($c)<6){
  18. $c .= sprintf("%02X", mt_rand(0, 255));
  19. }
  20. return "#".$c;
  21. }
  22.  
  23. public function circle($center_x,$center_y,$radius)
  24. {
  25. $this->svg_code_middle .= &quot;&lt;circle cx=\&quot;$center_x\&quot; cy=\&quot;$center_y\&quot; r=\&quot;$radius\&quot; stroke=\&quot;&quot;.$this-&gt;random_color().&quot;\&quot; stroke-width=\&quot;1\&quot; fill=\&quot;&quot;.$this-&gt;random_color().&quot;\&quot;/&gt;\n&quot;;
  26. }
  27.  
  28. public function line($x1,$y1,$x2,$y2)
  29. {
  30. $this->svg_code_middle .= &quot;&lt;line x1=\&quot;$x1\&quot; y1=\&quot;$y1\&quot; x2=\&quot;$x2\&quot; y2=\&quot;$y2\&quot; stroke=\&quot;&quot;.$this-&gt;random_color().&quot;\&quot; stroke-width=\&quot;1\&quot;/&gt;\n&quot;;
  31. }
  32.  
  33. public function text($x,$y,$pr,$site)
  34. {
  35. $this->svg_code_middle .= &quot;&lt;text id=\&quot;TextElement\&quot; x=\&quot;&quot;.($x-40).&quot;\&quot; y=\&quot;&quot;.($y+40).&quot;\&quot; style=\&quot;font:bold 12px tahoma;\&quot;&gt;pr of $site is $pr&lt;/text&gt;\n&quot;;
  36. }
  37.  
  38. public function curve($x1,$y1,$x2,$y2)
  39. {
  40. $middle_x = ($x1+$x2)/2;
  41. $middle_y = ($y1+$y2)/8;
  42. $this->svg_code_middle .= &quot;&lt;path d=\&quot;M $x1,$y1 Q $middle_x,$middle_y $x2,$y2\&quot; style=\&quot;fill:none;stroke-width:0.4\&quot; stroke=\&quot;black\&quot; /&gt;\n&quot;;
  43. }
  44.  
  45. private function get_items()
  46. {
  47. return $this->svg_code_start. $this->svg_code_middle.$this->svg_code_end;
  48. }
  49.  
  50. private function svg_file_name()
  51. {
  52. $random_number = rand(10000,10000000000);
  53. $date = date("d_m_y-h.i.s");
  54. $random_code = $random_number.$date;
  55. return $random_code;
  56. }
  57.  
  58. public function save_svg()
  59. {
  60. $file_name = $this->svg_file_name();
  61. $fp = fopen('svg_data/'.$file_name.'.svg', 'w');
  62. fwrite($fp, $this->get_items());
  63. fclose($fp);
  64. return $file_name;
  65. }
  66. }

For the connections

  1. include_once 'main.inc.php';
  2.  
  3. $my_universe = new universe(15);
  4.  
  5. $my_universe->add_connection(3,0);
  6. $my_universe->add_connection(3,1);
  7. $my_universe->add_connection(1,2);
  8. $my_universe->add_connection(4,3);
  9. $my_universe->add_connection(4,5);
  10. $my_universe->add_connection(5,4);
  11. $my_universe->add_connection(5,1);
  12. $my_universe->add_connection(0,6);
  13. $my_universe->add_connection(6,1);
  14. $my_universe->add_connection(6,4);
  15. $my_universe->add_connection(3,4);
  16. $my_universe->add_connection(1,4);
  17. $my_universe->add_connection(5,4);
  18. $my_universe->add_connection(2,4);
  19. $my_universe->add_connection(0,4);
  20. $my_universe->add_connection(1,14);
  21. $my_universe->add_connection(2,14);
  22. $my_universe->add_connection(3,13);
  23. $my_universe->add_connection(4,12);
  24. $my_universe->add_connection(5,11);
  25. $my_universe->add_connection(7,1);
  26. $my_universe->add_connection(7,4);
  27. $my_universe->add_connection(8,1);
  28. $my_universe->add_connection(8,4);
  29. $my_universe->add_connection(9,4);
  30. $my_universe->add_connection(10,4);
  31. /*$my_universe->add_connection(15,3);
  32. $my_universe->add_connection(3,12);
  33. $my_universe->add_connection(8,10);
  34. $my_universe->add_connection(2,11);
  35. $my_universe->add_connection(2,12);
  36. $my_universe->add_connection(12,1);
  37. $my_universe->add_connection(13,5);
  38. $my_universe->add_connection(4,10);
  39. $my_universe->add_connection(7,16);*/
  40.  
  41.  
  42.  
  43.  
  44. /*$my_universe->set_pr(0,2.15);
  45. $my_universe->set_pr(1,19.15);
  46. $my_universe->set_pr(2,22.15);
  47. $my_universe->set_pr(3,25.15);
  48. $my_universe->set_pr(4,26.15);
  49. $my_universe->set_pr(5,15.15);
  50. $my_universe->set_pr(6,4.15);
  51. $my_universe->set_pr(7,6.15);
  52. $my_universe->set_pr(8,7.15);
  53. $my_universe->set_pr(9,30.15);*/
  54.  
  55. $my_universe->ratio();
  56.  
  57. $result = $my_universe->calculate();

and to print it:

  1. include_once 'inc/main.inc.php';
  2.  
  3. $number_of_sites = count($result);
  4.  
  5. //echo var_dump(factorize($number_of_sites));
  6.  
  7. $my_svg = new svg();
  8.  
  9. $i = 0;
  10. $div_width = 980;
  11.  
  12. $height_between = 120;
  13. $column_number = 5;
  14. $width_between = ($div_width - $start_x*2)/$column_number;
  15. $start_x = 50;
  16. $start_y = 50;
  17.  
  18. $div_height = $number_of_sites*$height_between/$column_number + $start_y*2;
  19. $y = $start_y;
  20. $x = $start_x;
  21.  
  22. foreach ($result as $item) {
  23.  
  24. if($i<$column_number)
  25. {
  26. $y = $start_y;
  27. $x = $start_x + $i*$width_between;
  28. }
  29. else
  30. {
  31. if($i%$column_number==0)
  32. {
  33. $x = $start_x;
  34. $y = $start_y + $height_between*($i/$column_number);
  35. }
  36. else
  37. {
  38. $x += $width_between;
  39. }
  40. }
  41.  
  42. $my_svg->circle($x,$y,$item['pr']);
  43. $my_svg->text($x,$y,$item['pr'],$item['id']);
  44.  
  45. foreach ($item['links_to'] as $links)
  46. {
  47. $x2 = $start_x + $links%$column_number*$width_between;
  48. $y2 = $start_y + floor($links/$column_number)*$height_between;
  49. $my_svg->curve($x,$y,$x2,$y2);
  50. }
  51.  
  52. $i++;
  53. }

 

 

<html>
    <head>
    <title>pr simulator</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="DC.Title" content="pr simulator" />
        <meta name="keywords" content="pr simulator, page rank simulator, page rank example, pr example" />
        <meta name="robots" content="index,follow" />

    </head>
    <style>
        #mainWrapper{
            width:<?php echo $div_width;?>px;
            height:<?php echo $div_height;?>;
            border:1px solid red;
            margin:0 auto;
        }
    </style>
    <body>
        <div id="mainWrapper">
            <embed src="svg_data/<?=$my_svg->save_svg();?>.svg" width="100%" height="100%" type="image/svg+xml" pluginspage="http://www.adobe.com/svg/viewer/install/" />
        </div>
    </body>

</html>

If you want to create your own universe, download the software, decide the number of the sites which are included by the universe, and make the connections
sorry for the ui :)