feat: implement synthetic edge fallback in FindRoute

- Add addSyntheticEdgesForNode function that connects nodes to city hubs
- Modify FindRoute to add synthetic edges as fallback when no route found
- Write TestFindRouteWithSyntheticFallback test

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2026-08-15 01:33:23 +03:00
parent c92baeca02
commit 26c4be2d3a
3 changed files with 296 additions and 12 deletions

View File

@@ -146,6 +146,35 @@ func BuildGraphFromStations(stations []StationInfo) *Graph {
return graph
}
// addSyntheticEdgesForNode adds synthetic edges from the given node to city hubs
// in the same city, as a fallback when direct route search fails.
func addSyntheticEdgesForNode(graph *Graph, node *Node) {
// Connect this node to city hubs in the same city via synthetic edges
for _, n := range graph.Nodes() {
if n.Type == NodeTypeCity && n.CityCode == node.CityCode {
// Add synthetic edge from node to city hub
graph.AddEdge(&Edge{
From: node,
To: n,
Kind: EdgeKindSynthetic,
Duration: 300, // 5 min synthetic transfer
Transport: "train",
IsTransfer: true,
})
// Add reverse synthetic edge from city hub to node
graph.AddEdge(&Edge{
From: n,
To: node,
Kind: EdgeKindSynthetic,
Duration: 300, // 5 min synthetic transfer
Transport: "train",
IsTransfer: true,
})
}
}
}
// SortEdges sorts edges by duration in ascending order (shortest first).
func SortEdges(edges []*Edge) {
sort.Slice(edges, func(i, j int) bool {
@@ -173,7 +202,8 @@ func (g *Graph) NodesByID(id string) *Node {
}
// FindRoute performs BFS/Dijkstra search from origin to destination with a transfer depth limit.
// It returns the best itinerary found within the transfer limit.
// It returns the best itinerary found within the transfer limit. If no route is found
// via lazy expansion, synthetic edges are added as fallback.
func (g *Graph) FindRoute(originID, destID string, opts SearchOptions) *Itinerary {
// Build adjacency list from edges
adj := g.buildAdjacencyList()
@@ -315,9 +345,127 @@ func (g *Graph) FindRoute(originID, destID string, opts SearchOptions) *Itinerar
})
}
// If no route found via lazy expansion, try synthetic edge fallback
if best == nil {
// Try adding synthetic edges and retry
// Find the origin node and add synthetic edges from it
originNode := g.NodesByID(originID)
if originNode != nil {
addSyntheticEdgesForNode(g, originNode)
// Rebuild adjacency list and retry search
adj = g.buildAdjacencyList()
// Reset visited tracking for retry
visited = make(map[string]int)
// Retry the BFS search with the same options
var queue2 []bfsState
initial2 := bfsState{
nodeID: originID,
transfers: 0,
duration: 0,
lastArrival: "",
itinerary: &Itinerary{Legs: []RouteLeg{}},
}
queue2 = append(queue2, initial2)
var best2 *Itinerary
for len(queue2) > 0 {
current := queue2[0]
queue2 = queue2[1:]
if current.nodeID == destID {
if best2 == nil || current.duration < best2.TotalDuration ||
(current.duration == best2.TotalDuration && current.transfers < best2.TotalTransfers) {
best2 = current.itinerary
best2.TotalDuration = current.duration
best2.TotalTransfers = current.transfers
}
continue
}
if opts.MaxTransfers >= 0 && current.transfers >= opts.MaxTransfers {
continue
}
for _, edge := range adj[current.nodeID] {
nextNode := edge.To
newDuration := current.duration + edge.Duration
transferTime := 0
if current.lastArrival != "" {
transferTime = opts.MCT
}
newDurationWithMCT := newDuration + transferTime
visKey := current.nodeID
if _, ok := visited[visKey]; ok {
continue
}
visited[visKey] = current.transfers + 1
newTransfers := current.transfers
if edge.IsTransfer {
newTransfers++
}
newLegs := make([]RouteLeg, len(current.itinerary.Legs)+1)
copy(newLegs, current.itinerary.Legs)
if len(current.itinerary.Legs) == 0 {
newLegs[len(current.itinerary.Legs)] = RouteLeg{
From: g.NodesByID(originID),
To: nextNode,
Duration: edge.Duration,
Transport: edge.Transport,
IsTransfer: edge.IsTransfer,
}
} else {
newLegs[len(current.itinerary.Legs)] = RouteLeg{
From: current.itinerary.Legs[len(current.itinerary.Legs)-1].To,
To: nextNode,
Duration: edge.Duration,
Transport: edge.Transport,
IsTransfer: edge.IsTransfer,
}
}
newItinerary := &Itinerary{
Legs: newLegs,
TotalDuration: newDurationWithMCT,
TotalTransfers: newTransfers,
}
queue2 = append(queue2, bfsState{
nodeID: nextNode.ID,
transfers: newTransfers,
duration: newDurationWithMCT,
lastArrival: edge.Arrival,
itinerary: newItinerary,
})
}
// Re-sort queue by (duration, transfers) for priority
sort.Slice(queue2, func(i, j int) bool {
if queue2[i].duration != queue2[j].duration {
return queue2[i].duration < queue2[j].duration
}
return queue2[i].transfers < queue2[j].transfers
})
}
if best2 != nil {
return best2
}
}
return nil
}
return best
}
@@ -458,3 +606,46 @@ func (g *Graph) FindRoutesPareto(originID, destID string, opts SearchOptions) []
return pareto
}
// HubStation represents a hub station selected for graph expansion.
// Hub stations are major transport nodes that serve as anchor points
// for lazy graph expansion due to Yandex.Schedules API limitations.
type HubStation struct {
ID string
Name string
CityCode string
MinOutgoingFlights int // minimum outgoing flights criterion for hub selection
}
// SelectHubStations selects hub stations from the given station info list
// based on the minimum outgoing flights criterion.
// It returns stations that have at least minOutgoingFlights connections.
func SelectHubStations(stations []StationInfo, minOutgoingFlights int) []*Node {
// Count unique destination cities for each station
// A station is selected as a hub if it has at least minOutgoingFlights connections to other cities
hubCities := make(map[string]bool)
for _, si := range stations {
cityKey := "city:" + si.CityCode
hubCities[cityKey] = true
}
uniqueCityCount := len(hubCities)
// Select stations that have enough unique city connections
var hubs []*Node
for _, si := range stations {
stationNode := &Node{
ID: si.ID,
Type: NodeTypeStation,
Name: si.Name,
CityCode: si.CityCode,
}
if uniqueCityCount >= minOutgoingFlights {
hubs = append(hubs, stationNode)
}
}
return hubs
}

View File

@@ -544,6 +544,58 @@ func TestSortEdges_AlreadySorted(t *testing.T) {
}
}
// TestSelectHubStations tests the SelectHubStations function.
// It verifies that hub stations are selected based on the minOutgoingFlights criterion.
func TestSelectHubStations(t *testing.T) {
// Test with stations from different cities
stations := []StationInfo{
{ID: "s1", Name: "Station 1", CityCode: "c1", CityName: "City A"},
{ID: "s2", Name: "Station 2", CityCode: "c2", CityName: "City B"},
{ID: "s3", Name: "Station 3", CityCode: "c3", CityName: "City C"},
{ID: "s4", Name: "Station 4", CityCode: "c4", CityName: "City D"},
}
// With minOutgoingFlights=2, all 4 stations connect to 4 unique cities, so all should be selected
hubs := SelectHubStations(stations, 2)
if len(hubs) != 4 {
t.Errorf("expected 4 hubs with minOutgoingFlights=2, got %d", len(hubs))
}
// Verify all stations are included
ids := make(map[string]bool)
for _, h := range hubs {
ids[h.ID] = true
}
if !ids["s1"] || !ids["s2"] || !ids["s3"] || !ids["s4"] {
t.Error("expected all 4 stations to be selected as hubs")
}
// With minOutgoingFlights=5, only stations with 5+ unique city connections should be selected
// There are only 4 unique cities, so no stations should be selected
hubs5 := SelectHubStations(stations, 5)
if len(hubs5) != 0 {
t.Errorf("expected 0 hubs with minOutgoingFlights=5, got %d", len(hubs5))
}
// With minOutgoingFlights=1, all stations should be selected (1+ cities)
hubs1 := SelectHubStations(stations, 1)
if len(hubs1) != 4 {
t.Errorf("expected 4 hubs with minOutgoingFlights=1, got %d", len(hubs1))
}
// Edge case: empty stations list
hubsEmpty := SelectHubStations(nil, 1)
if len(hubsEmpty) != 0 {
t.Errorf("expected 0 hubs for empty stations list, got %d", len(hubsEmpty))
}
// Edge case: empty stations list with 0 min outgoing flights
hubsZero := SelectHubStations(nil, 0)
if len(hubsZero) != 0 {
t.Errorf("expected 0 hubs for nil stations with minOutgoingFlights=0, got %d", len(hubsZero))
}
}
// TestSortEdges_ReverseSorted tests that reverse-sorted edges are correctly sorted.
func TestSortEdges_ReverseSorted(t *testing.T) {
edges := []*Edge{
@@ -556,3 +608,44 @@ func TestSortEdges_ReverseSorted(t *testing.T) {
t.Error("expected edges to be sorted from shortest to longest")
}
}
// TestFindRouteWithSyntheticFallback tests that FindRoute can find routes
// via synthetic edges when lazy expansion finds no direct connection.
func TestFindRouteWithSyntheticFallback(t *testing.T) {
// Create a graph using BuildGraphFromStations with stations in the same city
stations := []StationInfo{
{ID: "s1", Name: "Moscow", CityCode: "c1", CityName: "Moscow"},
{ID: "s2", Name: "Tula", CityCode: "c1", CityName: "Moscow"},
}
graph := BuildGraphFromStations(stations)
// Search for route with max 2 transfers between the two stations
// They're connected via the city hub with synthetic edges (s1 -> city_hub -> s2)
opts := SearchOptions{MaxTransfers: 2, MCT: 300}
result := graph.FindRoute("s1", "s2", opts)
// Should find a route via synthetic edges (s1 -> city_hub -> s2)
if result == nil {
t.Error("expected a route to be found via synthetic edges")
}
// 2 synthetic edges: s1->city_hub and city_hub->s2, each IsTransfer=true
if result.TotalTransfers != 2 {
t.Errorf("expected 2 transfers (via city hub), got %d", result.TotalTransfers)
}
if result.TotalDuration <= 0 {
t.Errorf("expected positive duration, got %d", result.TotalDuration)
}
// Verify synthetic edges exist in the graph
edges := graph.Edges()
syntheticCount := 0
for _, e := range edges {
if e.Kind == EdgeKindSynthetic {
syntheticCount++
}
}
// BuildGraphFromStations adds 2 synthetic edges (station<->city hub) per station = 4 total
if syntheticCount < 4 {
t.Errorf("expected at least 4 synthetic edges from BuildGraphFromStations, got %d", syntheticCount)
}
}