AddEdge adds (or reinforces) an undirected edge. It is a no-op when either endpoint is missing or when a == b, so callers can wire edges optimistically without pre-checking existence. Repeated edges accumulate weight.
(a, b string, w float64)
| 94 | // endpoint is missing or when a == b, so callers can wire edges optimistically |
| 95 | // without pre-checking existence. Repeated edges accumulate weight. |
| 96 | func (g *Graph) AddEdge(a, b string, w float64) { |
| 97 | if a == b { |
| 98 | return |
| 99 | } |
| 100 | if _, ok := g.nodes[a]; !ok { |
| 101 | return |
| 102 | } |
| 103 | if _, ok := g.nodes[b]; !ok { |
| 104 | return |
| 105 | } |
| 106 | if w <= 0 { |
| 107 | w = 1 |
| 108 | } |
| 109 | if g.adj[a] == nil { |
| 110 | g.adj[a] = make(map[string]float64) |
| 111 | } |
| 112 | if g.adj[b] == nil { |
| 113 | g.adj[b] = make(map[string]float64) |
| 114 | } |
| 115 | g.adj[a][b] += w |
| 116 | g.adj[b][a] += w |
| 117 | } |
| 118 | |
| 119 | // Node returns a node by ID. |
| 120 | func (g *Graph) Node(id string) (*Node, bool) { |
no outgoing calls