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}) | |
} |
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 Stack struct { | |
nodes [][2]*Node | |
} | |
func (s *Stack) Push(n [2]*Node) { | |
s.nodes = append(s.nodes, n) | |
} | |
func (s *Stack) Pop() (n [2]*Node) { | |
n = s.nodes[len(s.nodes)-1] |
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) isCyclic(name string) (yes bool) { | |
stack := &Stack{} | |
visited := make(map[string]bool) | |
startNode := g.GetNode(name) | |
// use a 2 node array with first node as the from node and the second as the to node | |
stack.Push([2]*Node{startNode, startNode}) | |
for len(stack.nodes) > 0 { | |
n := stack.Pop() | |
// if it's not visited before |
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) RemoveEdge(n1, n2 string) { | |
rmEdge(g, n1, n2) | |
rmEdge(g, n2, n1) | |
} | |
func rmEdge(g *Graph, m, n string) { | |
edges := g.Edges[m] | |
r := -1 | |
for i, edge := range edges { | |
if edge.node.name == n { |