Hi, everyone. Here is an introduction of topological sort program,
There's an input file indicating the layer constraints in the following format:
5
1 2 3 4 5
3
1 > 3
5 > 2
3 > 4
Which means there are five layers, with indexes 1, 2, 3, 4, and 5, respectively. Also, there are three constraints, indicating “the first layer should be above the third layer”, “the fifth layer should be above the second layer”, and “the third layer should be above the fourth layer”. Then, you have to output the sequence
4 3 1 2 5
that can satisfy the constraints. Note that the sequence is not unique. There could be many correct sequences. You just need to output one of them. |
I'm wondering whether this "layer" is just an alias to the well known "vertex" or "node", or there are other more profound meanings ?
I also have a preliminary idea, but not sure whether it is feasible.
1. First, use an array to store all the integer layers in order
2. For every constraint, adjust the mentioned elements's index if necessary.
(e.g the original sequence is 1 2 3 4 5, when encountering 1 > 3, which is false for now, swap 1 & 3. Resulting in 3 2 1 4 5, and so on.)
3. After processing all the constraints, show the result.
However, this idea seems not using the concept of Topology.
Thx for your help in advance !