AddEdge adds an edge to the dag. AddEdge is not concurrent safe. All vertexes and edges are expected to be added synchronously before calling Run. If the new edge leads to a cycle, AddEdge will return error.
(from, to VertexID)
| 45 | // |
| 46 | // If the new edge leads to a cycle, AddEdge will return error. |
| 47 | func (d *DAG) AddEdge(from, to VertexID) error { |
| 48 | if len(d.vertexes) <= int(from) || from < 0 { |
| 49 | return errors.Errorf("invalid vertex id %d", from) |
| 50 | } |
| 51 | if to < 0 || len(d.vertexes) <= int(to) { |
| 52 | return errors.Errorf("invalid vertex id %d", to) |
| 53 | } |
| 54 | d.vertexes[from].addChild(d.vertexes[to]) |
| 55 | if yes, edges := IsAcyclic(d); !yes { |
| 56 | d.vertexes[from].removeChild(d.vertexes[to]) |
| 57 | return errors.Errorf("dag is not acyclic: %s", d.fmtEdges(edges)) |
| 58 | } |
| 59 | return nil |
| 60 | } |
| 61 | |
| 62 | // AddEdges adds multiple edges in one batch. See AddEdge for more details. |
| 63 | func (d *DAG) AddEdges(edges Edges) error { |