Note that there are some explanatory texts on larger screens.

plurals
  1. POFile input for Dijkstra's algorithm
    primarykey
    data
    text
    <p>I am having trouble figuring out how to read an input file with java. The file has the following format:</p> <pre><code>u1 v1 w1 u2 v2 w2 ... um vm wm -1 source </code></pre> <p>Each 3-tuple denotes an edge, which is specified by its source-vertex, its destination-vertex, and its weight (example: newyork boston 30). The description of the graph is terminated by a “flag”, the integer -1. A string follows this flag; this string is the name of the source vertex for the Dijkstra shortest-path algorithm. That is, you are to determine and print out the shortest path from this source vertex to every other vertex in the graph. Here is my current work.</p> <pre><code>import java.io.File; import java.io.FileNotFoundException; import java.util.PriorityQueue; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; class Vertex implements Comparable&lt;Vertex&gt; { public final String name; public Edge[] adjacencies; public double minDistance = Double.POSITIVE_INFINITY; public Vertex previous; public Vertex(String argName) { name = argName; } public String toString() { return name; } public int compareTo(Vertex other) { return Double.compare(minDistance, other.minDistance); } } class Edge { public final Vertex target; public final double weight; public Edge(Vertex argTarget, double argWeight) { target = argTarget; weight = argWeight; } } public class Dijkstra { public static void computePaths(Vertex source) { source.minDistance = 0.; PriorityQueue&lt;Vertex&gt; vertexQueue = new PriorityQueue&lt;Vertex&gt;(); vertexQueue.add(source); while (!vertexQueue.isEmpty()) { Vertex u = vertexQueue.poll(); // Visit each edge exiting u for (Edge e : u.adjacencies) { Vertex v = e.target; double weight = e.weight; double distanceThroughU = u.minDistance + weight; if (distanceThroughU &lt; v.minDistance) { vertexQueue.remove(v); v.minDistance = distanceThroughU; v.previous = u; vertexQueue.add(v); } } } } public static ArrayList&lt;Vertex&gt; getShortestPathTo(Vertex target) { ArrayList&lt;Vertex&gt; path = new ArrayList&lt;Vertex&gt;(); for (Vertex vertex = target; vertex != null; vertex = vertex.previous) path.add(vertex); Collections.reverse(path); return path; } public String[] readFile(String fileName) throws FileNotFoundException { Scanner input = new Scanner(new File(fileName)); String line = ""; while (input.hasNext()) { line = line.concat(input.nextLine()); } String[] graph = line.split(""); return graph; } public static void main(String[] args) throws FileNotFoundException { final String TEST = "/TestInput.txt"; Scanner input = new Scanner(new File(TEST)); String line = ""; while (input.hasNext()) { line = line.concat(input.nextLine()); } String[] graph = line.split(" "); for (int i = 0; i &lt; graph.length; i++) { System.out.println(graph[i]); } Vertex[] verts = new Vertex[graph.length]; Edge[] edges = new Edge[graph.length]; Vertex v1 = new Vertex(""); Vertex v2 = new Vertex(""); Vertex source = new Vertex(""); int count = 0; outerloop: for (int i = 0; i &lt; (graph.length); i++) { if (graph[i].equals("-1")) { // do algorithm initialization here w/ source } if (i == 0) { verts[i] = new Vertex(graph[i]); count++; } else { innerloop: for (int j = count; j &gt;= 0; j--) { if (i / 3 == 0) { if (graph[i].equals(verts[j].toString())) { break innerloop; } else if (j == 0) { verts[count] = new Vertex(graph[i]); v1 = verts[count]; count++; } } if (i / 3 == 1) { if (graph[i].equals(verts[j])) { break innerloop; } else if (j == 0) { verts[count] = new Vertex(graph[i]); v2 = verts[count]; count++; } } if (i / 3 == 2) { } } } } for (int i = 0; i &lt; verts.length; i++) { System.out.println(verts[i]); } } } </code></pre> <p>So my only problem is how to get from the given .txt file format to a graph. Any suggestions are welcome.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload