Breadth First Search

BFS performs breadth first search on graph with source and target vectors.

You are now following this Submission

BFS performs breadth first search on graph with source and target
vectors.
Syntax:

[searchNodes,iterations] = bfs(source,target,names,startNode)
[searchNodes,iterations] = bfs(source,target,startNode)

Inputs:

source = Vector or cell array containing starting node of each of the edge.
target = Vector or cell array containing ending node of each of the edge.
names = Cell array containing string names of each of the node.
startNode = Initial node in the graph.

Outputs:

path = Cell array containing search path.
iterations = Table containing bfs iteration summary.

Example 01:

s = {'A','A','A','B','B','C'};
t = {'B','C','D','E','F','G'};
[searchNodes,iterations] = bfs(s,t,'A')

Example 02:

s = [1 1 1 2 2 3];
t = [2 3 4 5 6 7];
names = {'A','B','C','D','E','F','G'};
[searchNodes,iterations] = bfs(s,t,names,'A')

Example 03:

s = [1 1 1 2 2 3];
t = [2 3 4 5 6 7];
[searchNodes,iterations] = bfs(s,t,1)

Coded by Ali Asghar Manjotho
Lecturer, CSE-MUET
Email: ali.manjotho.ali@gmail.com

Cite As

Ali Asghar Manjotho (2026). Breadth First Search (https://www.mathworks.com/matlabcentral/fileexchange/58584-breadth-first-search), MATLAB Central File Exchange. Retrieved .

Categories

Find more on Graph and Network Algorithms in Help Center and MATLAB Answers

General Information

MATLAB Release Compatibility

  • Compatible with any release

Platform Compatibility

  • Windows
  • macOS
  • Linux
Version Published Release Notes Action
1.0.0.0