0% found this document useful (0 votes)
52 views

Document

Gjjbb

Uploaded by

Deon Sequeira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Document

Gjjbb

Uploaded by

Deon Sequeira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Uninformed and informed search strategies.

Searching is a process of finding a sequence of steps


needed to solve any problem. The prior difference
between informed and uninformed search is that the
informed search provides the guidance on where and how
to find the solution. Conversely, the uninformed search
gives no additional information about the problem except
its specification.

However, between both informed and uninformed search


techniques, the informed search is more efficient and
cost-effective.

1.
Comparison Chart

BASIS FOR INFORMED UNINFORMED

COMPARISON SEARCH SEARCH

Basic Uses knowledge No use of knowledge

to find the steps

to the solution.

Efficiency Highly efficient as Efficiency is

consumes less mediatory

time and cost.

Cost Low Comparatively high

Performance Finds solution Speed is slower than

more quickly informed search

Algorithms Heuristic depth Depth-first search,

first and breadth- breadth-first search

first search, and and lowest cost first

A* search search
Definition of Informed search

The informed search technique utilizes the problem specific


knowledge in order to give a clue to the solution of the problem.
This type of search strategy actually prevents the algorithms from
stumbling about the goal and the direction to the solution. Informed
search can be advantageous in terms of the cost where the
optimality is achieved at lower search costs.

To search an optimal path cost in a graph by implementing informed


search strategy the most promising nodes n are inserted to the
heuristic function h(n). Then the function returns a non-negative
real number which is an approximate path cost calculated from node
n to the target node.

Here the most important part of the informed technique is the


heuristic function which facilitates in imparting the additional
knowledge of the problem to the algorithm. As a result, it helps in
finding the way to the goal through the various neighbouring nodes.
There are various algorithms based on the informed search such as
heuristic depth-first search, heuristic breadth-first search, A*search,
etcetera. Let’s now understand heuristic depth-first search.

Definition of Uninformed search

The uninformed search is different from informed search in the way


that it just provides the problem definition but no further step to
finding the solution to the problem. The primary objective of
uninformed search is to differentiate between the target and non-
target state, and it totally ignores the destination it is heading
towards in the path until it discovers the goal and reports successor.
This strategy is also known as a blind search.

There are various search algorithms under this category such as


depth-first search, uniform cost search, breadth-first search, and so
on. Let us now understand the concept behind the uninformed
search with the help of depth-first search.
Breadth first search

Traversal means visiting all the nodes of a graph. Breadth first traversal or Breadth first Search
is a recursive algorithm for searching all the vertices of a graph or tree data structure. In this
article, you will learn with the help of examples the BFS algorithm, BFS pseudocode and the
code of the breadth first search algorithm with implementation in C++, C, Java and Python
programs.

BFS algorithm
A standard DFS implementation puts each vertex of the graph into one of two categories:

1. Visited
2. Not Visited

The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.

The algorithm works as follows:

1. Start by putting any one of the graph's vertices at the back of a queue.
2. Take the front item of the queue and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list
to the back of the queue.
4. Keep repeating steps 2 and 3 until the queue is empty.

The graph might have two different disconnected parts so to make sure that we cover every
vertex, we can also run the BFS algorithm on every node

BFS example
Let's see how the Breadth First Search algorithm works with an example. We use an undirected
graph with 5 vertices.
We start from vertex 0, the BFS algorithm starts by putting it in the Visited list and putting all its
adjacent vertices in the stack.

Next, we visit the element at the front of queue i.e. 1 and go to its adjacent nodes. Since 0 has
already been visited, we visit 2 instead.

4, so we add that to the back of the queue and visit 3, which is at the front of the queue.
Only 4 remains in the queue since the only adjacent node of 3 i.e. 0 is already visited. We visit
it.

Since the queue is empty, we have completed the Depth First Traversal of the graph.

BFS pseudocode
create a queue Q

mark v as visited and put v into Q

while Q is non-empty

remove the head u of Q


mark and enqueue all (unvisited) neighbours of u

BFS code
The code for the Breadth First Search Algorithm with an example is shown below. The code has
been simplified so that we can focus on the algorithm rather than other details.

BFS in Python

5. import collections
6.
7. def bfs(graph, root):
8. visited, queue = set(), collections.deque([root])
9. visited.add(root)
10. while queue:
11. vertex = queue.popleft()
12. for neighbour in graph[vertex]:
13. if neighbour not in visited:
14. visited.add(neighbour)
15. queue.append(neighbour)
16.
17.
18. if __name__ == '__main__':
19. graph = {0: [1, 2], 1: [2], 2: [3], 3: [1,2]}
20. breadth_first_search(graph, 0)

DFS algorithm
Traversal means visiting all the nodes of a graph. Depth first traversal or Depth first Search is a
recursive algorithm for searching all the vertices of a graph or tree data structure. In this article,
you will learn with the help of examples the DFS algorithm, DFS pseudocode and the code of
the depth first search algorithm with implementation in C++, C, Java and Python programs.

DFS algorithm
A standard DFS implementation puts each vertex of the graph into one of two categories:

21. Visited
22. Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.

The DFS algorithm works as follows:

23. Start by putting any one of the graph's vertices on top of a stack.
24. Take the top item of the stack and add it to the visited list.
25. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list
to the top of stack.
26. Keep repeating steps 2 and 3 until the stack is empty.

DFS example
Let's see how the Depth First Search algorithm works with an example. We use an undirected
graph with 5 vertices.

We start from vertex 0, the DFS algorithm starts by putting it in the Visited list and putting all its
adjacent vertices in the stack.
Next, we visit the element at the top of stack i.e. 1 and go to its adjacent nodes. Since 0 has
already been visited, we visit 2 instead.

Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the top of the stack and visit it.

After we visit the last element 3, it doesn't have any unvisited adjacent nodes, so we have
completed the Depth First Traversal of the graph.
DFS code
The code for the Depth First Search Algorithm with an example is shown below. The code has
been simplified so that we can focus on the algorithm rather than other details.

DFS in Python
27. def dfs(graph, start, visited=None):
28. if visited is None:
29. visited = set()
30. visited.add(start)
31. print(start)
32. for next in graph[start] - visited:
33. dfs(graph, next, visited)
34. return visited
35.
36. graph = {'0': set(['1', '2']),
37. '1': set(['0', '3', '4']),
38. '2': set(['0']),
39. '3': set(['1']),
40. '4': set(['2', '3'])}
41.
42. dfs(graph, '0')

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy