Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use a <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html" rel="nofollow">Scanner</a> to parse the file data. For each tuple, if the source vertex hasn't been created, create it, otherwise find it in the pre-existing graph -- create a search function. Do the same for the target vertex. Next, create an edge with a weight equal to the third token in the tuple, and add the target vertex to the edge. Finally, add the edge to the adjacency list of the source vertex.</p> <p>For the previously mentioned search function, you can implement something that can search through each vertex of the graph starting from any vertex. Recursion will be necessary.</p> <pre><code>public static Vertex search(Vertex src, String name); </code></pre> <p>A simpler solution is to keep a list of all the vertices you create as your constructing the graph and search through that.</p> <pre><code>public static Vertex search(List&lt;Vertex&gt; vertices, String name); </code></pre> <p>When your done constructing the graph and you have the name of the vertex where Dijkstra's algorithm will begin, you can use the search function to get a reference to the vertex.</p> <pre><code>Dijkstra.computePath(search(vertices, startVertexName)); </code></pre> <p>And, that's it. Here's an example of how to parse your file data:</p> <pre><code>List&lt;Vertex&gt; vertices = new ArrayList&lt;Vertex&gt;(); String src = "Pittsburgh Philadelphia 323 "+ "Pittsburgh Ohio 125 "+ "Ohio Philadelphia 400 "+ "-1 Ohio"; //new Scanner(new File(fileName)); Scanner scnr = new Scanner(src); String src, target; int weight; while(scnr.hasNext()) { src = scnr.next(); if(src.equals("-1")) break; else { target = scnr.next(); weight = scnr.nextInt(); } //call search(), implement logic in addToGraph() addVertexToGraph(src, target, weight, vertices); } String startVertexName = scnr.next(); scnr.close(); </code></pre> <p>Note that <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next%28%29" rel="nofollow">Scanner.next</a> returns the next token separated by white space (the default delimiter), so your file data must be formatted that way.</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. CO+1 nice answer.
      singulars
      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