Module 2 Lecture 2
Module 2 Lecture 2
01/24/2025 BMEE407L-Module 2 1
Reachability, connectedness
• Reachable: Vertex a is reachable from b V
a b
if a path exists from a to b.
d
U X Z
h
• Connected: A graph is connected if every c e
vertex is reachable from any other. W g
– Is the graph at top right connected? f
Y
• Strongly connected: When every vertex
has an edge to every other vertex.
a b
a b d
c
c d e
01/24/2025 BMEE407L-Module 2 2
Loops and cycles
• Cycle: A path that begins and ends at the same
node.
– example: {b, g, f, c, a} or {V, X, Y, W, U, V}.
– example: {c, d, a} or {U, W, V, U}.
– acyclic graph: One that does V
a b
not contain any cycles. d
U X Z
h
c e
• Loop: An edge directly from W g
a node to itself. f
Y
– Many graphs don't allow loops.
01/24/2025 BMEE407L-Module 2 3
Directed graphs
• Directed graph ("digraph"): One where edges are
one-way connections between vertices.
– If graph is directed, a vertex has a separate in/out
degree.
– A digraph can be weighted or unweighted.
– Is the graph below connected? Why or why not?
a b
c d e
f g
01/24/2025 BMEE407L-Module 2 4
Directed Acyclic Graph
• DAG is a type of graph structure
• It consists of vertex/nodes connected by edges
• Each edge has a direction
• There are no cycles
01/24/2025 BMEE407L-Module 2 5
DAG Components
1.Directed Graph:
In a directed graph, also known as a digraph, edges
have a specific direction. Each edge connects two
nodes, and the direction indicates a one-way
relationship from one node (the "tail") to another
(the "head").
2.Acyclic:
"Acyclic" means that the graph does not contain any
cycles. A cycle is a path that starts and ends at the
same node, traversing one or more edges. In other
words, you cannot follow the directed edges in a
loop and return to the same node in a DAG.
01/24/2025 BMEE407L-Module 2 6
Linked Lists, Trees, Graphs
• A binary tree is a graph with some restrictions:
– The tree is an unweighted, directed, acyclic graph (DAG).
– Each node's in-degree is at most 1, and out-degree is at most 2.
– There is exactly one path from the root to every node.
F
• A linked list is also a graph:
– Unweighted DAG.
– In/out degree of at most 1 for all nodes. B K
A B C D A E H
G J
01/24/2025 BMEE407L-Module 2 7
Searching for paths
• Searching for a path from one vertex to another:
– Sometimes, we just want any path (or want to know there is a
path).
– Sometimes, we want to minimize path length (# of edges).
– Sometimes, we want to minimize path cost (sum of edge weights).
What is the shortest path from MIA to SFO?
Which path has the minimum cost?
$50 PVD
$70 ORD
SFO
$2
$130
00
170 $80 LGA
$
$60
$250 $140
HNL $120 00
LAX DFW $110 $1
$500 MIA
01/24/2025 BMEE407L-Module 2 8
UNINFORMED SEARCH
Uninformed search is a class of general-purpose search
algorithms that operates in a brute-force way. Uninformed
search algorithms do not have additional information about the
state or search space other than how to traverse the tree, so it is
also called blind search.
Uninformed search algorithms in AI do not contain any extra
data about the goal node. They rely solely on the information
provided when the problem is defined. The paths to reach the
goal state from the start state vary based on the sequence and
length of the actions performed.
01/24/2025 BMEE407L-Module 2 9
UNINFORMED SEARCH
Key Characteristics of Uninformed Search in AI:
01/24/2025 BMEE407L-Module 2 10
INFORMED SEARCH
Informed search algorithms in AI are equipped with information
about the goal state, which enables the AI to conduct more
efficient and accurate searches. This information is used by a
function to estimate the proximity of a state to its goal within
the system.
1. A* Search
2. Best-First Search
3. Heuristic search
4. Greedy best-first search
01/24/2025 BMEE407L-Module 2 11
INFORMED SEARCH
01/24/2025 BMEE407L-Module 2 12
INFORMED SEARCH
01/24/2025 BMEE407L-Module 2 13
INFORMED SEARCH
01/24/2025 BMEE407L-Module 2 14
INFORMED SEARCH
01/24/2025 BMEE407L-Module 2 15
BREADTH-FIRST SEARCH
01/24/2025 BMEE407L-Module 2 16
1. Breadth-first Search:
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.
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.
The breadth-first search algorithm is an example of a general-
graph search algorithm.
Breadth-first search implemented using FIFO queue data
structure.
Advantages:
BFS will provide a solution if any solution exists.
If there are more than one solutions for a given problem, then
BFS will provide the minimal solution which requires the least
number of steps.
01/24/2025 BMEE407L-Module 2 17
1. Breadth-first Search:
Disadvantages:
It requires lots of memory since each level of the tree must be saved in
memory to expand to the next level.
BFS needs lots of time if the solution is far away from the root node.
Applications:
BFS can be used to find the neighboring locations from a given source
location.
In a peer-to-peer network, the 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.
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
01/24/2025 BMEE407L-Module 2 18
1. Breadth-first Search:
Applications:
BFS is used to determine the shortest path and minimum
spanning tree.
BFS is also used in Cheney's technique to duplicate the garbage
collection.
It can be used in ford-Fulkerson method to compute the
maximum flow in a flow network.
Example
In the below tree structure, we have shown the traversing of the
tree using the BFS algorithm from the root node S to goal node
K. BFS search algorithm traverse in layers, so it will follow the
path which is shown by the dotted arrow, and the traversed
path will be:
S---> A--->B---->C--->D---->G--->H--->E---->F---->I---->K
01/24/2025 BMEE407L-Module 2 19
1. Breadth-first Search:
Example
S---> A--->B---->C--->D---->G--->H--->E---->F---->I---->K
Graph Traversal
The process of visiting and
exploring a graph for
processing is called graph
traversal. To be more
specific it is all about
visiting and exploring each
vertex and edge in a graph
such that all the vertices
are explored exactly once.
01/24/2025 BMEE407L-Module 2 20
1. Breadth-first Search:
Applications of BFS algorithm
The applications of breadth-first-algorithm are given as follows –
BFS can be used to find the neighboring locations from a given source
location.
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.
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.
BFS is used to determine the shortest path and minimum spanning tree.
BFS is also used in Cheney's technique to duplicate the garbage collection.
It can be used in ford-Fulkerson method to compute the maximum flow in a
flow network.
01/24/2025 BMEE407L-Module 2 21
1. Breadth-first Search:
Need for the Breadth First Search Algorithm
There are several reasons why using the BFS algorithm is essential:
1. Shortest Path Finding: BFS guarantees the shortest path between two
vertices in an unweighted graph, making it an ideal choice for route planning
or navigation systems.
2. Completeness: BFS is complete for finite graphs, ensuring it explores the
entire graph and visits all reachable vertices.
3. Breadth-First Traversal: BFS explores vertices at the same level before
moving to deeper levels, providing a breadth-first exploration of the graph.
4. Minimal Memory Usage: BFS uses minimal memory compared to other
graph traversal algorithms like DFS, as it only needs to store vertices in the
queue.
5. Optimal Solution and Accuracy: In unweighted graphs, BFS ensures that the
first occurrence of a target vertex will yield the shortest path, making it
efficient for searching tasks.
Programmers often use breadth first search Python for various applications in
graph theory and data structures.
01/24/2025 BMEE407L-Module 2 22
1. Breadth-first Search: BFS Algorithm Rules
Some important rules to remember when implementing the breadth first search
algorithm for graph traversal:
1. Queue: Use a queue data structure to keep track of the vertices to be visited in
the breadth first traversal.
2. Visited Marking: Maintain a visited array or set to keep track of the vertices
that have been visited during the traversal.
3. Enqueue and Mark: Enqueue the starting vertex into the queue and mark it
as visited.
4. Dequeue and Visit Neighbors: While the queue is not empty, dequeue a
vertex, visit it, and enqueue its unvisited neighbouring vertices.
5. Order of Visit: Visit the vertices in the order they were enqueued, ensuring a
breadth-first exploration.
6. Avoid Revisiting: Check if a vertex has already been visited before
enqueueing it to avoid revisiting vertices.
7. Termination: Terminate the algorithm when the queue becomes empty,
indicating that all reachable vertices have been visited.
8. Shortest Path: If finding the shortest path, track the parent of each vertex to
reconstruct the path once the destination is reached.
01/24/2025 BMEE407L-Module 2 23
1. Breadth-first Search: BFS Algorithm Architecture
The architecture of the BFS algorithm is broken down below:
1. Queue: The BFS algorithm uses a queue data structure to maintain an orderly process
when exploring vertex positions. The queue follows the First-In, First-Out (FIFO)
principle to ensure that vertices are processed in their order of arrival in the queue.
2. Visited Array or Set: A visited array or set tracks the vertices visited during BFS
traversal and ensures that each vertex is processed only once. It ensures no revisited
vertices occur while processing each vertex correctly only once.
3. BFS Tree: As the BFS algorithm explores the graph, it constructs a BFS tree. The BFS
tree represents the traversal path and reveals the hierarchical relationships between
vertices. Each vertex in the tree has its parent, the vertex discovered during the
traversal.
4. Enqueue and Mark: The BFS algorithm starts by enqueueing the source vertex into
the queue and marking it as visited.
5. Dequeue and Visit Neighbours: While the queue is not empty, the algorithm
dequeues a vertex, visits it, and explores its neighbouring vertices. Each unvisited
neighbour is enqueued into the queue and marked as visited.
6. Termination: The BFS algorithm terminates when the queue becomes empty. This
indicates that all reachable vertices have been visited and processed.
01/24/2025 BMEE407L-Module 2 24
Example
1
/\
2 3
/\ \
4 5 6
/\
7 8
Step 1: Initialization
Start with the root node (1) and enqueue it in the BFS queue.
Queue: 1
01/24/2025 BMEE407L-Module 2 25
Step 2: Explore Nodes at Depth 1
Dequeue the first node (1) and enqueue its children (2, 3).
Queue: 2 3
Dequeue the next node (2) and enqueue its children (4, 5).
Queue: 3 4 5
Dequeue the next node (3) and enqueue its child (6).
Queue: 4 5 6
01/24/2025 BMEE407L-Module 2 26
Step 5: Explore Nodes at Depth 4
Dequeue the next node (4) and enqueue its children (7, 8).
Queue: 5 6 7 8
Queue: 6 7 8
Queue: 7 8
01/24/2025 BMEE407L-Module 2 27
Step 8: Explore Nodes at Depth 7
Queue: 8
Queue:
01/24/2025 BMEE407L-Module 2 28
EXAMPLE-2
01/24/2025 BMEE407L-Module 2 29
EXAMPLE-3
01/24/2025 BMEE407L-Module 2 30
EXAMPLE-4
Solve the given graph using BFS method
01/24/2025 BMEE407L-Module 2 31
THANK YOU
01/24/2025 BMEE407L-Module 2 32