Skip to content

Commit 409124c

Browse files
committed
2016/03/03 add adjMatrix
1 parent 0c887ad commit 409124c

File tree

6 files changed

+49
-20
lines changed

6 files changed

+49
-20
lines changed

.test.php.swp

0 Bytes
Binary file not shown.

Graphic/.GraALG.php.swo

12 KB
Binary file not shown.

Graphic/.GraALG.php.swp

12 KB
Binary file not shown.

Graphic/GraALG.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
namespace Graphic;
44

55
class GraALG {
6+
const INFINITY = 65535;
7+
const CONNECT = 1;
68
public $verex;
79
public $edges;
810
public $graphic;
9-
11+
//dijkstr用来记录最短路径和前置节点
12+
public $dijkstra;
13+
public $adjMatrix;
1014
public function __construct($verex, $edges, $graphic) {
1115
$this->verex = $verex;
1216
$this->edges = $edges;
@@ -58,6 +62,24 @@ public function BFSTraverse() {
5862
}
5963
}
6064

65+
//邻接表转换成邻接矩阵
66+
public function toAdjMatrix() {
67+
$this->adjMatrix = array();
68+
foreach ($this->verex as $vex_i) {
69+
$arr = array();
70+
foreach ($this->verex as $vex_j) {
71+
$edge_key = $vex_i->name . $vex_j->name;
72+
if (!empty($this->edges[$edge_key])) {
73+
$arr [] = $this->edges[$edge_key]->weight;
74+
} else {
75+
$arr [] = self::INFINITY;
76+
}
77+
}
78+
$this->adjMatrix [] = $arr;
79+
}
80+
}
6181
//最短路径的问题
62-
82+
public function shortestPathDijkstrai() {
83+
84+
}
6385
}

index.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
4+
<head>
5+
<body>
6+
<div><a href='../test.php' />hello</div>
7+
</body>
8+
</html>

test.php

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,26 @@
88
use Graphic\Edge;
99
use Graphic\GraALG;
1010

11-
$begin_int = ord('A');
12-
$verex = array();
13-
//定义结点,初始化A-E的节点
1411
for ($i = 0; $i < 9; $i++) {
15-
$letter = chr($begin_int + $i);
16-
$verex [$letter] = new Vertex($letter);
12+
$verex [$i] = new Vertex($i);
1713
}
1814
//定义边
19-
$edge_arr = array('AB', 'AF',
20-
'BA', 'BC', 'BG', 'BI',
21-
'CB', 'CD', 'CI',
22-
'DC', 'DE', 'DG', 'DH', 'DI',
23-
'ED', 'EF', 'EH',
24-
'FA', 'FE', 'FG',
25-
'GB', 'GD', 'GF', 'GH',
26-
'HD', 'HE', 'HG',
27-
'IB', 'IC', 'ID'
15+
$edge_arr = array('011', '025',
16+
'101', '123', '137', '145',
17+
'205', '213', '241', '257',
18+
'317', '342', '363',
19+
'415', '421', '432', '453', '466', '479',
20+
'527', '543', '575',
21+
'633', '646', '672', '687',
22+
'749', '755', '762', '784',
23+
'867', '874'
2824
);
2925
$edges = array();
3026
foreach ($edge_arr as $edge) {
31-
$tail = substr($edge, 0, 1);
32-
$head = substr($edge, 1, 1);
33-
$edges[$edge] = new Edge($head, $tail);
27+
$tail = substr($edge, 0, 1);
28+
$head = substr($edge, 1, 1);
29+
$weight = intval(substr($edge, 2, 1));
30+
$edges[$tail.$head] = new Edge($head, $tail, $weight);
3431
}
3532
//定义图
3633
$graphic_arr = array();
@@ -45,4 +42,6 @@
4542
}
4643
$graphic = new GraALG($verex, $edges, $graphic_arr);
4744
#$graphic->DFSTraverse();
48-
$graphic->BFSTraverse();
45+
#$graphic->BFSTraverse();
46+
$graphic->toAdjMatrix();
47+
print_r($graphic->adjMatrix);

0 commit comments

Comments
 (0)