1

I have a collection of rooms, each of which is connected to one or more other rooms via the cardinal directions (north, south, east, west). The rooms have been connected in such a way that if A is west of B, then B is east of A; thus undirected graph. Now I need to take this collection of rooms and graph them on a coordinate plane. All edges must be parallel to the X or Y axis.

I've tried some different approaches, but I think the most effective so far is as follows:

  1. Find the "center" and assign it (0,0) (room for which the sum of the lengths of the shortest paths to every other room is smallest). I don't know if this is really necessary, but it makes centering the output easier.
  2. Walk out from the center C and for each room R encountered, assign a coordinate (X, Y), where P is the path connecting C=>R resulting in the greatest displacement on the coordinate plane, X is the net horizontal movement along P, and Y is the net vertical movement along P.

Assume the following vectors for directions: North = [0,1] South = [0,-1] East = [1,0] West = [-1,0]

Here is an example of correct output I've been able to produce. Unfortunately other graphs have not been completely successful.

Taylor
  • 51
  • 5

1 Answers1

1

That sounds like a reasonable approach. I think there are a whole family of approaches which amount to doing a topological sort on the graph, and then processing the nodes in order, so that for every node you have space to place it because the nodes less than it in topological order are all to one side of it.

Another way of viewing this is a topological sort is not to work from the centre but to form a directed graph in which each link is directed either north to south or east to west. Following a topological sort you know when placing a node that there is no node so far placed that lies to the north of it or the west of it, so you can assign its co-ordinates based on its east and south neighbours.

If you want the nodes placed around the origin as before, you could change them all afterwards, adding a constant to each co-ordinate value to shift them as desired.

mcdowella
  • 19,301
  • 2
  • 19
  • 25
  • First, thanks very much for your input. I had run into topological sorting before in my research, and thought it only applied to DAGs. Are you just saying to "pretend" that the graph is a DAG for the purposes of the algorithm? – Taylor Jan 12 '17 at 13:26
  • @Taylor yes, by saying that a North-South link is a directed link pointing North, and saying that an East-West link is a directed link pointing West. – mcdowella Jan 12 '17 at 18:19