reorderin

This commit is contained in:
Josh Deprez 2021-09-24 10:35:23 +10:00
parent 054d3bc5e5
commit 04f8399e24

View file

@ -336,20 +336,6 @@ func (d dag) Dot() string {
return sb.String() return sb.String()
} }
// addEdge adds the edge u-v in O(1).
func (d dag) addEdge(u, v Drawer) {
d.addVertex(u)
d.addVertex(v)
d[v].in[u] = struct{}{}
d[u].out[v] = struct{}{}
}
// removeEdge removes the edge u-v in O(1).
func (d dag) removeEdge(u, v Drawer) {
delete(d[v].in, u)
delete(d[u].out, v)
}
// addVertex ensures the vertex is present, even if there are no edges. // addVertex ensures the vertex is present, even if there are no edges.
func (d dag) addVertex(v Drawer) { func (d dag) addVertex(v Drawer) {
if _, found := d[v]; found { if _, found := d[v]; found {
@ -372,6 +358,20 @@ func (d dag) removeVertex(v Drawer) {
delete(d, v) delete(d, v)
} }
// addEdge adds the edge u-v in O(1).
func (d dag) addEdge(u, v Drawer) {
d.addVertex(u)
d.addVertex(v)
d[v].in[u] = struct{}{}
d[u].out[v] = struct{}{}
}
// removeEdge removes the edge u-v in O(1).
func (d dag) removeEdge(u, v Drawer) {
delete(d[v].in, u)
delete(d[u].out, v)
}
// topWalk visits each vertex in topological order, in time O(|V| + |E|) and // topWalk visits each vertex in topological order, in time O(|V| + |E|) and
// O(|V|) temporary memory (for acyclic graphs) and a bit longer if it has to // O(|V|) temporary memory (for acyclic graphs) and a bit longer if it has to
// break cycles. // break cycles.