#StackBounty: #code-golf #ascii-art #graph-theory Draw a network of nodes

Bounty: 50

There is a network of up to 26 nodes (named A to Z or a to z as per your wish). Every pair of nodes may be connected or disconnected. A node may be connected to at most 4 other nodes. Your task is to draw the network in a 2D diagram. Input will be given such that this task is possible (see more constraints in output section).


Format

Input

  • Pairs of letters (A to Z or a to z as per your wish). They are not sorted in any order.
  • Optional – number of pairs

Output

  • An ASCII drawing that shows the actual links between the nodes. Nodes are given by a to z or A to Z. Use - for horizontal links and | for vertical links. Links may be of any (non-zero) length but they should be straight horizontal/vertical lines that do not bend. Spaces can be added provided they don’t disfigure the picture.

You may not use built-ins that help in layout of the graph. Other graph-related built-ins may be allowed (though solutions without built-ins would be more appreciated). Shortest code wins.


Sample data

Input

A B
B F
B L
F K
L K
K R
K S
R P
S J
S P
J A
T V
V N

Output

A - B - F   T - V
|   |   |       |
|   L - K - R   N
|       |   |
J ----- S - P

Input

H C
G H
A B
B F
B C
F G
C D
D A

Output

A - B ----- F
|   |       |
D - C - H - G


Get this bounty!!!

Leave a Reply