Depth-first search (DFS) and breadth-first search (BFS) are algorithms for traversing or searching trees and graphs. DFS uses a stack and recursively explores as far as possible along each branch before backtracking, while BFS uses a queue and explores neighboring nodes first before moving to the next level. A minimum spanning tree (MST) is a subgraph that connects all vertices with minimum total edge weight. Common algorithms to find an MST are Kruskal's algorithm, which adds edges in order of weight, and Prim's algorithm, which grows the tree from an initial vertex.
2. • Traversal means visiting all the nodes of a graph. Depth first traversal
or Depth first Search is a recursive algorithm for searching all the
vertices of a graph or tree data structure.
3.
4. DFS algorithm
A standard DFS implementation puts each vertex of the graph into one of two categories:
• Visited
• Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.
The DFS algorithm works as follows:
• Start by putting any one of the graph's vertices on top of a stack.
• Take the top item of the stack and add it to the visited list.
• Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list
to the top of stack.
• Keep repeating steps 2 and 3 until the stack is empty.
5. Breadth first search
• Breadth first traversal or Breadth first Search is a recursive algorithm
for searching all the vertices of a graph or tree data structure.
6.
7.
8.
9. BFS algorithm
A standard DFS implementation puts each vertex of the graph into one of two
categories:
• Visited
• Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding
cycles.
The algorithm works as follows:
• Start by putting any one of the graph's vertices at the back of a queue.
• Take the front item of the queue and add it to the visited list.
• Create a list of that vertex's adjacent nodes. Add the ones which aren't in the
visited list to the back of the queue.
• Keep repeating steps 2 and 3 until the queue is empty.
10. What is a Spanning Tree?
• Given an undirected and connected graph G=(V,E), a spanning tree of the graph G is a
tree that spans G (that is, it includes every vertex of G) and is a subgraph of G (every
edge in the tree belongs to G)
• What is a Minimum Spanning Tree?
The cost of the spanning tree is the sum of the weights of all the edges in the tree. There
can be many spanning trees. Minimum spanning tree is the spanning tree where the cost is
minimum among all the spanning trees. There also can be many minimum spanning trees.
Minimum spanning tree has direct application in the design of networks. It is used in
algorithms approximating the travelling salesman problem, multi-terminal minimum cut
problem and minimum-cost weighted perfect matching. Other practical applications are:
• Cluster Analysis
• Handwriting recognition
• Image segmentation
11.
12. There are two famous algorithms for finding the Minimum Spanning Tree:
Kruskal’s Algorithm
• Kruskal’s Algorithm builds the spanning tree by adding edges one by one
into a growing spanning tree. Kruskal's algorithm follows greedy approach
as in each iteration it finds an edge which has least weight and add it to the
growing spanning tree.
Algorithm Steps:
• Sort the graph edges with respect to their weights.
• Start adding edges to the MST from the edge with the smallest weight until
the edge of the largest weight.
• Only add edges which doesn't form a cycle , edges which connect only
disconnected components.
13.
14.
15.
16.
17. • Time Complexity:
In Kruskal’s algorithm, most time consuming operation is sorting
because the total complexity of the Disjoint-Set operations will
be O(ElogV), which is the overall Time Complexity of the algorithm.
18. Prim’s Algorithm
Prim’s Algorithm also use Greedy approach to find the minimum spanning tree. In
Prim’s Algorithm we grow the spanning tree from a starting position. Unlike
an edge in Kruskal's, we add vertex to the growing spanning tree in Prim's.
Algorithm Steps:
• 1) Create a set mstSet that keeps track of vertices already included in MST.
2) Assign a key value to all vertices in the input graph. Initialize all key values as
INFINITE. Assign key value as 0 for the first vertex so that it is picked first.
3) While mstSet doesn’t include all vertices
….a) Pick a vertex u which is not there in mstSet and has minimum key value.
….b) Include u to mstSet.
….c) Update key value of all adjacent vertices of u. To update the key values,
iterate through all adjacent vertices. For every adjacent vertex v, if weight of
edge u-v is less than the previous key value of v, update the key value as weight
of u-v
19. • Pick the vertex with minimum key value and not already included in
MST (not in mstSET). The vertex 1 is picked and added to mstSet. So
mstSet now becomes {0, 1}. Update the key values of adjacent
vertices of 1. The key value of vertex 2 becomes 8.
20. • Pick the vertex with minimum key value and not already included in
MST (not in mstSET). We can either pick vertex 7 or vertex 2, let
vertex 7 is picked. So mstSet now becomes {0, 1, 7}. Update the key
values of adjacent vertices of 7. The key value of vertex 6 and 8
becomes finite (1 and 7 respectively).
21. • Pick the vertex with minimum key value and not already included in
MST (not in mstSET). Vertex 6 is picked. So mstSet now becomes {0, 1,
7, 6}. Update the key values of adjacent vertices of 6. The key value of
vertex 5 and 8 are updated.
22. • We repeat the above steps until mstSet includes all vertices of given
graph. Finally, we get the following graph.