Write a JAVA program of A Data File Structure for a connected graph that produces a tree. There is a cycle in a graph only if there is a back edge present in the graph. A back edge is an edge that is joining a node to itself (self-loop) or one of its ancestor in the tree produced by Data File Structure. To find the back edge to any of its ancestor keep a visited array and if there is a back edge to any visited node then there is a loop and return true. Algorithm: 1. Create the graph using the given number of edges and vertices. 2. Create a recursive function that that current index or vertex, visited and recursion stack. 3. Mark the current node as visited and also mark the index in recursion stack. 4. Find all the vertices which are not visited and are adjacent to the current node. Recursively call the function for those vertices, if the recursive function returns true return true. 5. If the adjacent vertices are already marked in the recursion stack then return true. 6. Create a wrapper class, that calls the recursive function for all the vertices and if any function returns true, return true. 7. Else if for all vertices the function returns false return false. 1 Adja cent list (g) 0-1,2,3 1-0,2 2-0, 1 3- 0,4 4-3 3

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question
100%

Please help with this Java program, and include explanations and comments

Write a JAVA program of A Data File Structure for a connected graph that produces a tree. There
is a cycle in a graph only if there is a back edge present in the graph. A back edge is an edge that
is joining a node to itself (self-loop) or one of its ancestor in the tree produced by Data File
Structure.
To find the back edge to any of its ancestor keep a visited array and if there is a back edge to any
visited node then there is a loop and return true.
Algorithm:
1. Create the graph using the given number of edges and vertices.
2. Create a recursive function that that current index or vertex, visited and recursion
stack.
3. Mark the current node as visited and also mark the index in recursion stack.
4. Find all the vertices which are not visited and are adjacent to the current node.
Recursively call the function for those vertices, if the recursive function returns true
return true.
5. If the adjacent vertices are already marked in the recursion stack then return true.
6. Create a wrapper class, that calls the recursive function for all the vertices and if any
function returns true, return true.
7. Else if for all vertices the function returns false return false.
2
0
Adja cent list (g) 01, 2, 3
1-0, 2
2-0, 1
3- 0,4
4-3
3
v[ 0 ] is already
visited and *i= parent
4
isCyclicUtil( 0,-1), vis[ 0 ] = true
isCyclicUtil(1,0), vis[ 1 ] = true
2
iscyclicutil(2,1), vis[ 2 ] = true
v[ 0 ] is already visited and *i!= parent
cycle found
Transcribed Image Text:Write a JAVA program of A Data File Structure for a connected graph that produces a tree. There is a cycle in a graph only if there is a back edge present in the graph. A back edge is an edge that is joining a node to itself (self-loop) or one of its ancestor in the tree produced by Data File Structure. To find the back edge to any of its ancestor keep a visited array and if there is a back edge to any visited node then there is a loop and return true. Algorithm: 1. Create the graph using the given number of edges and vertices. 2. Create a recursive function that that current index or vertex, visited and recursion stack. 3. Mark the current node as visited and also mark the index in recursion stack. 4. Find all the vertices which are not visited and are adjacent to the current node. Recursively call the function for those vertices, if the recursive function returns true return true. 5. If the adjacent vertices are already marked in the recursion stack then return true. 6. Create a wrapper class, that calls the recursive function for all the vertices and if any function returns true, return true. 7. Else if for all vertices the function returns false return false. 2 0 Adja cent list (g) 01, 2, 3 1-0, 2 2-0, 1 3- 0,4 4-3 3 v[ 0 ] is already visited and *i= parent 4 isCyclicUtil( 0,-1), vis[ 0 ] = true isCyclicUtil(1,0), vis[ 1 ] = true 2 iscyclicutil(2,1), vis[ 2 ] = true v[ 0 ] is already visited and *i!= parent cycle found
import java.io.*;
import java.util. *;
// This class represents a
// directed graph using adjacent list Representation.
class Graph
{
// No. of vertices
private int V;
// Adjacency List Representation
private LinkedList<Integer> adj [];
// Constructor
Graph (int v)
{
}
V = v;
adj = new LinkedList [v];
for (int i=0; i<v; ++i)
adj [i] new LinkedList ();
=
Your code continues below
Transcribed Image Text:import java.io.*; import java.util. *; // This class represents a // directed graph using adjacent list Representation. class Graph { // No. of vertices private int V; // Adjacency List Representation private LinkedList<Integer> adj []; // Constructor Graph (int v) { } V = v; adj = new LinkedList [v]; for (int i=0; i<v; ++i) adj [i] new LinkedList (); = Your code continues below
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning