Advertisement
sebbu

Graphs (2D version)

Nov 18th, 2014
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.87 KB | None | 0 0
  1. <?php
  2. function array_splice_INV(array $array , int $offset , int $length = NULL , mixed $replacement = NULL ) {
  3.     return array_merge(array_slice($array, 0, $offset), $replacement, array_slice($array, $offset+$length));
  4. }
  5. interface Graph {
  6.     //points / vertices / nodes
  7.     public function addPoint($name);
  8.     public function countPoints();
  9.     public function pointExists($name);
  10.     public function delPoint($name);
  11.     //arcs / edges / links / lines
  12.     public function addLine($src, $dst);
  13.     public function countLines();
  14.     public function lineExists($src, $dst);
  15.     public function delLine($src, $dst);
  16.     //browsing
  17.     public function listLinesFrom($src);
  18.     public function listLinesTo($dst);
  19.     //starts and ends
  20.     public function listSources();
  21.     public function listSinks();
  22.     //iterators ?
  23. };
  24.  
  25. class AdjencyList2D implements Graph {
  26.     private $names;
  27.     private $points;
  28.     private $lines;
  29.     public function dump() {
  30.         echo '$names=';var_dump($this->names);
  31.         echo '$points=';var_dump($this->points);
  32.         echo '$lines=';var_dump($this->lines);
  33.     }
  34.     public function __construct() {
  35.         $this->names=array();
  36.         $this->points=array();
  37.         $this->lines=array();
  38.     }
  39.     //names
  40.     public function getName($index) {
  41.         return $this->names[$index];
  42.     }
  43.     //points
  44.     public function addPoint($name) {
  45.         $i=count($this->names);
  46.         $this->names[$i]=$name;
  47.         $this->points[$i]=0;
  48.         return $i;
  49.     }
  50.     public function countPoints() {
  51.         assert(count($this->names)==count($this->points));
  52.         return count($this->points);
  53.     }
  54.     public function pointExists($name) {
  55.         return array_search($name, $this->names, true);
  56.     }
  57.     public function delPoint($name) {
  58.         $i=$this->pointExists($name);
  59.         if($i===false) return false;
  60.         unset($this->names[$i]);
  61.         unset($this->points[$i]);
  62.         unset($this->lines[$i]);
  63.         foreach($this->lines as $k=>$v) {
  64.             $s=array_search($i, $v);
  65.             if($s!==false) {
  66.                 $ar=array_splice($this->lines[$k], $s, 1);
  67.                 assert($ar==array($i));
  68.             }
  69.         }
  70.     }
  71.     //lines
  72.     public function addLine($src, $dst) {
  73.         $s=$this->pointExists($src);
  74.         $d=$this->pointExists($dst);
  75.         if($s===false || $d===false) return false;
  76.         if(!array_key_exists($s,$this->lines)) $this->lines[$s]=array();
  77.         $ext=&$this->lines[$s];
  78.         $ext[]=$d;
  79.         $ext=array_unique($ext);
  80.         sort($ext);
  81.         $this->points[$s]=count($ext);
  82.         return true;
  83.     }
  84.     public function countLines() {
  85.         return count($this->lines);
  86.     }
  87.     public function lineExists($src, $dst) {
  88.         $s=$this->pointExists($src);
  89.         $d=$this->pointExists($dst);
  90.         if($s===false || $d===false) return false;
  91.         return array_search($d, $this->lines[$s], true);
  92.     }
  93.     public function delLine($src, $dst) {
  94.         $s=$this->pointExists($src);
  95.         $d=$this->pointExists($dst);
  96.         if($s===false||$d===false) return false;
  97.         $p=array_search($d, $this->lines[$s]);
  98.         if($p===false) return false;
  99.         $ar=array_splice($this->lines[$s], $p, 1);
  100.         assert($ar==array($d));
  101.     }
  102.     //browsing
  103.     public function listLinesFrom($src) {
  104.         $s=$this->pointExists($src);
  105.         if($s===false) return false;
  106.         return $this->lines[$s];
  107.     }
  108.     public function listLinesTo($dst) {
  109.         $d=$this->pointExists($dst);
  110.         $res=array();
  111.         foreach($this->lines as $k=>$v) {
  112.             if(in_array($d, $v)) $res[]=$k;
  113.         }
  114.         return $res;
  115.     }
  116.     //starts and ends
  117.     public function listSources() {
  118.         $res=array();
  119.         for($i=0;$i<count($this->points);$i++) {
  120.             foreach($this->lines as $k=>$v) {
  121.                 if(in_array($i, $v)) continue(2);
  122.             }
  123.             $res[]=$i;
  124.         }
  125.         return $res;
  126.     }
  127.     public function listSinks() {
  128.         $res=array();
  129.         foreach($this->points as $p) {
  130.             if(!array_key_exists($p, $this->lines) || count($this->lines[$p])==0) $res[]=$p;
  131.         }
  132.         return $res;
  133.     }
  134. };
  135. $graph=new AdjencyList2D();
  136. $graph->addPoint('A');
  137. $graph->addPoint('B');
  138. $graph->addPoint('C');
  139. $graph->addLine('A','B');
  140. $graph->addLine('B','C');
  141. $graph->addLine('C','A');
  142. $graph->addLine('C','B');
  143. $graph->addLine('A','C');
  144. $graph->addLine('B','A');
  145. $graph->dump();
  146. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement