This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import "golang.org/x/exp/constraints" | |
| type Number interface { | |
| constraints.Integer | constraints.Float | |
| } | |
| func AddNumbers[T Number](a, b T) T { | |
| return a + b | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func Add[T int | float64](a, b T) T { | |
| return a + b | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Integer struct { | |
| ... | |
| } | |
| func (i Integer) Plus(a Number) Number { | |
| ... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Number interface { | |
| Plus(Number) Number | |
| } | |
| func Add(a, b Number) Number { | |
| return a.Plus(b) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func AddInt(a, b int) int { | |
| return a + b | |
| } | |
| func AddFloat(a, b float64) float64 { | |
| return a + b | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| unc main() { | |
| graph := buildGraph() | |
| fmt.Println("GRAPH\n-----") | |
| nodes := sortNodes(graph.Nodes) | |
| for _, node := range nodes { | |
| fmt.Printf("%s -> %v\n", node.name, graph.Edges[node.name]) | |
| } | |
| fmt.Println() | |
| bMST := boruvka(graph) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "testing" | |
| ) | |
| func BenchmarkBoruvka(b *testing.B) { | |
| graph := buildGraph() | |
| b.ResetTimer() | |
| for i := 0; i < b.N; i++ { | |
| boruvka(graph) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func main() { | |
| graph := buildGraph() | |
| fmt.Println("GRAPH\n-----") | |
| nodes := sortNodes(graph.Nodes) | |
| for _, node := range nodes { | |
| fmt.Printf("%s -> %v\n", node.name, graph.Edges[node.name]) | |
| } | |
| fmt.Println() | |
| bMST := boruvka(graph) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func (g *Graph) HasNode(name string) (yes bool) { | |
| for _, n := range g.Nodes { | |
| if n.name == name { | |
| yes = true | |
| } | |
| } | |
| return | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func prim(graph *Graph, nodeName string) (mst *Graph) { | |
| mst = NewGraph() | |
| h := &Heap{} | |
| heap.Init(h) | |
| startNode := graph.GetNode(nodeName) | |
| mst.AddNode(startNode) | |
| for _, edge := range graph.Edges[startNode.name] { | |
| heap.Push(h, NodePair{startNode, edge}) | |
| } |