Searching Algorithms
Searching
Searching is a process of finding a particular element among several given
elements.
The search is successful if the required element is found.
Otherwise, the search is unsuccessful.
Linear Search-
Linear Search is the simplest searching algorithm.
It traverses the array sequentially to locate the required element.
It searches for an element by comparing it with each element of the array
one by one.
So, it is also called as Sequential Search.
Binary Search-
Binary Search is one of the fastest searching algorithms.
It is used for finding the location of an element.
It works on the principle of divide and conquer technique.
Binary Search Algorithm can be applied only on Sorted arrays.
Spanning Tree
A spanning tree is a subset of Graph G, which has all the vertices covered with
minimum possible number of edges. Hence, a spanning tree does not have cycles
and it cannot be disconnected.
We found three spanning trees off one complete graph. A complete undirected
graph can have maximum nn-2 number of spanning trees, where n is the number of
nodes. In the above addressed example, n is 3, hence 33−2 = 3 spanning trees are
possible.
Application of Spanning Tree
Spanning tree is basically used to find a minimum path to connect all nodes in a
graph. Common application of spanning trees are −
Civil Network Planning
Computer Network Routing Protocol
Cluster Analysis
Minimum Spanning Tree (MST)
In a weighted graph, a minimum spanning tree is a spanning tree that has minimum
weight than all other spanning trees of the same graph.
The possible spanning trees from the above graph are:
The minimum spanning tree from the above spanning trees is:
Minimum Spanning tree Applications
To find paths in the map
To design networks like telecommunication networks, water supply
networks, and electrical grids.
Prim’s Algorithm-
Prim’s Algorithm is a famous greedy algorithm.
It is used for finding the Minimum Spanning Tree (MST) of a given graph.
To apply Prim’s algorithm, the given graph must be weighted, connected
and undirected.
Step 1 - Remove all loops and parallel edges
Remove all loops and parallel edges from the given graph. In case of parallel
edges, keep the one which has the least cost associated and remove all others.
Step 2 - Choose any arbitrary node as root node
In this case, we choose S node as the root node of Prim's spanning tree. This node
is arbitrarily chosen, so any node can be the root node.
Step 3 - Check outgoing edges and select the one with less cost
Step-01:
Step-02:
Step-03:
Step-04:
Step-05:
Step-06:
Now, Cost of Minimum Spanning Tree
= Sum of all edge weights
= 10 + 25 + 22 + 12 + 16 + 14
= 99 units
Worst case time complexity of Prim’s Algorithm is-
O(ElogV) using binary heap
O(E + VlogV) using Fibonacci heap
Kruskal’s Algorithm-
Step-01:
Step-02:
Step-03:
Step-04:
Step-05:
Step-06:
Step-07:
Since all the vertices have been connected / included in the MST, so we stop.
Weight of the MST
= Sum of all edge weights
= 10 + 25 + 22 + 12 + 16 + 14
= 99 units
Worst case time complexity of Kruskal’s Algorithm is-
Worst case time complexity of Kruskal’s Algorithm = O(ElogV) or O(ElogE)
PRACTICE:
Using Prim’s Algorithm, find the cost of minimum spanning tree (MST) of the given
graph-
Using Kruskal’s Algorithm, find the cost of minimum spanning tree (MST) of the given
graph-