DAA
DAA
• 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.
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.
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.
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}.