Programming Articles - Page 2371 of 3366

Check if a graph is strongly connected - Set 1 (Kosaraju using DFS) in C++

Ravi Ranjan
Updated on 30-May-2025 18:58:57

368 Views

To check a strongly connected graph using Kosaraju's algorithm, we need to understand the strongly connected graph and Kosaraju's algorithm. For a graph to be strongly connected, it should be a directed graph, and for any pair of vertices u and v in the directed graph, there exists a directed path from u to v and a directed path from v to u. In this article, we have a directed graph with five vertices. Our task is to use Kosaraju's algorithm to check if the given graph is strongly ... Read More

Check if a given tree graph is linear or not in C++

Arnab Chakraborty
Updated on 22-Oct-2019 08:26:43

199 Views

Here we will see how to check whether a tree graph is linear or not. A linear tree graph can be expressed in one line, suppose this is an example of a linear tree graph.But this is not linear −To check a graph is linear or not, we can follow two conditionsIf the number of nodes is 1, then the tree graph is linearIf (n – 2) of its nodes have in-degree 2Example Live Demo#include #include #define N 4 using namespace std; class Graph{    private:    int V;    vector *adj;    public:    Graph(int v){     ... Read More

Check if a given string is sum-string in C++

Arnab Chakraborty
Updated on 22-Oct-2019 08:21:51

378 Views

Here we will see how to check whether a string is sum-string or not. A string is said to be sum-string if the rightmost substring can be written as the sum of two substrings before it, and the same is recursively true for substring before it. Suppose a string like 12243660 is a sum string, like 12 + 24 = 36, and the 36 is present after 12 and 24 in the string, again 24 + 36 = 60, this is also present in the string.A string S can be called sum-string, if it follows this rule −𝑠𝑢𝑏𝑠𝑡𝑟𝑖𝑛𝑔(𝑖, 𝑥)+𝑠𝑢𝑏𝑠𝑡𝑟𝑖𝑛𝑔(𝑥+1, 𝑗)= ... Read More

Check if a given directed graph is strongly connected in C++

Arnab Chakraborty
Updated on 22-Oct-2019 08:16:56

410 Views

Suppose we have a graph. We have to check whether the graph is strongly connected or not. A graph is said to be strongly connected, if any two vertices have a path between them, then the graph is connected. An undirected graph is strongly connected graph. Some undirected graph may be connected but not strongly connected. This is an example of a strongly connected graph.This is an example of a connected, but not strongly connected graph.Here we will see, how to check a graph is strongly connected or not using the following steps.Steps −Mark all nodes as not visitedStart DFS ... Read More

How to parse a JSON string using Streaming API in Java?

raja
Updated on 07-Jul-2020 07:40:21

2K+ Views

The Streaming API consists of an important interface JsonParser and this interface contains methods to parse JSON in a streaming way and provides forward, read-only access to JSON data. The Json class contains the methods to create parsers from input sources. We can parse a JSON using the static method createParser() of Json class.Syntaxpublic static JsonParser createParser(Reader reader)Exampleimport java.io.*; import javax.json.Json; import javax.json.stream.JsonParser; import javax.json.stream.JsonParser.Event; public class JSONParseringTest {    public static void main(String[] args) {       String jsonString = "{\"name\":\"Adithya\", \"employeeId\":\"115\", \"age\":\"30\"}";       JsonParser parser = Json.createParser(new StringReader(jsonString));       while(parser.hasNext()) {          Event event = ... Read More

Check if a given circle lies completely inside the ring formed by two concentric circles in C++

Arnab Chakraborty
Updated on 22-Oct-2019 08:12:55

995 Views

We have two circles. The center of both of them is at the origin. The radius of these two circles is given. They are r and R, R > r. Another circle is also present. Its radius (r1) and the center point are given, we have to check whether that point is inside the ring formed by the first two circles or not.We can solve this using the Pythagorean theorem. compute the distance from the center of the circle and origin. Then if (distance – r1) >= r and (distance – r1) = R && dis+r1

Check if a given Binary Tree is SumTree in C++

Arnab Chakraborty
Updated on 22-Oct-2019 08:05:45

238 Views

Here we will see how to check whether a binary tree is sum-tree or not. Now the question is what is the sum-tree. A sum-tree is a binary tree where a node will hold the sum value of its children. The root of the tree will contain an entire sum of all elements below it. This is an example of sum-tree −To check this, we will follow a simple trick, we will find the sum of left and right subtree elements if the sum value is the same as the root, then that is sum-tree. This will be one recursive ... Read More

Check if a given array contains duplicate elements within k distance from each in C++

Arnab Chakraborty
Updated on 22-Oct-2019 08:02:29

451 Views

Here we will see how to check whether an unsorted array has duplicate elements within k distance from each other or not. Suppose a list of elements is {1, 2, 3, 1, 4, 5}, here if k = 3, then the program will return true, as the distance between two 1s is 3.We will solve this using a hash table. The steps will be like below −Create one empty hash tableFor each index i, let the element e = arr[i] in the list, doif e is present in the hash table, then return trueotherwise, add e to hash table, and ... Read More

Check if a doubly linked list of characters is palindrome or not in C++

Arnab Chakraborty
Updated on 22-Oct-2019 07:54:29

736 Views

Here we will see how to check a string is a palindrome or not using a Doubly linked list. Here we will push each character of a string inside one doubly linked list. There will be two pointers, left and right. Then start scanning from both sides. If a left character is the same as right character, then move the left pointer to the next node, and move the right pointer to the previous node. Otherwise, return false. This process will be continued until the left and right are pointing at the same node, or the right pointer is pointing ... Read More

Check if a binary tree is subtree of another binary tree in C++

Arnab Chakraborty
Updated on 22-Oct-2019 07:52:12

797 Views

Suppose we have two binary trees. We have to check whether the smaller tree is a subtree of another binary tree or not. Consider these two trees are given.There are two trees. The second tree is the subtree of the first one. To check this property, we will traverse the tree in post-order fashion, then if the subtree rooted with this node is identical to the second tree, then it is subtree.Example Live Demo#include using namespace std; class node {    public:    int data;    node *left, *right; }; bool areTwoTreeSame(node * t1, node *t2) {    if (t1 ... Read More

Advertisements