Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please complete the DFS method. here\'s part 1 of the question: https://www.cheg

ID: 3820310 • Letter: P

Question

Please complete the DFS method.

here's part 1 of the question: https://www.chegg.com/homework-help/questions-and-answers/please-complete-completegraph-method-q20794417

here's part 2 of the question: https://www.chegg.com/homework-help/questions-and-answers/please-complete-valence-method-s-part-1-question-https-wwwcheggcom-homework-help-questions-q20794561

1 import java .ut ArrayList 6 Generic vertex class 7 class Vertex public T data public boolean visited 10e public Vertex data null visited false 130 public Vertex(T data data -data; visited false 16e public string tostring t return data. tos tring 19 Declare any additional vertex attribute you may need 20 22 public class Graph T 23 array of vertices 24 protected ArrayList verts 25 26 20 array representing adjacency matrix of an unweighted graph If adj Mat is true, there is an edge from verte i to j 27 otherwise the element adj Mat l is false. 28 29 protected boolean adj Mat 30 32 public Graph ArrayList tex> verts boolean -mat) check the validity of input parameters 33 int nverts J verts 34 adjacency matrix must be square matrix of NxN 35 if Jmat. ength nverts return 36 for (int i30; i nverts i++) 37 if -mat ength nverts return 38

Explanation / Answer

class Graph
{
private int V; // No. of vertices

// Array of lists for 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();
}

//Function to add an edge into the graph
void addEdge(int v, int w)
{
adj[v].add(w); // Add w to v's list.
}

// A function used by DFS
void DFSUtil(int v,boolean visited[])
{
// Mark the current node as visited and print it
visited[v] = true;
System.out.print(v+" ");

// Recur for all the vertices adjacent to this vertex
Iterator<Integer> i = adj[v].listIterator();
while (i.hasNext())
{
int n = i.next();
if (!visited[n])
DFSUtil(n, visited);
}
}

// The function to do DFS traversal. It uses recursive DFSUtil()
void DFS(int v)
{
// Mark all the vertices as not visited(set as
// false by default in java)
boolean visited[] = new boolean[V];

// Call the recursive helper function to print DFS traversal
DFSUtil(v, visited);
}

public static void main(String args[])
{
Graph g = new Graph(4);

g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);

System.out.println("Following is Depth First Traversal "+
"(starting from vertex 2)");

g.DFS(2);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote