Question

Make a DirectedGraph. Then make a function visitAll(Node startNode). This function needs to return every value if it can get through all the nodes without visiting the nodes more than one time. If it cant do this, return null.

Depth-first-search is also needed.

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

import java.util.ArrayList;


public class DirectedGraph {

static class Node //Node class to hold data
{
char data; //Char data
ArrayList<Edge> edges; //List of edges leading out from this vertex
boolean discovered; //Check if discovered in DFS function
public Node(char data) // Constructor
{
edges = new ArrayList<Edge>(); //Initiate edge array
this.data = data; //Store data
discovered = false; //Set discovered to false
}
}

static class Edge //Edge class, stores starting vertext and ending vertex
{
Node start;
Node end;

public Edge(Node start, Node end)
{
this.start = start;
this.end = end;
}
}

private int size; //Holds size of graph (in number of vertices)
private ArrayList<Node> nodes; //Node list for graph (edges contained within nodes

public DirectedGraph() //Constructor, set size to 0 and initiate arraylist of nodes
{
size = 0;
nodes = new ArrayList<Node>();
}

public void addNode(char data) //Add node to array based on data, increment size
{
nodes.add(new Node(data));
size++;
}...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Related Homework Solutions

ChatBot Using Java Programming
Homework Solution
$7.00
Java
Programming
Coding
Chatting
Bot
Users
Computers
Sentences
Symbols
Instant Messaging
Input
Output
Special Characters
Even Numbers
Odd Numbers
Java Program For Vending Machine
Homework Solution
$40.00
Java
Programming
Computer Science
Vending Machine
Drinks
Password
Screen
Inventory
Menu
Errors
Money
Java Programming: Arrays Of Marks & Circles
Homework Solution
$50.00
Java
Programming
Codes
Computer Science
Algorithms
Arrays
Marks
Circles
Geometry
Mathematics
Radius
Random Numbers
Boundaries
Statements
Loops
Variables
Points
Coordinates
Perimeters
Area
Get help from a qualified tutor
Live Chats