Skip to content

Commit 609b50a

Browse files
committed
refactor: improve comments for clarity in analyzer and path implementations
1 parent 0b02c5a commit 609b50a

5 files changed

Lines changed: 9 additions & 12 deletions

File tree

v2/graph/analyzer/analyzer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestShortestPaths(t *testing.T) {
7272
}
7373
}
7474

75-
// equalPathSlices compares two graph.Path instances for equality, considering both the sequence of nodes and the total distance. It returns true if both paths are infinite or if they have the same nodes in the same order and the same total distance.
75+
// equalPathSlices compares two graph.Path values by node sequence and total distance.
7676
func equalPathSlices(a, b graph.Path) bool {
7777
nodesA := a.Nodes()
7878
nodesB := b.Nodes()

v2/graph/analyzer/dijkstra.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (pq *dijkstraPQ) Push(x any) {
3333
}
3434

3535
// Pop removes and returns the item with the smallest distance from the priority queue.
36-
// It removes the last item from the slice, which is the smallest due to the heap property.
36+
// container/heap moves the smallest item to the end before calling this method.
3737
func (pq *dijkstraPQ) Pop() any {
3838
old := *pq
3939
n := len(old)

v2/graph/analyzer/shortest_path.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ func (a *Analyzer) ShortestPaths(start, end graph.NodeID) ([]graph.Path, error)
5656
return result, nil
5757
}
5858

59-
// Path computes the path and total distance for a given sequence of nodes. It returns a Path containing
60-
// the total distance and the sequence of nodes. If any edge in the path does not exist, it returns a
61-
// Path marked as infinite (unreachable) and an error indicating which edge is missing.
59+
// allShortestPaths computes all shortest paths between reachable node pairs in the graph.
6260
func allShortestPaths(g *graph.Graph) (map[graph.NodeID]map[graph.NodeID][]graph.Path, error) {
6361
result := make(map[graph.NodeID]map[graph.NodeID][]graph.Path)
6462

v2/graph/path.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ package graph
22

33
import "fmt"
44

5-
// Path represents a path through the graph, consisting of a sequence of node IDs and the total distance (weight) of the path.
5+
// Path represents a sequence of nodes in the graph along with the total distance (weight) of the path.
6+
// It contains a slice of distances, where each distance represents a hop from one node to the next, including the weight of that hop.
67
type Path struct {
78
distances []distance
89
}
@@ -13,10 +14,8 @@ type distance struct {
1314
weight Weight
1415
}
1516

16-
// Path returns a Path object representing the path through the graph defined by the given sequence of node IDs.
17-
// If the path is valid (i.e., there are edges between all consecutive nodes), it returns a Path with the total
18-
// distance and the sequence of nodes. If any edge in the path does not exist, it returns a Path marked as
19-
// infinite (unreachable) and an error indicating which edge is missing.
17+
// Path builds a Path for the given node sequence.
18+
// It returns an error if any consecutive edge does not exist.
2019
func (g *Graph) Path(nodes ...NodeID) (*Path, error) {
2120
if len(nodes) == 0 {
2221
return &Path{

v2/graph/standard/random_regular.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
// RandomRegularGraph generates a random k-regular graph with n nodes.
1010
// Each node has exactly degree k. Returns nil if impossible.
11-
// For undirected graphs, n*k must be even. For directed graphs, no such constraint exists.
11+
// This implementation requires n*k to be even.
1212
func RandomRegularGraph(seed int, directed bool, weightFunc WeightedFunc, n, k int) (*graph.Graph, error) {
1313
if weightFunc == nil {
1414
weightFunc = Unweighted()
@@ -22,7 +22,7 @@ func RandomRegularGraph(seed int, directed bool, weightFunc WeightedFunc, n, k i
2222
}
2323

2424
if (n*k)%2 != 0 {
25-
// if undirected, n*k must be even
25+
// n*k must be even for this implementation
2626
return nil, fmt.Errorf("invalid parameters: n*k must be even for undirected graphs")
2727
}
2828

0 commit comments

Comments
 (0)