Java Program and Prims Algorithm Computer Science

User Generated

WbaWbarf840

Programming

Description

Include all pertinent files in a word doc. Also be sure sure to run a j unit test and include the test result. Follow the prompt attached and use the code provided.

Unformatted Attachment Preview

package ch09; //---------------------------------------------------------------------------// UseGraph.java by Dale/Joyce/Weems Chapter 9 // // Examples of uses of the Graph ADT. //---------------------------------------------------------------------------import ch05.queues.*; import ch03.stacks.*; import ch09.graphs.*; // remove answers. import ch09.priorityQueues.*; import support.Flight; public class UseGraph { private static void shortestPaths(WeightedGraphInterface graph, String startVertex ) // Writes the shortest distance from startVertex to every // other reachable vertex in graph. { Flight flight; Flight saveFlight; // for saving on priority queue int minDistance; int newDistance; PriQueueInterface pq = new Heap(20); // Assume at most 20 vertices String vertex; UnboundedQueueInterface vertexQueue = new LinkedUnbndQueue(); graph.clearMarks(); saveFlight = new Flight(startVertex, startVertex, 0); pq.enqueue(saveFlight); System.out.println("Last Vertex Destination Distance"); System.out.println("------------------------------------"); do { flight = pq.dequeue(); if (!graph.isMarked(flight.getToVertex())) { graph.markVertex(flight.getToVertex()); System.out.println(flight); flight.setFromVertex(flight.getToVertex()); minDistance = flight.getDistance(); vertexQueue = graph.getToVertices(flight.getFromVertex()); while (!vertexQueue.isEmpty()) { vertex = vertexQueue.dequeue(); if (!graph.isMarked(vertex)) { newDistance = minDistance + graph.weightIs(flight.getFromVertex(), vertex); saveFlight = new Flight(flight.getFromVertex(), vertex, newDistance); pq.enqueue(saveFlight); } } } } while (!pq.isEmpty()); System.out.println(); System.out.println("The unreachable vertices are:"); vertex = graph.getUnmarked(); while (vertex != null) { System.out.println(vertex); graph.markVertex(vertex); vertex = graph.getUnmarked(); } System.out.println(); } private static boolean isPath(WeightedGraphInterface graph, String startVertex, String endVertex ) // Returns true if a path exists on graph, from startVertex to endVertex; // otherwise returns false. Uses depth-first search algorithm. { UnboundedStackInterface stack = new LinkedStack(); UnboundedQueueInterface vertexQueue = new LinkedUnbndQueue(); boolean found = false; String vertex; String item; graph.clearMarks(); stack.push(startVertex); do { vertex = stack.top(); stack.pop(); if (vertex == endVertex) found = true; else { if (!graph.isMarked(vertex)) { graph.markVertex(vertex); vertexQueue = graph.getToVertices(vertex); while (!vertexQueue.isEmpty()) { item = vertexQueue.dequeue(); if (!graph.isMarked(item)) stack.push(item); } } } } while (!stack.isEmpty() && !found); return found; } private static boolean isPath2(WeightedGraphInterface graph, String startVertex, String endVertex ) // Returns true if a path exists on graph, from startVertex to endVertex; // otherwise returns false. Uses breadth-first search algorithm. { UnboundedQueueInterface queue = new LinkedUnbndQueue(); UnboundedQueueInterface vertexQueue = new LinkedUnbndQueue(); boolean found = false; String vertex; String item; graph.clearMarks(); queue.enqueue(startVertex); do { vertex = queue.dequeue(); if (vertex == endVertex) found = true; else { if (!graph.isMarked(vertex)) { graph.markVertex(vertex); vertexQueue = graph.getToVertices(vertex); while (!vertexQueue.isEmpty()) { item = vertexQueue.dequeue(); if (!graph.isMarked(item)) queue.enqueue(item); } } } } while (!queue.isEmpty() && !found); return found; } public static void main(String[] args) { WeightedGraphInterface graph = new WeightedGraph(); String s0 = new String("Atlanta "); String s1 = new String("Austin "); String s2 = new String("Chicago "); String s3 = new String("Dallas "); String s4 = new String("Denver "); String s5 = new String("Houston "); String s6 = new String("Washington"); graph.addVertex(s0); graph.addVertex(s1); graph.addVertex(s2); graph.addVertex(s3); graph.addVertex(s4); graph.addVertex(s5); graph.addVertex(s6); graph.addEdge(s0, s5, 800); graph.addEdge(s0, s6, 600); graph.addEdge(s1, s3, 200); graph.addEdge(s1, s5, 160); graph.addEdge(s2, s4, 1000); graph.addEdge(s3, s1, 200); graph.addEdge(s3, s2, 900); graph.addEdge(s3, s4, 780); graph.addEdge(s4, s0, 1400); graph.addEdge(s4, s2, 1000); graph.addEdge(s5, s0, 800); graph.addEdge(s6, s0, 600); graph.addEdge(s6, s3, 1300); boolean result; System.out.println("depth first ..."); result = isPath(graph, s1, s2); System.out.println("s1 s2 " + result); result = isPath(graph, s1, s6); System.out.println("s1 s6 " + result); result = isPath(graph, s6, s5); System.out.println("s6 s5 " + result); result = isPath(graph, s6, s3); System.out.println("s6 s3 " + result); result = isPath(graph, s6, s1); System.out.println("s6 s1 " + result); System.out.println(); System.out.println("breadth first ..."); result = isPath2(graph, s1, s2); System.out.println("s1 s2 " + result); result = isPath2(graph, s1, s6); System.out.println("s1 s6 " + result); result = isPath2(graph, s6, s5); System.out.println("s6 s5 " + result); result = isPath2(graph, s6, s3); System.out.println("s6 s3 " + result); result = isPath2(graph, s6, s1); System.out.println("s6 s1 " + result); System.out.println(); shortestPaths(graph, s6); System.out.println(); System.out.println(); shortestPaths(graph, s4); System.out.println(); System.out.println(); System.out.println("a new graph without Wash - Dallas leg"); System.out.println(); graph = new WeightedGraph(); s0 = new String("Atlanta "); s1 = new String("Austin "); s2 = new String("Chicago "); s3 = new String("Dallas "); s4 = new String("Denver "); s5 = new String("Houston "); s6 = new String("Washington"); graph.addVertex(s0); graph.addVertex(s1); graph.addVertex(s2); graph.addVertex(s3); graph.addVertex(s4); graph.addVertex(s5); graph.addVertex(s6); graph.addEdge(s0, s5, 800); graph.addEdge(s0, s6, 600); graph.addEdge(s1, s3, 200); graph.addEdge(s1, s5, 160); graph.addEdge(s2, s4, 1000); graph.addEdge(s3, s1, 200); graph.addEdge(s3, s2, 900); graph.addEdge(s3, s4, 780); graph.addEdge(s4, s0, 1400); graph.addEdge(s4, s2, 1000); graph.addEdge(s5, s0, 800); graph.addEdge(s6, s0, 600); // graph.addEdge(s6, s3, 1300); System.out.println("depth first ..."); result = isPath(graph, s1, s2); System.out.println("s1 s2 " + result); result = isPath(graph, s1, s6); System.out.println("s1 s6 " + result); result = isPath(graph, s6, s5); System.out.println("s6 s5 " + result); result = isPath(graph, s6, s3); System.out.println("s6 s3 " + result); result = isPath(graph, s6, s1); System.out.println("s6 s1 " + result); System.out.println(); System.out.println("breadth first ..."); result = isPath2(graph, s1, s2); System.out.println("s1 s2 " + result); result = isPath2(graph, s1, s6); System.out.println("s1 s6 " + result); result = isPath2(graph, s6, s5); System.out.println("s6 s5 " + result); result = isPath2(graph, s6, s3); System.out.println("s6 s3 " + result); result = isPath2(graph, s6, s1); System.out.println("s6 s1 " + result); System.out.println(); shortestPaths(graph, s6); System.out.println(); System.out.println(); shortestPaths(graph, s4); } } //---------------------------------------------------------------------------// WeightedGraphInterface.java by Dale/Joyce/Weems Chapter 9 // // Interface for a class that implements a directed graph with weighted edges. // Vertices are objects of class T and can be marked as having been visited. // Edge weights are integers. // Equivalence of vertices is determined by the vertices' equals method. // // General precondition: Except for the addVertex and hasVertex methods, // any vertex passed as an argument to a method is in this graph. //---------------------------------------------------------------------------package ch09.graphs; import ch05.queues.*; public interface WeightedGraphInterface { boolean isEmpty(); // Returns true if this graph is empty; otherwise, returns false. boolean isFull(); // Returns true if this graph is full; otherwise, returns false. void addVertex(T vertex); // Preconditions: This graph is not full. // Vertex is not already in this graph. // Vertex is not null. // // Adds vertex to this graph. boolean hasVertex(T vertex); // Returns true if this graph contains vertex; otherwise, returns false. void addEdge(T fromVertex, T toVertex, int weight); // Adds an edge with the specified weight from fromVertex to toVertex. int weightIs(T fromVertex, T toVertex); // If edge from fromVertex to toVertex exists, returns the weight of edge; // otherwise, returns a special �null-edge� value. UnboundedQueueInterface getToVertices(T vertex); // Returns a queue of the vertices that are adjacent from vertex. void clearMarks(); // Sets marks for all vertices to false. void markVertex(T vertex); // Sets mark for vertex to true. boolean isMarked(T vertex); // Returns true if vertex is marked; otherwise, returns false. T getUnmarked(); // Returns an unmarked vertex if any exist; otherwise, returns null. } Use the Dale 3e weighted graph interface to do the following. 1. (20 points) (Max 10 if not completed in class today.) Hack UseGraph.java to find the shortest path from A to Z on the following graphs. (These graphs are the tests for this part.) All edges below are undirected, but WeightedGraphInterface uses directed edges. Two opposing directed edges are equivalent to one undirected edge. Because I'm lazy, I made a method addUndirectedEdge in WeightedGraphInterface, so I wouldn't have to manually add the opposing edge. B 3 E 10 8 SH 8 С 9 A 12 N co 14 17 1 F 4 H A. 7 B A 5 B 9 8 4 8 5 8 2 7 11 14 13 15 D 4 3 G Z 9 C D 11 G E 3 11 5 15 9 12 7 11 13 E J F 4 6 Z F 2. (30 points) Implement Prim's algorithm for solving the undirected minimum spanning tree problem. Print out the order in which the edges are added to the tree. Test on the above graphs. 3. Printed on paper to hand in: all pertinent files. Please number your pages and complete the Table of Contents.
Purchase answer to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

Here is the answer.

Table of contents
UseGraph: ................................................................................................................................................ 1
WeightedGraphInterface.java ................................................................................................................. 7
WeightedGraph.java ............................................................................................................................... 8
Test files................................................................................................................................................. 13

0

UseGraph:
package ch09;
//--------------------------------------------------------------------------// UseGraph.java
by Dale/Joyce/Weems
Chapter 9
//
// Examples of uses of the Graph ADT.
//--------------------------------------------------------------------------import ch05.queues.*;
import ch03.stacks.*;
import ch09.graphs.WeightedGraph;
import ch09.graphs.WeightedGraphInterface;
import ch09.priorityQueues.*;
import support.Flight;
public class UseGraph {
private static void shortestPaths(WeightedGraphInterface graph,
String startVertex) throws PriQOverflowException // Writes the
shortest distance from startVertex to every
// other reachable vertex in graph.
{
Flight flight;
Flight saveFlight;
// for saving on priority queue
int minDistance;
int newDistance;
PriQueueInterface pq = new Heap(20);
most 20 vertices
String vertex;
UnboundedQueueInterface vertexQueue = new
LinkedUnbndQueue();

// Assume at

graph.clearMarks();
saveFlight = new Flight(startVertex, startVertex, 0);
pq.enqueue(saveFlight);
System.out.println("Last Vertex
Destination
Distance");
System.out.println("------------------------------------");
do {
try {
flight = pq.dequeue();
if (!graph.isMarked(flight.getToVertex())) {
graph.markVertex(flight.getToVertex());
System.out.println(flight);
flight.setFromVertex(flight.getToVertex());
minDistance = flight.getDistance();
vertexQueue =
graph.getToVertices(flight.getFromVertex());
while (!vertexQueue.isEmpty()) {
vertex = vertexQueue.dequeue();
if (!graph.isMarked(vertex)) {
newDistance = minDistance
+

1

graph.weightIs(flight.getFromVertex(), vertex);
saveFlight = new Flight(flight.getFromVertex(),
vertex, newDistance);
pq.enqueue(saveFlight);
}
}
}
} catch (Exception e) {
}
} while (!pq.isEmpty());
System.out.println();
System.out.println("The unreachable vertices are:");
vertex = graph.getUnmarked();
while (vertex != null) {
System.out.println(vertex);
graph.markVertex(vertex);
vertex = graph.getUnmarked();
}
System.out.println();
}
private static boolean isPath(WeightedGraphInterface graph,
String startVertex,
String endVertex) throws StackUnderflowException,
QueueUnderFlowException // Returns true if a path exists on graph, from
startVertex to endVertex;
// otherwise returns false. Uses depth-first search algorithm.
{
UnboundedStackInterface stack = new LinkedStack();
UnboundedQueueInterface vertexQueue = new
LinkedUnbndQueue();
boolean found = false;
String vertex;
String item;
graph.clearMarks();
stack.push(startVertex);
do {
vertex = stack.top();
stack.pop();
if (vertex == endVertex) {
found = true;
} else {
if (!graph.isMarked(vertex)) {
graph.markVertex(vertex);
vertexQueue = graph.getToVertices(vertex);
while (!vertexQueue.isEmpty()) {
item = vertexQueue.dequeue();
if (!graph.isMarked(item)) {
stack.push(item);
}
}
}
}
} while (!stack.isEmpty() && !found);
return found;
}

2

private static boolean isPath2(WeightedGraphInterface graph,
String startVertex,
String endVertex) throws QueueUnderFlowException // Returns
true if a path exists on graph, from startVertex to endVertex;
// otherwise returns false. Uses breadth-first search algorithm.
{
UnboundedQueueInterface queue = new
LinkedUnbndQueue();
UnboundedQueueInterface vertexQueue = new
LinkedUnbndQueue();
boolean found = false;
String vertex;
String item;
graph.clearMarks();
queue.enqueue(startVertex);
do {
vertex = queue.dequeue();
if (vertex == endVertex) {
found = true;
} else {
if (!graph.isMarked(vertex)) {
graph.markVertex(vertex);
vertexQueue = graph.getToVertice...


Anonymous
Great study resource, helped me a lot.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags