From e810e5cad7560061fc2f88598b09ecef90b6da09 Mon Sep 17 00:00:00 2001 From: Jason Hickner Date: Thu, 11 May 2023 11:23:19 -0700 Subject: [PATCH 1/2] visit nodes in deterministic order in mutable graph --- mutable.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/mutable.go b/mutable.go index ad6cefa..b3a52b8 100644 --- a/mutable.go +++ b/mutable.go @@ -1,7 +1,10 @@ package graph import ( + "sort" "strconv" + + "golang.org/x/exp/maps" ) const initialMapSize = 4 @@ -11,7 +14,6 @@ const initialMapSize = 4 // The implementation uses hash maps to associate each vertex in the graph with // its adjacent vertices. This gives constant time performance for // all basic operations. -// type Mutable struct { // The map edges[v] contains the mapping {w:c} if there is an edge // from v to w, and c is the cost assigned to this edge. @@ -85,12 +87,15 @@ func (g *Mutable) Order() int { // If do returns true, Visit returns immediately, // skipping any remaining neighbors, and returns true. // -// The iteration order is not specified and is not guaranteed -// to be the same every time. +// The iteration order is deterministic. // It is safe to delete, but not to add, edges adjacent to v // during a call to this method. func (g *Mutable) Visit(v int, do func(w int, c int64) bool) bool { - for w, c := range g.edges[v] { + keys := maps.Keys(g.edges[v]) + sort.Ints(keys) + + for _, w := range keys { + c := g.edges[v][w] if do(w, c) { return true } From f72387f6a2abd98c9a9b7f9c00dfcefef4a881d4 Mon Sep 17 00:00:00 2001 From: Jason Hickner Date: Thu, 11 May 2023 11:49:10 -0700 Subject: [PATCH 2/2] add go.mod, go.sum --- go.mod | 8 ++++++++ go.sum | 4 ++++ 2 files changed, 12 insertions(+) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2b9f4b2 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module github.com/jhickner/graph + +go 1.19 + +require ( + github.com/yourbasic/graph v0.0.0-20210606180040-8ecfec1c2869 + golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6b5fed6 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/yourbasic/graph v0.0.0-20210606180040-8ecfec1c2869 h1:7v7L5lsfw4w8iqBBXETukHo4IPltmD+mWoLRYUmeGN8= +github.com/yourbasic/graph v0.0.0-20210606180040-8ecfec1c2869/go.mod h1:Rfzr+sqaDreiCaoQbFCu3sTXxeFq/9kXRuyOoSlGQHE= +golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea h1:vLCWI/yYrdEHyN2JzIzPO3aaQJHQdp89IZBA/+azVC4= +golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=