forked from Project-OSRM/osrm-backend
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Refactor dijkstra algorithm to make which depends on graph …
…interface issue: #287
- Loading branch information
1 parent
faab967
commit 6e4d180
Showing
7 changed files
with
230 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package stationgraph | ||
|
||
import "github.com/golang/glog" | ||
|
||
// dijkstra accepts IGraph and returns all node ids in the shortest path, except start node and end node | ||
func dijkstra(g IGraph) []nodeID { | ||
m := newQueryHeap() | ||
|
||
// init | ||
m.add(g.StartNodeID(), invalidNodeID, 0, 0) | ||
|
||
for { | ||
currID := m.next() | ||
|
||
// stop condition | ||
if currID == invalidNodeID { | ||
glog.Warning("PriorityQueue is empty before solution is found.") | ||
return nil | ||
} | ||
if currID == g.EndNodeID() { | ||
return m.retrieve(currID) | ||
} | ||
|
||
// relax | ||
node := g.Node(currID) | ||
for _, targetID := range g.AdjacentList(currID) { | ||
if g.Edge(currID, targetID) == nil { | ||
glog.Errorf("No connectivity between %#v and %#v which is unexpected, check your logic.\n", currID, targetID) | ||
} | ||
|
||
len := g.Edge(currID, targetID).distance | ||
t := g.Edge(currID, targetID).duration | ||
|
||
if g.Node(currID).reachableByDistance(len) { | ||
chargeTimeNeeded := g.Node(targetID).calcChargeTime(node, len, g.ChargeStrategy()) | ||
if m.add(targetID, currID, len, chargeTimeNeeded+t) { | ||
g.Node(targetID).updateArrivalEnergy(node, len) | ||
g.Node(targetID).updateChargingTime(chargeTimeNeeded) | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.