Skip to content

Commit f29db03

Browse files
committed
Dijkstra is OK
1 parent 8177c5c commit f29db03

File tree

5 files changed

+57
-25
lines changed

5 files changed

+57
-25
lines changed

.test.php.swp

0 Bytes
Binary file not shown.

Graphic/.GraALG.php.swp

12 KB
Binary file not shown.

Graphic/GraALG.php

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,22 @@ class GraALG {
1111
public $edges;
1212
//邻接表
1313
public $graphic;
14-
15-
//dijkstr用来记录最短路径和前置节点
16-
public $dijkstra;
1714
//邻接矩阵表示
1815
public $adjMatrix;
19-
20-
public function __construct($verex, $edges, $graphic) {
16+
//Floyd最短路径算法
17+
public $depth;
18+
public $path;
19+
20+
public function __construct($verex, $edges, $graphic)
21+
{
2122
$this->verex = $verex;
2223
$this->edges = $edges;
2324
$this->graphic = $graphic;
2425
}
2526

2627
//深度优先
27-
public function DFS($tail) {
28+
public function DFS($tail)
29+
{
2830
//do sth, not only echo
2931
echo $tail;
3032
foreach ($this->graphic[$tail] as $heads) {
@@ -37,7 +39,8 @@ public function DFS($tail) {
3739
}
3840

3941
//深度优先,depth_first_search
40-
public function DFSTraverse() {
42+
public function DFSTraverse()
43+
{
4144
foreach ($this->verex as $ver) {
4245
if ($ver->visited == 0) {
4346
$this->DFS($ver->name);
@@ -47,7 +50,8 @@ public function DFSTraverse() {
4750

4851
//广度优先,breadth_first_searth
4952
//array_pop取出最后一个值,array_shift取出第一个值,array_push
50-
public function BFSTraverse() {
53+
public function BFSTraverse()
54+
{
5155
$queue = array();
5256
$queue [] = $this->verex['A']->name;
5357
$this->verex[$vex]->visited = 1;
@@ -69,7 +73,8 @@ public function BFSTraverse() {
6973
}
7074

7175
//邻接表转换成邻接矩阵
72-
public function toAdjMatrix() {
76+
public function toAdjMatrix()
77+
{
7378
$this->adjMatrix = array();
7479
foreach ($this->verex as $vex_i) {
7580
$arr = array();
@@ -85,21 +90,45 @@ public function toAdjMatrix() {
8590
}
8691
}
8792

88-
//最短路径的问题,$dijkstra,邻接矩阵表示$adjMatrix;
89-
public function shortestPathDijkstrai() {
90-
$pre = 0;
91-
$shortest = 1;
92-
$this->dijkstra = array();
93-
//第一个数值表示本节点的前置节点为0,
94-
//第二个数值表示本节点到源点的最短距离
95-
$row = array(0, 0);
96-
$this->dijkstra [0] = $row;
97-
$this->verex[0]->visited = 1;
98-
99-
foreach ($this->verex as $vex) {
100-
if ($vex->visited == 0) {
101-
93+
//最短路径问题,Dijkstra算法,邻接矩阵表示$adjMatrix;
94+
public function shortestPathDijkstra($source)
95+
{
96+
foreach($this->verex as $vex) {
97+
$vex->visited = 0;
98+
$vex->pre = 0;
99+
$vex->shortest = $this->adjMatrix[$source][$vex->name];
100+
}
101+
$this->verex[$source]->visited = 1;
102+
$this->verex[$source]->shortest = 0;
103+
foreach ($this->verex as $verer) {
104+
$min = self::INFINITY;
105+
$key = 0;
106+
foreach ($this->verex as $vex) {
107+
if ($vex->visited == 0 && $vex->shortest < $min) {
108+
$min = $vex->shortest;
109+
$key = $vex->name;
110+
echo $min.'--'.$key, PHP_EOL;
111+
}
102112
}
103-
}
113+
echo $key,PHP_EOL;
114+
$this->verex[$key]->visited = 1;
115+
foreach ($this->verex as $vex) {
116+
if ($vex->visited == 0 &&
117+
$vex->shortest > $min + $this->adjMatrix[$key][$vex->name]) {
118+
$vex->pre = $key;
119+
$vex->shortest = $min + $this->adjMatrix[$key][$vex->name];
120+
}
121+
}
122+
}
123+
}
124+
125+
public function shortestFloyd()
126+
{
127+
//$this->depth;
128+
//$this->path;
129+
$length = count($this->verex);
130+
for ($i=0; $i < $length; $i++) {
131+
132+
}
104133
}
105134
}

Graphic/Vertex.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class Vertex {
66
public $name;
77
public $data;
88
public $visited;
9+
public $pre;
10+
public $shortest;
911

1012
public function __construct($name, $data = 0, $visited = 0) {
1113
$this->name = $name;

test.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@
4444
#$graphic->DFSTraverse();
4545
#$graphic->BFSTraverse();
4646
$graphic->toAdjMatrix();
47-
print_r($graphic->adjMatrix);
47+
$graphic->shortestPathDijkstra(0);
48+
print_r($graphic->verex);

0 commit comments

Comments
 (0)