Ai Unit 2 Notes
Ai Unit 2 Notes
1. In the various levels of the data, you can mark any node as the
starting or initial node to begin traversing. The BFS will visit the
node and mark it as visited and places it in the queue.
2. Now the BFS will visit the nearest and un-visited nodes and
marks them. These values are also added to the queue. The
queue works on the FIFO model.
3. In a similar manner, the remaining nearest and un-visited nodes
on the graph are analyzed marked and added to the queue.
These items are deleted from the queue as receive and printed
as the result.
Use Of BFS Algorithm
There are numerous reasons to utilize the BFS Algorithm to use as
searching for your dataset. Some of the most vital aspects that make
this algorithm your first choice are:
• BFS is useful for analyzing the nodes in a graph and constructing
the shortest path of traversing through these.
• BFS can traverse through a graph in the smallest number of
iterations.
• The architecture of the BFS algorithm is simple and robust.
• The result of the BFS algorithm holds a high level of accuracy in
comparison to other algorithms.
• BFS iterations are seamless, and there is no possibility of this
algorithm getting caught up in an infinite loop problem.
Rules of BFS Algorithm
Here, are important rules for using BFS algorithm:
• A queue (FIFO-First in First Out) data structure is used by BFS.
• You mark any node in the graph as root and start traversing the
data from it.
• BFS traverses all the nodes in the graph and keeps dropping
them as completed.
• BFS visits an adjacent unvisited node, marks it as done, and
inserts it into a queue.
• Removes the previous vertex from the queue in case no
adjacent vertex is found.
• BFS algorithm iterates until all the vertices in the graph are
successfully traversed and marked as completed.
• There are no loops caused by BFS during the traversing of data
from any node.
Applications of BFS Algorithm
Let’s take a look at some of the real-life applications where a BFS
algorithm implementation can be highly effective.
• Un-weighted Graphs: BFS algorithm can easily create the
shortest path and a minimum spanning tree to visit all the
vertices of the graph in the shortest time possible with high
accuracy.
• P2P Networks: BFS can be implemented to locate all the
nearest or neighboring nodes in a peer to peer network. This
will find the required data faster.
• Web Crawlers: Search engines or web crawlers can easily build
multiple levels of indexes by employing BFS. BFS
implementation starts from the source, which is the web page,
and then it visits all the links from that source.
• Navigation Systems: BFS can help find all the neighboring
locations from the main or source location.
• Network Broadcasting: A broadcasted packet is guided by the
BFS algorithm to find and reach all the nodes it has the address
for.
Applications of DFS
DFS is a versatile algorithm that can be used to solve a variety of
problems in AI, including:
1. Finding the Shortest Path between Two Nodes in a Graph: DFS
can help find the shortest path between two nodes in a graph.
It explores all possible paths and remembers the shortest to
find. This is helpful for problems like finding the quickest way
between two places on a map or finishing a set of tasks faster.
2. Finding all of the possible Solutions to a Problem: DFS can find
all possible solutions by exploring all branches of the problem
space. This is useful for problems such as finding all of the
possible ways to arrange a set of objects or finding all of the
possible ways to win a game.
3. Detecting Cycles in a Graph: DFS can be used to detect cycles in
a graph by keeping track of the nodes that have already been
visited. If DFS ever visits a node that has already been visited,
then there is a cycle in the graph. This is helpful for solving
issues like finding loops in a program or detecting deadlocks in
a computer system.
4. Topological sorting of a Graph: DFS can be used to topologically
sort a graph by exploring the graph in reverse order. You can
use this for tasks scheduled in order or finding the order to
compile modules.
5. Finding all of the strongly Connected Components in a
Graph: DFS can be used to find all of the strongly connected
components in a graph by exploring the graph twice. This is
helpful for problems like finding communities in a social
network or connected circuit parts.
Heuristic Function
A heuristic is a function that determines how near a state is to the
desired state. Heuristics functions vary depending on the problem
and must be tailored to match that particular challenge. The majority
of AI problems revolve around a large amount of information, data,
and constraints, and the task is to find a way to reach the goal state.
The heuristics function in this situation informs us of the proximity to
the desired condition. The distance formula is an excellent option if
one needed a heuristic function to assess how close a location in a
two-dimensional space was to the objective point.
Properties of a Heuristic Search Algorithm
Heuristic search algorithms have the following properties:
• Admissible Condition: If an algorithm produces an optimal
result, it is considered admissible.
• Completeness: If an algorithm ends with a solution, it is
considered complete.
• Dominance Property: If A1 and A2 are two heuristic algorithms
and have h1 and h2 heuristic functions, respectively, then A1
Will dominate A2 if h1 is superior to h2 for all possible values of
node n.
• Optimality Property: If an algorithm is thorough, allowable, and
dominates the other algorithms, that'll be the optimal one and
will unquestionably produce an optimal result.
Different Categories of Heuristic Search Techniques in AI
We can categorize the Heuristic Search techniques into two types:
Direct Heuristic Search Techniques
Direct heuristic search techniques may also be called blind control
strategy, blind search, and uninformed search.
They utilize an arbitrary sequencing of operations and look for a
solution throughout the entire state space. These include Depth First
Search (DFS) and Breadth First Search (BFS).
BFS is a heuristic search method to diagram data or quickly scan
intersection or tree structures. DFS is predicated on the likelihood of
last in, first out. Similarly, the LIFO stack data structure is used to
complete the process in recursion.
Weak Heuristic Techniques
Weak heuristic techniques are known as a Heuristic control strategy,
informed search, and Heuristic search. These are successful when
used effectively on the appropriate tasks and typically require
domain-specific knowledge.To explore and expand, users require
additional information to compute preferences across child nodes. A
heuristic function is connected to each node. Let's first look at some
of the strategies we frequently see before detailing specific ones.
Here are a few examples.
• A* Search
• Best-first search
• Tabu search
• Bidirectional search
• Constant satisfaction problems
• Hill climbing
Examples of Heuristic Functions in AI
What is the quickest path between each city and its starting point,
given a list of cities and the distances between each pair of them?
This problem could be brute-forced for a small number of cities. But
as the number of cities grows, finding a solution becomes more
challenging.
This issue is well-solved by the nearest-neighbor heuristic, which
directs the computer to always choose the closest unexplored city as
the next stop on the path. While NN only sometimes offers the
optimum solution, it is frequently near enough that the variation is
insignificant to respond to the salesman's problem. This approach
decreases TSP's complexity from O(n!) to O (n^2).
Search Engine
AND-OR Graph
In the above figure, the buying of a car may be broken down into
smaller problems or tasks that can be accomplished to achieve the
main goal in the above figure, which is an example of a simple AND-
OR graph. The other task is to either steal a car that will help us
accomplish the main goal or use your own money to purchase a car
that will accomplish the main goal. The AND symbol is used to
indicate the AND part of the graphs, which refers to the need that all
subproblems containing the AND to be resolved before the preceding
node or issue may be finished.
The start state and the target state are already known in the
knowledge-based search strategy known as the AO* algorithm, and
the best path is identified by heuristics. The informed search
technique considerably reduces the algorithm’s time complexity. The
AO* algorithm is far more effective in searching AND-OR
trees than the A* algorithm.
Working of AO* algorithm:
The evaluation function in AO* looks like this:
f(n) = g(n) + h(n)
f(n) = Actual cost + Estimated cost
here,
f(n) = The actual cost of traversal.
g(n) = the cost from the initial node to the current node.
h(n) = estimated cost from the current node to the goal state.