@@ -11,20 +11,22 @@ class GraALG {
11
11
public $ edges ;
12
12
//邻接表
13
13
public $ graphic ;
14
-
15
- //dijkstr用来记录最短路径和前置节点
16
- public $ dijkstra ;
17
14
//邻接矩阵表示
18
15
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
+ {
21
22
$ this ->verex = $ verex ;
22
23
$ this ->edges = $ edges ;
23
24
$ this ->graphic = $ graphic ;
24
25
}
25
26
26
27
//深度优先
27
- public function DFS ($ tail ) {
28
+ public function DFS ($ tail )
29
+ {
28
30
//do sth, not only echo
29
31
echo $ tail ;
30
32
foreach ($ this ->graphic [$ tail ] as $ heads ) {
@@ -37,7 +39,8 @@ public function DFS($tail) {
37
39
}
38
40
39
41
//深度优先,depth_first_search
40
- public function DFSTraverse () {
42
+ public function DFSTraverse ()
43
+ {
41
44
foreach ($ this ->verex as $ ver ) {
42
45
if ($ ver ->visited == 0 ) {
43
46
$ this ->DFS ($ ver ->name );
@@ -47,7 +50,8 @@ public function DFSTraverse() {
47
50
48
51
//广度优先,breadth_first_searth
49
52
//array_pop取出最后一个值,array_shift取出第一个值,array_push
50
- public function BFSTraverse () {
53
+ public function BFSTraverse ()
54
+ {
51
55
$ queue = array ();
52
56
$ queue [] = $ this ->verex ['A ' ]->name ;
53
57
$ this ->verex [$ vex ]->visited = 1 ;
@@ -69,7 +73,8 @@ public function BFSTraverse() {
69
73
}
70
74
71
75
//邻接表转换成邻接矩阵
72
- public function toAdjMatrix () {
76
+ public function toAdjMatrix ()
77
+ {
73
78
$ this ->adjMatrix = array ();
74
79
foreach ($ this ->verex as $ vex_i ) {
75
80
$ arr = array ();
@@ -85,21 +90,45 @@ public function toAdjMatrix() {
85
90
}
86
91
}
87
92
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
+ }
102
112
}
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
+ }
104
133
}
105
134
}
0 commit comments