The Computer Science solves a lot of daily problems in our lifes, one of them is search problems. These problems sometimes are so hard to find a good solution because is necessary study hard to comprehend the problem, modeling it and after this propose a solution. In this homework, my goal is define and explain the differ- ences between the algorithms DFS - Depth-First Search and Backtrancking. Firstly, I will introduce these algorithms in the section 2 and 3 to DFS and Backtracking respectively. In the section 4 I will show the differences between them. Finally, the conclusion in the section 5.
1. Eotvos Lorand University - ELTE
Faculty of Informatics
Artificial Intelligence
5th Homework
Johnnatan Messias P. Afonso
Professor - Tibor Gregorics
Budapest - Hungary
March 26, 2014
3. 1 Introduction
The Computer Science solves a lot of daily problems in our lifes, one of them
is search problems. These problems sometimes are so hard to find a good solution
because is necessary study hard to comprehend the problem, modeling it and after
this propose a solution. In this homework, my goal is define and explain the differ-
ences between the algorithms DFS - Depth-First Search and Backtrancking. Firstly,
I will introduce these algorithms in the section 2 and 3 to DFS and Backtracking
respectively. In the section 4 I will show the differences between them. Finally, the
conclusion in the section 5.
2 DFS - Depth-First Search
The DFS is an algorithm for searching in a tree structure or graph. It explores
so far as possible before start the traversing with the other node. Unfortunately the
DFS algorithm is Brute-Force.
This algorithm uses the following strategy:
• The edges are explored from the v vertice most recently discovered that has
edges to explorer from it.
• When all the edges are explored so the algorithm return and go to the next
vertice dicovered.
• If there is no edges to the next vertice but if we have a vertice not explored so
the algorithm will run for it too.
• The algorithm will run until all the vertices are explored.
We can see the pseudo-code in the Program 1.
procedure DFS−i t e r a t i v e (G, v) :
l e t S be a stack
S . push (v)
while S i s not empty
5 v = S . pop ()
i f v i s not labeled as discovered :
label v as discovered
for a l l edges from v to w in G. adjacentEdges (v) do
S . push (w)
Program 1: DFS Pseudo-Code
3 Backtracking
The Backtracking, like explained in 4 is a modified DFS of a tree consisting
of all of the possible sequences that can be constructed from the problem set. In
Backtracking we perform a DFS traversal of this tree until we reach a node that
is non-viable or non-promising, at which point we prune the subtree rooted at this
node, and continue the DFS traversal of the tree.
This algorithm uses the following strategy:
1
4. • Similar to DFS
• The Solution is a vector, each variable of a finite field
• At each step, defines the next variable to explore and values.
• Disconsider infeasible partial solutions
We can see the pseudo-code in the Program 2.
procedure bt ( c )
i f r e j e c t (P, c ) then return
i f accept (P, c ) then output (P, c )
s = f i r s t (P, c )
5 while s <> null do
bt ( s )
s = next (P, s )
Program 2: Backtracking Pseudo-Code
4 The differences
In the sections 2 and 3 we understood how the DFS and Backtracking algorithms
work. We saw how Backtracking is better than DFS, but what are the differences
between them? The reason that Backtracking is better than DFS is due to the first
one disconsider infeasible partial solutions therefore will be executed and return a
solution very faster than the second one.
Example by 5: The search space of a sorted list <5, 2, 4> would include all
3! permutations of solutions, as in the one at left below. The one at right shows
only those necessary to be examined to determine which permutation is the sorted
(descending) solution, <5, 4, 2.
In the Figure 1 we can see an example about the explored tree with these algo-
rithms.
5 Conclusion
In Computer Science we have a lot of solutions for the same problem, a lot
of algorithms to solve them. Although the Backtracking is better than DFS your
worst case is the same of the DFS therefore we always need to check the input of
the problem. I mean the Backtracking is better for which input? This information
is important because we need to check our problem before try to find a solution for
it.
6 References
1. Algoritimos - Teoria e Pratica, 2nd edition, Thomas H.Cormen
2. http://en.wikipedia.org/wiki/Depth-first_search
3. https://www.princeton.edu/~achaney/tmve/wiki100k/docs/Depth-first_
search.html
2
5. (a) DFS (b) Backtracking
Figure 1: Difference between DFS and Backtracking
4. http://www.academic.marist.edu/~jzbv/algorithms/Backtracking.htm
5. http://homepages.ius.edu/RWISMAN/C455/html/notes/Backtracking/Backtracking.
htm
3