Skip to content

Commit

Permalink
Merge pull request #53 from garborg/fastfind
Browse files Browse the repository at this point in the history
findIntersections from  O(N^2) to O(N) time
  • Loading branch information
garborg committed Dec 16, 2014
2 parents 35712b2 + d984881 commit 2293c61
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
41 changes: 19 additions & 22 deletions src/intersections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,36 @@

### Generate a list of intersections ###
function findIntersections(highways::Dict{Int,Highway})
intersections = Dict{Int,Intersection}()
crossings = Int[]
seen = Set{Int}()
intersections = Dict{Int,Intersection}()

for hwy in values(highways)
n_nodes = length(hwy.nodes)

for i in 1:n_nodes
node = hwy.nodes[i]

# Highway ends
for (k, highway_k) in highways
n_nodes = length(highway_k.nodes)
for i in 1:max(1, n_nodes - 1):n_nodes
node = highway_k.nodes[i]
hwys = get!(Intersection, intersections, node).highways
push!(hwys, k)
if i == 1 || i == n_nodes || in(node, seen)
get!(Intersection, intersections, node)
else
push!(seen, node)
end
end
end

# Highway crossings
for (i, highway_i) in highways
for j in keys(highways)
if i > j
for node in intersect(highway_i.nodes, highways[j].nodes)
for (hwy_key, hwy) in highways
n_nodes = length(hwy.nodes)

hwys = get!(Intersection, intersections, node).highways
push!(hwys, i, j)
for i in 1:n_nodes
node = hwy.nodes[i]

if !in(node, seen)
push!(seen, node)
push!(crossings, node)
end
end
if i == 1 || i == n_nodes || haskey(intersections, node)
push!(intersections[node].highways, hwy_key)
end
end
end

return intersections, crossings
return intersections
end

### Generate a new list of highways divided up by intersections
Expand Down
2 changes: 1 addition & 1 deletion test/routes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ end
@test edges[50].source.index == 22

# Form transportation network from segments
intersections, crossings = findIntersections(hwys)
intersections = findIntersections(hwys)
segments = segmentHighways(nodesENU, hwys, intersections, roads, Set(1:8))
segment_network = createGraph(segments, intersections)

Expand Down

0 comments on commit 2293c61

Please sign in to comment.