问题
Is there any way to print the pre-order traversal of the output given by MST (using Kruskal or Prim's algorithm). I have a confusion because the output may be or not a binary tree always. So, how the pre-order traversal is possible here? Can a normal DFS do the task?
回答1:
The main issue when working with this kind of problem is the ambiguity of the word tree in algorithmic problems. There are two main definitions (those may in fact differ slightly):
- A tree is an acyclic connected (undirected) graph.
- A tree is basically a set of nodes, each one having a father (except one node, called the root node) and the list of sons for each node is ordered.
However, you have to work with the second definition to assume that the concept of preoder traversal is well-defined (i.e. unique).
A spanning tree, though, is only a tree in the first meaning. In particular, you have to pick out a root node and some order for the sons. Afterwards, a DFS will give you a preoder traversal indeed.
来源:https://stackoverflow.com/questions/39401245/pre-order-traversal-of-a-minimum-spanning-tree