Skip to content

Commit

Permalink
refactor: Refactor dijkstra algorithm to make which depends on graph …
Browse files Browse the repository at this point in the history
…interface

issue: #287
  • Loading branch information
CodeBear801 committed Apr 23, 2020
1 parent faab967 commit 6e4d180
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 141 deletions.
43 changes: 43 additions & 0 deletions integration/service/oasis/stationgraph/dijkstra.go
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)
}
}
}
}
}
Loading

0 comments on commit 6e4d180

Please sign in to comment.