22
\$\begingroup\$

The goal of this challenge is to write a program that visualizes a dependency graph in the form of a tree. While "dependency graph" in this context means nothing more than a directed graph, the visualization method described here works best for graphs describing some dependency relation (as an exercise, after you've read the challenge, try to reverse the direction of one of the sample graphs, and see if the result is as useful.)

The input to the program consists of one or more target definitions, which are lines of the form

Target DirectDependency1 DirectDependency2 ...

, defining a target, and its associated direct dependencies, if any. Targets and their dependencies are collectively called objects. If an object appears only as a dependency, and not as a target, it has no dependencies. The set of all objects that appear in the input is called Γ. (See the Input and Output section for more details about the input format.)

For any pair of objects, A and B, we say that:

  • A depends on B (equivalently, B is required by A), if A directly depends on B, or if A directly depends on B', and B' depends on B, for some object B';
  • A properly depends on B (equivalently, B is properly required by A), if A depends on B, and B does not depend on A.

We define a contrived object, ʀooᴛ, not in Γ, such that ʀooᴛ is not directly required by any object, and such that, for all objects A, ʀooᴛ directly depends on A if and only if A is in Γ, and A is not properly required by any object in Γ (in other words, ʀooᴛ directly depends on A if no other object depends on A, or if all objects that depend on A are also required by A.)

Output Tree

We construct a tree, whose root node is ʀooᴛ, and such that the children of each node are its direct dependencies. For example, given the input

Bread Dough Yeast
Dough Flour Water
Butter Milk

, the resulting tree is

Output Tree Example

, or, in ASCII form,

ʀooᴛ
+-Bread
| +-Dough
| | +-Flour
| | +-Water
| +-Yeast
+-Butter
  +-Milk

. The program's output is the above-defined tree, printed without the ʀooᴛ node. So, for example, the corresponding output for the above input is

Bread
+-Dough
| +-Flour
| +-Water
+-Yeast
Butter
+-Milk

. A detailed description of the layout of the output tree is given later.

Node Order

The child nodes of a given parent node, P, should be sorted, such that, for all child nodes A and B of P, A appears before B if and only if

  • there exists a child node C of P, such that A is properly required by C, and C precedes, or equals to, B, according to the same order; or,
  • A alphabetically precedes B (more preceisely, A precedes B using ASCII collation,) and there exists no child node C of P, such that B is properly required by C, and C precedes, or equals to, A, according to the same order.

(People looking for a mathematical challenge might want to show that this relation is well defined, and that it's, in fact, a strict total order. Don't forget that Γ is finite!)

For example, given the input

X D C B A
B D
C A

, the output should be

X
+-A
+-D
+-B
| +-D
+-C
  +-A

. A appears before B, and B appears before C, due to their alphabetical order; D appears before B, since it is properly required by it, and after A, since it alphabetically follows it; B and C do not appear before D, even though they precede it alphabetically, since there exists a node, namely, B, that properly requires D, and that equals to B (i.e., itself), and preceeds C, according to the same rules.

Repetitions

The same object, A, may appear more than once in the output, if, for example, it is required by more than one object. If A has no dependencies of its own, no special handling is required in this case. Otherwise, in order to minimize the verbosity of the output, and to avoid infinite recursion due to circular dependencies, the dependencies of A are listed only on its first occurrence for which none of the ancestors are siblings of another A node; any other occurrence of A should have no children, and should appear followed by a space and an ellipsis, as in A....

For example, given the input

IP Ethernet
TCP IP
UDP IP
WebRTC TCP UDP

, the output should be

WebRTC
+-TCP
| +-IP
|   +-Ethernet
+-UDP
  +-IP ...

. As another example, featuring both circular dependency and ancestry considerations,

Rock Scissors
Paper Rock
Scissors Paper

, should result in

Paper
+-Rock ...
Rock
+-Scissors ...
Scissors
+-Paper ...

. Note that, for example, the first occurrence of Rock does not list its dependencies, since its parent, Paper, is a sibling of another Rock node. The parent of the second Rock node, ʀooᴛ (which doesn't appear in the output), doesn't have Rock as a sibling, so the dependencies of Rock are listed on this node.

Output Tree Layout

I'm sure you got the hang of how the tree should be represented as ASCII art (and feel free to skip this section if you have,) but for the sake of completeness...

The child nodes of ʀooᴛ are printed on separate lines, without any indentation, in order. Each node is immediately followed by its children, if any, printed in the same fashion, recursively, indented by two characters to the right. For each node that has children, a vertical line, consisting of | (pipe) characters, extends down from the character directly below the first character of the node, until the row of its last child node, not including the last child node's children. If the indentation of a node is nonzero, it is preceded by +- (on the same indentation level as its parent), overwriting the vertical line described above.

Input and Output

You may read the input through STDIN, or using an equivalent method. You may assume that there are no empty lines, and you may require that the last line ends, or doesn't end, in a newline character. You may assume that object names consists of printable ASCII characters (not including space). You may assume that objects in a target definition are separated by a single space character, and that there are no leading or trailing spaces. You may assume that each target is defined at most once, and that there are no repetitions in its dependency list.

You may write the output to STDOUT, or use an equivalent method. All output lines, except for the longest, may include trailing spaces. The last output line may, or may not, end in a newline character.

Score

This is code-golf. The shortest answer, in bytes, wins.

Test Cases

Your program should process each of the following test cases in a reasonable amount of time.


Input

Depender Dependee
Independent

Output

Depender
+-Dependee
Independent

Input

Earth Turtle
Turtle Turtle

Output

Earth
+-Turtle
  +-Turtle ...

Input

F A C B D I
A B
B A C
D E H
C
G F
J H G C E I
E D
H D
I G

Output

J
+-C
+-E
| +-D
|   +-E ...
|   +-H ...
+-H
| +-D ...
+-G
| +-F
|   +-C
|   +-A
|   | +-B ...
|   +-B
|   | +-C
|   | +-A ...
|   +-D ...
|   +-I ...
+-I
  +-G ...

Civilization V Technology Tree

Input

Pottery Agriculture
AnimalHusbandry Agriculture
Archery Agriculture
Mining Agriculture
Sailing Pottery
Calendar Pottery
Writing Pottery
Trapping AnimalHusbandry
TheWheel AnimalHusbandry
Masonry Mining
BronzeWorking Mining
Optics Sailing
Philosophy Writing
HorsebackRiding TheWheel
Mathematics TheWheel Archery
Construction Masonry
IronWorking BronzeWorking
Theology Calendar Philosophy
CivilService Philosophy Trapping
Currency Mathematics
Engineering Mathematics Construction
MetalCasting Construction IronWorking
Compass Optics
Education Theology CivilService
Chivalry CivilService HorsebackRiding Currency
Machinery Engineering
Physics Engineering MetalCasting
Steel MetalCasting
Astronomy Compass Education
Acoustics Education Chivalry
Banking Chivalry
PrintingPress Machinery Phyisics
Gunpowder Physics Steel
Navigation Astronomy
Economics Banking PrintingPress
Chemistry Gunpowder
Metallurgy Gunpowder
Archaeology Navigation
ScientificTheory Navigation Acoustics Economics
MilitaryScience Economics Chemistry
Fertilizer Chemistry
Rifling Metallurgy
Biology Archaeology ScientificTheory
SteamPower ScientificTheory MilitaryScience
Dynamite MilitaryScience Fertilizer Rifling
Electricity Biology SteamPower
ReplaceableParts SteamPower
Railroad SteamPower Dynamite
Refrigeration Electricity
Telegraph Electricity
Radio Electricity
Flight ReplaceableParts
Combustion ReplaceableParts Railroad
Penicillin Refrigeration
Plastics Refrigeration
Electronics Telegraph
MassMedia Radio
Radar Radio Flight Combustion
AtomicTheory Combustion
Ecology Penicillin Plastics
Computers Electronics MassMedia Radar
Rocketry Radar
Lasers Radar
NuclearFission AtomicTheory
Globalization Ecology Computers
Robotics Computers
Satellites Rocketry
Stealth Lasers
AdvancedBallistics Lasers NuclearFission
ParticlePhysics Robotics Satellites
NuclearFusion Satellites Stealth AdvancedBallistics
Nanotechnology ParticlePhysics
FutureTech Globalization Nanotechnology NuclearFusion

Output

FutureTech
+-Globalization
| +-Computers
| | +-Electronics
| | | +-Telegraph
| | |   +-Electricity
| | |     +-Biology
| | |     | +-Archaeology
| | |     | | +-Navigation
| | |     | |   +-Astronomy
| | |     | |     +-Compass
| | |     | |     | +-Optics
| | |     | |     |   +-Sailing
| | |     | |     |     +-Pottery
| | |     | |     |       +-Agriculture
| | |     | |     +-Education
| | |     | |       +-CivilService
| | |     | |       | +-Philosophy
| | |     | |       | | +-Writing
| | |     | |       | |   +-Pottery ...
| | |     | |       | +-Trapping
| | |     | |       |   +-AnimalHusbandry
| | |     | |       |     +-Agriculture
| | |     | |       +-Theology
| | |     | |         +-Calendar
| | |     | |         | +-Pottery ...
| | |     | |         +-Philosophy ...
| | |     | +-ScientificTheory
| | |     |   +-Acoustics
| | |     |   | +-Chivalry
| | |     |   | | +-CivilService ...
| | |     |   | | +-Currency
| | |     |   | | | +-Mathematics
| | |     |   | | |   +-Archery
| | |     |   | | |   | +-Agriculture
| | |     |   | | |   +-TheWheel
| | |     |   | | |     +-AnimalHusbandry ...
| | |     |   | | +-HorsebackRiding
| | |     |   | |   +-TheWheel ...
| | |     |   | +-Education ...
| | |     |   +-Economics
| | |     |   | +-Banking
| | |     |   | | +-Chivalry ...
| | |     |   | +-PrintingPress
| | |     |   |   +-Machinery
| | |     |   |   | +-Engineering
| | |     |   |   |   +-Construction
| | |     |   |   |   | +-Masonry
| | |     |   |   |   |   +-Mining
| | |     |   |   |   |     +-Agriculture
| | |     |   |   |   +-Mathematics ...
| | |     |   |   +-Phyisics
| | |     |   +-Navigation ...
| | |     +-SteamPower
| | |       +-MilitaryScience
| | |       | +-Chemistry
| | |       | | +-Gunpowder
| | |       | |   +-Physics
| | |       | |   | +-Engineering ...
| | |       | |   | +-MetalCasting
| | |       | |   |   +-Construction ...
| | |       | |   |   +-IronWorking
| | |       | |   |     +-BronzeWorking
| | |       | |   |       +-Mining ...
| | |       | |   +-Steel
| | |       | |     +-MetalCasting ...
| | |       | +-Economics ...
| | |       +-ScientificTheory ...
| | +-MassMedia
| | | +-Radio
| | |   +-Electricity ...
| | +-Radar
| |   +-Combustion
| |   | +-Railroad
| |   | | +-Dynamite
| |   | | | +-Fertilizer
| |   | | | | +-Chemistry ...
| |   | | | +-MilitaryScience ...
| |   | | | +-Rifling
| |   | | |   +-Metallurgy
| |   | | |     +-Gunpowder ...
| |   | | +-SteamPower ...
| |   | +-ReplaceableParts
| |   |   +-SteamPower ...
| |   +-Flight
| |   | +-ReplaceableParts ...
| |   +-Radio ...
| +-Ecology
|   +-Penicillin
|   | +-Refrigeration
|   |   +-Electricity ...
|   +-Plastics
|     +-Refrigeration ...
+-Nanotechnology
| +-ParticlePhysics
|   +-Robotics
|   | +-Computers ...
|   +-Satellites
|     +-Rocketry
|       +-Radar ...
+-NuclearFusion
  +-AdvancedBallistics
  | +-Lasers
  | | +-Radar ...
  | +-NuclearFission
  |   +-AtomicTheory
  |     +-Combustion ...
  +-Satellites ...
  +-Stealth
    +-Lasers ...


Cygwin syslog-ng Package Dependency Graph

Input

p11-kit-trust cygwin libtasn1_6
libtasn1_6 cygwin
libp11-kit0 cygwin libffi6 libintl8
libopenssl100 cygwin libgcc1 zlib0 ca-certificates
gsettings-desktop-schemas libglib2.0_0
p11-kit cygwin libp11-kit0 libtasn1_6
zlib0 libgcc1
libpcre1 cygwin
libfam0 gamin
shared-mime-info cygwin libglib2.0_0 libxml2
coreutils cygwin libattr1 libgcc1 libgmp10 libiconv2 libintl8 tzcode _update-info-dir
syslog-ng bash gawk libevtlog0 libgcc1 libglib2.0_0 libopenssl100 libpcre1 libwrap0 tzcode
libxml2 libiconv2 libreadline7 zlib0
cygwin base-cygwin
ca-certificates bash p11-kit p11-kit-trust cygwin
libintl8 cygwin libiconv2
libncursesw10 cygwin libgcc1 libstdc++6 terminfo
libstdc++6 libgcc1
bash coreutils cygwin libgcc1 libiconv2 libintl8 libncursesw10 libreadline7 _update-info-dir
libmpfr4 libgcc1 libgmp10
tzcode bash cygwin libgcc1
_update-info-dir cygwin
libreadline7 libncursesw10
gawk bash cygwin libgcc1 libgmp10 libintl8 libmpfr4 libreadline7
gamin libglib2.0_0
libglib2.0_0 libfam0 libffi6 libgcc1 libiconv2 libintl8 libpcre1 zlib0 gsettings-desktop-schemas shared-mime-info tzcode

Output

syslog-ng
+-libgcc1
+-bash
| +-cygwin
| | +-base-cygwin
| +-_update-info-dir
| | +-cygwin ...
| +-libgcc1
| +-libiconv2
| +-libintl8
| | +-cygwin ...
| | +-libiconv2
| +-libncursesw10
| | +-cygwin ...
| | +-libgcc1
| | +-libstdc++6
| | | +-libgcc1
| | +-terminfo
| +-libreadline7
| | +-libncursesw10 ...
| +-coreutils
|   +-cygwin ...
|   +-_update-info-dir ...
|   +-libattr1
|   +-libgcc1
|   +-libgmp10
|   +-libiconv2
|   +-libintl8 ...
|   +-tzcode ...
+-tzcode
| +-cygwin ...
| +-libgcc1
| +-bash ...
+-gawk
| +-cygwin ...
| +-libgcc1
| +-libgmp10
| +-libintl8 ...
| +-libreadline7 ...
| +-bash ...
| +-libmpfr4
|   +-libgcc1
|   +-libgmp10
+-libevtlog0
+-libpcre1
| +-cygwin ...
+-libglib2.0_0
| +-libffi6
| +-libgcc1
| +-libiconv2
| +-libintl8 ...
| +-libpcre1 ...
| +-tzcode ...
| +-zlib0
| | +-libgcc1
| +-gsettings-desktop-schemas
| | +-libglib2.0_0 ...
| +-libfam0
| | +-gamin
| |   +-libglib2.0_0 ...
| +-shared-mime-info
|   +-cygwin ...
|   +-libxml2
|   | +-libiconv2
|   | +-libreadline7 ...
|   | +-zlib0 ...
|   +-libglib2.0_0 ...
+-libopenssl100
| +-cygwin ...
| +-libgcc1
| +-ca-certificates
| | +-cygwin ...
| | +-bash ...
| | +-p11-kit
| | | +-cygwin ...
| | | +-libp11-kit0
| | | | +-cygwin ...
| | | | +-libffi6
| | | | +-libintl8 ...
| | | +-libtasn1_6
| | |   +-cygwin ...
| | +-p11-kit-trust
| |   +-cygwin ...
| |   +-libtasn1_6 ...
| +-zlib0 ...
+-libwrap0


GNU grep regex.c Call Graph

Input

check_dst_limits_calc_pos_1 check_dst_limits_calc_pos_1
re_string_destruct
match_ctx_add_sublast
build_charclass bitset_set
match_ctx_init
rpl_re_set_syntax
link_nfa_nodes re_node_set_init_1 re_node_set_init_2
re_node_set_init_union re_node_set_init_copy
register_state re_node_set_insert_last re_node_set_alloc
rpl_re_search re_search_stub
re_node_set_init_2
re_search_2_stub re_search_stub
merge_state_array re_acquire_state re_node_set_init_union
calc_eclosure calc_eclosure_iter
create_token_tree
create_tree create_token_tree
pop_fail_stack
re_compile_fastmap_iter
rpl_regcomp rpl_re_compile_fastmap re_compile_internal
check_arrival_expand_ecl find_subexp_node check_arrival_expand_ecl_sub re_node_set_merge re_node_set_alloc
clean_state_log_if_needed extend_buffers
lower_subexps lower_subexp
duplicate_node re_dfa_add_node
re_node_set_merge
build_equiv_class bitset_set
create_initial_state re_acquire_state_context re_node_set_merge re_node_set_init_copy re_node_set_contains
rpl_regfree free_dfa_content
check_halt_state_context check_halt_node_context re_string_context_at
check_dst_limits search_cur_bkref_entry check_dst_limits_calc_pos
re_node_set_insert re_node_set_init_1
transit_state_bkref transit_state_bkref check_subexp_matching_top get_subexp re_string_context_at re_node_set_init_union re_acquire_state_context
get_subexp_sub match_ctx_add_entry clean_state_log_if_needed check_arrival
free_tree free_token
parse_reg_exp free_tree parse_branch fetch_token postorder create_tree
re_string_skip_chars
match_ctx_clean
re_copy_regs
parse_expression parse_dup_op parse_sub_exp build_charclass_op parse_expression init_word_char create_tree free_tree create_token_tree fetch_token parse_bracket_exp postorder
init_dfa
re_acquire_state_context calc_state_hash re_node_set_compare create_cd_newstate
re_node_set_add_intersect
merge_state_with_log transit_state_bkref check_subexp_matching_top re_acquire_state_context re_string_context_at re_node_set_init_union
free_fail_stack_return
check_subexp_limits sub_epsilon_src_nodes re_node_set_contains
build_charclass_op create_tree bitset_not bitset_set build_charclass create_token_tree bitset_mask free_charset
re_node_set_alloc
check_arrival_add_next_nodes check_node_accept_bytes re_acquire_state re_node_set_merge check_node_accept re_node_set_insert
check_node_accept_bytes re_string_char_size_at re_string_wchar_at re_string_elem_size_at
re_string_allocate re_string_construct_common re_string_realloc_buffers
lower_subexp create_tree
parse_branch parse_expression create_tree postorder free_tree
rpl_regexec re_search_internal
free_state
expand_bkref_cache re_node_set_init_1 re_node_set_insert search_cur_bkref_entry re_node_set_merge re_node_set_init_copy check_arrival_expand_ecl re_node_set_contains re_acquire_state
peek_token re_string_peek_byte_case re_string_wchar_at peek_token
re_string_construct build_wcs_upper_buffer re_string_construct_common build_upper_buffer re_string_translate_buffer re_string_realloc_buffers build_wcs_buffer
re_string_realloc_buffers
calc_inveclosure re_node_set_insert_last
sub_epsilon_src_nodes re_node_set_add_intersect re_node_set_contains re_node_set_remove_at
sift_ctx_init
re_string_fetch_byte_case re_string_char_size_at
find_recover_state merge_state_with_log
sift_states_iter_mb check_node_accept_bytes re_node_set_contains
group_nodes_into_DFAstates bitset_set_all bitset_copy bitset_set re_node_set_init_1 re_node_set_insert re_node_set_init_copy bitset_merge bitset_clear bitset_contain bitset_empty
push_fail_stack re_node_set_init_copy
check_node_accept bitset_contain re_string_context_at
match_ctx_free match_ctx_clean
build_wcs_upper_buffer
rpl_regerror
sift_states_backward re_node_set_init_1 update_cur_sifted_state build_sifted_states
build_sifted_states re_node_set_insert sift_states_iter_mb check_dst_limits check_node_accept re_node_set_contains
transit_state_mb check_node_accept_bytes re_acquire_state_context re_string_context_at re_node_set_init_union clean_state_log_if_needed
optimize_utf8
build_collating_symbol bitset_set
re_search_stub rpl_re_compile_fastmap re_search_internal re_copy_regs
check_matching merge_state_with_log find_recover_state re_acquire_state_context transit_state check_subexp_matching_top transit_state_bkref extend_buffers check_halt_state_context re_string_context_at
extend_buffers build_wcs_upper_buffer re_string_translate_buffer re_string_realloc_buffers build_wcs_buffer build_upper_buffer
check_arrival re_node_set_init_1 re_string_context_at expand_bkref_cache re_node_set_merge re_node_set_init_copy re_node_set_contains check_arrival_expand_ecl check_arrival_add_next_nodes re_acquire_state_context
free_workarea_compile
match_ctx_add_entry
parse_bracket_symbol re_string_fetch_byte_case
free_charset
build_trtable re_node_set_merge re_node_set_alloc group_nodes_into_DFAstates re_acquire_state_context bitset_merge bitset_contain bitset_empty
re_node_set_init_copy
duplicate_node_closure duplicate_node_closure re_node_set_insert duplicate_node search_duplicated_node
parse_bracket_exp build_equiv_class bitset_not bitset_set parse_bracket_element build_range_exp free_charset peek_token_bracket bitset_mask build_charclass create_tree build_collating_symbol create_token_tree
parse_bracket_element peek_token_bracket re_string_wchar_at parse_bracket_symbol re_string_char_size_at
re_search_internal re_string_destruct re_string_allocate match_ctx_init match_ctx_free check_matching check_halt_state_context re_string_reconstruct match_ctx_clean set_regs prune_impossible_nodes
set_regs free_fail_stack_return update_regs pop_fail_stack proceed_next_node
prune_impossible_nodes sift_ctx_init check_halt_state_context sift_states_backward merge_state_array
check_arrival_expand_ecl_sub re_node_set_insert re_node_set_contains check_arrival_expand_ecl_sub
re_acquire_state re_node_set_compare create_ci_newstate calc_state_hash
build_wcs_buffer
rpl_re_compile_pattern re_compile_internal
free_dfa_content free_token free_state
get_subexp match_ctx_add_sublast get_subexp_sub clean_state_log_if_needed extend_buffers search_cur_bkref_entry find_subexp_node check_arrival
analyze calc_first optimize_subexps postorder lower_subexps calc_eclosure preorder link_nfa_nodes calc_inveclosure calc_next
re_node_set_init_1
duplicate_tree create_token_tree
calc_eclosure_iter duplicate_node_closure calc_eclosure_iter re_node_set_alloc re_node_set_insert re_node_set_merge
bitset_empty
free_token free_charset
bitset_copy
parse_dup_op fetch_token mark_opt_subexp fetch_number create_tree postorder duplicate_tree free_tree
rpl_re_compile_fastmap re_compile_fastmap_iter
re_dfa_add_node
re_node_set_insert_last
sift_states_bkref re_node_set_remove_at sift_states_backward search_cur_bkref_entry re_node_set_insert check_dst_limits merge_state_array re_node_set_init_copy re_node_set_contains
fetch_number fetch_token
match_ctx_add_subtop
fetch_token peek_token
bitset_set_all
re_string_reconstruct build_wcs_buffer re_string_translate_buffer re_string_skip_chars bitset_contain build_upper_buffer build_wcs_upper_buffer re_string_context_at
rpl_re_match_2 re_search_2_stub
create_cd_newstate re_node_set_init_copy re_node_set_remove_at free_state register_state
check_subexp_matching_top match_ctx_add_subtop
proceed_next_node check_node_accept re_node_set_contains push_fail_stack check_node_accept_bytes re_node_set_insert
rpl_re_match re_search_stub
build_range_exp bitset_set
add_epsilon_src_nodes re_node_set_add_intersect re_node_set_merge re_acquire_state re_node_set_alloc
update_regs
rpl_re_search_2 re_search_2_stub
transit_state re_string_context_at build_trtable transit_state_mb
check_dst_limits_calc_pos check_dst_limits_calc_pos_1
build_upper_buffer
calc_first re_dfa_add_node
re_compile_internal optimize_utf8 re_string_destruct init_dfa free_workarea_compile parse create_initial_state analyze free_dfa_content re_string_construct
parse_sub_exp postorder create_tree fetch_token free_tree parse_reg_exp
re_string_context_at bitset_contain
init_word_char
create_ci_newstate register_state free_state re_node_set_init_copy
update_cur_sifted_state check_subexp_limits re_acquire_state sift_states_bkref add_epsilon_src_nodes
parse fetch_token create_tree parse_reg_exp
rpl_re_set_syntax
rpl_re_set_registers
rpl_regerror

Output (Whoops! Too long for SE to handle.)

\$\endgroup\$
  • 5
    \$\begingroup\$ Well-specified! \$\endgroup\$ – Not that Charles Sep 30 '15 at 19:23
  • \$\begingroup\$ The self-reference in the Node order section makes my head hurt. \$\endgroup\$ – recursive Oct 8 '15 at 14:43
5
+200
\$\begingroup\$

Haskell, 512 bytes

import Data.List
r=reverse
n j|let(w,s)#p|let a?b=or[q!b<GT|(q,r)<-i,a==r,elem q(h p)>elem(a,q)i];a!b|a==b=EQ|a?b||(a<b)>b?a=LT;_!_=GT;l=nub.sortBy(!)$h p;m(v,s)q|h q==[]=(v,[q]:s)|elem q w=(v,[q++" ..."]:s)|(w,x:y)<-(v,[])#q=(w,(q:(u"| "=<<r y)++u"  "x):s)=foldl m(l++w,[])l;c(p,q)=z$p:q:h q;y=z=<<j;i=iterate(nub.sort.(c=<<))y!!length j;h""=[p|p<-id=<<j,and[elem(p,r)i|(r,q)<-i,p==q]];h p=[r|(q,r)<-y,p==q]=unlines=<<r(snd$mempty#"")
u s(x:y)=("+-"++x):map(s++)y
z(x:y)=(,)x<$>y
main=interact$n.map words.lines

Run online at Ideone

\$\endgroup\$
  • \$\begingroup\$ Congrats. Very nice! \$\endgroup\$ – Ell Oct 7 '15 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.