<p>I ended up doing the following:</p>

<p>Vinicius Santos' answer is the first part of it. As he says, at any frame I either add a new node and a parent-child edge connecting to it, or just add a cross-edge between two existing nodes. Parent-child edges offer no opportunities for changing the tree structure, only cross-edges do. Consider adding a cross-edge E between nodes A and B, where B has the greater tree depth. If (A.depth + 1) &lt; B.depth, then we can lessen B.depth by making it a child of A.</p>

<p>Having lessened the depth of B, we now must check B's neighbors, to see if they may lessen their depth by becoming children of B. We therefore perform a breadth-first traversal from B, which traverses an edge from X to Y if X.depth+1 &lt; Y.depth, and sets Y to be a child of X.</p>

CC BY-SA 3.0
2011-03-26T16:00:08.850Z
2011-12-29T00:47:32.110Z
2011-12-29T00:47:32.110Z
SuperElectric
<p>Every time you add a new vertex with an edge, you don't have options.
Every time you add a new edge, if the current distance to the root is bigger than the distance through the new edge, you remove the old edge in the old shortest path and add the new one. Otherwise, you just keep your tree unchanged. I think this way you get something very similar to a BFS tree in the sense that the levels of the tree will contain the same vertices and the distance from a vertex to the root will be the same as the distance in the BFS tree (and in the graph), but I don't know if that's enough to satisfy your "ideal balanced spanning tree" condition.</p>

CC BY-SA 2.5
2011-01-14T18:13:18.197Z
2011-01-14T18:13:18.197Z
Vinicius dos Santos
<p>I am looking for ways to maintain a relatively balanced spanning tree of a graph, as I add new nodes/edges to the graph.</p>

<p>I have an undirected graph that starts as a single node, the "root".</p>

<p>At each step, I add to the graph either a new node and an edge connecting it to the graph, or just a new edge, connecting two old nodes. As I grow the graph, I maintain a spanning tree. Most of the time, this means that when I add a new node and edge, I set the new node to be the child of the old node that it connects to.</p>

<p>I have no control over the order in which the new nodes are added, so the above tree-building algorithm can obviously lead to imbalanced spanning trees.</p>

<p>Does anybody know of online heuristics that will keep the spanning tree "relatively balanced", while minimizing the amount of work done in re-treeing? I have full control over the tree structure. What I don't control is the graph connectivity, or the order in which new nodes are added.</p>

<p>Note that the standard Google responses to terms like "balanced" "spanning" and "tree" seem to be binary trees and B-trees, neither of which apply. My graph nodes can have any number of neighbors, so the tree nodes can have any number of children, not 2 like binary trees. B-trees maintain balance by changing their adjacency lists, and I cannot change the graph connectivity.</p>

CC BY-SA 2.5
2011-03-14T12:12:58.243Z
Have you examined top trees? http://en.wikipedia.org/wiki/Top_tree
Peer Sommerlund
CC BY-SA 2.5
2010-12-20T06:12:10.247Z
Maybe it would help if you were more specific about what your ideal balanced spanning tree of a static graph would be. Is a BFS tree automatically a good choice as a balanced tree (it's as shallow as possible, if you choose the right root, or within a factor of two regardless of root)? Do you need the number of nodes in each subtree to be smaller by a constant factor than the number of nodes at the parent, everywhere in the tree, and if so what do you do for graphs that don't have such trees?
David Eppstein
CC BY-SA 2.5
2010-12-20T06:20:37.013Z
A BFS tree would indeed be an ideal balanced spanning tree if I were running this offline, with the entire graph given at once. There's no need for the number of nodes in each subtree to be smaller by a constant factor than the number of nodes in the parent.
SuperElectric
<graph-theory><ds.data-structures><graph-algorithms><tree><online-algorithms>
maintaining a balanced spanning tree of a growing undirected graph
SuperElectric
# maintaining a balanced spanning tree of a growing undirected graph

(Asked by: username_0 on 2010-12-19)

I am looking for ways to maintain a relatively balanced spanning tree of a graph, as I add new nodes/edges to the graph.

I have an undirected graph that starts as a single node, the "root".

At each step, I add to the graph either a new node and an edge connecting it to the graph, or just a new edge, connecting two old nodes. As I grow the graph, I maintain a spanning tree. Most of the time, this means that when I add a new node and edge, I set the new node to be the child of the old node that it connects to.

I have no control over the order in which the new nodes are added, so the above tree-building algorithm can obviously lead to imbalanced spanning trees.

Does anybody know of online heuristics that will keep the spanning tree "relatively balanced", while minimizing the amount of work done in re-treeing? I have full control over the tree structure. What I don't control is the graph connectivity, or the order in which new nodes are added.

Note that the standard Google responses to terms like "balanced" "spanning" and "tree" seem to be binary trees and B-trees, neither of which apply. My graph nodes can have any number of neighbors, so the tree nodes can have any number of children, not 2 like binary trees. B-trees maintain balance by changing their adjacency lists, and I cannot change the graph connectivity.

**Tags:** graph-theory, ds.data-structures, graph-algorithms, tree, online-algorithms
> **Comments:**
> **username_2**: Maybe it would help if you were more specific about what your ideal balanced spanning tree of a static graph would be. Is a BFS tree automatically a good choice as a balanced tree (it's as shallow as possible, if you choose the right root, or within a factor of two regardless of root)? Do you need the number of nodes in each subtree to be smaller by a constant factor than the number of nodes at the parent, everywhere in the tree, and if so what do you do for graphs that don't have such trees?  
> **username_0**: A BFS tree would indeed be an ideal balanced spanning tree if I were running this offline, with the entire graph given at once. There's no need for the number of nodes in each subtree to be smaller by a constant factor than the number of nodes in the parent.  
> **username_1**: Have you examined top trees? http://en.wikipedia.org/wiki/Top_tree  

---

## 2 answers

### Answer by username_0 on 2011-03-26. Score: 3 | Accepted answer

I ended up doing the following:

Vinicius Santos' answer is the first part of it. As he says, at any frame I either add a new node and a parent-child edge connecting to it, or just add a cross-edge between two existing nodes. Parent-child edges offer no opportunities for changing the tree structure, only cross-edges do. Consider adding a cross-edge E between nodes A and B, where B has the greater tree depth. If (A.depth + 1) < B.depth, then we can lessen B.depth by making it a child of A.

Having lessened the depth of B, we now must check B's neighbors, to see if they may lessen their depth by becoming children of B. We therefore perform a breadth-first traversal from B, which traverses an edge from X to Y if X.depth+1 < Y.depth, and sets Y to be a child of X.


---

### Answer by username_3 on 2011-01-14. Score: 5

Every time you add a new vertex with an edge, you don't have options.
Every time you add a new edge, if the current distance to the root is bigger than the distance through the new edge, you remove the old edge in the old shortest path and add the new one. Otherwise, you just keep your tree unchanged. I think this way you get something very similar to a BFS tree in the sense that the levels of the tree will contain the same vertices and the distance from a vertex to the root will be the same as the distance in the BFS tree (and in the graph), but I don't know if that's enough to satisfy your "ideal balanced spanning tree" condition.


---

