jeffgerickson / algorithms

Bug-tracking for Jeff's algorithms book, notes, etc.
7.95k stars 1.02k forks source link

[Oops.] Floyd-Warshall missing initialization with infinity #212

Open echuber2 opened 4 years ago

echuber2 commented 4 years ago

Please verify that the error is present in the most recent revision before reporting.

Yes, I believe so as of the June 2019 version.

Chapter number or note title: Section 9.8 on Floyd-Warshall

Page number: 319

Error description: Not all dist[u,v] are initialized, because w(u->v) had only been previously defined as either the weight of an edge (u,v) (if it exists) or as 0 when u==v. (I may have overlooked an overload that w(u->v)=infinity otherwise, though, in which case it wouldn't hurt to make that explicit, as the previously listed algorithms had.)

Suggested fix (if any): The two algorithms on page 319 probably ought to initialize dist[u,v] as infinity, for those (u,v) that are not edges in the graph, and where u!=v. (Or, to be concise, point out that w(v->v)=0 and w(u->v)=inf when (u,v) is not an edge and u!=v.)

Here I've typed -> where what I really mean is →, because that's how I do.