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
Programming Articles - Page 2651 of 3366
689 Views
In this article, we will learn what is topological sorting, how to use it to detect cycles in a directed graph, and how to implement it in C++. What is Topological Sort? Topological sorting is an operation used to detect cycle in a graph. In this operation we order the vertices in such a way that for every directed edge u -> v, vertex u comes before vertex v in the ordering. If we are able to perform a topological sort in a graph, it means that the graph is a directed acyclic graph (DAG). The image below show ... Read More
479 Views
In this problem, we are given adjacency lists of a directed graph and we need to check if there is a cycle in the graph using topological sort. If a cycle exists, it is not possible to perform a topological sort. Example: // Input Graph ( as adjacency list ) 0 -> 1 1 -> 2 1 -> 3 2 -> 0 Output: Cycle exists Explanation: The graph has a cycle (0 -> 1 -> 2 -> 0). To solve this problem, we can use Khan's Algorithm, which is a BFS based topological sorting algorithm. To ... Read More
179 Views
We are given an array of integers containing the degree of each vertex in a graph. Our task is to check if it is possible to construct a graph with the given degree sequence. Example: int degrees[] = {3, 2, 2, 0} Output: Not Possible Explanation: The first vertex has degree 3, which means it must be connected to three other vertices. But the last vertex has degree 0, meaning it cannot be connected to any other vertex. Hence, it is impossible to construct a graph with this degree sequence. To implement this in C++, we can ... Read More
609 Views
In this article, we will learn how to print a diamond shape with 2n rows and n columns for a given size n using C++. For example, if n = 4, the diamond shape will look like this: Algorithm to Print Diamond Shape To print the diamond shape, we can follow these steps: Take an integer input n from the user. To print upper half of the diamond, use a loop that runs from i = 1 to i
296 Views
In this article, we will explain the Vizing's theorem and implement it in C++ to color a graph using the Greedy Coloring Algorithm. What is Vizing's Theorem? Vizing's theorem states that for any graph, the minimum number of colors needed to color the edges (chromatic index) is either equal to the maximum degree G of the graph or one more than maximum degree G + 1. The degree of a vertex is the number of edges connected to it. The maximum degree G refer to highest degree for any vertex in the graph. It is ... Read More
754 Views
In this article, we will explain the 4 color problem to color a graph and implement the backtracking algorithm to solve it in C++. The 4 Color Problem The 4-color problem states that the maximum number of colors needed to color any planar graph (or a 2D map) is four, such that no two adjacent nodes have the same color. For example, suppose that you want to color a world map, such that no two countries sharing a border have the same color. According to this theorem, the maximum number of colors needed to do this is four. Now, ... Read More
450 Views
In this article, we will explain the dominating set problem and implement it's solution in C++. First of all, let's understand what a dominating set is. Dominating Set of a Graph A dominating set for a graph is a subset of the set of all the vertices. Every vertex that is not in the dominating set should be adjacent of at least one vertex in the dominating set. To understand this clearly, consider the following graph: In the above graph, the set of vertices {B, D} is one of the dominating sets, because: ... Read More
291 Views
The subsets of a set refers to all the possible combinations of choosing elements from the set. For example, if we have a set {1, 2}, the subsets are {}, {1}, {2}, and {1, 2}. In this article, we will learn how to generate a random subset for a set using coin flipping technique in C++. // Set of elements int arr[] = {1, 2, 3}; // Subsets of the above set {}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3} Coin Flipping Technique The coin flipping technique is a simple way ... Read More
1K+ Views
The subsets of a set refers to all the possible combinations of choosing elements from the set. For example, if we have a set {1, 2}, the subsets are {}, {1}, {2}, and {1, 2}. In this article, we will learn how to generate subsets of a set using the Binary Counting Method in C++. // Set of elements int arr[] = {1, 2, 3}; // Subsets of the above set {}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3} Binary Counting Method The binary counting method is a technique used to generate ... Read More
138 Views
This is a C++ program to implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for elements from 1 to NAlgorithmsBegin function AlexanderBogomolny() to implement the Algorithms Arguments: Val[] = an array N = number of elements taken as input. K = level Body of the function: intialize l = -1 l = l+1 Val[k] = l if (l == N) Call function display(Val, N) else ... Read More