Graph Data Structure: Breadth First Search (BFS)
Graph Data Structure: Breadth First Search (BFS)
Graph Data Structure: Breadth First Search (BFS)
There are many ways to traverse graphs. BFS is the most commonly used approach.
BFS is a traversing algorithm where you should start traversing from a selected node (source or
starting node) and traverse the graph layerwise thus exploring the neighbour nodes (nodes which
are directly connected to source node). You must then move towards the next-level neighbour
nodes.
As the name BFS suggests, you are required to traverse the graph breadthwise as follows:
1. First move horizontally and visit all the nodes of the current layer
2. Move to the next layer
Depth-first search is like walking through a corn maze. You explore one path, hit a dead
end, and go back and try a different one.
Advantages:
Disadvantages
A DFS doesn't necessarily find the shortest path to a node, while breadth-first search
does.
Breadth First Search (BFS) Algorithm
o Step 1: SET STATUS = 1 (ready state)
for each node in G
o Step 2: Enqueue the starting node A
and set its STATUS = 2
(waiting state)
o Step 3: Repeat Steps 4 and 5 until
QUEUE is empty
o Step 4: Dequeue a node N. Process it
and set its STATUS = 3
(processed state).
o 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]
o Step 6: EXIT
o Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting
state)
o Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state)
o Step 5: Push on the stack all the neighbours of N that are in the ready state
(whose STATUS = 1) and set their
STATUS = 2 (waiting state)
[END OF LOOP]
o Step 6: EXIT
o Spanning Tree
o Spanning tree can be defined as a sub-graph of connected, undirected graph G
that is a tree produced by removing the desired number of edges from a graph.
In other words, Spanning tree is a non-cyclic sub-graph of a connected and
undirected graph G that connects all the vertices together. A graph G can have
multiple spanning trees.
There are two algorithms which are being used for this purpose.
o Prim's Algorithm
o Kruskal's Algorithm
o next →← prev
o Prim's Algorithm
o Prim's Algorithm is used to find the minimum spanning tree from a graph. Prim's
algorithm finds the subset of edges that includes every vertex of the graph such
that the sum of the weights of the edges can be minimized.
o Prim's algorithm starts with the single node and explore all the adjacent nodes
with all the connecting edges at every step. The edges with the minimal weights
causing no cycles in the graph got selected.
Algorithm
o Step 1: Select a starting vertex
o Step 2: Repeat Steps 3 and 4 until there are fringe vertices
o Step 3: Select an edge e connecting the tree vertex and fringe vertex that has
minimum weight
o Step 4: Add the selected edge and the vertex to the minimum spanning tree T
[END OF LOOP]
o Step 5: EXIT
Kruskal's Algorithm
Kruskal's Algorithm is used to find the minimum spanning tree for a connected
weighted graph. The main target of the algorithm is to find the subset of edges
by using which, we can traverse every vertex of the graph. Kruskal's algorithm
follows greedy approach which finds an optimum solution at every stage instead
of focusing on a global optimum.
Algorithm
o Step 1: Create a forest in such a way that each graph is a separate tree.
o Step 2: Create a priority queue Q that contains all the edges of the graph.
o Step 3: Repeat Steps 4 and 5 while Q is NOT EMPTY
o Step 4: Remove an edge from Q
o Step 5: IF the edge obtained in Step 4 connects two different trees, then Add it
to the forest (for combining two trees into one tree).
ELSE
Discard the edge
o Step 6: END
Solve the problem itself
Write the step also..
Greedy Algorithms
Greedy is an algorithmic paradigm that builds up a solution piece by piece, always
choosing the next piece that offers the most obvious and immediate benefit. So the
problems where choosing locally optimal also leads to global solution are best fit for
Greedy.