JuliaAttic/OldGraphs.jl

How to process dijkstra shortest path parents that return #undef?

Closed this issue · 7 comments

This may be a more general Julia question, but I'm at a loss as to how to treat something like this:

h = graph(["a","b","c","d"],[])
add_edge!(h,"a","b"); add_edge!(h,"b","c"); add_edge!(h,"a","c"); add_edge!(h,"c","d")


julia> dijkstra_shortest_paths(h,"b").parents
4-element Array{ASCIIString,1}:
 #undef
    "b"
    "b"
    "c"

How do I test for this #undef in code?
Also, more of an enhancement request, I guess - is there ANY way to get Graphs to return something consistent when a node is not reachable? That is, strings return #undef, ints return either some very large number or 0 (not sure why):

julia> dijkstra_shortest_paths(g,2).parents
4-element Array{Int64,1}:
 140253310593744
               2
               2
               3
...

julia> dijkstra_shortest_paths(g,2).parents
4-element Array{Int64,1}:
 0
 2
 2
 3

and I'm not quite sure how to test for all these cases other than checking to see whether the value is in graph.vertices, which apparently fails with ERROR: access to undefined reference if the value is #undef...

Well, you can use isdefined(x,10) to check if x[10] is #undef. I agree though that this behavior could be improved. Maybe parents should use the new Nullable type?

I'd like some consistent way - right now it's two checks that cover strings and ints, but I don't have a ton of confidence that I've caught edge cases.

The other case where I'm seeing what appears to be uninitialized array values (see 140253310593744 above) is additionally worrying. I'll try to dig into the code a bit but I'm neck-deep in centrality right now.

I think @mlubin's suggestion of using Nullable is the way to go here.

Is Nullable only available in base julia 0.4?

yes

Or we can just use a simple bool array to indicate if a parent has been set or not.

Yeah, I was thinking the same thing, at least for older versions of Julia