-
-
Save kentfredric/5703858 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import graph.Vertex; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
import java.util.Stack; | |
public class ShortestVertex { | |
private int num_vertices; | |
private int start; | |
private int end; | |
private ArrayList<Vertex> vertices; | |
public ShortestVertex( int user_num_vertices, int user_start, int user_end ) | |
{ | |
num_vertices = user_num_vertice; | |
start = user_start; | |
end = user_end; | |
vertices = new ArrayList<Vertex>(); | |
for(int i = 0; i < num_vertices;i++) | |
{ | |
//make a new vertex for how many vertexes were called | |
vertices.add(new Vertex(i)); | |
} | |
} | |
public ArrayList<Vertex> get_vertices(){ | |
return vertices; | |
} | |
public void associate_vertices( int left, int right ){ | |
Vertex v_left = vertices.get(left); | |
Vertex v_right = vertices.get(right); | |
v_left.adj.add(v_right); | |
} | |
} | |
public class ShortestVertexScanner { | |
private Scanner input; | |
private PrintStream output; | |
private ShortestVertex stash; | |
public ShortestVertexScanner( Scanner user_input, PrintStream user_output ){ | |
input = user_input; | |
output = user_output; | |
stash = new ShortestVertex( | |
input.nextInt(), | |
input.nextInt(), | |
input.nextInt() | |
); | |
} | |
public void scan_matrix() { | |
//load in matrix line | |
int node1 = input.nextInt(); | |
int node2 = input.nextInt(); | |
while (node1 != -1 && node2 != -1) | |
{ | |
stash.associate_vertices( node1, node2 ); | |
//load next matrix line | |
node1 = scn.nextInt(); | |
node2 = scn.nextInt(); | |
} | |
} | |
public void dump_vertices(PrintStream out) | |
{ | |
for( Vertex v: stash.get_vertices() ){ | |
out.println(vertexOutput); | |
} | |
} | |
public void dump_vertices() | |
{ | |
return this.dump_vertices( out ); | |
} | |
} | |
public class Main { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) throws FileNotFoundException | |
{ | |
// Vertex scanner that reads from a file, writes to System.out | |
ShortestVertexScanner c = new ShortestVertexScanner( | |
new Scanner(new FileReader("vertex.txt")), | |
System.out | |
); | |
c.scan_matrix(); | |
c.dump_vertices(); | |
//start destination queue | |
ArrayList<Vertex> vertexQue = new ArrayList<Vertex>(); //create a que of vertexs | |
vertices.get(start).isstartnode = true; //tell the start that it is the starting point | |
vertexQue.add(vertices.get(start)); //get start from original vertex list and place it in the que | |
while(!vertexQue.isEmpty()) | |
{ | |
Vertex parent = vertexQue.remove(0); | |
for(Vertex child : parent.adj) | |
{ | |
if (!child.isstartnode == true) | |
{ | |
child.isstartnode = true; | |
child.destination = parent; | |
vertexQue.add(child); | |
} | |
} | |
} | |
//start ordered traverse | |
Stack<Vertex> vertexStack = new Stack<Vertex>(); //create a new stack of vertexs | |
Vertex currentvertex = vertices.get(end); //select the end node | |
vertexStack.push(currentvertex); //add end node to the top of the stack | |
while(!vertexStack.peek().equals(vertices.get(start))) //while the stack hasnt reached the start node | |
{ | |
currentvertex = currentvertex.destination; //the current vertex becomes the destination vertex | |
vertexStack.push(currentvertex); //push the current vertex onto the stack | |
} | |
while(!vertexStack.isEmpty()) //while the stack isnt empty | |
{ | |
Vertex tempvertex = vertexStack.pop(); //take a vertex off the stack (starting with the start) | |
System.out.print(tempvertex.id); //print it out nicely in order | |
if(!vertexStack.isEmpty()) | |
{ | |
System.out.print("->"); | |
} | |
} | |
System.out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment