Lake flow

Started by
1 comment, last by viper110110 8 years, 10 months ago

I have created some lakes, some of which have rivers. These rivers flow out into other lakes, or sometimes out to the ocean (which I have represented as a lake). For the purpose of the graphical world making sense, I now need to raise these lakes so that when they flow, they flow downhill. I'm now stuck on the algorithm for determining the heights.

Guarantees:

I always have a reference to the ocean.

I can traverse this graph of lakes in either direction, despite the rivers having a direction assigned.

A lake can have more than one river flowing in and/or out.

There are no cycles in this graph of lakes.

I can ignore any lakes that don't have rivers, or separate graphs of lakes/rivers that don't flow out to the ocean.

Advertisement
Breadth-first traversal: Start at the ocean, follow rivers upstream, set the height of each lake to max(current height, downstream lake/ocean height + some amount). Allow the traversal to revisit nodes even if they were reached from another path.

There are more efficient ways to traverse the graph if you have huge numbers of lakes and rivers, but that should get you started, anyway. Ideally, you only want to set a lake's height after you've calculated all the heights of its downstream lakes. https://en.wikipedia.org/wiki/Topological_sorting would order the lakes so that this condition is met.

It sounds similar to algorithms used in https://en.wikipedia.org/wiki/Layered_graph_drawing , actually.

Breadth-first traversal: Start at the ocean, follow rivers upstream, set the height of each lake to max(current height, downstream lake/ocean height + some amount). Allow the traversal to revisit nodes even if they were reached from another path.

There are more efficient ways to traverse the graph if you have huge numbers of lakes and rivers, but that should get you started, anyway.

It sounds similar to algorithms used in https://en.wikipedia.org/wiki/Layered_graph_drawing , actually.

Sounds good, I'll give it a shot. I only have a handful of lakes for now, possibly ranging into the dozens, so time isn't much of an issue here.

This topic is closed to new replies.

Advertisement