Module 3 and 4 Notes
Module 3 and 4 Notes
First convert the array into heap data structure using heapify, then one by
one delete the root node of the Max-heap and replace it with the last node
in the heap and then heapify the root of the heap. Repeat this process until
size of heap is greater than 1.
● Build a heap from the given input array.
● Repeat the following steps until the heap contains only one element:
● Swap the root element of the heap (which is the largest
element) with the last element of the heap.
● Remove the last element of the heap (which is now in the
correct position).
● Heapify the remaining elements of the heap.
● The sorted array is obtained by reversing the order of the elements in
the input array.
Build Complete Binary Tree: Build a complete binary tree from the array.
int largest = i;
// left = 2*i + 1
int l = 2 * i + 1;
// right = 2*i + 2
int r = 2 * i + 2;
largest = l;
// so far
largest = r;
// If largest is not root
if (largest != i) {
swap(arr[i], arr[largest]);
// sub-tree
heapify(arr, N, largest);
heapify(arr, N, i);
// from heap
swap(arr[0], arr[i]);
heapify(arr, i, 0);
Prims Algorithm
Spanning tree - A spanning tree is the subgraph of an undirected connected graph.
Minimum Spanning tree - Minimum spanning tree can be defined as the spanning tree
in which the sum of the weights of the edge is minimum. The weight of the spanning
tree is the sum of the weights given to the edges of the spanning tree.
Prim's Algorithm is a greedy algorithm that 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.
Prim's algorithm starts with the single node and explores 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.
Step 1 - First, we have to choose a vertex from the above graph. Let's choose B.
Step 2 - Now, we have to choose and add the shortest edge from vertex B. There are
two edges from vertex B that are B to C with weight 10 and edge B to D with weight 4.
Among the edges, the edge BD has the minimum weight. So, add it to the MST.
Step 3 - Now, again, choose the edge with the minimum weight among all the other
edges. In this case, the edges DE and CD are such edges. Add them to MST and explore
the adjacent of C, i.e., E and A. So, select the edge DE and add it to the MST.
Step 4 - Now, select the edge CD, and add it to the MST.
Step 5 - Now, choose the edge CA. Here, we cannot select the edge CE as it would
create a cycle to the graph. So, choose the edge CA and add it to the MST.
So, the graph produced in step 5 is the minimum spanning tree of the given graph. The
cost of the MST is given below -
ADVERTISEMENT
Algorithm
1. Step 1: Select a starting vertex
2. Step 2: Repeat Steps 3 and 4 until there are fringe vertices
3. Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that has minimum weight
4. Step 4: Add the selected edge and the vertex to the minimum spanning tree T
5. [END OF LOOP]
6. Step 5: EXIT
Data structure used for the minimum edge weight Time Complexity
Kruskals algorithm
Edge AB AC AD AE BC CD DE
Weight 1 7 10 5 3 4 2
Now, sort the edges given above in the ascending order of their weights.
Edge AB DE BC CD AE AC AD
Weight 1 2 3 4 5 7 10
Step 3 - Add the edge BC with weight 3 to the MST, as it is not creating any cycle or
loop.
Step 4 - Now, pick the edge CD with weight 4 to the MST, as it is not forming the cycle.
Step 5 - After that, pick the edge AE with weight 5. Including this edge will create the
cycle, so discard it.
Step 6 - Pick the edge AC with weight 7. Including this edge will create the cycle, so
discard it.
Step 7 - Pick the edge AD with weight 10. Including this edge will also create the cycle,
so discard it.
So, the final minimum spanning tree obtained from the given weighted graph by using
Kruskal's algorithm is -
The cost of the MST is = AB + DE + BC + CD = 1 + 2 + 3 + 4 = 10.
Now, the number of edges in the above tree equals the number of vertices minus 1. So,
the algorithm stops here.
Algorithm
1. Step 1: Create a forest F in such a way that every vertex of the graph is a separate tree.
2. Step 2: Create a set E that contains all the edges of the graph.
3. Step 3: Repeat Steps 4 and 5 while E is NOT EMPTY and F is not spanning
4. Step 4: Remove an edge from E with minimum weight
5. Step 5: IF the edge obtained in Step 4 connects two different trees, then add it to the forest F
6. (for combining two trees into one tree).
7. ELSE
8. Discard the edge
9. Step 6: END
A graph and source vertex are requirements for Dijkstra's Algorithm. This Algorithm is established on
Greedy Approach and thus finds the locally optimal choice (local minima in this case) at each step of
the Algorithm.
Dijikstras Algorithm
Each Vertex in this Algorithm will have two properties defined for it:
Visited Property
Path Property
Visited Property:
The 'visited' property signifies whether or not the node has been visited.
A node is marked visited only when the shortest path has been found.
Path Property:
The 'path' property stores the value of the current minimum path to the node.
The current minimum path implies the shortest way we have reached this node till now.
This property is significant because it will store the final answer for each node.
Initially, we mark all the vertices, or nodes, unvisited as they have yet to be visited. The path to all
the nodes is also set to infinity apart from the source node. Moreover, the path to the source node is
set to zero (0).
We then select the source node and mark it as visited. After that, we access all the neighboring
nodes of the source node and perform relaxation on every node. Relaxation is the process of
lowering the cost of reaching a node with the help of another node.
In the process of relaxation, the path of each node is revised to the minimum value amongst the
node's current path, the sum of the path to the previous node, and the path from the previous node
to the current node.
Let us suppose that p[n] is the value of the current path for node n, p[m] is the value of the path up
to the previously visited node m, and w is the weight of the edge between the current node and
previously visited one (edge weight between n and m).
We then mark an unvisited node with the least path as visited in every subsequent step and update
its neighbor's paths.
We repeat this procedure until all the nodes in the graph are marked visited.
Whenever we add a node to the visited set, the path to all its neighboring nodes also changes
accordingly.
If any node is left unreachable (disconnected component), its path remains 'infinity'. In case the
source itself is a separate component, then the path to all other nodes remains 'infinity'.
The following is the step that we will follow to implement Dijkstra's Algorithm:
Step 1: First, we will mark the source node with a current distance of 0 and set the rest of the nodes
to INFINITY.
Step 2: We will then set the unvisited node with the smallest current distance as the current node,
suppose X.
Step 3: For each neighbor N of the current node X: We will then add the current distance of X with
the weight of the edge joining X-N. If it is smaller than the current distance of N, set it as the new
current distance of N.
Step 5: We will repeat the process from 'Step 2' if there is any node unvisited left in the graph.
Let us now understand the implementation of the algorithm with the help of an example:
Dijkstra's Algorithm
We will use the above graph as the input, with node A as the source.
We will set the path to 0 at node A and INFINITY for all the other nodes.
We will now mark source node A as visited and access its neighboring nodes.
Note: We have only accessed the neighboring nodes, not visited them.
We will now update the path to node B by 4 with the help of relaxation because the path to node A is
0 and the path from node A to B is 4, and the minimum((0 + 4), INFINITY) is 4.
We will also update the path to node C by 5 with the help of relaxation because the path to node A is
0 and the path from node A to C is 5, and the minimum((0 + 5), INFINITY) is 5. Both the neighbors of
node A are now relaxed; therefore, we can move ahead.
We will now select the next unvisited node with the least path and visit it. Hence, we will visit node B
and perform relaxation on its unvisited neighbors. After performing relaxation, the path to node C
will remain 5, whereas the path to node E will become 11, and the path to node D will become 13.
We will now visit node E and perform relaxation on its neighboring nodes B, D, and F. Since only node
F is unvisited, it will be relaxed. Thus, the path to node B will remain as it is, i.e., 4, the path to node
D will also remain 13, and the path to node F will become 14 (8 + 6).
Now we will visit node D, and only node F will be relaxed. However, the path to node F will remain
unchanged, i.e., 14.
Since only node F is remaining, we will visit it but not perform any relaxation as all its neighboring
nodes are already visited.
Once all the nodes of the graphs are visited, the program will end.
B = 4 (A -> B)
C = 5 (A -> C)
D = 4 + 9 = 13 (A -> B -> D)
E = 5 + 3 = 8 (A -> C -> E)
Pseudocode:
// iterating through the nodes in Graph and set their distances to INFINITY
distance[N] = INFINITY
previous[N] = NULL
distance[source_node] = 0
mark Q visited
// iterating through the unvisited neighboring nodes of the node Q and performing relaxation
accordingly
// if the temporary distance is less than the given distance of the path to the Node, updating
the resultant distance with the minimum value
if temporary_distance < distance[N]
distance[N] := temporary_distance
previous[N] := Q
Explanation:
In the above pseudocode, we have defined a function that accepts multiple parameters - the Graph
consisting of the nodes and the source node. Inside this function, we have iterated through each
node in the Graph, set their initial distance to INFINITY, and set the previous node value to NULL. We
have also checked whether any selected node is not a source node and added the same into the
Priority Queue. Moreover, we have set the distance of the source node to 0. We then iterated
through the nodes in the priority queue, selected the node with the least distance, and marked it as
visited. We then iterated through the unvisited neighboring nodes of the selected node and
performed relaxation accordingly. At last, we have compared both the distances (original and
temporary distance) between the source node and the destination node, updated the resultant
distance with the minimum value and previous node information, and returned the final list of
distances with their previous node information.