Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Read sample code Graph and GraphTester, use them to create and test the graph as in the picture. import java.util.*; public class Graph { private

Read sample code Graph and GraphTester, use them to create and test the graph as in the picture.

import java.util.*; public class Graph { private final int V; private int E; private ArrayList[] adj; //constructor public Graph(int V) { if (V < 0) throw new IllegalArgumentException( "Number of vertices must be nonnegative"); this.V = V; this.E = 0; adj = (ArrayList[]) new ArrayList[V]; for (int v = 0; v < V; v++) { adj[v] = new ArrayList(); } } //getters public int V() { return V; } public int E() { return E; } public ArrayList getAdj(int v) { return adj[v]; } //set edges, undirected graph public void addEdge(int v, int u) { E++; adj[v].add(u); adj[u].add(v); } public String toString() { StringBuilder s = new StringBuilder(); s.append(V + " vertices, " + E + " edges "); for (int v = 0; v < V; v++) { s.append(v + ": "); for (Integer w : adj[v]) { s.append(w + " "); } s.append(" "); } return s.toString(); } }

________________________________________________________________________________________________

import java.util.ArrayList; public class GraphTester { public static void main(String[] args) { Graph g = new Graph(5); g.addEdge(0, 1); g.addEdge(0, 2); g.addEdge(0, 3); g.addEdge(0, 4); g.addEdge(1, 2); g.addEdge(2, 3); System.out.println(g); int V = g.V(); boolean [] marked = new boolean[V]; DFS(g, 0 , marked); } public static void DFS(Graph g, int v, boolean [] marked) { int i, u; marked[v] = true; ArrayList adjV = g.getAdj(v); for (i = 0; i < adjV.size(); i++ ) { u = adjV.get(i); if (!marked[u]) { System.out.print(v + "-" + u + " "); DFS(g, u, marked); } } } }student submitted image, transcription available below

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database Technology

Authors: Suad Alagic

1st Edition

354096276X, 978-3540962762

Students also viewed these Databases questions

Question

1. Communicating courses and programs to employees.

Answered: 1 week ago

Question

6. Testing equipment that will be used in instruction.

Answered: 1 week ago