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

Unit - 2 Problems, Problem Spaces, and Search

Unit 2 discusses state space search as a mathematical representation of problems in AI, highlighting its features such as exhaustiveness, completeness, and optimality. It outlines the steps for conducting a state space search using graphs and provides an example with the 8-puzzle, illustrating its advantages and disadvantages. Additionally, it covers production systems in AI, their components, classifications, and the characteristics of AI problems, along with uninformed search algorithms like breadth-first search.

Uploaded by

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

Unit - 2 Problems, Problem Spaces, and Search

Unit 2 discusses state space search as a mathematical representation of problems in AI, highlighting its features such as exhaustiveness, completeness, and optimality. It outlines the steps for conducting a state space search using graphs and provides an example with the 8-puzzle, illustrating its advantages and disadvantages. Additionally, it covers production systems in AI, their components, classifications, and the characteristics of AI problems, along with uninformed search algorithms like breadth-first search.

Uploaded by

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

Unit - 2 : Problems, Problem Spaces, and Search

Defining the Problem as a State Space Search:-


A state space is a mathematical representation of a problem that defines all possible states that the
problem can be in. Furthermore, in search algorithms, we use a state space to represent the current
state of the problem, the initial state, and the goal state.
State-space search, including artificial intelligence (AI), is used in computer science to locate a goal state
with the desired feature.
Features of State Space Search
State space search has several features that make it an effective problem-solving technique in Artificial
Intelligence. These features include:
• Exhaustiveness:
State space search explores all possible states of a problem to find a solution.
• Completeness:
If a solution exists, state space search will find it.
• Optimality:
Searching through a state space results in an optimal solution.
• Uninformed and InformedSearch:
State space search in artificial intelligence can be classified as uninformed if it provides additional
information about the problem.
Steps in State Space Search
The steps involved in state space search are as follows:
State Space Search Representation
State Space Representation involves identifying an initial state and a goal state, then finding a sequence
of actions (states) to navigate from the former to the latter. Here we provide a definition for important
terminologies required during state space search:
• State: It can be the Initial State, the Goal State, or any state generated by applying rules.
• Space: In AI, Space consists of the complete set of conceivable states for a given problem.
• Search: This technique progresses from the initial state to the desired state by applying effective
rules within the space of all possible states.
• Search Tree: It’s a visual representation of the problem, starting with th

Conducting a state space search with a graph involves exploring a set of nodes and edges with the goal
of finding a path from an initial node to a goal node. Below is a step-by-step procedure for conducting a
state space search using a graph:
• Step 1: Define the Problem: Clearly state the problem, specify the initial node, the goal node,
and the set of possible transitions (edges) between nodes.
• Step 2: Create the Graph: Represent the problem as a directed or undirected graph, where
nodes represent states and edges represent possible transitions or actions.
• Step 3: Select a Search Algorithm: Choose an appropriate search algorithm based on the
characteristics of the problem and the graph. Common choices include Breadth-First Search (BFS)
and Depth-First Search (DFS).
• Step 4: Initialize Data Structures: Create a data structure to keep track of visited nodes and
nodes to be explored. Add the initial node to the list of nodes to be explored.
• Step 5: Iterate through the Graph: While nodes remain to explore, extract the next node.
Verify if it’s the goal; if true, a solution is attained. If not, generate successor nodes by traversing
edges from the current node.
• Step 6: Check for Goal Node: Upon generating successor nodes, check if any of them are the
goal node. If so, the search is complete, and a solution has been found.
• Step 7: Manage Visited Nodes: Keep track of visited nodes to avoid revisiting them and
potentially entering loops.
• Step 8: Update Data Structures: Update the data structures with newly generated nodes. Add
them to the list of nodes to be explored.
• Step 9: Repeat Steps 5-8 until Goal Node is Found: Continue iterating through the graph,
generating successor nodes, and checking for the goal node until a solution is found.
• Step 10: Backtrack (If Necessary): In some cases, if a dead-end is reached, backtrack to a
previous node and explore alternative paths.
• Step 11: Retrieve Solution Path: Once the goal node is reached, trace back the path from the
goal node to the initial node to retrieve the sequence of actions or transitions that led to the
solution.
• Step 12: Evaluate Solution: Evaluate the solution based on relevant metrics, such as path cost,
optimality, and completeness.
• Step 13: Implement Post-Processing (if needed): Depending on the problem domain,
additional steps may be required to implement the solution.
Example of State Space Search in AI
The 8-puzzle is a popular sliding puzzle that involves a 3×3 grid where eight numbered tiles and one
blank space are arranged. The objective is to rearrange the tiles from an initial configuration to a
target configuration using a sequence of valid moves. Here’s a step-by-step explanation of the 8-
puzzle:
Step 1: Initial State
The 8-puzzle starts with an initial configuration where eight numbered tiles (usually from 1 to 8) and
one empty space are arranged randomly within a 3×3 grid. For example, an initial state could look like
this:

Here, the empty space is represented by a blank square.


Step 2: Goal State
The goal is to reach a predefined target state, which typically has the tiles arranged in numerical order.
The empty space is usually in the bottom-right corner. The goal state looks like this:

Step 3: Legal Moves


Tiles can only be moved into the empty space if they are adjacent to it (either horizontally or vertically,
not diagonally). This means that at any given state, you can slide a neighboring tile into the empty
space.
Step 4: Objective
The objective is to find a sequence of moves that transforms the initial configuration into the goal
configuration. Each move represents a state transition.
Step 5: State Space Search
State space search algorithms, like A* or Breadth-First Search, are used to systematically explore
possible states and find an optimal solution. These algorithms evaluate and prioritize states based on
certain criteria, such as distance to the goal state.
Step 6: Solution
The solution is a series of moves (states) that, when applied to the initial configuration, lead to the
goal configuration. For instance, a sequence of moves might look like this:
This sequence of moves transforms the initial configuration into the goal configuration.
Advantages of State Space Search in AI
State Space Search in AI exhibits several key advantages that make it a powerful and versatile problem-
solving technique. Here are some of its prominent attributes:
• Systematic Exploration: State space search systematically explores the possible states and
transitions of a problem, ensuring a comprehensive examination of potential solutions.
• Problem Representation: It allows for the representation of complex problems in a structured
manner, with states representing configurations and transitions depicting possible actions or
moves.
• Versatility: State space search can be applied to a wide range of problems across different
domains, from robotics and game-playing to natural language processing and scheduling.
• Adaptability: It can adapt to various problem types, including deterministic, stochastic, and
adversarial scenarios, making it applicable to a diverse set of challenges.
• Informed Decision-Making: Through the use of heuristic functions, state space search can
incorporate domain-specific knowledge, guiding the search process towards more efficient and
effective solutions.
• Optimality and Completeness: Depending on the algorithm employed, state space search can
guarantee either optimal solutions (finding the best possible outcome) or completeness (ensuring
a solution will be found if it exists).
• Memory Efficiency: Many state space search algorithms are designed to be memory-efficient,
allowing them to handle large state spaces without overwhelming computational resources.
Disadvantages of State Space Search in AI
While state space search in AI is a powerful technique, it does come with certain disadvantages:
• Exponential Growth: The state space can grow exponentially with the size of the problem,
leading to an impractical number of states to explore in some cases.
• Memory Intensive: Storing and managing a large state space can require significant memory
resources, which can be a limitation for systems with limited memory.
• Time-Consuming: In complex problems, the search process can be time-consuming, especially
if the state space is large or if the algorithm does not employ efficient heuristics.
• Limited to Deterministic Environments: State space search assumes deterministic
environments where the outcome of an action is always predictable. In stochastic or partially
observable environments, it may not perform optimally.
• Difficulty with Large Branching Factors: Problems with a large number of possible actions
from each state can lead to a high branching factor, making the search process more challenging.

Applications of State Space Search in AI


State space search is a fundamental technique in artificial intelligence with various applications across
different domains. Here are some key applications:
• Puzzle Solving: Solving puzzles like the 8-puzzle, Rubik’s Cube, and Sudoku using state space
search algorithms
• Pathfinding in Games: Finding the shortest path for characters or agents in video games is a
common use case for algorithms like A*
• Robotics: Planning the movement of robots in a physical environment to perform tasks or reach
specific locations
• Automated Planning: In areas like logistics, transportation, and manufacturing, state space
search helps in planning and scheduling tasks
• Natural Language Processing: In tasks like machine translation, state space search can be
used to generate optimal translations
• Chess and Games: Determining optimal moves in games with well-defined rules and states, like
chess, checkers, and Go
• Optimization Problems: Solving optimization problems in areas like resource allocation,
scheduling, and financial modeling
What is a Production System in AI?
A Production System in AI is like a set of instructions and a database that helps computers make
decisions. Imagine it like a recipe book: it contains rules (recipes) and facts (ingredients), and it helps
the computer figure out what to do based on those rules and facts. These systems are often used to
build computer programs that act like experts in certain subjects, making decisions or giving advice
based on the information they have.
Components of the Production System

The components of Production System in AI encompass three essential elements:


• Global Database: The global database serves as the system’s memory, storing facts, data, and
knowledge relevant to its operation. It is a repository that production rules can access to make
informed decisions and draw conclusions.
• Production Rules: Production rules form the core logic of the system. They are a set of
guidelines that the system follows while making decisions. These regulations outline the system’s
reaction to various inputs and circumstances.
• Control System: The control system manages the execution of production rules. It determines
the sequence in which rules are applied, ensuring efficient processing and optimizing the system’s
performance.
Features of a Production System in AI

AI Production Systems exhibit several key features that make them versatile and powerful tools for
automated decision-making and problem-solving:
• Simplicity: Production Systems offer a straightforward way to encode and execute rules, making
them accessible for developers and domain experts.
• Modularity: These systems are composed of modular components, allowing for the addition,
removal, or modification of rules without disrupting the entire system. This modularity enhances
flexibility and ease of maintenance.
• Modifiability: AI Production Systems are highly adaptable. Rules can be updated or replaced
without extensive reengineering, ensuring the system remains up-to-date and aligned with
evolving requirements.
• Knowledge-intensive: They excel in handling knowledge-rich tasks, relying on a comprehensive
global database.
• Adaptability: AI Production Systems can dynamically adapt to new data and scenarios. This
adaptability allows them to continuously improve.
Classification of Production Systems in AI
AI production systems can be classified into four common classifications:
• Monotonic Production System: In a monotonic production system, the laws and truths remain
constant while being carried out. A rule remains constant throughout the procedure once a fact
is deduced. This stability ensures predictability but may limit adaptability in dynamic
environments.
• Partially Commutative Production System: In this type of system, rules can be applied
flexibly, allowing for some degree of adaptability while maintaining certain constraints. Partial
commutativity strikes a balance between stability and flexibility.
• Non-monotonic Production System: Non-monotonic production systems are more dynamic
and adaptive. Rules can be added, modified, or retracted during execution. They are excellent for
situations where the knowledge base needs to change in response to shifting circumstances
because of their flexibility.
• Commutative System: Commutative systems have rules that can be applied in any sequence
without changing the result. In circumstances where the sequence of rule application is not
essential, this high degree of flexibility may be beneficial.
Problem Characteristics
Artificial Intelligence (AI) problems refer to a wide range of tasks that involve the development of
algorithms or systems that can perform tasks that typically require human-level intelligence, such as
decision-making, language processing, image recognition, and problem-solving.
AI problems are characterized by several key features, including:
1. Complexity: AI problems are often highly complex, requiring the processing of large amounts of
data and the ability to handle uncertainty and ambiguity. The algorithms and systems developed
to solve these problems must be able to manage complexity and make decisions based on
incomplete or uncertain information.
2. Non-linearity: Many AI problems exhibit non-linear relationships, meaning that small changes
in the input can lead to significant changes in the output. This makes it difficult to develop
algorithms that can accurately predict outcomes or make decisions based on input data.
3. Context dependence: AI problems often require the ability to understand context and make
decisions based on the specific situation or environment. For example, a language processing
system must be able to understand the meaning of words in the context of a sentence or
paragraph.
4. Creativity: Some AI problems require the ability to generate novel solutions or ideas, often using
techniques such as generative adversarial networks (GANs) or evolutionary algorithms. These
techniques allow AI systems to learn from existing data and generate new and innovative
solutions to complex problems.
5. Learning and adaptation: AI problems often require the ability to learn and adapt over time
based on new data or feedback. This is achieved through techniques such as machine learning,
deep learning, and reinforcement learning, which enable AI systems to improve their performance
over time.
6. Multi-disciplinary: AI problems often require expertise from multiple fields, including computer
science, mathematics, statistics, and cognitive psychology. Successful AI systems must be
designed and developed by teams with a wide range of skills and knowledge.
7. Ethical considerations: AI problems raise ethical and social concerns related to privacy,
security, bias, and fairness. The development of AI systems requires careful consideration of
these issues to ensure that the technology is used ethically and responsibly.

Uninformed Search Algorithms


Uninformed search is a class of general-purpose search algorithms which operates in brute force-way.
Uninformed search algorithms do not have additional information about state or search space other than
how to traverse the tree, so it is also called blind search.
Following are the various types of uninformed search algorithms:
1. Breadth-first Search
2. Depth-first Search
3. Depth-limited Search
4. Iterative deepening depth-first search
5. Uniform cost search
6. Bidirectional Search
1. Breadth-first Search:
o Breadth-first search is the most common search strategy for traversing a tree or graph. This
algorithm searches breadthwise in a tree or graph, so it is called breadth-first search.
o BFS algorithm starts searching from the root node of the tree and expands all successor node at
the current level before moving to nodes of next level.
o The breadth-first search algorithm is an example of a general-graph search algorithm.
o Breadth-first search implemented using FIFO queue data structur
Breadth-first search is a graph traversal algorithm that starts traversing the graph from the root node
and explores all the neighboring nodes. Then, it selects the nearest node and explores all the unexplored
nodes. While using BFS for traversal, any node in the graph can be considered as the root node.
There are many ways to traverse the graph, but among them, BFS is the most commonly used approach.
It is a recursive algorithm to search all the vertices of a tree or graph data structure. BFS puts every
vertex of the graph into two categories - visited and non-visited. It selects a single node in a graph and,
after that, visits all the nodes adjacent to the selected node.
Applications of BFS algorithm
The applications of breadth-first-algorithm are given as follows –
o BFS can be used to find the neighboring locations from a given source location.
o In a peer-to-peer network, BFS algorithm can be used as a traversal method to find all the
neighboring nodes. Most torrent clients, such as BitTorrent, uTorrent, etc. employ this process to
find "seeds" and "peers" in the network.
o BFS can be used in web crawlers to create web page indexes. It is one of the main algorithms
that can be used to index web pages. It starts traversing from the source page and follows the
links associated with the page. Here, every web page is considered as a node in the graph.
o BFS is used to determine the shortest path and minimum spanning tree.
o BFS is also used in Cheney's technique to duplicate the garbage collection.
o It can be used in ford-Fulkerson method to compute the maximum flow in a flow network

Advantage of BFS Algorithm


Breadth-First Search (BFS) is a widely used algorithm in the field of Artificial Intelligence (AI) and
computer science in general. Here are some advantages of BFS in AI:
1. Completeness: BFS is a complete search algorithm, meaning that it is guaranteed to find a
solution if one exists. This is particularly important in AI applications where finding a solution is
crucial.
2. Optimality for Unweighted Graphs: In scenarios where all edges have the same cost
(unweighted graphs), BFS guarantees an optimal solution. It explores nodes level by level,
ensuring the shortest path is found first.
3. Simplicity: BFS is easy to understand and implement. It uses a simple queue data structure to
manage the order in which nodes are explored.
4. Memory Efficiency for Tree Structures: In tree structures, BFS is more memory-efficient than
depth-first search (DFS) because it only needs to store a single level of nodes in the queue at a
time. This can be advantageous in AI applications with limited memory resources.
5. Useful for State Space Search: In AI, BFS is commonly applied in state space search problems,
where the search space can be represented as a graph or tree. It helps explore possible states
systematically.
6. Identifying the Shallowest Goal: BFS explores nodes in levels, making it suitable for scenarios
where the goal is likely to be closer to the starting state. It is efficient in scenarios where the
solution is shallow in the search tree.
7. Can be Adapted for Bidirectional Search: BFS can be easily adapted for bidirectional search
strategies, where the search is performed simultaneously from the start state and the goal state.
This can significantly reduce the search space.
8. Cyclic Graph Handling: BFS can handle cyclic graphs by keeping track of visited nodes. This is
important in AI applications where cyclic relationships may exist in the problem domain.

Disadvantage of BFS
While Breadth-First Search (BFS) has several advantages, it also has some limitations and disadvantages
in certain contexts, especially in Artificial Intelligence (AI) applications. Here are some drawbacks of BFS:
1. Memory Consumption: BFS can consume a significant amount of memory, especially when
dealing with large state spaces or graphs. This is because it needs to store all the nodes at the
current level in the queue before moving to the next level. In situations with limited memory
resources, this can be a significant drawback.
2. Inefficiency for Large Graphs or State Spaces: In scenarios where the search space is
extensive, BFS may become inefficient due to its need to explore all nodes at each level before
moving to the next level. This can result in a large number of nodes being generated and stored.
3. Not Suitable for Graphs with Varying Edge Costs: BFS assumes that all edges have the same
cost. In cases where the edges have varying costs (weighted graphs), BFS may not provide the
optimal solution. In such scenarios, algorithms like Dijkstra's or A* may be more appropriate.
4. Inability to Handle Infinite Branching Factor: If the branching factor of the search tree is
very high or infinite, BFS may not be practical. The number of nodes generated and the memory
requirements can become unmanageable.
5. Limited Use in Finding Specific Paths: While BFS can find the shortest path in unweighted
graphs, it may not be suitable for scenarios where the goal is to find a specific type of path (e.g.,
paths with certain properties or constraints). Other algorithms like DFS or A* with appropriate
heuristics might be more suitable for such cases.
6. No Guidance Toward Goal Direction: BFS does not use any information about the potential
closeness of the goal during its search. In contrast, algorithms like A* use heuristics to guide the
search toward the goal more efficiently.
7. Doesn't Exploit Problem-Specific Knowledge: BFS is a generic algorithm and does not exploit
any problem-specific knowledge that might be available in certain AI applications. This can result
in a less efficient exploration of the search space.
8. Not Suitable for Online or Incremental Search: In situations where the search space is
dynamic and changes over time, BFS might not be well-suite
BFS Algorithm
The steps involved in the BFS algorithm to explore a graph are given as follows
Step 1: SET STATUS = 1 (ready state) for each node in G
Step 2: Enqueue the starting node A and set its STATUS = 2 (waiting state)
Step 3: Repeat Steps 4 and 5 until QUEUE is empty
Step 4: Dequeue a node N. Process it and set its STATUS = 3 (processed state).
Step 5: Enqueue all the neighbours of N that are in the ready state (whose STATUS = 1) and set
their STATUS = 2
(waiting state)
[END OF LOOP]
Step 6: EXIT
Example of BFS algorithm
Now, let's understand the working of BFS algorithm by using an example. In the example given below,
there is a directed graph having 7 vertices.

In the above graph, minimum path 'P' can be found by using the BFS that will start from Node A and end
at Node E. The algorithm uses two queues, namely QUEUE1 and QUEUE2. QUEUE1 holds all the nodes
that are to be processed, while QUEUE2 holds all the nodes that are processed and deleted from QUEUE1.
Now, let's start examining the graph starting from Node A.
Step 1 - First, add A to queue1 and NULL to queue2.

1. QUEUE1 = {A}
2. QUEUE2 = {NULL}

Step 2 - Now, delete node A from queue1 and add it into queue2. Insert all neighbors of node A to
queue1.

1. QUEUE1 = {B, D}
2. QUEUE2 = {A}

Step 3 - Now, delete node B from queue1 and add it into queue2. Insert all neighbors of node B to
queue1.

1. QUEUE1 = {D, C, F}
2. QUEUE2 = {A, B}

Step 4 - Now, delete node D from queue1 and add it into queue2. Insert all neighbors of node D to
queue1. The only neighbor of Node D is F since it is already inserted, so it will not be inserted again.
1. QUEUE1 = {C, F}
2. QUEUE2 = {A, B, D}

Step 5 - Delete node C from queue1 and add it into queue2. Insert all neighbors of node C to
queue1.

1. QUEUE1 = {F, E, G}
2. QUEUE2 = {A, B, D, C}

Step 6 - Delete node E from queue1. Since all of its neighbors have already been added, so we will
not insert them again. Now, all the nodes are visited, and the target node E is encountered into
queue2.

1. QUEUE1 = {G}
2. QUEUE2 = {A, B, D, C, F, E}

Complexity of BFS algorithm


Time complexity of BFS depends upon the data structure used to represent the graph. The time
complexity of BFS algorithm is O(V+E), since in the worst case, BFS algorithm explores every node
and edge. In a graph, the number of vertices is O(V), whereas the number of edges is O(E).

The space complexity of BFS can be expressed as O(V), where V is the number of vertices.

Implementation of BFS algorithm


Now, let's see the implementation of BFS algorithm in java.

In this code, we are using the adjacency list to represent our graph. Implementing the Breadth-First
Search algorithm in Java makes it much easier to deal with the adjacency list since we only have to
travel through the list of nodes attached to each node once the node is dequeued from the head
(or start) of the queue.

In this example, the graph that we are using to demonstrate the code is given as follows -
# Create a graph given in the above diagram.
graph = {
'A': ['B', 'C', 'D'],
'B': ['A'],
'C': ['A', 'D'],
'D': ['A', 'C', 'E'],
'E': ['D'],
}

# to print a BFS of a graph


def bfs(node):

# mark vertices as False means not visited


visited = [False] * (len(graph))

# make an empty queue for bfs


queue = []

# mark gave node as visited and add it to the queue


visited.append(node)
queue.append(node)

while queue:
# Remove the front vertex or the vertex at the 0th index from the queue and print that vertex.
v = queue.pop(0)
print(v, end=" ")

# Get all adjacent nodes of the removed node v from the graph hash table.
# If an adjacent node has not been visited yet,
# then mark it as visited and add it to the queue.
for neigh in graph[v]:
if neigh not in visited:
visited.append(neigh)
queue.append(neigh)

# Driver Code
if __name__ == "__main__":
bfs('A')
DFS (Depth First Search) algorithm
It is a recursive algorithm to search all the vertices of a tree data structure or a graph. The depth-first
search (DFS) algorithm starts with the initial node of graph G and goes deeper until we find the goal
node or the node with no children.
Because of the recursive nature, stack data structure can be used to implement the DFS algorithm. The
process of implementing the DFS is similar to the BFS algorithm.
The step-by-step process to implement the DFS traversal is given as follows -
1. First, create a stack with the total number of vertices in the graph.
2. Now, choose any vertex as the starting point of traversal, and push that vertex into the stack.
3. After that, push a non-visited vertex (adjacent to the vertex on the top of the stack) to the top
of the stack.
4. Now, repeat steps 3 and 4 until no vertices are left to visit from the vertex on the stack's top.
5. If no vertex is left, go back and pop a vertex from the stack.
6. Repeat steps 2, 3, and 4 until the stack is empty.
Applications of DFS algorithm
The applications of using the DFS algorithm are given as follows -
Backward Skip 10sPlay Video Forward Skip 10s

o DFS algorithm can be used to implement the topological sorting.


o It can be used to find the paths between two vertices.
o It can also be used to detect cycles in the graph.
o DFS algorithm is also used for one solution puzzles.
o DFS is used to determine if a graph is bipartite or not.

Algorithm

Step 1: SET STATUS = 1 (ready state) for each node in G

Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state)

Step 3: Repeat Steps 4 and 5 until STACK is empty

Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state)

Step 5: Push on the stack all the neighbors of N that are in the ready state (whose STATUS = 1) and
set their STATUS = 2 (waiting state)

[END OF LOOP]

Step 6: EXIT

Pseudocode

1. DFS(G,v) ( v is the vertex where the search starts )


2. Stack S := {}; ( start with an empty stack )
3. for each vertex u, set visited[u] := false;
4. push S, v;
5. while (S is not empty) do
6. u := pop S;
7. if (not visited[u]) then
8. visited[u] := true;
9. for each unvisited neighbour w of uu
10. push S, w;
11. end if
12. end while
13. END DFS()

Example of DFS algorithm

Now, let's understand the working of the DFS algorithm by using an example. In the example given
below, there is a directed graph having 7 vertices.

Now, let's start examining the graph starting from Node H.

Step 1 - First, push H onto the stack.

1. STACK: H

Step 2 - POP the top element from the stack, i.e., H, and print it. Now, PUSH all the neighbors of H
onto the stack that are in ready state.

1. Print: H]STACK: A

Step 3 - POP the top element from the stack, i.e., A, and print it. Now, PUSH all the neighbors of A
onto the stack that are in ready state.

1. Print: A
2. STACK: B, D
Step 4 - POP the top element from the stack, i.e., D, and print it. Now, PUSH all the neighbors of D onto
the stack that are in ready state.
1. Print: D
2. STACK: B, F
Step 5 - POP the top element from the stack, i.e., F, and print it. Now, PUSH all the neighbors of F onto
the stack that are in ready state.
1. Print: F
2. STACK: B
Step 6 - POP the top element from the stack, i.e., B, and print it. Now, PUSH all the neighbors of B onto
the stack that are in ready state.
1. Print: B
2. STACK: C
Step 7 - POP the top element from the stack, i.e., C, and print it. Now, PUSH all the neighbors of C onto
the stack that are in ready state.
1. Print: C
2. STACK: E, G
Step 8 - POP the top element from the stack, i.e., G and PUSH all the neighbors of G onto the stack that
are in ready state.
1. Print: G
2. STACK: E
Step 9 - POP the top element from the stack, i.e., E and PUSH all the neighbors of E onto the stack that
are in ready state.
1. Print: E
2. STACK:
Program
# DFS algorithm in Python
# DFS algorithm
def dfs(graph, start, visited=None):
if visited is None:
visited = set()
visited.add(start)
print(start)
for next in graph[start] - visited:
dfs(graph, next, visited)
return visited
graph = {'0': set(['1', '2']),
'1': set(['0', '3', '4']),
'2': set(['0']),
'3': set(['1']),
'4': set(['2', '3'])
dfs(graph, '0')
Difference Between BFS and DFS
S.no BFS DFS
BFS is an abbreviation of Breadth First DFS is an abbreviation of Depth First
Abbreviation for
Search. Search.
BFS (Breadth First Search) finds the
DFS (Depth First Search) makes use of
Data Organization shortest path using the Queue data
the Stack data structure.
structure.
Because BFS reaches a vertex with the
fewest edges from the source vertex, it In DFS, we may need to traverse more
Technique may be used to identify a single origin edges to go from a source vertex to a
shortest path inside an unweighted destination vertex.
graph.
The difference in DFS constructs the tree subtree by
BFS constructs a tree level per level.
Concepts subtree.
It is based on the FIFO principle (First in, It operates on the LIFO principle (Last
Utilised strategy
First Out). in First Out).
BFS is more suited for finding vertices If there are alternatives away from the
Appropriate for
that are near the provided source. source, DFS is more appropriate.
DFS is more suited to gaming or
Treestheirwinning BFS prioritises all
puzzle challenges. We make a choice
Suitable for neighbours and is hence unsuitable for
and then investigate all possible
Making a Decision decision-making trees in games or
outcomes. If this decision results in a
puzzles.
win-win situation, we cease.
The running time of BFS becomes O(V E) DFS has a time complexity of O(V E)
when using an Adjacency List and O(V2) when using an Adjacency List and
Time Complicacy when using an Adjacency Matrix, where V O(V2) when using an Adjacency
represents vertices and E represents Matrix, where V refers to vertices & E
edges. stands for edges.
The seen sites are added to a stack
Traversed Node Nodes that have been traversed multiple
whenever no more sites are visited
Removal times are removed from the queue.
and subsequently deleted.
BFS is utilised in various applications, DFS is utilised in various applications,
Applications including bipartite graphs, shortest including acyclic graphs & topological
routes, etc. order.
DFS requires less space since it only
The complexity of In BFS, case complexity is more
has to keep a single route from the
space important than time complexity.
root to a leaf node at a time.
When should you BFS works better whenever the target is DFS is advantageous whenever the
utilise it? near the source. target is remote from the source.
Speed When compared to DFS, BFS is slower. When compared to BFS, DFS is faster
Advantages of DFS:
1. Simplicity: DFS is a straightforward algorithm and is easy to implement.
2. Memory Efficiency: DFS can be more memory-efficient compared to Breadth-First Search (BFS)
as it requires less memory to store the visited nodes.
3. Space Complexity: In the worst case, the space complexity of DFS is O(h), where h is the
maximum depth of your tree. This can be advantageous when dealing with deep trees or graphs.
4. Versatility: DFS can be adapted for various applications, such as topological sorting, detecting
cycles in a graph, and solving puzzles like mazes.
5. Solution Optimality: In certain cases, DFS can find the solution with less memory consumption
compared to BFS.
Disadvantages of DFS:
1. Completeness: DFS may not find the shortest path in a weighted graph or the optimal solution
in certain cases. It is not guaranteed to find the most optimal solution because it may explore
paths that are longer before discovering a shorter one.
2. Non-Optimal for Shortest Paths: If the goal is to find the shortest path in a graph, DFS may
not be the best choice, as it can continue exploring a branch without considering the weight of
edges.
3. Vulnerability to Infinite Branching: In graphs with infinite branching (e.g., trees with infinite
nodes), DFS may not terminate unless there are mechanisms to handle this situation.
4. Not Ideal for Dense Graphs: DFS may not perform well on dense graphs because it tends to
go deep into a branch before backtracking. In dense graphs, this can lead to many unnecessary
explorations.
5. Pathological Cases: DFS might go deep into one branch before considering other options,
leading to inefficient solutions or longer paths.

You might also like