0% found this document useful (0 votes)
70 views45 pages

Searching Algorithms (BFS, DFS, DLS, UCS)

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

Searching Algorithms (BFS, DFS, DLS, UCS)

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

Breadth-First

Search (BFS)
Introduction to Breadth-First Search (BFS)
Definition: Breadth-First Search (BFS) is an algorithm used to traverse or search
through graph or tree data structures.

Traversal Approach: BFS explores nodes layer by layer, starting at a root node and
visiting all of its immediate neighbors before moving deeper into the structure.

Core Mechanism: BFS uses a queue data structure to manage nodes, ensuring it
explores each level completely before moving to the next.

Typical Use: Commonly used for finding the shortest path in unweighted graphs and
performing level-order traversal in trees.
Why We Use BFS
Shortest Path:

● BFS is ideal for finding the shortest path in unweighted graphs, where each step between nodes has the same
"cost."
● Example: Finding the shortest route between cities in an unweighted road network.

Level-Order Traversal:

● BFS visits nodes level by level, making it especially useful in scenarios requiring a broad, systematic approach.
● Example: In organizational charts, where BFS can process each hierarchy level in order.

Connected Components:

● BFS can identify all nodes connected to a starting node, making it useful for finding clusters or connected
groups in undirected graphs.
● Example: Discovering all members in a social network who are directly or indirectly connected.
When to Use BFS
Closer Solutions: When the solution or target node is likely closer to the root, BFS
will typically locate it faster than Depth-First Search (DFS).

Layered Exploration: BFS is ideal when nodes need to be explored in a specific


level-by-level order.

Complete Exploration: BFS visits every reachable node from the starting point,
which ensures no parts of the graph are left unexplored in connected components.
Pros and Cons of BFS
Advantages Disadvantages

Shortest Path: Finds shortest path in unweighted Memory Usage: Stores entire levels at once, which
graphs can consume more memory

Level-Order Traversal: Visits nodes layer by layer Performance: Can be slower for very deep trees or
large graphs

Good for locating nodes near the root Not Depth-Optimal: May be inefficient for deep trees
or far-off solutions
BFS Traversal Example in a Tree
Let's consider the following binary tree as an example of BFS traversal:
Steps for BFS Traversal
1. Start from the root node 1.
2. Visit 1's children: 2 and 3.
3. Move to the next level and visit the children of 2 and 3: 4, 5, and 6.

BFS Traversal Result: 1 -> 2 -> 3 -> 4 -> 5 -> 6

BFS Traversal Explained

4. Starting Point: Begin at the root node 1.


5. First Level: Visit all children of 1 (i.e., 2 and 3).
6. Next Level: Visit each child of 2 and 3 (i.e., 4, 5, 6).

This approach continues until all levels are traversed. The queue data structure stores nodes temporarily until they
are fully explored.
BFS Pseudo-Code
Here’s an example of BFS pseudo-code, using a queue to manage the nodes.
Real-World Applications of BFS
Social Networks

● Use: Find the degree of separation between people.


● Example: Determine the "friendship distance" between two users on social platforms.
● Benefit: BFS identifies shortest connections, helping analyze user connectivity within networks.

GPS Navigation

● Use: Determine the shortest path between two locations.


● Example: Mapping apps use BFS-like algorithms for minimum turns or shortest paths.
● Benefit: Efficient routing by exploring all possible paths in stages from the starting point.
Real-World Applications of BFS
Web Crawling

● Use: Explore linked content from a starting URL in a structured way.


● Example: Search engines use web crawlers to discover and index new content.
● Benefit: BFS systematically explores all reachable links layer by layer.

AI and Games

● Use: Find shortest move sequences or paths in puzzles and games.


● Example: In a maze game, BFS finds the minimum steps needed to reach an exit.
● Benefit: Provides optimal solutions by exploring possible moves layer by layer.
Summary
Definition: BFS traverses structures level by level, exploring nodes by proximity.

Best Suited For:

● Shortest Paths: In unweighted graphs, finds the shortest path.


● Level-Order Traversal: Ideal for tree-like data structures where each level has a meaning.

Applications:

● Pathfinding: Useful in navigation, logistics, and GPS systems.


● Network Analysis: Social and professional networks, finding connection paths.
● Data Exploration: Internet crawling and systematic database traversal.
● AI and Games: For game-based puzzles and strategy solutions.
Depth-First
Search (DFS)
Introduction to Depth-First Search (DFS)
Definition: Depth-First Search (DFS) is an algorithm for traversing or searching tree
and graph data structures. Unlike BFS, DFS explores as far down a branch as possible
before backtracking.

Traversal Approach: DFS explores nodes by diving deep into one path until it
reaches a dead-end (or the goal), then it backtracks to explore other paths.

Core Mechanism: DFS uses a stack data structure, often implemented with
recursion, to keep track of nodes. This stack-like behavior allows it to backtrack and
continue exploring unexplored paths.

Typical Use: DFS is commonly used in scenarios where we need to explore all
possible paths or detect cycles in graphs.
Why We Use DFS
Path Exploration:

● DFS is ideal for exploring all paths in a graph or tree to their depths, useful for understanding entire structures
or configurations.
● Example: Navigating through a maze, where we explore each path deeply before trying another.

Detecting Cycles:

● DFS helps in detecting cycles within graphs, which is especially useful in detecting deadlocks in computer
systems and dependencies in dependency graphs.
● Example: Checking for circular dependencies in package management.

Topological Sorting:

● In directed acyclic graphs (DAGs), DFS helps produce a topological order, which is essential in task scheduling
and resolving dependencies.
● Example: Course prerequisites where some courses must be taken before others.
When to Use DFS
● Deep Solutions: DFS is efficient when solutions or goals are expected to be far
from the starting node or root, as it explores depth-first.

● Space Efficiency: In general, DFS requires less memory than BFS, especially
when implemented recursively, as it doesn’t store entire levels.

● Complete Path Exploration: DFS ensures that each path from a starting point
is fully explored, making it useful for tasks that require complete examination.
Pros and Cons of DFS
Advantages Disadvantages

Memory Efficient: Typically requires less memory than Not Optimal for Shortest Path: DFS may not yield the
BFS due to recursive implementation shortest path in unweighted graphs

Full Path Exploration: Useful for tasks that need Risk of Infinite Loops: Can get stuck in cycles if not
every path explored handled properly in graphs

Works Well for Deep Solutions May Miss Closer Solutions: DFS could miss closer
solutions by going deep into other branches
DFS Traversal Example in a Tree
Let's consider the following binary tree as an example of DFS traversal:
Steps for DFS Traversal (Pre-order example):
Steps for DFS Traversal (Pre-order example):

1. Start from the root node 1.


2. Visit 1's left child: 2.
3. Visit 2's left child: 4.
4. Backtrack to 2 and visit its right child: 5.
5. Backtrack to 1 and visit its right child: 3.
6. Visit 3's right child: 6.

DFS Traversal Result (Pre-order): 1 -> 2 -> 4 -> 5 -> 3 -> 6

DFS Traversal Explained

7. Starting Point: Begin at the root node 1.


8. Depth Exploration: Go as deep as possible along each branch before backtracking.
9. Backtracking: When a branch reaches an end (or node with no unexplored children), DFS backtracks to the most recent node with
unexplored paths.

This approach continues until all nodes are visited. The stack data structure, which can be a call stack in recursion, temporarily stores nodes until
they’re fully explored.
DFS Pseudo-Code
Here’s an example of DFS pseudo-code using recursion, which mimics a
stack:
Real-World Applications of DFS
Maze and Puzzle Solving

● Use: Explore all possible solutions to reach a goal.


● Example: DFS can attempt all paths in a maze or puzzle to find one or multiple solutions.
● Benefit: DFS ensures all possible routes are explored to reach a solution.

Cycle Detection

● Use: Identify cycles in graphs, which is essential in deadlock detection or finding circular dependencies.
● Example: Detecting deadlocks in an operating system.
● Benefit: Efficient in cycle detection by visiting each node only once.
Real-World Applications of DFS
Topological Sorting

● Use: Arrange tasks or nodes in a linear order based on dependencies.


● Example: Scheduling tasks with dependencies, like course prerequisites.
● Benefit: Provides a logical ordering for tasks that must occur sequentially.

Pathfinding for Depth-Dependent Solutions

● Use: Situations where a solution is likely to be deep in the structure.


● Example: Exploring all possible configurations in chess or game state trees.
● Benefit: DFS efficiently reaches deeper levels in a structure to find solutions or analyze states.
Summary
Definition: DFS traverses structures by exploring depth-first, diving deep into one branch before backtracking.

Best Suited For:

● Cycle Detection: Helps identify circular dependencies and deadlocks.


● Topological Sorting: Essential in task scheduling for dependency-based ordering.
● Deep Pathfinding: Effective for deep solutions in puzzles and game states.

Applications:

● Maze and Puzzle Solving: Finds all possible solutions.


● Cycle Detection: Prevents deadlocks and circular dependencies.
● Topological Sorting: Useful for ordering tasks with dependencies.
● Game State Exploration: Used for analyzing move configurations in games.
Depth-Limited
Search (DLS)
Introduction to Depth-Limited Search (DLS)
● Definition: Depth-Limited Search (DLS) is a variant of Depth-First Search (DFS)
where the search is restricted to a specific depth, preventing it from going deeper
than the set limit.
● Traversal Approach: DLS explores nodes by going as deep as possible along
each branch, similar to DFS, but stops exploring further once it reaches the
predefined depth limit.
● Core Mechanism: DLS uses a stack or recursive approach to manage nodes, but
it imposes a limit on depth, which controls how far it explores in any path.
● Typical Use: DLS is useful when we know a solution lies within a specific depth
range, or to avoid the excessive memory usage and risk of infinite loops that can
occur with unrestricted DFS.
Why We Use DLS
Depth Constraints:

● DLS limits the depth of exploration, which is useful when we know that the solution lies within a specific depth
range.
● Example: Searching for a file within a folder structure only to a certain folder depth.

Avoiding Infinite Loops:

● In graphs with cycles, DLS prevents infinite loops by restricting the depth, unlike DFS, which could get trapped
in cycles.
● Example: In an AI game, limiting the search depth can prevent endlessly evaluating repeating states.

Reduced Memory Usage:

● By limiting the search depth, DLS is often more memory-efficient than DFS, which continues exploring until all
nodes are visited.
● Example: In large graphs where deep exploration is memory-intensive, limiting depth conserves resources.
When to Use DLS
Known Depth Range: When the solution is known or suspected to be within a certain
depth, DLS is efficient and avoids unnecessary exploration.

Infinite or Cyclic Graphs: In structures where cycles or repeated paths are possible,
DLS prevents infinite exploration by limiting depth.

Memory Constraints: In environments with limited memory, restricting depth keeps


the search focused and conserves resources.
Pros and Cons of DLS
Advantages Disadvantages

Prevents Infinite Loops: Limits depth to avoid cycles Incomplete: Might miss solutions beyond the depth
limit

Memory Efficient: Uses less memory than DFS when Solution Depth Dependent: Requires prior knowledge
depth is constrained or estimation of depth

Control Over Exploration Depth Suboptimal for Deep Solutions: Misses solutions at
greater depths without increasing limit
DLS Traversal Example in a Tree
Consider the following binary tree and a depth limit of 2 as an example of DLS traversal:
Steps for DFS Traversal (Pre-order example):
Steps for DLS Traversal (with depth limit 2):

1. Start from the root node 1 (depth 0).


2. Visit 1's children: 2 and 3 (depth 1).
3. Move to the next level, but only visit the children of 2 and 3 (depth 2): 4, 5, and 6.

Since the depth limit is 2, nodes beyond this level are not explored.

DLS Traversal Result: 1 -> 2 -> 3 -> 4 -> 5 -> 6

DLS Traversal Explained

4. Starting Point: Begin at the root node 1 with a depth counter set to zero.
5. Depth-Limited Exploration: Traverse each branch until the depth limit is reached, then backtrack.
6. Stop at Limit: When the depth limit is reached, stop further exploration in that branch.

This approach prevents exploring beyond a certain level, making DLS useful when we need to restrict exploration within a fixed
range.
DLS Pseudo-Code
Here’s an example of DLS pseudo-code using recursion and a depth parameter to
limit the search.

This code limits depth by checking if the current depth exceeds the specified limit
before further exploration.
Real-World Applications of DLS
Limited-Depth Search in File Systems

● Use: Explore files within a folder hierarchy only up to a specified level.


● Example: Finding specific files but only within a certain number of subfolder levels.
● Benefit: Reduces unnecessary exploration in complex or large file systems.

Cycle Detection with Depth Control

● Use: Detect cycles while preventing excessive exploration in graphs.


● Example: In networks with repeating paths, limit exploration to avoid infinite cycles.
● Benefit: Provides a balance between cycle detection and limited exploration.
Real-World Applications of DLS
AI Games with Depth Constraints

● Use: Evaluate moves only up to a certain depth to limit computational overhead.


● Example: In turn-based games, AI explores moves only a few layers deep to make quicker decisions.
● Benefit: Enables faster decision-making by limiting the search depth.

Resource-Limited Search in Large Graphs

● Use: Explore only parts of a graph within a certain depth to avoid memory issues.
● Example: Searching web links on a site up to a certain link depth to avoid excessive data.
● Benefit: Conserves memory by focusing only on nodes within a set depth.
Summary
Definition: DLS is a depth-first search with a predefined depth limit, exploring nodes up to that limit before
backtracking.

Best Suited For:

● Depth-Restricted Searches: Ideal when we only want to search up to a known depth.


● Cycle Prevention: Useful in cyclic graphs to prevent infinite loops.
● Memory-Constrained Environments: Conserves memory by limiting depth of exploration.

Applications:

● File System Search: Explore directories within a certain depth.


● Cycle Detection with Depth Control: Detect cycles without excessive exploration.
● AI and Games: Limit exploration depth in decision-making.
● Web Link Search: Explore link structures within limited depths.
Uniform Cost
Search (UCS)
Introduction to Uniform Cost Search (UCS)
Definition: Uniform Cost Search (UCS) is an algorithm used to find the least-cost path
from a starting node to a goal in a weighted graph. It explores paths in increasing
order of cost, ensuring that it always finds the least-cost solution in weighted graphs.

Traversal Approach: UCS expands nodes with the lowest cumulative path cost first.
It uses a priority queue to keep track of nodes, ordering them by their path cost.

Core Mechanism: UCS leverages a priority queue to expand the least-cost node at
each step, making it an optimal choice for finding shortest paths when weights vary.

Typical Use: UCS is widely used for optimal pathfinding in weighted graphs, such as
in routing and navigation systems.
Why We Use UCS
Optimal Path in Weighted Graphs:

● UCS is ideal for finding the minimum-cost path in graphs where edges have different weights, unlike BFS, which
assumes uniform edge costs.
● Example: Determining the cheapest route between cities with roads of varying tolls.

Guaranteed Optimal Solution:

● UCS is guaranteed to find the optimal path in terms of cost because it always expands the lowest-cost node
first.
● Example: Finding the shortest or least expensive path in logistics and supply chain networks.

Works Well with Positive Edge Weights:

● UCS functions optimally when all weights are non-negative, ensuring that it consistently finds the minimum
path without backtracking.
● Example: In transportation networks where distances or costs cannot be negative.
When to Use UCS
Weighted Graphs with Variable Costs: UCS is particularly useful in graphs where
edge weights vary, as it ensures that the least-cost path is found.

Optimal Pathfinding Requirements: UCS is ideal when it’s crucial to find the
optimal (least-cost) solution, as in routing or logistics.

Non-Negative Weights: UCS is best suited for graphs with non-negative weights
since negative weights can lead to suboptimal paths.
Pros and Cons of UCS
Advantages Disadvantages

Optimal Path: Finds the minimum-cost path in Memory Intensive: Stores many nodes in the priority
weighted graphs queue, especially in dense graphs

Handles Variable Weights: Works well where edge Slow for Large Graphs: Performance can be slow for
weights differ large or dense graphs

No Backtracking Required: Guaranteed to find the Not Suitable for Negative Weights: Fails with
optimal path without needing to revisit nodes negative-weight edges
UCS Traversal Example in a Weighted Graph
Consider the following weighted graph as an example for UCS traversal:

Each edge in the graph has a weight, shown by the numbers on each line. We will use UCS to find the lowest-cost path from node
(0) to node (2).
Steps for UCS Traversal
The goal here is to find the minimum-cost path from node 0 to node 2.
Starting Node:
● Begin at node 0 with a path cost of 0.
● Add nodes reachable from node 0 to the priority queue based on their costs:
○ Path to node 1 with cost 3.
○ Path to node 4 with cost 8.
○ Path to node 3 with cost 7.
Expand Node with Lowest Cost:
● Expand node 1 (current cost 3), as it has the lowest cost in the queue.
● From node 1, add paths to reachable nodes:
○ Path to node 2 with cumulative cost 4 (3 + 1).
○ Path to node 3 with cumulative cost 7 (3 + 4).
Continue Expanding Lowest Cost Paths:
● Now, expand node 2 (current cost 4), as it has the lowest cumulative cost.
● Node 2 is our goal node, so we stop the search.
UCS Traversal Result: The minimum-cost path from node 0 to node 2 is 0 -> 1 -> 2 with a cost of 4.
UCS Traversal Explained
Starting Point: Begin at node 0, adding paths from node 0 to all directly connected
nodes in a priority queue ordered by cumulative path cost.

Expand Nodes by Lowest Cost: At each step, expand the node with the lowest
cumulative path cost in the queue. This ensures that the UCS algorithm always
explores the least expensive path available.

Goal Node Check: When node 2 (the goal) is reached, the path and cost are
returned as it is guaranteed to be the least-cost path due to UCS's priority on
minimum cumulative cost.

This approach ensures that UCS finds the minimum-cost path in a weighted graph by
prioritizing nodes with the least cumulative path cost at each step.
UCS Pseudo-Code
Here’s an example of UCS pseudo-code using a priority queue to manage nodes, ensuring it expands the node with the
lowest path cost at each step.

This code finds the minimum-cost path in a weighted graph by ensuring that only the least-cost paths are explored.
Real-World Applications of UCS
Road Network Routing

● Use: Find the cheapest or fastest route between locations.


● Example: GPS navigation uses UCS to find the route with the least travel time or cost.
● Benefit: Ensures optimal routes by considering variable distances, tolls, or traffic conditions.

Supply Chain and Logistics

● Use: Determine the least-cost route for delivering goods.


● Example: Shipping companies use UCS to minimize fuel and time in deliveries.
● Benefit: Reduces transportation costs by finding the optimal path for each delivery.
Real-World Applications of UCS
Network Packet Routing

● Use: Route packets through the internet with the lowest latency or cost.
● Example: Internet routing protocols can leverage UCS to find the most efficient paths.
● Benefit: Optimizes network speed by directing packets along the least-latency routes.

Resource Allocation in AI

● Use: Manage resources in simulations or game AI to ensure efficient usage.


● Example: AI systems use UCS to optimize moves based on cumulative costs.
● Benefit: Allows AI to make optimal decisions by balancing resource costs.
Summary
Definition: UCS finds the minimum-cost path in a weighted graph, exploring nodes in increasing order of cost.

Best Suited For:

● Weighted Pathfinding: Finds optimal paths in graphs with varying edge weights.
● Optimal Solution Requirements: Suitable for scenarios where the optimal (least-cost) solution is critical.
● Non-Negative Weights: Works best with non-negative weights, as it assumes positive cumulative path costs.

Applications:

● GPS and Road Routing: Optimal pathfinding for navigation.


● Logistics and Supply Chains: Cost-efficient pathfinding for deliveries.
● Internet Routing: Efficient data packet routing.
● AI Resource Management: Cost-sensitive decision-making in simulations and games.

You might also like