
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 507 Articles for Algorithms

3K+ Views
Using a Depth First Search (DFS) traversal algorithm we can detect cycles in a directed graph. If there is any self-loop in any node, it will be considered as a cycle, otherwise, when the child node has another edge to connect its parent, it will also a cycle.For the disconnected graph, there may different trees present, we can call them a forest. Now we have to detect cycle for all trees of the forest.In this approach, we will use different sets to assign nodes to perform the DFS traversal. There are three different sets, the White, Grey and the Black. ... Read More

8K+ Views
To detect if there is any cycle in the undirected graph or not, we will use the DFS traversal for the given graph. For every visited vertex v, when we have found any adjacent vertex u, such that u is already visited, and u is not the parent of vertex v. Then one cycle is detected. We will assume that there are no parallel edges for any pair of vertices.Input and Output: Adjacency matrix 0 1 0 0 0 1 0 1 1 0 0 1 0 0 1 0 1 ... Read More

3K+ Views
The Depth-First Search (DFS) is a graph traversal algorithm. In this algorithm, one starting vertex is given, and when an adjacent vertex is found, it moves to that adjacent vertex first and tries to traverse in the same manner.It moves through the whole depth, as much as it can go, after that it backtracks to reach previous vertices to find the new path.To implement DFS in an iterative way, we need to use the stack data structure. If we want to do it recursively, external stacks are not needed, it can be done internal stacks for the recursion calls.Input and ... Read More

3K+ Views
To check connectivity of a graph, we will try to traverse all nodes using any traversal algorithm. After completing the traversal, if there is any node, which is not visited, then the graph is not connected.For the directed graph, we will start traversing from all nodes to check connectivity. Sometimes one edge can have the only outward edge but no inward edge, so that node will be unvisited from any other starting node.In this case, the traversal algorithm is recursive DFS traversal.Input and OutputInput: Adjacency matrix of a graph 0 1 0 0 0 0 0 1 0 ... Read More

4K+ Views
In this problem, one undirected graph is given, we have to check the graph is tree or not. We can simply find it by checking the criteria of a tree. A tree will not contain a cycle, so if there is any cycle in the graph, it is not a tree.We can check it using another approach, if the graph is connected and it has V-1 edges, it could be a tree. Here V is the number of vertices in the graph.Input and OutputInput: The adjacency matrix. 0 0 0 0 1 0 0 0 0 1 0 0 0 ... Read More

2K+ Views
An edge in an undirected graph is said to be a bridge, if and only if by removing it, disconnects the graph, or make different components of the graph. In a practical approach, if some bridges are present in a network when the connection of bridges is broken, it can break the whole network.Input and OutputInput: The adjacency matrix of the graph. 0 1 1 1 0 1 0 1 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 1 0 Output: Bridges in given graph: Bridge 3--4 Bridge 0--3AlgorithmbridgeFind(start, visited, disc, low, ... Read More

4K+ Views
An undirected graph is said to be a biconnected graph, if there are two vertex-disjoint paths between any two vertices are present. In other words, we can say that there is a cycle between any two vertices.We can say that a graph G is a bi-connected graph if it is connected, and there are no articulation points or cut vertex are present in the graph.To solve this problem, we will use the DFS traversal. Using DFS, we will try to find if there is any articulation point is present or not. We also check whether all vertices are visited by ... Read More

718 Views
Medians are the middle numbers, in other words, the median value is the middle observation in an ordered list. It corresponds to the cumulative percentage of 50%.The size of two arrays must be same, we will find the median of two separate arrays at first, then compare the separate medians to get an actual median of two lists.Input and OutputInput: Two sorted array are given. Array 1: {1, 2, 3, 6, 7} Array 2: {4, 6, 8, 10, 11} Output: The median from two array. Here the median value is 6. Merge the given lists into one. {1, 2, 3, ... Read More

557 Views
The inversions of an array indicate; how many changes are required to convert the array into its sorted form. When an array is already sorted, it needs 0 inversions, and in another case, the number of inversions will be maximum, if the array is reversed.To solve this problem, we will follow the Merge sort approach to reduce the time complexity, and make it in Divide and Conquer algorithm.Input and OutputInput: A sequence of numbers. (1, 5, 6, 4, 20). Output: The number of inversions required to arrange the numbers into ascending order. Here the number of inversions are 2. First ... Read More

1K+ Views
An item is said to be a peak element when it is greater than or equal with all four neighbor of that element. The neighbor elements are the top, bottom, left and right elements. For this problem, we will consider some bounds. The diagonal elements are not checked as neighbor elements. More than one peak element may present in a matrix, and the peak element is not necessarily the largest element in the matrix.Input and OutputInput: A matrix of different numbers. 10 8 10 10 14 13 12 11 15 9 11 11 15 9 11 21 16 17 19 ... Read More