Algorithms Articles

Page 28 of 39

How to find if a graph is Bipartite?

Arjun Thakur
Arjun Thakur
Updated on 16-Jun-2020 4K+ Views

A graph is said to be a bipartite graph, when vertices of that graph can be divided into two independent sets such that every edge in the graph is either start from the first set and ended in the second set, or starts from the second set, connected to the first set, in other words, we can say that no edge can found in the same set.Checking of a bipartite graph is possible by using the vertex coloring. When a vertex is in the same set, it has the same color, for another set, the color will change.Input and OutputInput: ...

Read More

Shortest path with exactly k Edges

Samual Sam
Samual Sam
Updated on 16-Jun-2020 702 Views

One directed graph is provided with the weight between each pair of vertices, and two vertices u and v are also provided. Our task is to find the shortest distance from vertex u to vertex v, with exactly k number of edges. To solve this problem, we will start from vertex u and go to all adjacent vertices and recur for adjacent vertices using the k value as k - 1.Input and OutputInput: The cost matrix of the graph. 0 10 3 2 ∞  0 ∞ 7 ∞  ∞ 0 6 ∞  ∞ ∞ 0 Output: Weight of the shortest ...

Read More

Check if a given graph is tree or not

karthikeya Boyini
karthikeya Boyini
Updated on 16-Jun-2020 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

Connectivity in a directed graph

Samual Sam
Samual Sam
Updated on 16-Jun-2020 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

Detect Cycle in a an Undirected Graph

Samual Sam
Samual Sam
Updated on 16-Jun-2020 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

Bridges in a Graph

Samual Sam
Samual Sam
Updated on 16-Jun-2020 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

Word Break Problem

Samual Sam
Samual Sam
Updated on 16-Jun-2020 526 Views

In the input of this problem, one sentence is given without spaces, another dictionary is also provided of some valid English words. We have to find the possible ways to break the sentence in individual dictionary words.We will try to search from the left of the string to find a valid word when a valid word is found, we will search for words in the next part of that string.Input and OutputInput: A set of valid words as dictionary, and a string where different words are placed without spaces. Dictionary: {mobile, sam, sung, man, mango, icecream, and, go, i, love, ...

Read More

Closest Pair of Points Problem

Samual Sam
Samual Sam
Updated on 16-Jun-2020 13K+ Views

In this problem, a set of n points are given on the 2D plane. In this problem, we have to find the pair of points, whose distance is minimum.To solve this problem, we have to divide points into two halves, after that smallest distance between two points is calculated in a recursive way. Using distances from the middle line, the points are separated into some strips. We will find the smallest distance from the strip array. At first two lists are created with data points, one list will hold points which are sorted on x values, another will hold data ...

Read More

Peak Element in 2D array

karthikeya Boyini
karthikeya Boyini
Updated on 16-Jun-2020 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

Count Inversions in an array

Samual Sam
Samual Sam
Updated on 16-Jun-2020 664 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
Showing 271–280 of 386 articles
« Prev 1 26 27 28 29 30 39 Next »
Advertisements