22
33namespace Graphp \Algorithms \Search ;
44
5- use Fhaculty \Graph \Vertex ;
65use Fhaculty \Graph \Set \Vertices ;
76
87class DepthFirst extends Base
98{
109 /**
10+ * calculates an iterative depth-first search
1111 *
12- * calculates the recursive algorithm
13- *
14- * fills $this->visitedVertices
15- *
16- * @param Vertex $vertex
12+ * @return Vertices
1713 */
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 ()
3615 {
3716 $ visited = array ();
38- $ todo = array ($ vertex );
17+ $ todo = array ($ this -> vertex );
3918 while ($ vertex = array_shift ($ todo )) {
4019 if (!isset ($ visited [$ vertex ->getId ()])) {
4120 $ visited [$ vertex ->getId ()] = $ vertex ;
@@ -48,19 +27,4 @@ private function iterativeDepthFirstSearch(Vertex $vertex)
4827
4928 return new Vertices ($ visited );
5029 }
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- }
6630}
0 commit comments