a person wants to visit some places. he starts from a vertex and then wants to visit every vertex till it finishes from one vertex, backtracks and then explore other vertex from same vertex. what algorithm he should use?
The described traversal matches the Depth First Search (DFS) algorithm.
đź§ Quick Scoop
“A person wants to visit some places. He starts from a vertex and then wants to visit every vertex till it finishes from one vertex, backtracks and then explore other vertex from same vertex. What algorithm he should use?”
This is a classic graph traversal description used in many exam-style questions, and the standard answer is Depth First Search (DFS).
Why DFS is the Right Answer
DFS works exactly like what your story describes: go as deep as possible along one path, then backtrack and try another.
- You start from a vertex (the starting place).
- You visit an unvisited adjacent vertex and keep going deeper.
- When you reach a vertex with no unvisited neighbors , you backtrack to the previous vertex.
- From there, you explore another unvisited adjacent vertex and repeat.
- This continues until all reachable vertices have been visited.
That “go deep, then backtrack, then explore other options” behavior is the signature of DFS.
Mini Story Version
Imagine a traveler exploring a cave system:
- He enters the cave at the main entrance (start vertex).
- At each junction, he always picks one unexplored tunnel and follows it all the way until it ends.
- When he hits a dead end, he walks back to the last junction that still has an unexplored tunnel.
- He repeats this until every tunnel connected to the cave system has been explored.
This is exactly how DFS conceptually behaves on a graph.
DFS vs BFS (So You Don’t Confuse Them)
Many multiple-choice questions pair this with Breadth First Search (BFS), so it helps to contrast them.
| Feature | Depth First Search (DFS) | Breadth First Search (BFS) |
|---|---|---|
| Traversal style | Goes deep along one path, then backtracks. | [3][9]Visits neighbors level by level. | [9]
| Backtracking | Explicitly backtracks when no unvisited neighbor remains. | [3][9]Uses a queue; doesn’t “backtrack” in the same sense. | [9]
| Matches the question text? | Yes – “visit every vertex, then backtrack and explore other vertex.” | [10][4][7]No – BFS is about exploring all neighbors before going deeper. | [9]
| Typical options in MCQs | DFS is usually the correct one for this wording. | [8][2][4][7][10]Often included as a distractor. | [2][7][8][10]
How You’d Phrase the Answer in an Exam
If this is for a test or assignment, a clean answer would be:
Answer: Depth First Search (DFS).
Because the person visits vertices by going as deep as possible from the current vertex, then backtracks to explore other unvisited vertices from the same starting point, which is exactly how DFS traverses a graph.
Bottom note: Information gathered from public forums or data available on the internet and portrayed here.