Detect cycle in a directed graph17 Mar 2025 | 4 min read In a directed graph, we will check whether the graph contains cycle or not. A directed graph is a set of vertices or nodes connected by edges, and each edge is associated with some direction. Consider the below directed graph to detect the cycle. ![]() Now, we will use the DFS technique to detect cycles in a directed graph. As we are using the DFS technique, so we will use the stack data structure for the implementation. Here, we will use the flag variable that contains three values, i.e., 0, 1, and -1. Here, 0 means that the node is visited and available in the stack, -1 means that the node is unvisited, and 1 means that the node is visited and has been popped out from the stack. Initially, all the vertices are marked with -1 as they all are not visited. Step 1: First, we will visit vertex A and will be marked as 0. Since node A has been visited so it will be marked as 0, and node A is pushed into the stack shown as below: ![]() ![]() The visited set contains the node A shown as below: Visited set: A The parent map table is given below: ![]() Since node A has been visited, A comes under the vertex column and the parent column is left blank as node A is a source vertex. Step 2: The next vertex is B. Now, we will visit vertex B and will be marked as 0. Since the node B has been visited so it will be marked as 0, and node B is pushed into the stack shown as below: ![]() ![]() The visited node contains the nodes A and B shown as below: Visited set: A, B The parent map table is shown below: ![]() Since node B has been visited, so B comes under the vertex column, and A comes under the parent column as B comes from node A. Step 3: The adjacent vertices of B are C and D means we can either visit C or D. Suppose we visit vertex C, so vertex C will be marked as 0 and node C is pushed into the stack shown as below: ![]() ![]() Now, the visited set contains the nodes A, B, and C shown as below: Visited set: A, B, C The parent map table is shown below: ![]() Since node C has been visited, so C comes under the vertex column, and B comes under the parent column. Step 4: There are no further vertices to be visited from C, so we will perform backtracking. In order to perform backtracking, we will pop the node. First, we will pop node C from the stack. Since node C has been popped out, so we will mark the node C as 1 shown as below: ![]() The next topmost node in the stack is B, so we will backtrack to the vertex B shown as below: Step 5: Now, we will see 'is there any adjacent vertex left to be visited'. We can observe in the above graph that the vertex D is left unvisited. So, now we will move from the vertex B to the vertex D. The flag value of vertex D is now changed to 0, and the vertex D is pushed into the stack shown as below: ![]() ![]() Now the visited set contains the nodes A, B, C, D The parent map table is shown below: ![]() Since node D has been visited, it comes under the vertex column, and the node D has arrived from vertex B, so vertex B comes under the parent column. Step 6: The adjacent vertex of node D is node E which is left unvisited. Now we will visit the vertex E and will mark its flag value as 0. The node E is pushed into the stack shown as below: ![]() ![]() Now, the visited set contains the nodes A, B, C, D, E. The parent map table is shown below: ![]() Since node E has been visited, it comes under the vertex column, and node E has arrived from the vertex D, so D comes under the parent column. Condition There is one condition that determines whether the graph contains a cycle or not. If the adjacent vertex of any vertex is having a 0 flag value means that the graph contains a cycle. In the above graph, the adjacent vertex of E is B, and its flag value is 0; therefore, the graph contains a cycle. Now we will determine the nodes that create a cycle in a graph. The adjacent vertex of E is B; E->B The parent of E is D so; D->E->B The parent of D is B so, B->D->E->B (creates a cycle) Next TopicOptimal Binary Search Tree |
Create a function that would reverse every t nodes in a linked list (where t is an input to the function). Example: • Input: 11->12->13->14->15->16->17->18->NULL, t = 3 Output: 13->12->11->16->15->14->18->17->NULL • Input: 11->12->13->14->15->16->17->18->NULL, t = 5 Output: 15->14->13->12->11->18->17->16->NULL Algorithm: reverse(head, t) Reverse the first...
4 min read
Introduction An effective algorithmic technique for effectively resolving problems involving arrays and strings is the sliding window approach. Finding contiguous subarrays or substrings that meet specific requirements is a task that it is particularly well suited for. The sliding window technique enables us to efficiently update the window...
5 min read
Linked lists are fundamental data structures used in computer science and programming interviews. They provide an efficient way to store and access sequential data. A key challenge with linked lists is sorting their elements efficiently. Unlike arrays, linked lists only provide sequential access to nodes. We can't...
7 min read
As a way for computer science enthusiasts to demonstrate their problem-solving abilities, competitive programming has become incredibly popular in recent years. In competitive programming, bracket issues are one of the most prominent and fascinating categories. Competing programmers must be proficient in these issues since they require...
6 min read
B-tree and B+ trees are typically used to achieve dynamic multilevel indexing. However, the disadvantage of the B-tree used for indexing is that it also keeps the data pointer (a pointer to the disc file block containing the key value), corresponding to a certain key value,...
20 min read
Practical Byzantine Fault Tolerance (pBFT) is a type of consequence algorithm. It was introduced by Barbara Liskov and Miguel Castro in the 90s. It was designed to perform the work operation efficiently. It is optimized to work on low time. Its main goal is to solve...
4 min read
In a binary search tree, the concept of floor and ceiling play a vital role in looking for the elements that may not exist in the tree. The floor of the given value in the binary search tree points out the biggest value that is smaller...
6 min read
, named after the Colombian mathematician Bernardo Recaman Santos, is a fascinating integer sequence that has captured the imagination of mathematicians and computer scientists alike. It's defined by a simple yet intriguing rule, making it an excellent Java exploration topic. Understanding Recamán's Sequence starts with the first...
6 min read
Data structures are specific layouts used to arrange, handle, and retain data within a computer system. They offer a method to effectively handle substantial quantities of data, enabling simple retrieval and alterations. Sophisticated data structures like trees, graphs, hash tables, and heaps go beyond what simple...
10 min read
Introduction Coders who enjoy a fast-paced, competitive setting can demonstrate their problem-solving skills in competitive programming, an exciting arena. In order to effectively traverse the complexities of algorithmic problems, one needs to make use of the capabilities of multiple data structures, with the simple queue standing tall...
9 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India