update neighboor node

This commit is contained in:
Antoine 2020-12-20 16:18:34 +01:00
parent 54dcabc7ed
commit 5c037bd458
Signed by: antoine
GPG Key ID: 098FB66FC0475E70
5 changed files with 72 additions and 14 deletions

View File

@ -1,7 +1,10 @@
.PHONY: build
build:
go build -o bin/resolve cmd/resolve.go
run:
./bin/resolve -i graph.hcl
SRC=$(shell find ./ -name *.go -type f)
build: $(SRC)
@go build -o bin/resolve cmd/resolve.go
run: build
@./bin/resolve -i graph.hcl

View File

@ -7,6 +7,7 @@ import (
"log"
"antoine-roux.ml/projects/go/dijkstra/internal"
stringset "antoine-roux.ml/projects/go/stringset/pkg"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hclsyntax"
@ -33,17 +34,31 @@ func main() {
log.Fatalf("config parse: %s", fmt.Errorf("%w", diags))
}
fmt.Printf("%#v\n", *graph)
for _, p := range graph.Nodes {
fmt.Printf("%+v\n", p)
}
graph.InitDefaultValue()
/*
fmt.Printf("%#v\n", *graph)
for _, p := range graph.Nodes {
fmt.Printf("%+v\n", p)
}
*/
start, err := graph.GetStartingNode()
fmt.Printf("%#v\n", start)
fmt.Printf("neighbourNode of %+v : \n", start)
ss := stringset.New(start)
ss.ToString()
fmt.Printf("neighbourNode of start %+v : \n", start)
neig := graph.GetNeighbour(start)
for _, n := range neig {
fmt.Printf("%+v\n", n)
g.UpdateNodeValue(start, n)
}
fmt.Println("level 2 neighbour")
neig_level2 := graph.GetNeighbourForList(neig)
for _, n := range neig_level2 {
fmt.Printf("%+v\n", n)
}
}

5
go.mod
View File

@ -2,6 +2,9 @@ module antoine-roux.ml/projects/go/dijkstra
go 1.15
require github.com/hashicorp/hcl/v2 v2.7.1 // indirect
require (
antoine-roux.ml/projects/go/stringset v0.0.0-20201210194816-217c0c6b0107 // indirect
github.com/hashicorp/hcl/v2 v2.7.1 // indirect
)
replace antoine-roux.ml/projects/go/dijkstra/internal => ./internal

13
go.sum
View File

@ -1,3 +1,16 @@
antoine-roux.ml/projects/go/stringset v0.0.0-20201210000104-ec47cfc5deb8 h1:naE6AbkrQrQ2Qjf9DNv7LyJCnXTTaeWh29uYeq6Gqj8=
antoine-roux.ml/projects/go/stringset v0.0.0-20201210000104-ec47cfc5deb8/go.mod h1:GIIinJ3Sk+3SAeD5mgCIZA41nHQPQugBPRWuovH7A04=
antoine-roux.ml/projects/go/stringset v0.0.0-20201210011018-5e719ecd03b8 h1:psgKgVoX0mjIEkSpph8FwaWksUPx+yBBfPUk2+kQB6w=
antoine-roux.ml/projects/go/stringset v0.0.0-20201210194816-217c0c6b0107 h1:OZVm6yoUyTwqSN4lvsPtC277bD9yLUCUdXJ6EAxY5Uo=
antoine-roux.ml/projects/go/stringset v0.0.0-20201210194816-217c0c6b0107/go.mod h1:GIIinJ3Sk+3SAeD5mgCIZA41nHQPQugBPRWuovH7A04=
antoine-roux.ml/projects/go/stringset.git v0.0.0-20201210000104-ec47cfc5deb8 h1:Wd0y/9Q2/HykAgrtjzHCAWYwtMp0FY5rLTN4BLmTiDc=
antoine-roux.ml/projects/go/stringset.git v0.0.0-20201210000104-ec47cfc5deb8/go.mod h1:fJ7Mch8To4ZrL7lX5Dym4p+BAdSPtXnEq4Cf9/14Ei4=
antoine-roux.ml/projects/go/stringset.git v0.0.0-20201210010243-7fbab5b6f83c h1:HmZ2AqLnsY8cXtUT9fVRD2r/9pgLsOuEr01FZSL49Ow=
antoine-roux.ml/projects/go/stringset.git v0.0.0-20201210010243-7fbab5b6f83c/go.mod h1:fJ7Mch8To4ZrL7lX5Dym4p+BAdSPtXnEq4Cf9/14Ei4=
antoine-roux.ml/projects/go/stringset.git v0.0.0-20201210011018-5e719ecd03b8 h1:AdMbWCM8OChpBWtKnTsuDbZyTFNINx20DL/fwB06/DA=
antoine-roux.ml/projects/go/stringset.git v0.0.0-20201210011018-5e719ecd03b8/go.mod h1:fJ7Mch8To4ZrL7lX5Dym4p+BAdSPtXnEq4Cf9/14Ei4=
antoine-roux.ml/projects/go/stringset.git v0.0.0-20201210194816-217c0c6b0107 h1:Jm9nLeJy25ICJauRSVqFqsiZgQ0MuKcTkT5I287Eq5s=
antoine-roux.ml/projects/go/stringset.git v0.0.0-20201210194816-217c0c6b0107/go.mod h1:fJ7Mch8To4ZrL7lX5Dym4p+BAdSPtXnEq4Cf9/14Ei4=
github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8=
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM=

View File

@ -20,7 +20,7 @@ func (g *Graph) GetStartingNode() (Node, error) {
return Node{}, errors.New("No start node found")
}
func (g *Graph) GetNeighbour(n Node) []Node {
func (g *Graph) GetNeighbour(n Node) []*Node {
var neighbourNodeName []string
for i := 0; i < len(g.Links); i++ {
if g.Links[i].From == n.Name {
@ -29,14 +29,38 @@ func (g *Graph) GetNeighbour(n Node) []Node {
}
// create temporary map of node by here name for direct future access
var nodeByName = make(map[string]Node)
var nodeByName = make(map[string]*Node)
for i := 0; i < len(g.Nodes); i++ {
nodeByName[g.Nodes[i].Name] = (*g.Nodes[i])
nodeByName[g.Nodes[i].Name] = g.Nodes[i]
}
var neighbourNode = make([]Node, len(neighbourNodeName))
var neighbourNode = make([]*Node, len(neighbourNodeName))
for i := 0; i < len(neighbourNodeName); i++ {
neighbourNode[i] = nodeByName[neighbourNodeName[i]]
}
return neighbourNode
}
func (g *Graph) GetNeighbourForList(nodes []*Node) []*Node {
var allNeighbour []*Node
for i := 0; i < len(nodes); i++ {
allNeighbour = append(allNeighbour, g.GetNeighbour(*nodes[i])...)
}
return allNeighbour
}
func (g *Graph) InitDefaultValue() {
for i := 0; i < len(g.Nodes); i++ {
if !g.Nodes[i].IsStart {
g.Nodes[i].Value = -1
}
}
}
func (g *Graph) UpdateNodeValue(source *Node, nodeToUpdate *[]Node) {
for _, n := range nodeToUpdate {
newPotentialValue = n.Value +
n.Value += source.Value
}
}