Commit d7a68ee3 authored by trh010's avatar trh010
Browse files

lots of progress made

parent 59292373
......@@ -30,10 +30,10 @@ public class Heap {
HeapElt temp = array[pos2];
array[pos2] = array[pos1];
array[pos2].setHandle(pos2);
array[pos2].setAdjListIndex(pos2);
array[pos1] = temp;
array[pos1].setHandle(pos1);
array[pos1].setAdjListIndex(pos1);
}
......@@ -121,7 +121,7 @@ public class Heap {
heapsize++;
array[heapsize] = inElt;
array[heapsize].setHandle(heapsize);
array[heapsize].setAdjListIndex(heapsize);
heapifyUp(heapsize);
}
......@@ -164,7 +164,7 @@ public class Heap {
public void printHeap() {
for (int i = 1; i <= heapsize; i++) {
String out = i + ": key- " + array[i].getHandle() + " | value- "
String out = i + ": key- " + array[i].getIndex() + " | value- "
+ array[i].getRecord();
System.out.println(out);
}
......
......@@ -20,11 +20,11 @@ public class HeapElt {
return record;
}
public void setHandle(int inHandle) {
public void setAdjListIndex(int inHandle) {
handle = inHandle;
}
public int getHandle() {
public int getIndex() {
return handle;
}
......
......@@ -49,7 +49,7 @@ public class TestHeap {
for (int i = 1; i <= hsize; i++) {
System.out.println(" " + array[i].getRecord().toString()
+ " " + array[i].getHandle());
+ " " + array[i].getIndex());
}
System.out.println("\n\nPrinting heap with printHeap(): \n");
......@@ -61,7 +61,7 @@ public class TestHeap {
array[8].setRecord(new Integer(11));
myHeap.heapifyDown(array[8].getHandle());
myHeap.heapifyDown(array[8].getIndex());
System.out.println("\nRevised handle info:\n");
System.out.println("value handle");
......@@ -69,7 +69,7 @@ public class TestHeap {
for (int i = 1; i <= hsize; i++) {
System.out.println(" " + array[i].getRecord().toString()
+ " " + array[i].getHandle());
+ " " + array[i].getIndex());
}
System.out.println("\n\nSorting by removing minimum at each step:\n");
......
package main;
import heap.Heap;
import java.util.ArrayList;
public class AdjacencyList {
......@@ -21,7 +23,7 @@ public class AdjacencyList {
ArrayList<Edge> neighbors = new ArrayList<Edge>();
for (int i = 0; i < adjList.size(); i++) {
Vertex testVertex = adjList.get(i);
int numDifferences = numDifferences((String)testVertex.getRecord(), newWord);
int numDifferences = numDifferences(testVertex.getKey(), newWord);
if (2 >= numDifferences) {
int weight = 0;
if(numDifferences == 1)
......@@ -55,7 +57,7 @@ public class AdjacencyList {
StringBuffer out = new StringBuffer();
for (int i = 0; i < v.getEdges().size(); i++) {
int neighborHandle = v.getEdges().get(i).toVertex;
String neighborKey = (String)adjList.get(neighborHandle).getRecord();
String neighborKey = adjList.get(neighborHandle).getKey();
out.append(neighborKey + " (" + v.getEdges().get(i).weight + "), ");
}
return out.toString();
......@@ -65,7 +67,7 @@ public class AdjacencyList {
StringBuffer out = new StringBuffer();
for (int i = 0; i < v.getEdges().size(); i++) {
int neighborHandle = v.getEdges().get(i).toVertex;
String neighborKey = (String)adjList.get(neighborHandle).getRecord();
String neighborKey = adjList.get(neighborHandle).getKey();
out.append(" " + neighborKey + " ("
+ v.getEdges().get(i).weight + ") ");
if (i % 6 == 5)
......@@ -80,46 +82,91 @@ public class AdjacencyList {
public int findStringHandle(String s) {
for (int i = 0; i < adjList.size(); i++) {
if (s.equals(adjList.get(i).getRecord()))
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", i, adjList.get(i).getRecord())
+ " | " + neighborsToString(adjList.get(i)) + "\n");
}
return out.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", i, adjList.get(i).getKey())
+ " | " + neighborsToString(adjList.get(i)) + "\n");
}
return out.toString();
}
private void buildDijkstraGraph(int startingVertex){
Vertex start = getVertexFromHandle(startingVertex);
//Initialize graph
Vertex temp;
for(int i = 0; i < adjList.size(); i++){
temp = adjList.get(i);
temp.setDist(-1);
temp.setDist(Integer.MAX_VALUE);
temp.setPred(-1);
temp.setRecord(Integer.MAX_VALUE);
}
getVertexFromHandle(startingVertex).setDist(0);
start.setDist(0);
//Initialize heap
Heap heap = new Heap();
heap.insert(start);
for(int i = 0; i< adjList.size();i++)
heap.insert(adjList.get(i));
//Loop through
Vertex adj;
while(heap.getHeapsize() != 0){
try {
temp = (Vertex)heap.removeMin();
for(Edge e: temp.getEdges()){
adj = getVertexFromHandle(e.toVertex);
if(temp.getDist() + e.weight < adj.getDist()){
adj.setPred(temp.getAdjListIndex());
adj.setDist(temp.getDist() + 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");
}
}
}
private ArrayList<Integer> findPath(int start, int end, ArrayList<Integer> path){
return path;
private ArrayList<Integer> findPathRev(Vertex start, Vertex end, ArrayList<Integer> path){
if(end.getPred() == -1)
return path;
Vertex next = adjList.get(end.getPred());
path.add(next.getAdjListIndex());
return findPathRev(start, next, path);
}
public String printPath(ArrayList<Integer> reversedPath){
StringBuffer out = new StringBuffer();
for (int i = reversedPath.size() - 1; i >= 0; i--){
out.append(" " + adjList.get(reversedPath.get(i)));
}
return out.toString();
}
public ArrayList<Integer> findMinimumPath(String s1, String s2){
int s1Handle = findStringHandle(s1);
int s2Handle = findStringHandle(s2);
buildDijkstraGraph(s1Handle);
buildDijkstraGraph(s1Handle);
return findPath(s1Handle, s2Handle, new ArrayList<Integer>());
return findPathRev(adjList.get(s1Handle), adjList.get(s2Handle), new ArrayList<Integer>());
}
public int getDistance(String end){
int index = findStringHandle(end);
return adjList.get(index).getDist();
}
}
......@@ -96,6 +96,7 @@ public class Main {
+ " is not in the dictionary");
continue;
}
System.out.println("Please provide the second 5 letter word: ");
String s2 = in.next();
int s2Handle = adjList.findStringHandle(s2.toUpperCase());
if (s2Handle == -1){
......@@ -137,7 +138,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! - " + e.toString());
......
......@@ -5,16 +5,23 @@ import java.util.ArrayList;
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) {
this.record = word;
this.handle = handle;
this.key = word;
this.adjListIndex = handle;
this.edges = neighbors;
pred = -1;
dist = -1;
record = -1;
}
public String getKey(){
return key;
}
public int getPred() {
......@@ -33,12 +40,12 @@ public class Vertex extends HeapElt{
this.dist = dist;
}
public int getHandle() {
return handle;
public int getAdjListIndex() {
return adjListIndex;
}
public void setHandle(int handle) {
this.handle = handle;
public void setAdjListIndex(int handle) {
this.adjListIndex = handle;
}
public void addEdge(int n, int weight) {
......@@ -52,5 +59,7 @@ public class Vertex extends HeapElt{
public ArrayList<Edge> getEdges() {
return edges;
}
}
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