2
2
3
3
namespace Graphp \Algorithms \Search ;
4
4
5
- use Fhaculty \Graph \Vertex ;
6
5
use Fhaculty \Graph \Set \Vertices ;
7
6
8
7
class DepthFirst extends Base
9
8
{
10
9
/**
10
+ * calculates an iterative depth-first search
11
11
*
12
- * calculates the recursive algorithm
13
- *
14
- * fills $this->visitedVertices
15
- *
16
- * @param Vertex $vertex
12
+ * @return Vertices
17
13
*/
18
- private function recursiveDepthFirstSearch (Vertex $ vertex , array & $ visitedVertices )
19
- {
20
- // If I didn't visited this vertex before
21
- if (!isset ($ visitedVertices [$ vertex ->getId ()])) {
22
- // Add Vertex to already visited vertices
23
- $ visitedVertices [$ vertex ->getId ()] = $ vertex ;
24
-
25
- // Get next vertices
26
- $ nextVertices = $ vertex ->getVerticesEdgeTo ();
27
-
28
- foreach ($ nextVertices as $ nextVertix ) {
29
- // recursive call for next vertices
30
- $ this ->recursiveDepthFirstSearch ($ nextVertix , $ visitedVertices );
31
- }
32
- }
33
- }
34
-
35
- private function iterativeDepthFirstSearch (Vertex $ vertex )
14
+ public function getVertices ()
36
15
{
37
16
$ visited = array ();
38
- $ todo = array ($ vertex );
17
+ $ todo = array ($ this -> vertex );
39
18
while ($ vertex = array_shift ($ todo )) {
40
19
if (!isset ($ visited [$ vertex ->getId ()])) {
41
20
$ visited [$ vertex ->getId ()] = $ vertex ;
@@ -48,19 +27,4 @@ private function iterativeDepthFirstSearch(Vertex $vertex)
48
27
49
28
return new Vertices ($ visited );
50
29
}
51
-
52
- /**
53
- * calculates a recursive depth-first search
54
- *
55
- * @return Vertices
56
- */
57
- public function getVertices ()
58
- {
59
- return $ this ->iterativeDepthFirstSearch ($ this ->vertex );
60
-
61
- $ visitedVertices = array ();
62
- $ this ->recursiveDepthFirstSearch ($ this ->vertex , $ visitedVertices );
63
-
64
- return $ visitedVertices ;
65
- }
66
30
}
0 commit comments