0% found this document useful (0 votes)
5 views13 pages

DAA

Data structure and analysis

Uploaded by

iamsapnakumari26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views13 pages

DAA

Data structure and analysis

Uploaded by

iamsapnakumari26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Asymptotic notations are the mathematical Solving Recurrence relations using Master’s Average Case Analysis of QuickSort MergeSort

Analysis of QuickSort MergeSort Algorithm Strassen’s Matrix Multiplication


notations used to describe the running time of Theorem Let the elements of array are - function mergeSort(arr): The idea of Strassen’s method is to reduce the
an algorithm when the input tends towards a if length(arr) <= 1: number of recursive calls to 7. Strassen’s
particular value or a limiting value. return arr method is similar to above simple divide and
There are mainly three asymptotic notations: conquer method in the sense that this method
In the given array, we consider the leftmost element as // Split the array into two halves also divide matrices to sub-matrices of size
• Big-O notation pivot. So, in this case, a[left] = 24, a[right] = 27 and a[pivot] N/2 x N/2 as shown in the above diagram, but
= 24.
• Omega notation Since, pivot is at left, so algorithm starts from right and
middle = length(arr) / 2 in Strassen’s method, the four sub-matrices of
• Theta notation move towards left. left = arr[0 to middle - 1] result are calculated using following formulae.
right = arr[middle to end]
Big-O Notation (O-notation)
Big-O notation represents the upper bound of // Recursively sort both halves
the running time of an algorithm. Thus, it gives
the worst-case complexity of an algorithm. left = mergeSort(left)
Now, a[pivot] < a[right], so algorithm moves forward one
position towards left, i.e. -
right = mergeSort(right)

// Merge the sorted halves

result = merge(left, right)


return result
Use this formula to solve the given recurrence Now, a[left] = 24, a[right] = 19, and a[pivot] = 24.Because, Time Complexity of Strassen’s Method
relation. a[pivot] > a[right], so, algorithm will swap a[pivot] with function merge(left, right):
a[right], and pivot moves to right, as - result = []
Addition and Subtraction of two matrices
Quick Sort Algorithm leftIndex = 0
Omega Notation (Ω-notation) takes O(N2) time. So time complexity can be
function quickSort(arr, low, high): rightIndex = 0
Omega notation represents the lower bound written as
if low < high:
of the running time of an algorithm. Thus, it // Compare elements from left and right
pivotIndex = partition(arr, low, high) T(N) = 7T(N/2) + O(N2)
provides the best case complexity of an subarrays and merge them into result
algorithm. Now, a[left] = 19, a[right] = 24, and a[pivot] = 24. Since,
quickSort(arr, low, pivotIndex - 1) pivot is at right, so algorithm starts from left and moves to From Master's Theorem, time complexity of
quickSort(arr, pivotIndex + 1, high) right.As a[pivot] > a[left], so algorithm moves one position while leftIndex < length(left) and rightIndex
above method is
to right as - < length(right):
O(NLog27) which is approximately O(N2.81).
function partition(arr, low, high): if left[leftIndex] < right[rightIndex]:
pivot = arr[high] result.append(left[leftIndex])
N-Queens Problem
i = low - 1 leftIndex++
Bounding Function = Not same
for j = low to high - 1: else:
Now, a[left] = 9, a[right] = 24, and a[pivot] = 24. As a[pivot] (Row,Column,Diagonal)
result.append(right[rightIndex])
> a[left], so algorithm moves one position to right as -
if arr[j] <= pivot: rightIndex++
Psuedocode
procedure solveNQueens(N):
Theta Notation (Θ-notation) i=i+1 // Append remaining elements (if any) from
create an empty N×N chessboard
Theta notation encloses the function from swap(arr[i], arr[j]) left and right subarrays
call the solve(0, N, chessboard) function
above and below. Since it represents the swap(arr[i + 1], arr[high]
upper and the lower bound of the running return i + 1 result.append(left[leftIndex to end])
Now, a[left] = 29, a[right] = 24, and a[pivot] = 24. As procedure solve(row, N, chessboard):
time of an algorithm, it is used for analysing Worst Case time complexity analysis of a[pivot] < a[left], so, swap a[pivot] and a[left], now pivot is
result.append(right[rightIndex to end])
if row equals N:
the average-case complexity of an algorithm. QuickSort at left, i.e. -
// All queens are placed successfully, print
Let’s assume that we choose a pivot element in such a way return result
that it gives the most unbalanced partitions possible: the solution
print chessboard
Analysis of Merge Sort
return
Here, the array is divided into two smaller
for each column in 0 to N-1:
Since, pivot is at left, so algorithm starts from right, and arrays
move to left. Now, a[left] = 24, a[right] = 29, and a[pivot]
if isSafe(row, column, N, chessboard):
If the size of array is N then the smaller arrays
= 24. As a[pivot] < a[right], so algorithm moves one // Place a queen at (row, column)
are of size N/2.
position to left, as - chessboard[row][column] = 'Q'
Here, at each level we have to merge N
// Recur to place the queens in the next
elements hence it takes N time to merge
row
them.
Various properties of Asymptotic Notations solve(row + 1, N, chessboard)
1. Big O – Reflexive and transitive All the numbers in the box denote the size of the arrays or the
T(N)=2T(N/2)+ N= O(NlogN)
2. Big Omega – Reflexive and transitive subarrays.
3. Theta – Reflexive, Symmetric and transitive Let’s consider an input array of size n. The first partition call Now, a[pivot] = 24, a[left] = 24, and a[right] = 14. As // Backtrack and remove the queen
takes n times to perform the partition step on the input array. a[pivot] > a[right], so, swap a[pivot] and a[right], now pivot
4. Small o – transitive from (row, column)
is at right, i.e. - Backtracking - Backtracking name itself
5. small omega – transitive Each partition step is invoked recursively from the previous chessboard[row][column] = '.'
one. Given that, we can take the complexity of each partition
suggests that we are going back and coming
call and sum them up to get our total complexity of the forward; if it satisfies the condition, then return
Solving Recurrence Relations using function isSafe(row, col, N, chessboard):
Quicksort algorithm. success, else we go back again. It is used to
Substitution Method // Check if no queens threaten the current
solve a problem in which a sequence of objects
Therefore, the time complexity of the Quicksort position (row, col)
algorithm in worst case is
is chosen from a specified set so that the
sequence satisfies some criteria. Backtracking
// Check the column
T(n) =[N+(N-1)+(N-2)+….+2]=[(N(N+1))/2]= O(N2) Now, a[pivot] = 24, a[left] = 14, and a[right] = 24. Pivot is
is a systematic method of trying out various
for i in 0 to row - 1:
at right, so the algorithm starts from left and move to right. sequences of decisions until you find out that
Linear Time Selection Algorithm if chessboard[i][col] == 'Q':
works.
The linear time selection algorithm, often referred to as the return false
"QuickSelect" algorithm, is a method for finding the k-th
smallest (or largest) element in an unsorted list or array. It is a Applications of Backtracking // Check the left diagonal
variation of the QuickSort algorithm and has an average-case
time complexity of O(n) where n is the number of elements in • N-queen problem for i, j in (row-1, col-1) to (0, 0):
the list. However, in the worst case, it can have a time • Sum of subset problem if i >= 0 and j >= 0 and chessboard[i][j] ==
complexity of O(n^2), although such cases are rare and can be • Graph colouring 'Q':
minimized with certain optimizations.
Now, a[pivot] = 24, a[left] = 24, and a[right] = 24. So, pivot, • Hamiliton cycle return false
left and right are pointing the same element. It represents
Here's a high-level description of the linear time selection
algorithm: the termination of procedure.
Element 24, which is the pivot element is placed at its exact // Check the right diagonal
Partitioning: position. Difference between the Backtracking and for i, j in (row-1, col+1) to (0, N-1):
1. Select a "pivot" element from the list. The choice of pivot Elements that are right side of element 24 are greater than Recursion if i >= 0 and j < N and chessboard[i][j] ==
can affect the algorithm's efficiency. it, and the elements that are left side of element 24 are 'Q':
Recursion is a technique that calls the same
2. Rearrange the elements of the list so that all elements smaller than it.
smaller than the pivot are on its left, and all elements greater function again and again until you reach the return false
than the pivot are on its right. The pivot is now in its final base case. Backtracking is an algorithm that
sorted position. return true
finds all the possible solutions and selects the
3. Count the number of elements that are to the left of the
pivot. This count is called the "rank" of the pivot. desired solution from the given set of
solutions. In this algorithm:
Recursive Selection: Now, in a similar manner, quick sort algorithm is separately
1. If the rank of the pivot is equal to k, the pivot is the k-th applied to the left and right sub-arrays. After sorting gets
When to use a Backtracking algorithm? • We start by initializing an empty
smallest element, and you can return it. done, the array will be - chessboard of size N×N.
2. If the rank is greater than k, recursively apply the algorithm When we have multiple choices, then we
to the left subarray (i.e., the elements smaller than the pivot). make the decisions from the available choices. • We call the solve(0, N, chessboard)
3. If the rank is less than k, recursively apply the algorithm to
the right subarray (i.e., the elements greater than the pivot). In the following cases, we need to use the function to begin the backtracking
Hence here the array is being divided into two smaller arrays backtracking algorithm: process, starting from the first row (row
Repeat: and no of comparisons done on each level n.
0).
Continue this process on the appropriate subarray until the T(n)=2T(n/2) + n = O(nlogn)
desired element is found. This is achieved when the rank of • A piece of sufficient information is
the pivot becomes equal to k. not available to make the best • In the solve function, we iterate through
each column in the current row and
choice, so we use the backtracking
Here is a simple Python implementation of the linear time check if it's safe to place a queen at that
selection algorithm: strategy to try out all the possible position using the isSafe function.
import random solutions.
Similarly, solve the given problem. def quick_select(arr, k):
• Each decision leads to a new set of • The isSafe function checks if no other
if len(arr) == 1: choices. Then again, we backtrack queens threaten the current position by
return arr[0] to make new decisions. In this case, looking at the column, left diagonal, and
we need to use the backtracking right diagonal.
pivot = random.choice(arr)
left = [x for x in arr if x < pivot] strategy.
• If it's safe to place a queen, we mark the
right = [x for x in arr if x > pivot]
equals = [x for x in arr if x == pivot] position on the chessboard and
recursively call solve for the next row.
if k < len(left):
return quick_select(left, k) • If we reach a solution (all queens placed
elif k < len(left) + len(equals): successfully), we print the chessboard,
return equals[0]
and then we backtrack by removing the
else:
return quick_select(right, k - len(left) - len(equals)) queen from the current position
(chessboard[row][column] = '.') to
# Example usage: explore other possibilities.
arr = [3, 2, 1, 5, 4]
k=3 The algorithm continues to explore all possible
result = quick_select(arr, k-1) # Finding the k-th smallest
element (k-1 because indexing starts from 0) combinations until all solutions are found .
print(result) # Output: 3
Heap Tree Heap Sort Greedy Algorithm - The greedy method is one of the Kruskal's Algorithm: Prim’s Algorithm
A heap tree, often simply referred to as a Heap sort processes the elements by creating strategies like Divide and conquer used to solve the problems. An algorithm to construct a Minimum Spanning Tree for a The algorithm starts with an empty spanning tree. The idea is
This method is used for solving optimization problems. An connected weighted graph. It is a Greedy Algorithm. The to maintain two sets of vertices. The first set contains the
"heap," is a specialized tree-based data the min-heap or max-heap using the elements optimization problem is a problem that demands either Greedy Choice is to put the smallest weight edge that does not vertices already included in the MST, and the other set
structure that satisfies the heap property. The of the given array. Min-heap or max-heap maximum or minimum results. Let's understand through some because a cycle in the MST constructed so far. contains the vertices not yet included. At every step, it
heap property is a specific ordering property represents the ordering of array in which the terms. considers all the edges that connect the two sets and picks the
If the graph is not linked, then it finds a Minimum Spanning minimum weight edge from these edges. After picking the
that depends on whether it is a min-heap or a root element represents the minimum or The Greedy method is the simplest and straightforward Tree. edge, it moves the other endpoint of the edge to the set
max-heap. maximum element of the array. Heapsort is a approach. It is not an algorithm, but it is a technique. The main containing MST.
popular and efficient sorting algorithm. The function of this approach is that the decision is taken on the Analysis: Where E is the number of edges in the graph and V is
basis of the currently available information. Whatever the the number of vertices, Kruskal's Algorithm can be shown to Consider the following graph as an example for which we
There are two main types of heaps: concept of heap sort is to eliminate the current information is present, the decision is made without run in O (E log E) time, or simply, O (E log V) time, all with simple need to find the Minimum Spanning Tree (MST).
elements one by one from the heap part of worrying about the effect of the current decision in future. data structures. These running times are equivalent because:
Min-Heap: In a min-heap, for any given node the list, and then insert them into the sorted
This technique is basically used to determine the feasible E is at most V2 and log V2= 2 x log V is O (log V).
N, the value of N is less than or equal to the part of the list. solution that may or may not be optimal. If we ignore isolated vertices, which will each their components
values of its children. In other words, the of the minimum spanning tree, V ≤ 2 E, so log V is O (log E).
smallest element is at the root, and for any Heapsort is the in-place sorting algorithm. Characteristics of Greedy algorithm Thus the total time is
• To construct the solution in an optimal way, this
given node, its value is less than or equal to Time complexity of heap sort is O(NlogN) algorithm creates two sets where one set O (E log E) = O (E log V).
the values of its children. This ensures that the contains all the chosen items, and another set For Example: Find the Minimum Spanning Tree of the following Example of a graph
minimum element is always at the top. Working of Heap Sort Algorithm- contains the rejected items. graph using Kruskal's algorithm.
In heap sort, basically, there are two phases involved in the • A Greedy algorithm makes good local choices in Step 1: Firstly, we select an arbitrary vertex that acts as the
sorting of elements. By using the heap sort algorithm, they are the hope that the solution should be either starting vertex of the Minimum Spanning Tree. Here we have
as follows –The first step includes the creation of a heap by feasible or optimal. selected vertex 0 as the starting vertex.
adjusting the elements of the array.
Applications of Greedy Algorithm
After the creation of heap, now remove the root element of the • It is used in finding the shortest path.
heap repeatedly by shifting it to the end of the array, and then
• It is used to find the minimum spanning tree
store the heap structure with the remaining elements.
using the prim's algorithm or the Kruskal's
algorithm. Solution: First we initialize the set A to the empty set and
Now let's see the working of heap sort in detail by using an • It is used in a job sequencing with a deadline. create |v| trees, one containing each vertex with MAKE-SET
procedure. Then sort the edges in E into order by non- 0 is selected as starting vertex
example. To understand it more clearly, let's take an unsorted • This algorithm is also used to solve the fractional
array and try to sort it using heap sort. It will make the knapsack problem. decreasing weight.
Max-Heap: In a max-heap, for any given node explanation clearer and easier. There are 9 vertices and 12 edges. So MST formed (9-1) = 8 Step 2: All the edges connecting the incomplete MST and
edges other vertices are the edges {0, 1} and {0, 7}. Between these
N, the value of N is greater than or equal to two the edge with minimum weight is {0, 1}. So include the
the values of its children. The largest element Disadvantages of using Greedy algorithm edge and vertex 1 in the MST.
Greedy algorithm makes decisions based on the information
is at the root, and for any given node, its value available at each phase without considering the broader
is greater than or equal to the values of its First, we have to construct a heap from the given array and
problem. So, there might be a possibility that the greedy
convert it into max heap.
children. This ensures that the maximum solution does not give the best solution for every problem.
It follows the local optimum choice at each stage with a intend
element is at the top. of finding the global optimum. Let's understand through an
example.
Consider the graph which is given below:
Now, check for each edge (u, v) whether the endpoints u and v 1 is added to the MST
belong to the same tree. If they do then the edge (u, v) cannot
be supplementary. Otherwise, the two vertices belong to Step 3: The edges connecting the incomplete MST to other
different trees, and the edge (u, v) is added to A, and the vertices are {0, 7}, {1, 7} and {1, 2}. Among these edges the
vertices in two trees are merged in by union procedure. minimum weight is 8 which is of the edges {0, 7} and {1, 2}.
Step1: So, first take (h, g) edge Let us here include the edge {0, 7} and the vertex 7 in the
After converting the given heap into max heap, the array MST. [We could have also included edge {1, 2} and vertex 2
elements are - in the MST].
We have to travel from the source to the destination at the
minimum cost. Since we have three feasible solutions having
Heaps are commonly used for implementing cost paths as 10, 20, and 5. 5 is the minimum cost path so it is
priority queues, which are data structures that the optimal solution. This is the local optimum, and in this way,
allow efficient retrieval of the minimum (or Next, we have to delete the root element (89) from the max
we find the local optimum at each stage in order to calculate
the global optimal solution
maximum) element in constant time, and heap. To delete this node, we have to swap it with the last
efficient insertion and deletion of elements in node, i.e. (11). After deleting the root element, we again have
to heapify it to convert it into max heap.
Minimum Spanning Tree Step 2: then (g, f) edge.
logarithmic time. General properties of minimum spanning tree: 7 is added in the MST

• If we remove any edge from the spanning tree, Step 4: The edges that connect the incomplete MST with the
Heap trees are usually implemented as binary then it becomes disconnected. Therefore, we fringe vertices are {1, 2}, {7, 6} and {7, 8}. Add the edge {7, 6}
trees, where each node has at most two cannot remove any edge from the spanning and the vertex 6 in the MST as it has the least weight (i.e., 1).
children (a left child and a right child). The tree.
• If we add an edge to the spanning tree then it
binary heap has the property that it's a
creates a loop. Therefore, we cannot add any
complete binary tree, meaning that all levels edge to the spanning tree.
of the tree are fully filled except possibly for After swapping the array element 89 with 11, and converting • In a graph, each edge has a distinct weight, then
the heap into max-heap, the elements of array are - there exists only a single and unique minimum
the last level, which is filled from left to right. Step 3: then (a, b) and (i, g) edges are considered, and the
spanning tree. If the edge weight is not distinct,
forest becomes
then there can be more than one minimum
Common operations on a heap include spanning tree.
inserting an element, deleting the minimum • A complete undirected graph can have an nn-2
In the next step, again, we have to delete the root number of spanning trees. 6 is added in the MST
(or maximum) element, and peeking at the element (81) from the max heap. To delete this node, we have
minimum (or maximum) element, all of which to swap it with the last node, i.e. (54). After deleting the root • Every connected and undirected graph contains Step 5: The connecting edges now are {7, 8}, {1, 2}, {6, 8} and
typically have efficient time complexities when element, we again have to heapify it to convert it into max atleast one spanning tree. {6, 5}. Include edge {6, 5} and vertex 5 in the MST as the edge
implemented using a heap structure. Heap trees are heap. • The disconnected graph does not have any has the minimum weight (i.e., 2) among them.
spanning tree.
widely used in algorithms like Heap Sort, Dijkstra's
• In a complete graph, we can remove maximum
algorithm, and Prim's algorithm, among others. (e-n+1) edges to construct a spanning tree.

Heap Tree Construction


There are two methods for construction of a heap tree.
1. Inserting Key – by – Key Disjoint Set
2. Heapify Method The disjoint set data structure is also known as union-find data Step 4: Now, edge (h, i). Both h and i vertices are in the same
structure and merge-find set. It is a data structure that contains Include vertex 5 in the MST
set. Thus it creates a cycle. So this edge is discarded.
1. Inserting Key-by-Key Algorithm analysis After swapping the array element 81 with 54 and converting a collection of disjoint or non-overlapping sets. The disjoint set Then edge (c, d), (b, c), (a, h), (d, e), (e, f) are considered,
• Insert key one at a time. To insert a key into an the heap into max-heap, the elements of array are - means that when the set is partitioned into the disjoint subsets. Step 6: Among the current connecting edges, the edge {5, 2}
and the forest becomes.
empty heap takes O(1) time. The various operations can be performed on the disjoint has the minimum weight. So include that edge and the vertex
• To insert a key into already constructed heap in subsets. In this case, we can add new sets, we can merge the 2 in the MST.
worst case logN comparisons and logN sets, and we can also find the representative member of a set.
swappings are required. It also allows to find out whether the two elements are in the
• Total N elements are there, So O(NlogN) time. In the next step, we have to delete the root element (76) from same set or not efficiently.
2. Heapify Method the max heap again. To delete this node, we have to swap it
Heapify is a fundamental operation used to convert an array with the last node, i.e. (9). After deleting the root element, we Let's understand the disjoint sets through an example.
or a binary tree into a heap data structure, specifically, a again have to heapify it to convert it into max heap.
binary heap. The purpose of heapify is to ensure that the heap Include vertex 2 in the MST
property is satisfied, which means that for a max-heap, the Step 5: In (e, f) edge both endpoints e and f exist in the same
value of each node is greater than or equal to the values of its tree so discarded this edge. Then (b, h) edge, it also creates a
Step 7: The connecting edges between the incomplete MST
children, and for a min-heap, the value of each node is less cycle.
and the other edges are {2, 8}, {2, 3}, {5, 3} and {5, 4}. The
than or equal to the values of its children. Step 6: After that edge (d, f) and the final spanning tree is
edge with minimum weight is edge {2, 8} which has weight 2.
Heapify can be applied to either the entire array (bottom-up shown as in dark lines.
So include this edge and the vertex 8 in the MST.
heapify) or a subtree within the array (top-down heapify).
Here, I'll describe the bottom-up heapify procedure, which is
commonly used to build a heap from an unsorted array.
After swapping the array element 76 with 9 and converting the
Bottom-Up Heapify (Build Heap) heap into max-heap, the elements of array are -
The bottom-up heapify algorithm starts from the middle of
the array and works its way up to the root. It ensures that the s1 = {1, 2, 3, 4}
subtree rooted at each node satisfies the heap property. s2 = {5, 6, 7, 8} Add vertex 8 in the MST
function heapify(arr, n, i): When the union operation is applied, the set would be
largest = i // Initialize the largest as the root represented as: Step 8: See here that the edges {7, 8} and {2, 3} both have
left_child = 2 * i + 1 In the next step, again we have to delete the root Step 7: This step will be required Minimum Spanning Tree same weight which are minimum. But 7 is already part of
right_child = 2 * i + 2 element (54) from the max heap. To delete this node, we have because it contains all the 9 vertices and (9 - 1) = 8 edges MST. So we will consider the edge {2, 3} and include that
to swap it with the last node, i.e. (14). After deleting the root e → f, b → h, d → f [cycle will be formed] edge and vertex 3 in the MST.
// Compare the root with the left child element, we again have to heapify it to convert it into max
if left_child < n and arr[left_child] > arr[largest]: heap.
largest = left_child

// Compare the root with the right child


if right_child < n and arr[right_child] > arr[largest]:
largest = right_child
Include vertex 3 in MST
// If the largest is not the root, swap the root and the
largest Step 9: Only the vertex 4 remains to be included. The
if largest != i: Minimum Cost MST minimum weighted edge from the incomplete MST to 4 is {3,
swap(arr[i], arr[largest]) After swapping the array element 54 with 14 and 4}.
s1Us2 = {1, 2, 3, 4, 5, 6, 7, 8}
converting the heap into max-heap, the elements of array Suppose we add one more edge between 1 and 5. Now the final
// Recursively heapify the affected subtree are - set can be represented as:
heapify(arr, n, largest) s3 = {1, 2, 3, 4, 5, 6, 7, 8}
If we consider any element from the above set, then all the
function buildHeap(arr): elements belong to the same set; it means that the cycle exists
n = length(arr) in a graph.
Similarly keep doing and at last, heap has only one element left.
// Start from the last non-leaf node and heapify all nodes After deleting it, heap will be empty.
for i from n/2 - 1 down to 0: Include vertex 4 in the MST
heapify(arr, n, i)
In the heapify function, we compare the root element with its The final structure of the MST is as follows and the weight of
left and right children, and if necessary, swap the root with the edges of the MST is (4 + 8 + 1 + 2 + 4 + 2 + 7 + 9) = 37.
the largest (for a max-heap) or smallest (for a min-heap) of
After completion of sorting, the array elements are -
the three elements. This ensures that the subtree rooted at
the current node satisfies the heap property.

The buildHeap function initializes the heap construction


process, starting from the last non-leaf node (the parent of
the last element) and moving up to the root of the tree. Now, the array is completely sorted.

After calling buildHeap, the entire array will satisfy the heap
property, and it can be used as a heap for efficient heap
operations like extracting the maximum (or minimum)
element or inserting new elements.
Cook's Theorem:
Fractional Knapsack Problem Dynamic Programming Optimal Matrix Chain Multiplication Types of Problems Cook's Theorem, also known as the Cook-Levin Theorem, is a
The fractional knapsack problem is also one of the techniques Dynamic programming is a technique that breaks the problems • Trackable fundamental result in the field of computational complexity
which are used to solve the knapsack problem. In fractional into sub-problems, and saves the result for future purposes so • Intrackable theory. It was proved by Stephen Cook in 1971 and is a
Dynamic Programming Approach
knapsack, the items are broken in order to maximize the that we do not need to compute the result again. The • Decision foundational concept in understanding the concept of NP-
Let Ai,j be the result of multiplying matrices i through j. It can be
profit. The problem in which we break the item is known as a subproblems are optimized to optimize the overall solution is • Optimization completeness. The theorem essentially provides a formal
seen that the dimension of Ai,j is pi-1 x pj matrix.
Fractional knapsack problem. known as optimal substructure property. The main use of • Trackable: Problems that can be solvable in a reasonable definition for what it means for a problem to be NP-complete
dynamic programming is to solve optimization problems. Here, (polynomial) time. and has profound implications for computer science and
In this approach, we will calculate the ratio of profit/weight. optimization problems mean that when we are trying to find Dynamic Programming solution involves breaking up the • Intrackable: Some problems are intractable, as they grow algorithms.
out the minimum or the maximum solution of a problem. The problems into subproblems whose solution can be combined large, we are unable to solve them in reasonable time. Statement of Cook's Theorem:
Objects: 1 2 3 4 5 6 7 dynamic programming guarantees to find the optimal solution to solve the global problem. • Optimization Problems A decision problem (a problem with a yes/no answer) is NP-
Profit (P): 5 10 15 7 8 9 4 of a problem if the solution exists. – An optimization problem is one which asks, complete if and only if it satisfies two conditions:
Weight(w): 1 3 5 4 1 3 2 The definition of dynamic programming says that it is a “What is the optimal solution to problem X?” 1. It is in the complexity class NP, which means that if you are
technique for solving a complex problem by first breaking into At the greatest level of parenthesization, we multiply two – Examples: given a potential solution, you can verify its correctness in
In this case, we first calculate the profit/weight ratio. a collection of simpler subproblems, solving each subproblem matrices 1. 0-1 Knapsack polynomial time.
just once, and then storing their solutions to avoid repetitive 2. Fractional Knapsack
3. Minimum Spanning Tree 2.It is NP-hard, meaning that every problem in NP can be
Object 1: 5/1 = 5 computations. polynomial-time reduced to it.
Object 2: 10/3 = 3. 33 A1.....n=A1....k x Ak+1....n) • Decision Problems
– A decision problem is one with yes/no answer The implications of Cook's Theorem are:
Object 3: 15/5 = 3 Divide and Conquer Dynamic Thus we are left with two questions: 1. Defines NP-Completeness: It formally defines NP-
Object 4: 7/4 = 1.7 – Examples:
Method Programming completeness, identifying problems that are both in NP and
Object 5: 8/1 = 8 1. Does a graph G have a MST of weight <= W?
Optimization VS Decision Problems NP-hard.
Object 6: 9/3 = 3 How to split the sequence of matrices?
Object 7: 4/2 = 2 • An optimization problem tries to find an optimal solution 2. Transitivity of Hardness: Solving one NP-complete problem
1.It deals (involves) three 1.It involves the How to parenthesize the subsequence A1.....k andAk+1......n? • A decision problem tries to answer a yes/no question in polynomial time implies solving all NP problems efficiently.
steps at each level of sequence of four steps: One possible answer to the first question for finding the best • Many problems will have decision and optimization versions
P:w: 5 3.3 3 1.7 8 3 2 value of 'k' is to check all possible choices of 'k' and consider 3. P vs. NP: It is central to the unsolved P vs. NP question;
recursion: – Eg: Traveling salesman problem
Characterize the the best among them. But that it can be observed that checking solving one NP-complete problem in polynomial time implies
Divide the problem into a • optimization: find hamiltonian cycle of minimum
In this approach, we will select the objects based on the structure of all possibilities will lead to an exponential number of total P = NP for all problems.
number of subproblems. weight
maximum profit/weight ratio. Since the P/W of object 5 is optimal possibilities. It can also be noticed that there exists only O (n2 ) 4. Practical Guidance: Helps identify computationally hard
maximum so we select object 5. Conquer the • decision: is there a hamiltonian cycle of weight k
solutions. different sequence of matrices, in this way do not reach the problems, guiding algorithm design and optimization efforts.
subproblems by solving exponential growth. Vertex Cover Problem
Object Profit Weight Remaining weight them recursively. P, NP, NP-Hard, NP-Complete - Definitions A vertex cover of an undirected graph is a subset of its
5 8 1 15 - 8 = 7 Combine the solution to Recursively The Class P vertices such that for every edge (u, v) of the graph, either ‘u’
After object 5, object 1 has the maximum profit/weight ratio, the subproblems into the defines the Step1: Structure of an optimal parenthesization: Our first step P: the class of problems that have polynomial-time or ‘v’ is in the vertex cover. Although the name is Vertex
i.e., 5. So, we select object 1 shown in the below table: solution for original in the dynamic paradigm is to find the optimal substructure and
values of optimal deterministic algorithms. Cover, the set covers all edges of the given graph. Given an
subproblems. solutions. then use it to construct an optimal solution to the problem – That is, they are solvable in O(p(n)), where p(n) is a undirected graph, the vertex cover problem is to find
Object Profit Weight Remaining weight from an optimal solution to subproblems. minimum size vertex cover.
polynomial on n
5 8 1 15 - 1 = 14 The following are some examples.
– A deterministic algorithm is (essentially) one that always
1 5 1 14 - 1 = 13
Compute the computes the correct answer.
After object 1, object 2 has the maximum profit/weight ratio, Let Ai....j where i≤ j denotes the matrix that results from
i.e., 3.3. So, we select object 2 having profit/weight ratio as value of optimal Sample Problems in P
evaluating the product
3.3. solutions in a • Fractional Knapsack
Bottom-up • MST
• Sorting Vertex Cover Problem is a known NP Complete problem, i.e.,
Object Profit Weight Remaining weight minimum. Ai Ai+1....Aj.
• Others? there is no polynomial-time solution for this unless P = NP.
5 8 1 15 - 1 = 14
The class NP There are approximate polynomial-time algorithms to solve
1 5 1 14 - 1 = 13
The NP problems set of problems whose solutions are hard to the problem though.
2 10 3 13 - 3 = 10 Construct an If i < j then any parenthesization of the product
find but easy to verify and are solved by Non-Deterministic Consider all the subset of vertices one by one and find out
After object 2, object 3 has the maximum profit/weight ratio, Optimal Solution Ai Ai+1 ......Aj must split that the product between Ak and
whether it covers all edges of the graph. For eg. in a graph
Machine in polynomial time (or with a nondeterministic
i.e., 3. So, we select object 3 having profit/weight ratio as 3. from computed Ak+1 for some integer k in the range i ≤ k ≤ j. That is for some
algorithm). consisting only 3 vertices the set consisting of the
information. value of k, we first compute the matrices Ai.....k & Ak+1....j and
Sample Problems in NP combination of vertices are:{0,1,2,{0,1},{0,2},{1,2},{0,1,2}} .
Object Profit Weight Remaining weight then multiply them together to produce the final product Ai....j.
• Fractional Knapsack Using each element of this set check whether these vertices
5 8 1 15 - 1 = 14 The cost of computing Ai....k plus the cost of computing
• MST cover all the edges of the graph. Hence update the optimal
1 5 1 14 - 1 = 13 2. It is Recursive. 2. It is non Recursive. Ak+1....j plus the cost of multiplying them together is the cost of
answer. And hence print the subset having minimum number
• Others?
2 10 3 13 - 3 = 10 parenthesization.
– Traveling Salesman of vertices which also covers all the edges of the graph.
3 15 5 10 - 5 = 5
– Graph Coloring Approximate Algorithm for Vertex Cover:
After object 3, object 6 has the maximum profit/weight ratio, 3. It does more work on 3. It solves
– Satisfiability (SAT) 1) Initialize the result as {}
i.e., 3. So we select object 6 having profit/weight ratio as 3. subproblems and hence subproblems only once Step 2: A Recursive Solution: Let m [i, j] be the minimum
Co-NP Class 2) Consider a set of all edges in given graph. Let the set be E.
has more time and then stores in the number of scalar multiplication needed to compute the
3) Do following while E is not empty
matrixAi....j. Co-NP stands for the complement of NP Class. It means if the
Object Profit Weight Remaining weight consumption. table. answer to a problem in Co-NP is No, then there is proof that a) Pick an arbitrary edge (u, v) from set E and add 'u' and 'v'
5 8 1 15 - 1 = 14
can be checked in polynomial time. to result.
1 5 1 14 - 1 = 13
Features: b) Remove all edges from E which are either incident on u or
2 10 3 13 - 3 = 10 4. It is a top-down 4. It is a Bottom-up If i=j the chain consist of just one matrix Ai....i=Ai so no scalar
multiplication are necessary to compute the product. Thus m 1. If a problem X is in NP, then its complement X’ is also in v.
3 15 5 10 - 5 = 5 approach. approach. 4) Return result
[i, j] = 0 for i= 1, 2, 3....n. CoNP.
6 9 3 5-3=2
2. For an NP and CoNP problem, there is no need to verify all
After object 6, object 7 has the maximum profit/weight ratio,
the answers at once in polynomial time, there is a need to
i.e., 2. So we select object 7 having profit/weight ratio as 2.
5. In this subproblems are 5. In this subproblems If i<j we assume that to optimally parenthesize the product we verify only one particular answer “yes” or “no” in polynomial
independent of each are interdependent. split it between Ak and Ak+1 where i≤ k ≤j. Then m [i,j] equals the time for a problem to be in NP or CoNP.
Object Profit Weight Remaining weight
5 8 1 15 - 1 = 14 other. minimum cost for computing the subproducts Ai....k and Ak+1....j+ Some example problems for CoNP are:
cost of multiplying them together. We know Ai has dimension • To check prime number.
1 5 1 14 - 1 = 13
pi-1 x pi, so computing the product Ai....k and Ak+1....jtakes pi- • Integer Factorization.
2 10 3 13 - 3 = 10
3 15 5 10 - 5 = 5 6. For example: Merge 6. For example: Matrix 1 pk pj scalar multiplication, we obtain
NP-Hard Problem:
6 9 3 5-3=2 Sort & Binary Search etc. Multiplication.
A Problem X is NP-Hard if there is an NP-Complete problem Y,
7 4 2 2-2=0
m [i,j] = m [i, k] + m [k + 1, j] + pi-1 pk pj such that Y is reducible to X in polynomial time. NP-Hard
As we can observe in the above table that the remaining
problems are as hard as NP-Complete problems. NP-Hard
weight is zero which means that the knapsack is full. We There are only (j-1) possible values for 'k' namely k = i, i+1.....j-
Problem need not be in NP class.
cannot add more objects in the knapsack. Therefore, the total 1. Since the optimal parenthesization must use one of these
If every problem of NP can be polynomial time reduced to it
profit would be equal to (8 + 5 + 10 + 15 + 9 + 4), i.e., 51. values for 'k' we need only check them all to find the best.
called as NP Hard.
A lot of times takes the particular problem solve and reducing
different problems.
Huffman Coding So the minimum cost of parenthesizing the product example :
Huffman Codes Dynamic Programming Greedy Method Ai Ai+1......Aj becomes 1. Hamiltonian cycle .
(i) Data can be encoded efficiently using Huffman Codes. 2. optimization problem .
(ii) It is a widely used and beneficial technique for 3. Shortest path
compressing data. NP-Complete Problem:
A problem X is NP-Complete if there is an NP problem Y, such
(iii) Huffman's greedy algorithm uses a table of the 1. Dynamic Programming 1. Greedy Method is also
frequencies of occurrences of each character to build up an that Y is reducible to X in polynomial time. NP-Complete
is used to obtain the used to get the optimal problems are as hard as NP problems. A problem is NP-
optimal way of representing each character as a binary string. To construct an optimal solution, let us define s [i,j] to be the
optimal solution. solution. Complete if it is a part of both NP and NP-Hard Problem. A
value of 'k' at which we can split the product A i Ai+1 .....Aj To
Huffman invented a greedy algorithm that creates an optimal obtain an optimal parenthesization i.e. s [i, j] = k such that non-deterministic Turing machine can solve NP-Complete
prefix code called a Huffman Code. problem in polynomial time.
A problem is np-complete when it is both np and np hard
m [i,j] = m [i, k] + m [k + 1, j] + pi-1 pk pj combines together.
this means np complete problems can be verified in
2. In Dynamic 2. In a greedy Algorithm, polynomial time.
Programming, we choose we make whatever Longest Common Subsequence Example:
The algorithm builds the tree T analogous to the optimal code at each step, but the choice seems best at the A subsequence of a given sequence is just the given sequence 1. Decision problems.
in a bottom-up manner. It starts with a set of |C| leaves (C is choice may depend on moment and then solve with some elements left out. 2. Regular graphs.
the number of characters) and performs |C| - 1 'merging' the solution to sub- the sub-problems Difference between NP-Hard and NP-Complete:
operations to create the final tree. In the Huffman algorithm 'n' problems. arising after the choice Given two sequences X and Y, we say that the sequence Z is a
denotes the quantity of a set of characters, z indicates the is made. common sequence of X and Y if Z is a subsequence of both X NP-hard NP-Complete
parent node, and x & y are the left & right child of z and Y.
respectively.
In the longest common subsequence problem, we are given
NP-Hard problems(say
Algorithm for Huffman Coding two sequences X = (x1 x2....xm) and Y = (y1 y2 yn) and wish to X) can be solved if and NP-Complete problems can
Step 1: Build a min-heap in which each node represents the find a maximum length common subsequence of X and Y. LCS only if there is a NP- be solved by a non-
root of a tree with a single node and holds 5 (the number of Problem can be solved using dynamic programming. Complete problem(say deterministic
unique characters from the provided stream of data). Y) that can be Algorithm/Turing Machine
Algorithm of Longest Common Subsequence
3. Less efficient as 3. More efficient as reducible into X in in polynomial time.
LCS-LENGTH (X, Y)
compared to a greedy compared to a greedy polynomial time.
1. m ← length [X]
approach approach
Step 2: Obtain two minimum frequency nodes from the min 2. n ← length [Y]
heap in step two. Add a third internal node, frequency 2 + 3 = 3. for i ← 1 to m
5, which is created by joining the two extracted nodes. 4. do c [i,0] ← 0 To solve this problem, To solve this problem, it
5. for j ← 0 to m it do not have to be in must be both NP and NP-
6. do c [0,j] ← 0 NP . hard problems.
7. for i ← 1 to m
4. Example: 0/1 Knapsack 4. Example: Fractional 8. do for j ← 1 to n
Knapsack 9. do if xi= yj
Step 3: Get the two minimum frequency nodes from the heap 10. then c [i,j] ← c [i-1,j-1] + 1 Time is unknown in Time is known as it is fixed
in a similar manner in step three. Additionally, add a new 11. b [i,j] ← "↖" NP-Hard. in NP-Hard.
internal node formed by joining the two extracted nodes; its 12. else if c[i-1,j] ≥ c[i,j-1]
frequency in the tree should be 4 + 4 = 8. 13. then c [i,j] ← c [i-1,j]
Step 4: Get the two minimum frequency nodes in step four. 5. It is guaranteed that 5. In Greedy Method, 14. b [i,j] ← "↑"
Additionally, add a new internal node formed by joining the Dynamic Programming there is no such 15. else c [i,j] ← c [i,j-1] NP-hard is not a NP-Complete is exclusively
two extracted nodes; its frequency in the tree should be 5 + 7 will generate an optimal guarantee of getting 16. b [i,j] ← "← " decision problem. a decision problem.
= 12. solution using Principle Optimal Solution. 17. return c and b.
of Optimality.

Not all NP-hard


All NP-complete problems
problems are NP-
are NP-hard
complete.

Step 5: Get the following two minimum frequency nodes in Do not have to be a It is exclusively a Decision
step 5. Additionally, add a new internal node formed by joining Decision problem. problem.
the two extracted nodes; its frequency in the tree should be 12
+ 8 = 20.
Continue until all of the distinct characters have been added to
It is optimization
the tree. The Huffman tree created for the specified cast of It is Decision problem used.
problem used.
characters is shown in the above image.
Now, for each non-leaf node, assign 0 to the left edge and 1 to
the right edge to create the code for each letter.
Rules to follow for determining edge weights:
Example: Determine
Following the weighting, the modified tree is displayed as
whether a graph has a
follows:
Example: Halting Hamiltonian cycle,
problem, Vertex cover Determine whether a
problem, etc. Boolean formula is
satisfiable or not, Circuit-
satisfiability problem, etc.
Representational issues in graphs Biconnected Components Bellman-Ford Algorithm The Floyd Warshall
Representational issues in graphs refer to challenges or Depth First Search for a Graph A graph is said to be Biconnected if: Bellman-Ford is a single source shortest path algorithm that The Floyd Warshall Algorithm is for solving all pairs of
limitations associated with how data is visually depicted in a DFS stands for Depth First Search. In DFS traversal, the stack It is connected, i.e. it is possible to reach every vertex from determines the shortest path between a given source vertex shortest-path problems. The problem is to find the shortest
graph. Graphs are commonly used to represent data in a data structure is used, which works on the LIFO (Last In First every other vertex, by a simple path. and every other vertex in a graph. This algorithm can be used distances between every pair of vertices in a given edge-
visual format, but the choice of representation can impact Out) principle. In DFS, traversing can be started from any Even after removing any vertex the graph remains connected. on both weighted and unweighted graphs. weighted directed Graph.
how accurately and effectively information is communicated. node, or we can say that any node can be considered as a root For example, consider the graph in the following figure It is an algorithm for finding the shortest path between all the
Here are some common representational issues in graphs: node until the root node is not mentioned in the problem. A Bellman-Ford algorithm is also guaranteed to find the pairs of vertices in a weighted graph. This algorithm follows
1.Misleading Axes: In the case of BFS, the element which is deleted from the shortest path in a graph, similar to Dijkstra’s algorithm. the dynamic programming approach to find the shortest path.
Scaling: Inappropriately scaled axes can distort the Queue, the adjacent nodes of the deleted node are added to Although Bellman-Ford is slower than Dijkstra’s algorithm, it is A C-function for a N x N graph is given below. The function
representation of data. For example, using a non-zero the Queue. In contrast, in DFS, the element which is removed capable of handling graphs with negative edge weights, which stores the all pair shortest path in the matrix cost [N][N]. The
baseline on a bar chart can exaggerate differences between from the stack, then only one adjacent node of a deleted node makes it more versatile. The shortest path cannot be found if cost matrix of the given graph is available in cost Mat [N][N].
values. is added in the stack. there exists a negative cycle in the graph. If we continue to go The Knuth-Morris-Pratt (KMP)Algorithm
Truncation: Truncating the axis can create a misleading Step1: Initially stack and visited arrays are empty. around the negative cycle an infinite number of times, then Knuth-Morris and Pratt introduce a linear time algorithm for
impression of the data by exaggerating or diminishing the the cost of the path will continue to decrease (even though the string matching problem. A matching time of O (n) is
apparent differences. the length of the path is increasing). As a result, Bellman-Ford achieved by avoiding comparison with an element of 'S' that
2. Incomplete Data: is also capable of detecting negative cycles, which is an have previously been involved in comparison with some
- Omission of Data: Graphs may not include all relevant data, important feature. element of the pattern 'p' to be matched. i.e., backtracking on
leading to an incomplete or biased representation. This can be The given graph is clearly connected. Now try removing the The idea behind Bellman Ford Algorithm: the string 'S' never occurs
intentional or unintentional but has the potential to mislead vertices one by one and observe. Removing any of the vertices The Bellman-Ford algorithm’s primary principle is that it starts Components of KMP Algorithm:
the audience. does not increase the number of connected components. So with a single source and calculates the distance to each node. 1. The Prefix Function (Π): The Prefix Function, Π for a pattern
3. Labeling Issues: the given graph is Biconnected. The distance is initially unknown and assumed to be infinite, encapsulates knowledge about how the pattern matches
- Incomplete Labels: Missing or insufficient labels can make Now consider the following graph which is a slight but as time goes on, the algorithm relaxes those paths by against the shift of itself. This information can be used to
it difficult for viewers to understand the context or meaning modification in the previous graph. identifying a few shorter paths. Hence it is said that Bellman- avoid a useless shift of the pattern 'p.' In other words, this
Step 2: Visit 0 and put its adjacent nodes which are not visited
of the data. Proper labeling is essential for clarity. Ford is based on “Principle of Relaxation“. enables avoiding backtracking of the string 'S.'
yet into the stack.
- Ambiguous Labels: Labels that are unclear or open to Principle of Relaxation of Edges for Bellman-Ford: 2. The KMP Matcher: With string 'S,' pattern 'p' and prefix
interpretation can lead to misunderstandings. -It states that for the graph having N vertices, all the edges function 'Π' as inputs, find the occurrence of 'p' in 'S' and
4. Graph Type Selection: should be relaxed N-1 times to compute the single source returns the number of shifts of 'p' after which occurrences are
- Inappropriate Graph Type: Choosing the wrong type of shortest path. found.
graph for the data can lead to misinterpretation. For instance, -In order to detect whether a negative cycle exists or not,
using a pie chart for data with too many categories can make relax all the edge one more time and if the shortest distance
it challenging to discern differences. for any node reduces then we can say that a negative cycle
5. 3D Effects: exists. In short if we relax the edges N times, and there is any
- Perspective Distortion: Adding unnecessary 3D effects to change in the shortest distance of any node between the N-
charts, like extruding bars in a bar chart, can distort the 1th and Nth relaxation than a negative cycle exists, otherwise
perception of values and make accurate comparisons difficult. not exist.
6. Color Choices Algorithm to Find Negative Cycle in a Directed Weighted
- Poor Color Contrast:Insufficient contrast between colors Step 3: Now, Node 1 at the top of the stack, so visit node 1 Graph Using Bellman-Ford:
can make it hard for individuals with color vision deficiencies and pop it from the stack and put all of its adjacent nodes a)Initialize distance array dist[] for each vertex ‘v‘ as dist[v] =
to interpret the graph. It can also reduce overall readability. which are not visited in the stack. In the above graph if the vertex 2 is removed, then here's how INFINITY.
7. Data Density: it will look: b) Assume any vertex (let’s say ‘0’) as source and assign dist =
- Overplotting: When there is too much data in a small 0.
space, points or lines may overlap, making it difficult to c) Relax all the edges(u,v,weight) N-1 times as per the below
discern individual data points. condition:
8. Data Manipulation: dist[v] = minimum(dist[v], distance[u] + weight)
- Cherry-Picking Data: Selectively including or excluding data d) Now, Relax all the edges one more time i.e. the Nth time
points to support a particular narrative can lead to a biased or and based on the below two cases we can detect the negative
inaccurate representation of the data. cycle:
9. Default Settings: Case 1 (Negative cycle exists): For any edge(u, v, weight), if
- Default Chart Settings: Relying on default settings in dist[u] + weight < dist[v]
graphing tools without considering the specific characteristics Step 4: Now, Node 2 at the top of the stack, so visit node 2 Case 2 (No Negative cycle) : case 1 fails for all the edges.
of the data can result in suboptimal visualizations. and pop it from the stack and put all of its adjacent nodes Dijkstra’s algorithm
10. Lack of Context: which are not visited (i.e, 3, 4) in the stack. Dijkstra’s algorithm is a popular algorithms for solving many
- Missing Contextual Information: Failing to provide Clearly the number of connected components have increased. single-source shortest path problems having non-negative
background information or context for the data may leave Similarly, if vertex 3 is removed there will be no path left to edge weight in the graphs i.e., it is to find the shortest
viewers without the necessary information to interpret the reach vertex 0 from any of the vertices 1, 2, 4 or 5. And same distance between two vertices on a graph
graph accurately. goes for vertex 4 and 1. Removing vertex 4 will disconnect 1 Basic Characterstics of Dijkstra’s Algorithm
Breadth First Search for a Graph from all other vertices 0, 2, 3 and 4. So the graph is not Below are the basic steps of how Dijkstra’s algorithm works:
The Breadth First Search (BFS) algorithm is used to search a Biconnected. -So Basically, Dijkstra’s algorithm starts at the node source
graph data structure for a node that meets a set of criteria. It Now what to look for in a graph to check if it's Biconnected. node we choose and then it analyzes the graph condition and
starts at the root of the graph and visits all nodes at the By now it is said that a graph is Biconnected if it has no vertex its paths to find the optimal shortest distance between the
current depth level before moving on to the nodes at the next such that its removal increases the number of connected given node and all other nodes in the graph.
depth level. components in the graph. And if there exists such a vertex -Dijkstra’s algorithm keeps track of the currently known
Starting from the root, all the nodes at a particular level are then it is not Biconnected. A vertex whose removal increases shortest distance from each node to the source node and
Step 5: Now, Node 4 at the top of the stack, so visit node 4
visited first and then the nodes of the next level are traversed the number of connected components is called an Articulation updates the value after it finds the optimal path once the
and pop it from the stack and put all of its adjacent nodes
till all the nodes are visited. Point. algorithm finds the shortest path between the source node
which are not visited in the stack.
To do this a queue is used. All the adjacent unvisited nodes of Topological Sorting and destination node then the specific node is marked as
the current level are pushed into the queue and the nodes of A topological sort or topological ordering of a directed visited.
the current level are marked visited and popped from the graph is a linear ordering of its vertices in which u occurs Algorithm for Dijkstra’s Algorithm:
queue. before v in the ordering for every directed edge uv from -Mark the source node with a current distance of 0 and the
Step1: Initially queue and visited arrays are empty. vertex u to vertex v. For example, the graph's vertices rest with infinity.
-Set the non-visited node with the smallest current distance as
could represent jobs to be completed, and the edges
the current node.
could reflect requirements that one work must be
-For each neighbor, N of the current node adds the current
completed before another. distance of the adjacent node with the weight of the edge
In this case, a topological ordering is just a legitimate connecting 0->1. If it is smaller than the current distance of
task sequence. A topological sort is a graph traversal in Node, set it as the new current distance of N.
Step 6: Now, Node 3 at the top of the stack, so visit node 3 which each node v is only visited after all of its -Mark the current node 1 as visited.
and pop it from the stack and put all of its adjacent nodes dependencies have been visited. If the graph contains no -Go to step 2 if there are any nodes are unvisited.
which are not visited in the stack. directed cycles, then it is a directed acyclic graph. Any
Step2: Push node 0 into queue and mark it visited. DAG has at least one topological ordering, and there Step 1: Start from Node 0 and mark Node as visited as you can
exist techniques for building topological orderings in check in below image visited Node is marked red.
linear time for any DAG.
Topological sorting has many applications, particularly in
ranking issues like the feedback arc set. Even if the DAG
includes disconnected components, topological sorting is
possible.
The Applications of Topological Sort are:
Step 3: Remove node 0 from the front of queue and visit the -Finding cycle in a graph: A topological ordering is
unvisited neighbours and push them into queue. possible only for directed acyclic graph(DAG). For a cyclic
Now, Stack becomes empty, which means we have visited all Step 2: Check for adjacent Nodes, Now we have to choices
graph topological ordering is not possible. If the given
the nodes and our DFS traversal ends. (Either choose Node1 with distance 2 or either choose Node 2
Strongly Connected Components graph contains a cycle, then there is at least one vertex
with distance 6 ) and choose Node with minimum distance. In
A strongly connected component is the component of a will break topological order. If topological sort isn't
this step Node 1 is Minimum distance adjacent Node, so
directed graph that has a path from every vertex to every defined then we can say that the graph is cyclic. marked it as visited and add up the distance.
other vertex in that component. It can only be used in a -Operation System deadlock detection: Topological Distance: Node 0 -> Node 1 = 2
directed graph. sorting is used here to identify a cycle. If the wait-for
For example, The below graph has two strongly connected graph has a cycle, then there is deadlock.
Step 4: Remove node 1 from the front of queue and visit the components {1,2,3,4} and {5,6,7} since there is path from each -Dependency resolution:
unvisited neighbours and push them into queue. vertex to every other vertex in the same strongly connected -Sentence Ordering
component. -Critical Path Analysis
-Course Schedule problem
-Other applications like manufacturing workflows, data
serialization and context-free grammar.
Transitive closure of a graph Step 3: Then Move Forward and check for adjacent Node
if a vertex j is reachable from another vertex i for all which is Node 3, so marked it as visited and add up the
vertex pairs (i, j) in the given graph. Here reachable mean distance, Now the distance will be:
Step 5: Remove node 2 from the front of queue and visit the that there is a path from vertex i to j. The reach-ability Distance: Node 0 -> Node 1 -> Node 3 = 2 + 5 = 7
unvisited neighbours and push them into queue.
matrix is called the transitive closure of a graph.

Let’s understand the approach with the help of following


example:-

Step 6: Remove node 3 from the front of queue and visit the Step 4: Again we have two choices for adjacent Nodes (Either
unvisited neighbours and push them into queue. we can choose Node 4 with distance 10 or either we can
As we can see that every neighbours of node 3 is visited, so Transitive closure of above graphs is choose Node 5 with distance 15) so choose Node with
move to the next node that are in the front of the queue.
1111 minimum distance. In this step Node 4 is Minimum distance
1111 adjacent Node, so marked it as visited and add up the
1111 distance.
0001 Distance: Node 0 -> Node 1 -> Node 3 -> Node 4 = 2 + 5 + 10 =
17
The graph is given in the form of adjacency matrix say
‘graph[V][V]’ where graph[i][j] is 1 if there is an edge from
1)Starting with vertex 1. There is path from vertex 1 to vertex
vertex i to vertex j or i is equal to j, otherwise graph[i][j] is
2 and vice-versa. Similarly there is a path from vertex 1 to
0.
Steps 7: Remove node 4 from the front of queue and visit the vertex 3 and vice versa. So, vertex 2 and 3 will be in the same
Strongly Connected Component as vertex 1. Although there is Floyd Warshall Algorithm can be used, we can calculate
unvisited neighbours and push them into queue.
As we can see that every neighbours of node 4 are visited, so directed path form vertex 1 to vertex 4 and vertex 5. But there the distance matrix dist[V][V] using Floyd Warshall, if
move to the next node that is in the front of the queue. is no directed path from vertex 4,5 to vertex 1 so vertex 4 and dist[i][j] is infinite, then j is not reachable from i.
5 won’t be in the same Strongly Connected Component as Otherwise, j is reachable and the value of dist[i][j] will be
vertex 1. Thus Vertex 1,2 and 3 forms a Strongly Connected less than V.
Component. Step 5: Again, Move Forward and check for adjacent Node
2)For Vertex 2 and 3, there Strongly Connected Component which is Node 6, so marked it as visited and add up the
has already been determined. distance, Now the distance will be:
3)For Vertex 4, there is a path from vertex 4 to vertex 5 but Distance: Node 0 -> Node 1 -> Node 3 -> Node 4 -> Node 6 = 2
there is no path from vertex 5 to vertex 4. So vertex 4 and + 5 + 10 + 2 = 19
vertex won’t be in the Same Strongly Connected Component.
Now, Queue becomes empty, So, terminate these process of So both Vertex 4 and Vertex 5 will be part of Single Strongly
iteration. Connected Component.
4)Hence there will be 3 Strongly Connected Component
{1,2,3}, {4} and {5}.

So, the Shortest Distance

You might also like