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 2 Adja cent list (g) 0-1,2,3 1-0, 2 2-0,1 3- 0,4 4 → 3 3 v[ 0 ] is already visited and *i= parent 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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Please use Java, include comments in the code, and explain what the code is doing. Problem is in the photos.

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 4 steps with 5 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY