Unit 3
Unit 3
A spanning tree of a connected undirected graph is a subgraph, i.e., a tree structure that binds all
vertices with a minimum edge cost sum. If you have graph G with vertices V and edges E, then
that graph can be represented as G(V, E). For this graph G(V, E), if you construct a tree structure
G’(V’, E’) such that the formed tree structure follows constraints mentioned below, then that
structure can be called a Spanning Tree.
Let's create spanning trees for a given graph topology to understand this concept better.
ST - 1 22
ST – 2 35
ST – 3 36
The spanning tree structure 1 has an overall edge weight cost of 22. Which is the least possible
substructure cost for given graph G. Thus, ST-1 is considered the minimum spanning tree of
graph G. A minimum spanning tree in a weighted graph is one that has the least weight of all the
other spanning tree structures.
Step 2: Pick an edge connecting any tree vertex and fringe vertex (adjacent vertex to visited
vertex) having the minimum edge weight.
Step 3: Add the selected edge to MST only if it doesn't form any closed cycle.
Step 4: Keep repeating steps 2 and 3 until the fringe vertices exist.
Step 5: End.
Kruskal’s Algorithm
Kruskal's approach sorts all the edges in ascending order of edge weights and only adds nodes to
the tree if the chosen edge does not form a cycle. It also selects the edge with the lowest cost first
and the edge with the highest cost last. As a result, we can say that the Kruskal algorithm makes
a locally optimum decision in the hopes of finding the global optimal solution. Hence, this
algorithm can also be considered as a Greedy Algorithm.
The steps involved in Kruskal’s algorithm to generate a minimum spanning tree are:
Step 1: Sort all edges in increasing order of their edge weights.
Step 2: Pick the smallest edge.
Step 3: Check if the new edge creates a cycle or loop in a spanning tree.
Step 4: If it doesn’t form the cycle, then include that edge in MST. Otherwise, discard it.
Step 5: Repeat from step 2 until it includes |V| - 1 edges in MST.
Applications of Minimum Spanning Tree
The following are the applications of minimum spanning tree in data structures:
Telecommunication Network Building: A basic naive approach will be more expensive if we
develop a telecommunication network for the entire city. Using the MST approach in data
structures, we can design a communication system at a much lesser cost. The distinction between
Naive and MST routing is illustrated in the diagram below.
Constructing Highways or Railroads: The Minimum Spanning Tree (MST) technique is used
globally for building roadways or railroads. The MST approach determines the best route
between two cities depending on all potential routes. Essentially, the algorithm treats cities as
vertices and roads connecting them as edges to produce a subtree that connects two cities with
less cost.
Bellman-Ford Algorithm
Bellman-Ford is a single source shortest path algorithm that determines the shortest path between
a given source vertex and every other vertex in a graph. This algorithm can be used on both
weighted and unweighted graphs.
A Bellman-Ford algorithm is also guaranteed to find the shortest path in a graph, similar
to Dijkstra’s algorithm . Although Bellman-Ford is slower than Dijkstra’s algorithm, it is capable
of handling graphs with negative edge weights, which makes it more versatile. The shortest
path cannot be found if there exists a negative cycle in the graph. If we continue to go around the
negative cycle an infinite number of times, then the cost of the path will continue to decrease
(even though the length of the path is increasing). As a result, Bellman-Ford is also capable of
detecting negative cycles, which is an important feature.
Why would one ever have edges with negative weights in real life?
Negative weight edges might seem useless at first but they can explain a lot of phenomena like
cashflow, the heat released/absorbed in a chemical reaction, etc.
For instance, if there are different ways to reach from one chemical A to another chemical B,
each method will have sub-reactions involving both heat dissipation and absorption.
If we want to find the set of reactions where minimum energy is required, then we will need to
be able to factor in the heat absorption as negative weights and heat dissipation as positive
weights.
Shortest path algorithms like Dijkstra's Algorithm that aren't able to detect such a cycle can give
an incorrect result because they can go through a negative weight cycle and reduce the path
length.
How Bellman Ford's algorithm works
Bellman Ford algorithm works by overestimating the length of the path from the starting vertex
to all other vertices. Then it iteratively relaxes those estimates by finding new paths that are
shorter than the previously overestimated paths.
Single Source Shortest Path in a directed Acyclic Graphs
topological sort of its vertices, we can figure out shortest paths from a single source in ∅(V+E)
By relaxing the edges of a weighted DAG (Directed Acyclic Graph) G = (V, E) according to a
time. Shortest paths are always well described in a dag, since even if there are negative-weight
edges, no negative-weight cycles can exist.
DAG - SHORTEST - PATHS (G, w, s)
1. Topologically sort the vertices of G.
2. INITIALIZE - SINGLE- SOURCE (G, s)
3. for each vertex u taken in topologically sorted order
4. do for each vertex v ∈ Adj [u]
5. do RELAX (u, v, w)
Dijkstra's algorithm, there is one repetition per vertex. For each vertex, the edges that leave the
edge. The running time is thus ∅ (V + E), which is linear in the size of an adjacency list
vertex are each examined exactly once. Unlike Dijkstra's algorithm, we use only O (1) time per
Now, take each vertex in topologically sorted order and relax each edge.
adj [t] → r, x
3+1<∞
d [r] ← 4
3+5≤2
adj [x] → y
2-3<∞
d [y] ← -1
adj [y] → r
-1 + 4 < 4
3 <4
d [r] ← 3
Thus the Shortest Path is:
1. s to x is 2
2. s to y is -1
3. s to t is 3
4. s to r is 3 .
Examples: The specific computer algorithms are based on the Divide & Conquer approach:
1. Maximum and Minimum Problem
2. Binary Search
3. Sorting (merge sort, quick sort)
4. Tower of Hanoi.
Max - Min Problem
Problem: Analyze the algorithm to find the maximum and minimum element from an array.
Method 1: if we apply the general approach to the array of size n, the number of comparisons
required are 2n-2.
Method-2: In another approach, we will divide the problem into sub-problems and find the max
and min of each group, now max. Of each group will compare with the only max of another
group and min with min.
Let T (n) = time required to apply the algorithm on an array of size n. Here we divide the terms
as T(n/2).
2 here tends to the comparison of the minimum with minimum and maximum with maximum as
in above example.
T (n) = 2 T → Eq (i)
T (2) = 1, time required to compare two elements/items. (Time is measured in units of the
number of comparisons)
→ Eq (ii)
Put eq (ii) in eq (i)
{Use recursion means, we will use some stopping condition to stop the algorithm}
elements/items =
Number of comparisons requires applying general approach on n elements = (n-1) + (n-1) = 2n-2
From this example, we can analyze, that how to reduce the number of comparisons by using this
technique.
Binary Search
1. In Binary Search technique, we search an element in a sorted array by recursively dividing the
interval in half.
3. If the Pivot Element (the item to be searched) is less than the item in the middle of the interval,
We discard the second half of the list and recursively repeat the process for the first half of the
list by calculating the new middle and last element.
4. If the Pivot Element (the item to be searched) is greater than the item in the middle of the
interval, we discard the first half of the list and work recursively on the second half by
calculating the new beginning and middle element.
Analysis:
Input: an array A of size n, already sorted in the ascending or descending order.
Output: analyze to search an element item in the sorted array of size n.
Logic: Let T (n) = number of comparisons of an item with n elements in a sorted array.
o Find mid =
o Compare the search item with the mid item.
Case 1: item = A[mid], then LOC = mid, but it the best case and T (n) = 1
Case 2: item ≠A [mid], then we will split the array into two equal parts of size .
And again find the midpoint of the half-sorted array and compare with search element.
{Time to compare the search element with mid element, then with half of the selected half part
of array}
At least there will be only one term left that's why that term will compare
Quick Sort
Introduction
Quick Sort is one of the most popular and efficient sorting algorithms. It is generally the default
sorting algorithms in many programming languages (including C++ and Java).
Even though the worst case time complexity of Quick Sort is O(n^2), it works at O(n log n) in
most cases and is generally much faster than merge sort if implemented properly.
Quicksort is a divide-and-conquer algorithm. It works by selecting a 'pivot' element from the
array and partitioning the other elements into two sub-arrays, according to whether they are less
than or greater than the pivot. The sub-arrays are then sorted recursively.
Algorithm
Pick an element, called a pivot, from the array.
Partitioning: reorder the array so that all elements with values less than the pivot come before the
pivot, while all elements with values greater than the pivot come after it (equal values can go
either way). After this partitioning, the pivot is in its final position. This is called the partition
operation.
Recursively apply the above steps to the sub-array of elements with smaller values and
separately to the sub-array of elements with greater values.
The base case of the recursion is arrays of size zero or one, which are in order by definition, so
they never need to be sorted.
There are multiple variants of quick sort depending on the choice of pivot. Some popular choices
of pivots being:
First element of the unsorted array
Last element of the unsorted array
Middle element of the unsorted array
Random element from the unsorted array
The choice of pivot determines the chances of the algorithm hitting the worst case for the given
array.
Example
Merge Sort
Introduction
Merge Sort is also a Divide and Conquer algorithm similar to Quick Sort. The input array right
into two parts in the middle. The two haves are then sorted recursively and then merged to create
the sorted array.
Algorithm
The algorithm looks something like this:
Sort the left half of the array
Sort the right half of the array
Merge both the halves of the array
The base case of the recursion is arrays of size zero or one, which are in order by definition, so
they never need to be sorted.
Example
Strassen’s Matrix Multiplication is the divide and conquer approach to solve the matrix
multiplication problems. The usual matrix multiplication method multiplies each row with each
column to achieve the product matrix. The time complexity taken by this approach is O(n3),
since it takes two loops to multiply. Strassen’s method was introduced to reduce the time
complexity from O(n3) to O(nlog 7)
Procedure of Strassen matrix multiplication
There are some procedures:
Divide a matrix of order of 2*2 recursively till we get the matrix of 2*2.
Use the previous set of formulas to carry out 2*2 matrix multiplication.
In this eight multiplication and four additions, subtraction are performed.
Combine the result of two matrixes to find the final product or final matrix.
Formulas for Stassen's matrix multiplication
In Strassen's matrix multiplication there are seven multiplication and four addition, subtraction in
total.
1. D1 = (a11 + a22) (b11 + b22)
2. D2 = (a21 + a22).b11
3. D3 = (b12 – b22).a11
4. D4 = (b21 – b11).a22
5. D5 = (a11 + a12).b22
6. D6 = (a21 – a11) . (b11 + b12)
7. D7 = (a12 – a22) . (b21 + b22)
C11 = d1 + d4 – d5 + d7
C12 = d3 + d5
C21 = d2 + d4
C22 = d1 + d3 – d2 – d6
Algorithm for Strassen's matrix multiplication
Algorithm Strassen(n, a, b, d)
begin
If n = threshold then compute
C = a * b is a conventional matrix.
Else
Partition a into four sub matrices a11, a12, a21, a22.
Partition b into four sub matrices b11, b12, b21, b22.
Strassen ( n/2, a11 + a22, b11 + b22, d1)
Strassen ( n/2, a21 + a22, b11, d2)
Strassen ( n/2, a11, b12 – b22, d3)
Strassen ( n/2, a22, b21 – b11, d4)
Strassen ( n/2, a11 + a12, b22, d5)
Strassen (n/2, a21 – a11, b11 + b22, d6)
Strassen (n/2, a12 – a22, b21 + b22, d7)
C = d1+d4-d5+d7 d3+d5
d2+d4 d1+d3-d2-d6
end if
return (C)
end.