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

Pilot Project in Java
Homework Solution
$98.00
Computer Science
Java Programming
Case Studies
Pilot Project
Database Management
Students
College
Courses
Relationships
Associative Entity Table
Classes
Primary Key
Foreign Key
Relational Integrity
Java Programming Questions
Homework Solution
$35.00
Computer Science
Java Programming
Algorithms
Integers
Negative Numbers
Applications
Future
Commands
Java Programming Discussion (390 words)
Homework Solution
$20.00
Computer Science
Java Programming
Hash Tables
Binary Trees
Sets
Maps
Libraries
Applications
Interface
Implementation
Keys
Iterators
Algorithms
Heapsort Using Java
Homework Solution
$13.00
Computer Science
Programming
Java
Heapsort
Methods
Algorithms
Codes
Variables
Statements
Indexes
Loops
Arrays
Tries and Stacks Questions
Homework Solution
$60.00
Computer Science
Algorithms
Tries
Stacks
Data Structures
Functions
Loops
Statements
Strings
ASCII Characters
Nodes
Arrays
Get help from a qualified tutor
Live Chats