Description
A module-dependency pair is of the form
MOD1,MOD2, which means:
- MOD1 is dependent on MOD2; therefore
- MOD2 must be installed before MOD1.
The input data is a list of module-dependency pairs, where
- two adjacent pairs are separated by exactly one space, and
- the list does not contain leading or trailing blanks.
- For example,
B,D A,B A,C A,D C,E C,F E,G F,H G,H is
a dependency-pair list.
To find an order to install modules in a list without violating the
dependencies, the following iteration repeats:
- Search the list for the modules can be installed directly,
i.e., NOT dependent on others.
- Print these modules, then remove them from the list.
This script is divided into two stages:
|
Stage1
- If a module is the first one of some pair,
then it depends on the second one. It can NOT be installed directly.
- This stage excludes them from the modules of the list by
- First remove the first module of every dependency-pair.
- The result may contain several instances of some modules.
We have to remove these duplicates.
|
| Raw Input
|
| Desired Output
| B,D A,B A,C A,D C,E C,F E,G F,H G,H
|
| D B C E F G H
|
Script [ 1] h
[ 2] s/( |^)[^ ,]+,/\1/g
[ 3] s/^/\n/
[ 4] :loop0
[ 5] s/\n([^ ]+)(( [^ ]+)*) \1( |$)/\n\1\2\4/
[ 6] t loop0
[ 7] s/\n([^ ]+ )/\1\n/
[ 8] /\n[^ ]*$/!b loop0
[ 9] s/\n//
| |
|
Stage2
- The result of feeding
B,D A,B A,C A,D C,E C,F E,G F,H G,H
to the first stage is D B C E F G H.
- Then we have to check each module of it with the original list to find
modules not depending on others, i.e.,
not the first modules of some pairs.
- For example, check D B C E F G H against
B,D A,B A,C A,D C,E C,F E,G F,H G,H,
modules can be installed directly are D
and H.
Script [10] G
[11] s/^/\n/
[12] :loop1
[13] /\n([^ \n]+)( [^\n]+|)\n(.* |)\1,/{
[14] s/\n([^ \n]+ ?)/\n/
[15] /\n\n/!b loop1
[16] }
[17] :loop2
[18] s/(\n([^ ]+)( [^ ]+)*\n(.*[ ,]|))\2( |$)/\1\5/
[19] t loop2
[20] s/\n([^ \n]+ ?)/\1\n/
[21] /\n\n/!b loop1
[22] s/ +/ /g
[23] s/\n /\n/
[24] s/ $//
[25] s/,( |$)/\1/g
[26] s/\n//
[27] P
[28] /\n$/d
[29] D
| |
|