Commit 128264a4 authored by trh010's avatar trh010
Browse files

cleaned up code comments and untracked .classpath to remove unnecessary

build dependencies
parent 5db7ce2e
import heap.Heap;
import java.util.ArrayList;
public class AdjacencyList {
/* the adjacency list */
private ArrayList<Vertex> adjList;
/* the adjacency list */
private ArrayList<Vertex> adjList;
/* simple constructor */
public AdjacencyList() {
adjList = new ArrayList<Vertex>();
}
/* simple constructor */
public AdjacencyList() {
adjList = new ArrayList<Vertex>();
}
public void addNode( String newWord ) {
public void addNode(String newWord) {
int newIndex = adjList.size();
ArrayList<Edge> neighbors = findAndSetNeighbors( newWord, newIndex );
Vertex newVertex = new Vertex( newWord, newIndex, neighbors );
adjList.add( newVertex );
}
int newIndex = adjList.size();
ArrayList<Edge> neighbors = findAndSetNeighbors(newWord, newIndex);
Vertex newVertex = new Vertex(newWord, newIndex, neighbors);
adjList.add(newVertex);
}
private ArrayList<Edge> findAndSetNeighbors( String newWord, int newHandle ) {
ArrayList<Edge> neighbors = new ArrayList<Edge>();
for ( int i = 0; i < adjList.size(); i++ ) {
Vertex testVertex = adjList.get( i );
int numDifferences = numDifferences( testVertex.getKey(), newWord );
if ( 2 >= numDifferences ) {
int weight = 0;
if ( numDifferences == 1 )
weight = 1;
else if ( numDifferences == 2 )
weight = 5;
testVertex.addEdge( new Edge( newHandle, weight ) );
neighbors.add( new Edge( i, weight ) );
}
}
return neighbors;
private ArrayList<Edge> findAndSetNeighbors(String newWord, int newHandle) {
ArrayList<Edge> neighbors = new ArrayList<Edge>();
for (int i = 0; i < adjList.size(); i++) {
Vertex testVertex = adjList.get(i);
int numDifferences = numDifferences(testVertex.getKey(), newWord);
if (2 >= numDifferences) {
int weight = 0;
if (numDifferences == 1)
weight = 1;
else if (numDifferences == 2)
weight = 5;
testVertex.addEdge(new Edge(newHandle, weight));
neighbors.add(new Edge(i, weight));
}
}
return neighbors;
}
public int numDifferences( String a, String b ) {
if ( a.length() != b.length() )
System.err
.println( "When testing if two strings are related, they "
+ "should have the same length." );
int numDifferences = 0;
for ( int i = 0; i < a.length(); i++ ) {
if ( a.charAt( i ) != b.charAt( i ) )
numDifferences++;
}
return numDifferences;
public int numDifferences(String a, String b) {
if (a.length() != b.length())
System.err.println("When testing if two strings are related, they "
+ "should have the same length.");
int numDifferences = 0;
for (int i = 0; i < a.length(); i++) {
if (a.charAt(i) != b.charAt(i))
numDifferences++;
}
return numDifferences;
}
public ArrayList<Edge> getEdges( int handle ) {
return adjList.get( handle ).getEdges();
}
public ArrayList<Edge> getEdges(int handle) {
return adjList.get(handle).getEdges();
}
public String neighborsToString( Vertex v ) {
StringBuffer out = new StringBuffer();
for ( int i = 0; i < v.getEdges().size(); i++ ) {
int neighborHandle = v.getEdges().get( i ).toVertex;
String neighborKey = adjList.get( neighborHandle ).getKey();
out.append( neighborKey + " (" + v.getEdges().get( i ).weight
+ "), " );
}
return out.toString();
public String neighborsToString(Vertex v) {
StringBuffer out = new StringBuffer();
for (int i = 0; i < v.getEdges().size(); i++) {
int neighborHandle = v.getEdges().get(i).toVertex;
String neighborKey = adjList.get(neighborHandle).getKey();
out.append(neighborKey + " (" + v.getEdges().get(i).weight + "), ");
}
return out.toString();
}
public String neighborsToString2( Vertex v ) {
StringBuffer out = new StringBuffer();
for ( int i = 0; i < v.getEdges().size(); i++ ) {
int neighborHandle = v.getEdges().get( i ).toVertex;
String neighborKey = adjList.get( neighborHandle ).getKey();
out.append( " " + neighborKey + " ("
+ v.getEdges().get( i ).weight + ") " );
if ( i % 6 == 5 )
out.append( "\n" );
}
return out.toString();
public String neighborsToString2(Vertex v) {
StringBuffer out = new StringBuffer();
for (int i = 0; i < v.getEdges().size(); i++) {
int neighborHandle = v.getEdges().get(i).toVertex;
String neighborKey = adjList.get(neighborHandle).getKey();
out.append(" " + neighborKey + " ("
+ v.getEdges().get(i).weight + ") ");
if (i % 6 == 5)
out.append("\n");
}
return out.toString();
}
/**
*
* @param handle
* - the index in the adjacency list
* @return Vertex - the vertex at the specified index in adj list
*/
public Vertex getVertexFromHandle( int handle ) {
return adjList.get( handle );
}
/**
*
* @param handle
* - the index in the adjacency list
* @return Vertex - the vertex at the specified index in adj list
*/
public Vertex getVertexFromHandle(int handle) {
return adjList.get(handle);
}
/**
* iterates along the adjacency list and returns the index of the vertex
* that has a key that matches s
*
* @param s
* - word you are trying to find
* @return int - the index in the adjacency list of a vertex
*/
public int findStringHandle( String s ) {
for ( int i = 0; i < adjList.size(); i++ ) {
if ( s.equals( adjList.get( i ).getKey() ) )
return i;
}
return -1;
/**
* iterates along the adjacency list and returns the index of the vertex
* that has a key that matches s
*
* @param s
* - word you are trying to find
* @return int - the index in the adjacency list of a vertex
*/
public int findStringHandle(String s) {
for (int i = 0; i < adjList.size(); i++) {
if (s.equals(adjList.get(i).getKey()))
return i;
}
return -1;
}
public String toString() {
StringBuffer out = new StringBuffer();
out.append( "Handle Key | Neighbors \n" );
out.append( "------------------|------------\n" );
for ( int i = 0; i < adjList.size(); i++ ) {
out.append( String.format( "%5d %5s", adjList.get( i )
.getAdjListIndex(), adjList.get( i ).getKey() )
+ " | "
+ neighborsToString( adjList.get( i ) )
+ " "
+ adjList.get( i ).getPred() + "\n" );
}
return out.toString();
public String toString() {
StringBuffer out = new StringBuffer();
out.append("Handle Key | Neighbors \n");
out.append("------------------|------------\n");
for (int i = 0; i < adjList.size(); i++) {
out.append(String.format("%5d %5s", adjList.get(i)
.getAdjListIndex(), adjList.get(i).getKey())
+ " | "
+ neighborsToString(adjList.get(i))
+ " "
+ adjList.get(i).getPred() + "\n");
}
return out.toString();
}
/**
* performs dijkstra's algorithm on the graph starting at startingVertex
*
* @param startingVertex
* - int - the starting vertex
*/
private void buildDijkstraGraph( int startingVertex ) {
Vertex start = adjList.get( startingVertex );
// Initialize graph
Vertex temp;
for ( int i = 0; i < adjList.size(); i++ ) {
temp = adjList.get( i );
temp.setPred( -1 );
temp.setRecord( Integer.MAX_VALUE );
}
start.setRecord( 0 );
// Initialize heap
Heap heap = new Heap();
heap.insert( start );
for ( int i = 0; i < adjList.size(); i++ ) {
if ( start.getAdjListIndex() != i )
heap.insert( adjList.get( i ) );
}
/**
* performs dijkstra's algorithm on the graph starting at startingVertex
*
* @param startingVertex
* - int - the starting vertex
*/
private void buildDijkstraGraph(int startingVertex) {
Vertex start = adjList.get(startingVertex);
// Initialize graph
Vertex temp;
for (int i = 0; i < adjList.size(); i++) {
temp = adjList.get(i);
temp.setPred(-1);
temp.setRecord(Integer.MAX_VALUE);
}
start.setRecord(0);
// Loop through
Vertex adj;
while ( heap.getHeapsize() != 0 ) {
try {
temp = (Vertex) heap.removeMin();
for ( Edge e : temp.getEdges() ) {
adj = getVertexFromHandle( e.toVertex );
if ( (Integer) temp.getRecord() + e.weight < (Integer) adj
.getRecord() ) {
adj.setPred( temp.getAdjListIndex() );
adj.setRecord( (Integer) temp.getRecord() + e.weight );
heap.heapifyUp( adj.getIndex() );
}
}
}
catch ( Exception e ) {
System.err
.println( "trying to remove vertex from empty heap. while loop should have caught this already" );
}
}
// Initialize heap
Heap heap = new Heap();
heap.insert(start);
for (int i = 0; i < adjList.size(); i++) {
if (start.getAdjListIndex() != i)
heap.insert(adjList.get(i));
}
/**
* constructs an arrayList of indices that refer to vertices along the
* shortest path from a start vertex to an end vertex.
*
* @param start
* - the starting vertex
* @param end
* - the ending vertex
* @param path
* - an array list that will be returned at the end, needs to be
* empty
* @return an arraylist of indices that represent the shortest path
*/
private ArrayList<Integer> findPathRev( Vertex start, Vertex end,
ArrayList<Integer> path ) {
if ( end.equals( start ) ) {
path.add( end.getAdjListIndex() );
return path;
// Loop through
Vertex adj;
while (heap.getHeapsize() != 0) {
try {
temp = (Vertex) heap.removeMin();
for (Edge e : temp.getEdges()) {
adj = getVertexFromHandle(e.toVertex);
if ((Integer) temp.getRecord() + e.weight < (Integer) adj
.getRecord()) {
adj.setPred(temp.getAdjListIndex());
adj.setRecord((Integer) temp.getRecord() + e.weight);
heap.heapifyUp(adj.getIndex());
}
}
Vertex next = adjList.get( end.getPred() );
path.add( end.getAdjListIndex() );
} catch (Exception e) {
System.err
.println("trying to remove vertex from empty heap. while loop should have caught this already");
}
}
}
return findPathRev( start, next, path );
/**
* constructs an arrayList of indices that refer to vertices along the
* shortest path from a start vertex to an end vertex.
*
* @param start
* - the starting vertex
* @param end
* - the ending vertex
* @param path
* - an array list that will be returned at the end, needs to be
* empty
* @return an arraylist of indices that represent the shortest path
*/
private ArrayList<Integer> findPathRev(Vertex start, Vertex end,
ArrayList<Integer> path) {
if (end.equals(start)) {
path.add(end.getAdjListIndex());
return path;
}
Vertex next = adjList.get(end.getPred());
path.add(end.getAdjListIndex());
/**
* Constructs the output that represents how a word is transformed from a
* starting word to the ending word
*
* @param reversedPath
* - an ArrayList that was filled in by the findPathReverse
* function
* @return String - how a word is transformed from one to another and
* eventually the ending word
*/
private String pathToString( ArrayList<Integer> reversedPath ) {
StringBuffer out = new StringBuffer();
return findPathRev(start, next, path);
}
for ( int i = reversedPath.size() - 1; i >= 0; i-- ) {
out.append( " " + adjList.get( reversedPath.get( i ) ).getKey() );
if ( ( reversedPath.size() - i ) % 10 == 0 )
out.append( "\n" );
}
/**
* Constructs the output that represents how a word is transformed from a
* starting word to the ending word
*
* @param reversedPath
* - an ArrayList that was filled in by the findPathReverse
* function
* @return String - how a word is transformed from one to another and
* eventually the ending word
*/
private String pathToString(ArrayList<Integer> reversedPath) {
StringBuffer out = new StringBuffer();
return out.toString();
for (int i = reversedPath.size() - 1; i >= 0; i--) {
out.append(" " + adjList.get(reversedPath.get(i)).getKey());
if ((reversedPath.size() - i) % 10 == 0)
out.append("\n");
}
/**
* constructs the output that appears on your screen
*
* @param s1
* - the index of a starting vertex in an adjacency list
* @param s2
* - the index of an ending vertex in an adjacency list
* @return String - the output that appears on your screen
*/
public String outputToString( int s1, int s2 ) {
ArrayList<Integer> revPath = findMinimumPath( s1, s2 );
StringBuffer out = new StringBuffer();
out.append( "The best score for " + adjList.get( s1 ).getKey() + " to "
+ adjList.get( s2 ).getKey() + " is " );
out.append( getDistance( s2 ) + "\n\n" );
out.append( pathToString( revPath ) + "\n" );
return out.toString();
}
return out.toString();
}
/**
* calls buildDijkstraGraph to build the minimum path tree from starting
* vertex s1
*
* @param s1
* - starting vertex
* @param s2
* - ending vertex
* @return - an ArrayList that represents the shortest path from s2 to s1
*/
public ArrayList<Integer> findMinimumPath( int s1, int s2 ) {
buildDijkstraGraph( s1 );
/**
* constructs the output that appears on your screen
*
* @param s1
* - the index of a starting vertex in an adjacency list
* @param s2
* - the index of an ending vertex in an adjacency list
* @return String - the output that appears on your screen
*/
public String outputToString(int s1, int s2) {
ArrayList<Integer> revPath = findMinimumPath(s1, s2);
StringBuffer out = new StringBuffer();
out.append("The best score for " + adjList.get(s1).getKey() + " to "
+ adjList.get(s2).getKey() + " is ");
out.append(getDistance(s2) + "\n\n");
out.append(pathToString(revPath) + "\n");
return out.toString();
}
return findPathRev( adjList.get( s1 ), adjList.get( s2 ),
new ArrayList<Integer>() );
}
/**
* calls buildDijkstraGraph to build the minimum path tree from starting
* vertex s1
*
* @param s1
* - starting vertex
* @param s2
* - ending vertex
* @return - an ArrayList that represents the shortest path from s2 to s1
*/
public ArrayList<Integer> findMinimumPath(int s1, int s2) {
buildDijkstraGraph(s1);
/**
* returns the shortest distance from the starting vertex to end,
* buildDijkstraGraph must have been ran already
*
* @param end
* - the ending vertex
* @return - the shortest distance
*/
public int getDistance( int end ) {
return (Integer) adjList.get( end ).getRecord();
}
return findPathRev(adjList.get(s1), adjList.get(s2),
new ArrayList<Integer>());
}
/**
* returns the shortest distance from the starting vertex to end,
* buildDijkstraGraph must have been ran already
*
* @param end
* - the ending vertex
* @return - the shortest distance
*/
public int getDistance(int end) {
return (Integer) adjList.get(end).getRecord();
}
}
/**
* specifies what an edge is
*
......@@ -8,23 +6,23 @@
*/
public class Edge {
/* the index of the vertice in the adjacency list that the edge goes to */
public int toVertex;
/* the index of the vertice in the adjacency list that the edge goes to */
public int toVertex;
/* weight of the edge */
public int weight;
/* weight of the edge */
public int weight;
/**
* simple constructor
*
* @param toVertex
* - int - index of a vertex in adjacency list
* @param weight
* - int - the weight of the edge (1 or 5)
*/
public Edge( int toVertex, int weight ) {
this.toVertex = toVertex;
this.weight = weight;
}
/**
* simple constructor
*
* @param toVertex
* - int - index of a vertex in adjacency list
* @param weight
* - int - the weight of the edge (1 or 5)
*/
public Edge(int toVertex, int weight) {
this.toVertex = toVertex;
this.weight = weight;
}
}
\ No newline at end of file
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
......@@ -78,7 +76,7 @@ public class Main {
}
in.close();
}
/**
* Queries the user for a Word, then lists the word's neighbors. Continues
* this cycle if desired. Provides the functionality necessary for Part I of
......@@ -91,20 +89,20 @@ public class Main {
System.out.println("Please provide the first 5 letter word: ");
String s1 = in.next();
int s1Handle = adjList.findStringHandle(s1.toUpperCase());
if (s1Handle == -1){
System.out.println("Input string " + s1
if (s1Handle == -1) {
System.out.println("Input string " + s1
+ " is not in the dictionary");
continue;
}
continue;
}
System.out.println("Please provide the second 5 letter word: ");
String s2 = in.next();
int s2Handle = adjList.findStringHandle(s2.toUpperCase());
if (s2Handle == -1){
System.out.println("Input string " + s2
if (s2Handle == -1) {
System.out.println("Input string " + s2
+ " is not in the dictionary");
continue;
}
continue;
}
System.out.println(adjList.outputToString(s1Handle, s2Handle));
boolean validResponse = false;
......@@ -138,7 +136,7 @@ public class Main {
try {
Main main = new Main();
main.createGraph(args[0]);
//main.queryUserPart1();
// main.queryUserPart1();
main.queryUserPart2();
} catch (Exception e) {
System.err.println("Error! - ");
......
import heap.HeapElt;
import java.util.ArrayList;
public class Vertex extends HeapElt {
/* in our case, this is a 5 letter word */
private String key;