Commit c6508433 authored by trh010's avatar trh010
Browse files

working

parent 09039f55
BEAST BLAST BOAST BOATS
COAST COATS CODES COSTS
ROADS ROAST TOADS TOAST
......@@ -30,10 +30,10 @@ public class Heap {
HeapElt temp = array[pos2];
array[pos2] = array[pos1];
array[pos2].setAdjListIndex(pos2);
array[pos2].setHandle(pos2);
array[pos1] = temp;
array[pos1].setAdjListIndex(pos1);
array[pos1].setHandle(pos1);
}
......@@ -45,7 +45,7 @@ public class Heap {
* Running time = O(n)
*/
private void doubleHeap() {
System.out.println("Doubling size");
//System.out.println("Doubling size");
HeapElt[] newArray = new HeapElt[2 * array.length];
for (int i = 0; i < array.length; i++)
......@@ -121,7 +121,7 @@ public class Heap {
heapsize++;
array[heapsize] = inElt;
array[heapsize].setAdjListIndex(heapsize);
array[heapsize].setHandle(heapsize);
heapifyUp(heapsize);
}
......
......@@ -20,7 +20,7 @@ public class HeapElt {
return record;
}
public void setAdjListIndex(int inHandle) {
public void setHandle(int inHandle) {
handle = inHandle;
}
......
......@@ -13,9 +13,10 @@ public class AdjacencyList {
}
public void addNode(String newWord) {
int newHandle = adjList.size();
ArrayList<Edge> neighbors = findAndSetNeighbors(newWord, newHandle);
Vertex newVertex = new Vertex(newWord, newHandle, neighbors);
int newIndex = adjList.size();
ArrayList<Edge> neighbors = findAndSetNeighbors(newWord, newIndex);
Vertex newVertex = new Vertex(newWord, newIndex, neighbors);
adjList.add(newVertex);
}
......@@ -94,8 +95,8 @@ public class AdjacencyList {
out.append("------------------|------------\n");
for (int i = 0; i < adjList.size(); i++) {
out.append(String
.format("%5d %5s", i, adjList.get(i).getKey())
+ " | " + neighborsToString(adjList.get(i)) + "\n");
.format("%5d %5s", adjList.get(i).getAdjListIndex(), adjList.get(i).getKey())
+ " | " + neighborsToString(adjList.get(i)) + " " + adjList.get(i).getPred()+"\n");
}
return out.toString();
}
......@@ -106,17 +107,18 @@ public class AdjacencyList {
Vertex temp;
for(int i = 0; i < adjList.size(); i++){
temp = adjList.get(i);
temp.setDist(Integer.MAX_VALUE);
temp.setPred(-1);
temp.setRecord(Integer.MAX_VALUE);
}
start.setDist(0);
start.setRecord(0);
//Initialize heap
Heap heap = new Heap();
heap.insert(start);
for(int i = 0; i< adjList.size();i++)
heap.insert(adjList.get(i));
for(int i = 0; i< adjList.size();i++){
if(start.getAdjListIndex()!=i)
heap.insert(adjList.get(i));
}
//Loop through
Vertex adj;
......@@ -125,9 +127,9 @@ public class AdjacencyList {
temp = (Vertex)heap.removeMin();
for(Edge e: temp.getEdges()){
adj = getVertexFromHandle(e.toVertex);
if(temp.getDist() + e.weight < adj.getDist()){
if((Integer)temp.getRecord() + e.weight < (Integer)adj.getRecord()){
adj.setPred(temp.getAdjListIndex());
adj.setDist(temp.getDist() + e.weight);
adj.setRecord((Integer)temp.getRecord() + e.weight);
heap.heapifyUp(adj.getIndex());
}
}
......@@ -138,10 +140,12 @@ public class AdjacencyList {
}
private ArrayList<Integer> findPathRev(Vertex start, Vertex end, ArrayList<Integer> path){
if(end.equals(start))
if(end.equals(start)){
path.add(end.getAdjListIndex());
return path;
}
Vertex next = adjList.get(end.getPred());
path.add(next.getAdjListIndex());
path.add(end.getAdjListIndex());
return findPathRev(start, next, path);
}
......@@ -150,7 +154,7 @@ public class AdjacencyList {
StringBuffer out = new StringBuffer();
for (int i = reversedPath.size() - 1; i >= 0; i--){
out.append(" " + adjList.get(reversedPath.get(i)));
out.append(" " + adjList.get(reversedPath.get(i)).getKey());
if((reversedPath.size() - i) % 10 == 0)
out.append("\n");
}
......@@ -160,19 +164,20 @@ public class AdjacencyList {
public String outputToString(int s1, int s2){
ArrayList<Integer> revPath = findMinimumPath(s1,s2);
return pathToString(revPath);
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();
}
public ArrayList<Integer> findMinimumPath(int s1, int s2){
System.out.println(s1);
public ArrayList<Integer> findMinimumPath(int s1, int s2){
buildDijkstraGraph(s1);
return findPathRev(adjList.get(s1), adjList.get(s2), new ArrayList<Integer>());
}
public int getDistance(String end){
int index = findStringHandle(end);
return adjList.get(index).getDist();
public int getDistance(int end){
return (Integer)adjList.get(end).getRecord();
}
}
......@@ -105,7 +105,6 @@ public class Main {
continue;
}
System.out.println(adjList.toString());
System.out.println(adjList.outputToString(s1Handle, s2Handle));
boolean validResponse = false;
......
......@@ -8,15 +8,13 @@ public class Vertex extends HeapElt{
private String key;
private ArrayList<Edge> edges;
private int pred;
private int dist;
private int adjListIndex;
public Vertex(String word, int handle, ArrayList<Edge> neighbors) {
public Vertex(String word, int index, ArrayList<Edge> neighbors) {
this.key = word;
this.adjListIndex = handle;
this.adjListIndex = index;
this.edges = neighbors;
pred = -1;
dist = -1;
record = -1;
}
......@@ -32,20 +30,12 @@ public class Vertex extends HeapElt{
this.pred = pred;
}
public int getDist() {
return dist;
}
public void setDist(int dist) {
this.dist = dist;
}
public int getAdjListIndex() {
return adjListIndex;
}
public void setAdjListIndex(int handle) {
this.adjListIndex = handle;
public void setAdjListIndex(int index) {
this.adjListIndex = index;
}
public void addEdge(int n, int weight) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment