免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2165 | 回复: 1
打印 上一主题 下一主题

PHP实现图的邻接矩阵 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-02 20:48 |只看该作者 |倒序浏览
PHP实现图的邻接矩阵





Php代码
  1. 1.<?php     
  2. 2.    //调用     
  3. 3.    require 'mGraph.php';     
  4. 4.    $a = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');     
  5. 5.    $b = array('ab', 'bc', 'be', 'cd', 'df', 'fg', 'gh', 'ga', 'hj', 'gi');     
  6. 6.     
  7. 7.    $test = new MGraph($a, $b);     
  8. 8.    print_r($test->bfs());     
  9. 9.?>   
  10.     <?php  
  11.         //调用  
  12.         require 'mGraph.php';  
  13.         $a = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');  
  14.         $b = array('ab', 'bc', 'be', 'cd', 'df', 'fg', 'gh', 'ga', 'hj', 'gi');  
  15.       
  16.         $test = new MGraph($a, $b);  
  17.         print_r($test->bfs());  
  18.     ?>  
复制代码
mGraph.php

Php代码
  1. 1.<?php   
  2. 2.    /**  
  3. 3.     * PHP 实现图邻接矩阵  
  4. 4.     *  
  5. 5.     * @author zhaojiangwei   
  6. 6.     * @since 2011/10/31 17:23  
  7. 7.     */  
  8. 8.  
  9. 9.    class MGraph{   
  10. 10.        private $vexs; //顶点数组   
  11. 11.        private $arc; //边邻接矩阵,即二维数组         
  12. 12.        private $arcData; //边的数组信息   
  13. 13.        private $direct; //图的类型(无向或有向)   
  14. 14.        private $hasList; //尝试遍历时存储遍历过的结点   
  15. 15.        private $queue; //广度优先遍历时存储孩子结点的队列,用数组模仿   
  16. 16.        //private $infinity = 65535;//代表无穷,即两点无连接,建带权值的图时用,本示例不带权值   
  17. 17.           
  18. 18.        public function MGraph($vexs, $arc, $direct = 0){   
  19. 19.            $this->vexs = $vexs;   
  20. 20.            $this->arcData = $arc;   
  21. 21.            $this->direct = $direct;   
  22. 22.            $this->initalizeArc();   
  23. 23.            $this->createArc();   
  24. 24.        }   
  25. 25.  
  26. 26.        private function initalizeArc(){   
  27. 27.            foreach($this->vexs as $value){   
  28. 28.                foreach($this->vexs as $cValue){   
  29. 29.                    $this->arc[$value][$cValue] = 0;   
  30. 30.                }   
  31. 31.            }   
  32. 32.        }   
  33. 33.  
  34. 34.        //创建图 $direct:0表示无向图,1表示有向图   
  35. 35.        private function createArc(){   
  36. 36.            foreach($this->arcData as $key=>$value){   
  37. 37.                $strArr = str_split($value);   
  38. 38.                $first = $strArr[0];   
  39. 39.                $last = $strArr[1];   
  40. 40.                    
  41. 41.                $this->arc[$first][$last] = 1;   
  42. 42.                if(!$this->direct){   
  43. 43.                    $this->arc[$last][$first] = 1;   
  44. 44.                }   
  45. 45.            }   
  46. 46.        }   
  47. 47.  
  48. 48.        //一般遍历   
  49. 49.        public function reserve(){   
  50. 50.            $this->hasList = array();   
  51. 51.  
  52. 52.            foreach($this->arc as $key=>$value){   
  53. 53.                if(!in_array($key, $this->hasList)){   
  54. 54.                    $this->hasList[] = $key;   
  55. 55.                }   
  56. 56.  
  57. 57.                foreach($value as $k=>$v){   
  58. 58.                    if($v == 1 && !in_array($k, $this->hasList)){   
  59. 59.                        $this->hasList[] = $k;      
  60. 60.                    }   
  61. 61.                }   
  62. 62.            }   
  63. 63.  
  64. 64.            foreach($this->vexs as $v){   
  65. 65.                if(!in_array($v, $this->hasList))   
  66. 66.                    $this->hasList[] = $v;   
  67. 67.            }   
  68. 68.  
  69. 69.            return implode($this->hasList);   
  70. 70.        }   
  71. 71.  
  72. 72.        //广度优先遍历   
  73. 73.        public function bfs(){   
  74. 74.            $this->hasList = array();   
  75. 75.            $this->queue = array();   
  76. 76.               
  77. 77.            foreach($this->arc as $key=>$value){   
  78. 78.                if(!in_array($key, $this->hasList)){   
  79. 79.                    $this->hasList[] = $key;   
  80. 80.                    $this->queue[] = $value;   
  81. 81.  
  82. 82.                    while(!emptyempty($this->queue)){   
  83. 83.                        $child = array_shift($this->queue);   
  84. 84.                           
  85. 85.                        foreach($child as $k=>$v){   
  86. 86.                            if($v == 1 && !in_array($k, $this->hasList)){   
  87. 87.                                $this->hasList[] = $k;   
  88. 88.                                $this->queue[] = $this->arc[$k];      
  89. 89.                            }   
  90. 90.                        }   
  91. 91.                    }   
  92. 92.                }   
  93. 93.            }   
  94. 94.  
  95. 95.            return implode($this->hasList);   
  96. 96.               
  97. 97.        }   
  98. 98.  
  99. 99.        //执行深度优先遍历   
  100. 100.        public function excuteDfs($key){   
  101. 101.            $this->hasList[] = $key;     
  102. 102.  
  103. 103.            foreach($this->arc[$key] as $k=>$v){   
  104. 104.                if($v == 1 && !in_array($k, $this->hasList))   
  105. 105.                    $this->excuteDfs($k);      
  106. 106.            }   
  107. 107.        }   
  108. 108.  
  109. 109.        //深度优先遍历   
  110. 110.        public function dfs(){   
  111. 111.            $this->hasList = array();   
  112. 112.  
  113. 113.            foreach($this->vexs as $key){   
  114. 114.                if(!in_array($key, $this->hasList))   
  115. 115.                    $this->excuteDfs($key);   
  116. 116.            }   
  117. 117.  
  118. 118.            return implode($this->hasList);   
  119. 119.        }   
  120. 120.  
  121. 121.        //返回图的二维数组表示   
  122. 122.        public function getArc(){   
  123. 123.            return $this->arc;   
  124. 124.        }   
  125. 125.  
  126. 126.        //返回结点个数   
  127. 127.        public function getVexCount(){   
  128. 128.            return count($this->vexs);   
  129. 129.        }   
  130. 130.    }   
  131. 131.  
  132. 132.?>  
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-11-04 23:36 |只看该作者
太辛苦了 楼主
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP