A traversal method that prioritizes going as far as possible and then backtracks when no further path is available.

Visualisation

Efficiency

  • Time complexity: where is the number of nodes and is the number of edges. This is because every vertex and every edge would be visited in the worst case scenario.
  • Space complexity: Maximum number of vertices in output list, and maximum number of vertices in visited list is . Hence using space complexity.

Implementation