欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

PHP实现图的邻矩阵及普利姆算法(Prim)

程序员文章站 2022-06-15 17:15:35
...
  1. require 'mGraph.php';
  2. $a = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');
  3. $b = array('ab'=>'10', 'af'=>'11', 'bg'=>'16', 'fg'=>'17', 'bc'=>'18', 'bi'=>'12', 'ci'=>'8', 'cd'=>'22', 'di'=>'21', 'dg'=>'24', 'gh'=>'19', 'dh'=>'16', 'de'=>'20', 'eh'=>'7','fe'=>'26');//键为边,值权值
  4. $test = new MGraph($a, $b);
  5. print_r($test->prim());
  6. ?>
  7. //mGraph.php
  8. class MGraph{
  9. private $vexs; //顶点数组
  10. private $arc; //边邻接矩阵,即二维数组
  11. private $arcData; //边的数组信息
  12. private $direct; //图的类型(无向或有向)
  13. private $hasList; //尝试遍历时存储遍历过的结点
  14. private $queue; //广度优先遍历时存储孩子结点的队列,用数组模仿
  15. private $infinity = 65535;//代表无穷,即两点无连接,建带权值的图时用,本示例不带权值
  16. private $primVexs; //prim算法时保存顶点
  17. private $primArc; //prim算法时保存边
  18. public function MGraph($vexs, $arc, $direct = 0){
  19. $this->vexs = $vexs;
  20. $this->arcData = $arc;
  21. $this->direct = $direct;
  22. $this->initalizeArc();
  23. $this->createArc();
  24. }
  25. private function initalizeArc(){
  26. foreach($this->vexs as $value){
  27. foreach($this->vexs as $cValue){
  28. $this->arc[$value][$cValue] = ($value == $cValue ? 0 : $this->infinity);
  29. }
  30. }
  31. }
  32. //创建图 $direct:0表示无向图,1表示有向图
  33. private function createArc(){
  34. foreach($this->arcData as $key=>$value){
  35. $strArr = str_split($key);
  36. $first = $strArr[0];
  37. $last = $strArr[1];
  38. $this->arc[$first][$last] = $value;
  39. if(!$this->direct){
  40. $this->arc[$last][$first] = $value;
  41. }
  42. }
  43. }
  44. //prim算法,生成最小生成树
  45. public function prim(){
  46. $this->primVexs = array();
  47. $this->primArc = array($this->vexs[0]=>0);
  48. for($i = 1; $i count($this->vexs); $i ++){
  49. $this->primArc[$this->vexs[$i]] = $this->arc[$this->vexs[0]][$this->vexs[$i]];
  50. $this->primVexs[$this->vexs[$i]] = $this->vexs[0];
  51. }
  52. for($i = 0; $i count($this->vexs); $i ++){
  53. $min = $this->infinity;
  54. $key;
  55. foreach($this->vexs as $k=>$v){
  56. if($this->primArc[$v] != 0 && $this->primArc[$v] $min){
  57. $key = $v;
  58. $min = $this->primArc[$v];
  59. }
  60. }
  61. $this->primArc[$key] = 0;
  62. foreach($this->arc[$key] as $k=>$v){
  63. if($this->primArc[$k] != 0 && $v $this->primArc[$k]){
  64. $this->primArc[$k] = $v;
  65. $this->primVexs[$k] = $key;
  66. }
  67. }
  68. }
  69. return $this->primVexs;
  70. }
  71. //一般算法,生成最小生成树
  72. public function bst(){
  73. $this->primVexs = array($this->vexs[0]);
  74. $this->primArc = array();
  75. next($this->arc[key($this->arc)]);
  76. $key = NULL;
  77. $current = NULL;
  78. while(count($this->primVexs) count($this->vexs)){
  79. foreach($this->primVexs as $value){
  80. foreach($this->arc[$value] as $k=>$v){
  81. if(!in_array($k, $this->primVexs) && $v != 0 && $v != $this->infinity){
  82. if($key == NULL $v $current)){
  83. $key = $k;
  84. $current = array($value . $k=>$v);
  85. }
  86. }
  87. }
  88. }
  89. $this->primVexs[] = $key;
  90. $this->primArc[key($current)] = current($current);
  91. $key = NULL;
  92. $current = NULL;
  93. }
  94. return array('vexs'=>$this->primVexs, 'arc'=>$this->primArc);
  95. }
  96. //一般遍历
  97. public function reserve(){
  98. $this->hasList = array();
  99. foreach($this->arc as $key=>$value){
  100. if(!in_array($key, $this->hasList)){
  101. $this->hasList[] = $key;
  102. }
  103. foreach($value as $k=>$v){
  104. if($v == 1 && !in_array($k, $this->hasList)){
  105. $this->hasList[] = $k;
  106. }
  107. }
  108. }
  109. foreach($this->vexs as $v){
  110. if(!in_array($v, $this->hasList))
  111. $this->hasList[] = $v;
  112. }
  113. return implode($this->hasList);
  114. }
  115. //广度优先遍历
  116. public function bfs(){
  117. $this->hasList = array();
  118. $this->queue = array();
  119. foreach($this->arc as $key=>$value){
  120. if(!in_array($key, $this->hasList)){
  121. $this->hasList[] = $key;
  122. $this->queue[] = $value;
  123. while(!emptyempty($this->queue)){
  124. $child = array_shift($this->queue);
  125. foreach($child as $k=>$v){
  126. if($v == 1 && !in_array($k, $this->hasList)){
  127. $this->hasList[] = $k;
  128. $this->queue[] = $this->arc[$k];
  129. }
  130. }
  131. }
  132. }
  133. }
  134. return implode($this->hasList);
  135. }
  136. //执行深度优先遍历
  137. public function excuteDfs($key){
  138. $this->hasList[] = $key;
  139. foreach($this->arc[$key] as $k=>$v){
  140. if($v == 1 && !in_array($k, $this->hasList))
  141. $this->excuteDfs($k);
  142. }
  143. }
  144. //深度优先遍历
  145. public function dfs(){
  146. $this->hasList = array();
  147. foreach($this->vexs as $key){
  148. if(!in_array($key, $this->hasList))
  149. $this->excuteDfs($key);
  150. }
  151. return implode($this->hasList);
  152. }
  153. //返回图的二维数组表示
  154. public function getArc(){
  155. return $this->arc;
  156. }
  157. //返回结点个数
  158. public function getVexCount(){
  159. return count($this->vexs);
  160. }
  161. }
  162. ?>