Note that there are some explanatory texts on larger screens.

plurals
  1. POSupplement code for the "Algorithms in Java, part 5" book (Sedgewick)
    primarykey
    data
    text
    <p>We are having an "Advanced Algorithms" curriculum at my college and each of us got an assignment to describe a specific algorithm from the "Algorithms in Java" book. We also have to implement the code from the book (in Java of course) to show how the algorithm works on a concrete example.</p> <p>The classes which we have to use for graphs, nodes, edges etc. are already available in the book, but it seems to me (and to some other students) that the code has a few errors that prevent it to compile. </p> <p>The only implementation I managed to find was this: <a href="http://www.cs.princeton.edu/~rs/Algs3.java5/code.txt" rel="nofollow">http://www.cs.princeton.edu/~rs/Algs3.java5/code.txt</a> But that is the same code from the book, and it doesn't work. </p> <p>The code I'm having problem with is here:</p> <pre><code> GraphSPT(Graph G, int s) { int V = G.V(), N = 0; wt = new double[V]; spt = new Edge[V]; for (int v = 0; v &lt; V; v++) wt[v] = maxWT; intQueue Q = new intQueue(G.E()); wt[s] = 0.0; Q.put(s); Q.put(V); while (!Q.empty()) { int v; while ((v = Q.get()) == V) { if (N++ &gt; V) return; Q.put(V); } AdjList A = G.getAdjList(v); for (Edge e = A.beg(); !A.end(); e = A.nxt()) /* this line is the one with the problem */ { int w = e.other(v); double P = wt[v] + e.wt(); if (P &lt; wt[w]) { wt[w] = P; Q.put(w); spt[w] = e; } } } } </code></pre> <p>I marked the problematic line. AdjList in the line before is an interface, but the getAdjList() method returns an instance of the AdjLinkedList class, which contains implementations of the beg(), end() and nxt() methods. The problem occurs because they return integers, and not instances of the Edge class. If I change the initializer in the for loop to "int e...", then the code inside the loop doesn't work because it calls the other() and wt() methods on the "e" object. </p> <p>Did someone have the same problem maybe, or does anyone know if there are any working code implementations from the book available for download?</p> <p>P.S. Using classes from the book is allowed, I just have to represent graphs with my own code which would not be a problem at all if the classes actually worked :)</p>
    singulars
    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.
    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