PathOfMostResistance

11
PathOfMostResistance.java package acyclicDigraph; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class PathOfMostResistance { /** * @param args * enter full path of file as args[0] */ public static void main(String[] args) { PathOfMostResistance fileHandler = new PathOfMostResistance(); List<String[]> rows = fileHandler.loadFile(args[0]); loadValues(rows); } /** * Loads values to object NodeCollection * @param rows * @return */ private static NodeCollection loadValues(List<String[]> rows) { NodeCollection nodeCollection = new NodeCollection(); int key = 0; for (int i = 0; i < rows.size(); i++) { nodeCollection.increaseLineNumber(); String[] line = rows.get(i); int lineLength = line.length; for (int l = 0; l < lineLength; l++) { int name = Integer.parseInt(line[l]); nodeCollection.addNode(key, name, i + 1, l); key++; } } nodeCollection.setRelationshipValues(nodeCollection); return nodeCollection; } /** * @param fileName

Transcript of PathOfMostResistance

Page 1: PathOfMostResistance

PathOfMostResistance.javapackage acyclicDigraph;

import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.List;

public class PathOfMostResistance {

/** * @param args * enter full path of file as args[0] */public static void main(String[] args) {

PathOfMostResistance fileHandler = new PathOfMostResistance();List<String[]> rows = fileHandler.loadFile(args[0]);loadValues(rows);

}

/** * Loads values to object NodeCollection * @param rows * @return */private static NodeCollection loadValues(List<String[]> rows) {

NodeCollection nodeCollection = new NodeCollection();int key = 0;for (int i = 0; i < rows.size(); i++) {

nodeCollection.increaseLineNumber();String[] line = rows.get(i);int lineLength = line.length;for (int l = 0; l < lineLength; l++) {

int name = Integer.parseInt(line[l]);nodeCollection.addNode(key, name, i + 1, l);key++;

}}nodeCollection.setRelationshipValues(nodeCollection);return nodeCollection;

}

/** * @param fileName * @return */private List<String[]> loadFile(String fileName) {

List<String[]> list = new ArrayList<String[]>();BufferedReader br = null;try {

br = new BufferedReader(new FileReader(fileName));try {

String line = null;

Page 2: PathOfMostResistance

while ((line = br.readLine()) != null) {String record[] = line.split(" ");list.add(record);

}} catch (IOException e) {

}} catch (FileNotFoundException e1) {

e1.printStackTrace();} finally {

try {br.close();

} catch (IOException e) {e.printStackTrace();

}}

return list;}

}NodeCollection.javapackage acyclicDigraph;

import java.util.Vector;

public class NodeCollection{Node root = null;private int lineNumber = 0;private Vector<Node> nodes = new Vector<Node>();

/** * @param nodeCollection * @return */public NodeCollection setRelationshipValues(NodeCollection

nodeCollection){int size = nodeCollection.getLength();for(int i = 0; i < size; i++){

int row = nodeCollection.findNode(i).getLevel();if(nodeCollection.hasNode(i + row)){

nodeCollection.findNode(i).setLeftChild(nodeCollection.findNode(i + row));

nodeCollection.findNode(i).getLeftChild().setRightParent(nodeCollection.findNode(i));

}if(nodeCollection.hasNode(i + row + 1)){

nodeCollection.findNode(i).setRightChild(nodeCollection.findNode(i + row + 1));

Page 3: PathOfMostResistance

nodeCollection.findNode(i).getRightChild().setLeftParent(nodeCollection.findNode(i));

}

Node node = nodeCollection.findNode(i);

int leftLeftTotal = node.hasLeftParent() ? node.getLeftParent().getLeftAccumulativeTotal() : 0;

int leftRightTotal = node.hasLeftParent() ? node.getLeftParent().getRightAccumulativeTotal() : 0;

int rightLeftTotal = node.hasRightParent() ? node.getRightParent().getLeftAccumulativeTotal() : 0;

int rightRightTotal = node.hasRightParent() ? node.getRightParent().getRightAccumulativeTotal() : 0;

int leftTotal = 0;int rightTotal = 0;if(leftLeftTotal > leftRightTotal){

leftTotal = leftLeftTotal;nodeCollection.findNode(i).setBest(true);

} else {leftTotal = leftRightTotal;nodeCollection.findNode(i).setBest(true);

}

if(rightLeftTotal > rightRightTotal){rightTotal = rightLeftTotal;nodeCollection.findNode(i).setBest(true);

} else {rightTotal = rightRightTotal;nodeCollection.findNode(i).setBest(true);

}int presentName = node.getName();

nodeCollection.findNode(i).setLeftAccumulativeTotal(leftTotal + presentName);

nodeCollection.findNode(i).setRightAccumulativeTotal(rightTotal + presentName);

}int length = nodeCollection.getLength();int lastLineNumber = nodeCollection.findNode(length -

1).getLevel();Node bottomNode = nodeCollection.findNode(length - 1);for(int i = length - lastLineNumber; i < length; i++){

Node presentNode = nodeCollection.findNode(i);

if((presentNode.getLeftAccumulativeTotal() > bottomNode.getLeftAccumulativeTotal()) ||

(presentNode.getLeftAccumulativeTotal() > bottomNode.getRightAccumulativeTotal()) ||

(presentNode.getRightAccumulativeTotal() > bottomNode.getLeftAccumulativeTotal()) ||

Page 4: PathOfMostResistance

(presentNode.getRightAccumulativeTotal() > bottomNode.getRightAccumulativeTotal())){

bottomNode = presentNode;}

}showHighestPath(bottomNode);

return nodeCollection;}

/** * @param bottomNode */private void showHighestPath(Node bottomNode) {

int accumLeft = bottomNode.getLeftAccumulativeTotal();int accumRight = bottomNode.getRightAccumulativeTotal();System.out.println("Highest Node Value: " + (accumLeft >

accumRight ? accumLeft : accumRight));System.out.println("Walking from bottom to top, the first node

is: " + bottomNode.getName());while(bottomNode != null){

accumLeft = bottomNode.getLeftAccumulativeTotal();accumRight = bottomNode.getRightAccumulativeTotal();if(accumLeft > accumRight){

bottomNode = bottomNode.getLeftParent();if(bottomNode != null) {

System.out.println("Going Left to: " + bottomNode.getName());

}} else {

bottomNode = bottomNode.getRightParent();if(bottomNode != null) {

System.out.println("Going right to: " + bottomNode.getName());

}}

}}

/** * */void increaseLineNumber() {

this.lineNumber++;}

/** * @return */int getLineNumber() {

return this.lineNumber;}

/** * @param key

Page 5: PathOfMostResistance

* @param name * @param row * @param position */void addNode(int key, int name, int row, int position) {

Node newNode = new Node(key, name, row, position);if (key == 0) {

this.root = newNode;}nodes.add(key, newNode);

}

/** * @param listNumber * @return */Node findNode(int listNumber) {

return nodes.get(listNumber);}

/** * @param nodeNumber * @return */boolean hasNode(int nodeNumber) {

return nodes.size() >= nodeNumber + 1;}

/** * @return */public int getLength() {

return nodes.size();}

/** * @return */public Node getRoot() {

return root;}

/** * @param root */public void setRoot(Node root) {

this.root = root;}

}

Node.javapackage acyclicDigraph;

Page 6: PathOfMostResistance

/** * @author Edward Cleveland * @date 05262015 * */public class Node {

private int key;private int name;private int row;private Node leftParentNode;private Node rightParentNode;private Node leftChildNode;private Node rightChildNode;private int length;private boolean best = false;private int leftAccumulativeTotal = 0;private int rightAccumulativeTotal = 0;

/** * @param key * @param name * @param row * @param position */Node(int key, int name, int row, int position) {

this.key = key;this.name = name;this.setLevel(row);

}

/** * @param key * @param name * @param leftParent * @param rightParent * @param leftChild * @param rightChild */Node(int key, int name, Node leftParent, Node rightParent, Node

leftChild, Node rightChild) {this.key = key;this.name = name;this.leftParentNode = leftParent;this.rightParentNode = rightParent;this.leftChildNode = leftChild;this.rightChildNode = rightChild;

}

/** * @param length */public void setLength(int length) {

this.length = length;

Page 7: PathOfMostResistance

}

/** * @return */public int getLength() {

return length;}

/** * @param key */public void setKey(int key) {

this.key = key;}

/** * @return */public int getKey() {

return key;}

/** * @param name */public void setName(int name) {

this.name = name;}

/** * @return */public int getName() {

return name;}

/** * @param leftParentNode */public void setLeftParent(Node leftParentNode) {

this.leftParentNode = leftParentNode;}

/** * @return */public boolean hasLeftParent() {

return leftParentNode != null;}/** * @return */public Node getLeftParent() {

return leftParentNode;

Page 8: PathOfMostResistance

}

/** * @param rightParentNode */public void setRightParent(Node rightParentNode) {

this.rightParentNode = rightParentNode;}

/** * @return */public boolean hasRightParent() {

return rightParentNode != null;}

/** * @return */public Node getRightParent() {

return rightParentNode;}

/** * @param leftChildNode */public void setLeftChild(Node leftChildNode) {

this.leftChildNode = leftChildNode;}

/** * @return */public boolean hasLeftChild() {

return leftChildNode != null;}

/** * @return */public Node getLeftChild() {

return leftChildNode;}

/** * @param rightChildNode */public void setRightChild(Node rightChildNode) {

this.rightChildNode = rightChildNode;}

/** * @return */public boolean hasRightChild() {

Page 9: PathOfMostResistance

return rightChildNode != null;}

/** * @return */public Node getRightChild() {

return rightChildNode;}

/** * @return */public int getLevel() {

return row;}

/** * @param row */public void setLevel(int row) {

this.row = row;}

/** * @return */public int getLeftAccumulativeTotal() {

return leftAccumulativeTotal;}

/** * @param leftAccumulativeTotal */public void setLeftAccumulativeTotal(int leftAccumulativeTotal) {

this.leftAccumulativeTotal = leftAccumulativeTotal;}

/** * @return */public int getRightAccumulativeTotal() {

return rightAccumulativeTotal;}

/** * @param rightAccumulativeTotal */public void setRightAccumulativeTotal(int rightAccumulativeTotal) {

this.rightAccumulativeTotal = rightAccumulativeTotal;}

/**

Page 10: PathOfMostResistance

* @return */public boolean isBest() {

return best;}

/** * @param best */public void setBest(boolean best) {

this.best = best;}

}