Anda di halaman 1dari 4

DEPTH FIRST SEARCH IN PROLOG

Satrio Anggoro Aji L


Informatics Engineering,
Pancasila University
4515210072@univpancasila.ac.id

Abstract— fastest path, one of them Dept-first Search.


Computers have become an important part Dept-first Search finds the path quickly but
for some people. Many people use not optimal because it directly searches for
computers for various things. The program the fastest path without looking for another
is one of the main parts of a computer. solution.
Browsing, gaming, studying, and much
more can be done by the computer, but
II. Theory
behind it all there is something important is
the program, without a computer program A. Prolog
will not be able to work. The programming Prolog is a general-purpose logic
language is the link between humans and programming language associated with
computers. Many things can be done with artificial intelligence and computational
the program, one of them is to find the path. linguistics. Prolog has its roots in first-order
In everyday life we have determined many logic, a formal logic, and unlike many other
options, for example, looking for path in the programming languages, Prolog is intended
journey. How can a program find a path? as primarily a declarative programming
This discussion will explain how depth first language: the program logic is expressed in
search works in program. terms of relations, represented as facts and
rules. A computation is initiated by running a
Keywords— search, prolog, path query over these relations.
I. Introduction B. Depth First Search

Today many people who understand Depth-first search (DFS) is an algorithm for
computer programming, even elementary traversing or searching tree or graph data
school children can learn it. The development structures. One starts at the root (selecting
of the program is very fast starting from some arbitrary node as the root in the case of
computers, notebooks, and smartphones. a graph) and explores as far as possible along
Many things from these developments in the each branch before backtracking. For
underlying language of the programming is applications of DFS in relation to specific
getting smarter in meeting human needs to domains, such as searching for solutions in
interact directly with these devices. In the artificial intelligence or web-crawling, the
smartphone there is a map application, the graph to be traversed is often either too large
map can give us the fastest way to achieve the to visit in its entirety or infinite (DFS may
goal. Then how the application can determine suffer from non-termination). In such cases,
the fastest path? Many methods to find the search is only performed to a limited depth;
due to limited resources, such as memory or analysis, with a branching factor greater than
disk space, one typically does not use data one, iterative deepening increases the
structures to keep track of the set of all running time by only a constant factor over
previously visited vertices. When an the case in which the correct depth limit is
appropriate depth limit is not known a priori, known due to the geometric growth of the
iterative deepening depth-first search applies number of nodes per level.
DFS repeatedly with a sequence of increasing
limits. In the artificial intelligence mode of
The following image shows how DFS works.
III.Algortihm of DFS
The DFS (Depth First Search) algorithm is
one of the algorithms used to find the path.
The example discussed this time is about
finding a path through all points.
This algorithm is similar to BFS (Breadth
First Search) algorithm. If the BFS (Breadth
First Search) algorithm performs a sequential
calculation from the first sequence to the last
sequence, then this algorithm does the
reverse, ie performing the ordered
calculations from the last sequence. Having
finished all the possibilities from the last
point, then retreat to the previous points up to
the first point. Depth first search begins by diving down as
quickly as possible to the leaf nodes of the
The DFS algorithm is a recursive algorithm tree (or graph). Traversal can be done by:
that uses the idea of backtracking. It involves
exhaustive searches of all the nodes by going  visiting the node first, then its
ahead, if possible, else by backtracking. children (pre-order traversal): a b d h
eijcfkg
Here, the word backtrack means that when  visiting the children first, then the
you are moving forward and there are no node (post-order traversal): h d i j e b
more nodes along the current path, you move kfgca
backwards on the same path to find nodes to
 visiting some of the children, then the
traverse. All the nodes will be visited on the
node, then the other children (in-order
current path till all the unvisited nodes have
traversal) h d b i e j a f k c g
been traversed after which the next path will
be selected.
IV. Simple Case
depthfirst(Path, Node,
[Node|Path]) :-
In this case there are 11 nodes with the goal(Node).
following illustrations. depthfirst(Path, Node, Sol)
:-
edge(Node, Node1),
not(member(Node1, Path)),
depthfirst([Node|Path],
Node1, Sol).

edge(bogor, depok).
edge(bogor, bekasi).
edge(depok, sukabumi).
edge(depok, jakarta).
edge(sukabumi, cianjur).
edge(jakarta, bandung).
edge(jakarta, semarang).
edge(bekasi, tangerang).
edge(bekasi, purwakarta).
start = bogor edge(tangerang, jogja).
goal = semarang edge(jogja, semarang).
goal(semarang).
With Depth-first search we will complete the
path. Logically, we will choose path bogor- from the source, it can be seen that the goal is
jakarta-semarang instead of bogor-bekasi- semarang goal(semarang).
tangerang-jogja-semarang because what we Then we execute with the command
are looking for is the fastest path. “solve(bogor, Soln).”
where a is the first state and Soln is the
V. Implementation with Prolog solution.

With the example of the case, we will try to


solve it in prologue. The rules used are still
the same that is looking for the fastest path
without looking for another solution.

search.pl
%Nama : Satrio Anggoro Aji
Leksono
%NPM : 4515210072
%Matkul : Pemrograman
Deklaratif (B) Soln = [semarang, jakarta, depok, bogor]
.How can the program find solutions?
solve(Node, Solution) :- Execute with “trace.” then “solve (a, Soln).”
depthfirst([], Node, then press enter until complete the task.
Solution).
VI. Conclusion
It can be concluded that Depth-first Search
has advantages and disadvantages.

Advantages :
• Memory usage only slightly, much
different from the BFS that must store
all nodes that have been raised.
• If the searched solution is at the deepest
and leftmost level, then DFS will find
it quickly.

Disadvantages :
• If the tree raised has a deep level
(infinity), then there is no guarantee to
find a solution (Not Complete).
• If there is more than one solution in
common but at different levels, then in
DFS there is no guarantee to find the
best solution (Not Optimal).

Source

[1] http://www.cse.unsw.edu.au/~billw/Justs
earch.html
[2] -, “Laporan Akhir Perancangan dan
Analisis Algoritma”, Gunadarma
University, 2014
[3] -, “ALGORITMA DEPTH FIRST
SEARCH”, University Negri Malang,
2017
[4] Jon Freeman, “Parallel Algorithms for
Depth-First Search”, University of
Pennsylvania, 1991

Anda mungkin juga menyukai