对多条河流支流结构进行排序
我有一个编号的流段列表。每个都列出了下一个下游流段。最后一个流段当然没有引用下游段。
我需要对整条河流进行排序,从最上面的溪流开始,一直到下游。在路口,我需要跳到下一个分支的顶部,顺流而下到路口,然后继续到下一个分支。一个交汇处可能有多个分支(任意数量)连接。
例如:Sub5 流向 Sub 12 SubSINK 是最后一个流段。 未排序:
#####START_TOPOLOGY_BLOCK##########|###########|###########|###########|
Sub5,2454692.294,2603426.954,2456317.294,2596676.954,Sub12
Sub7,2453067.294,2598176.954,2453317.294,2596676.954,Sub12
Sub11,2462692.294,2607676.954,2461067.294,2605176.954,Sub12
Sub13,2449817.294,2601426.954,2450317.294,2593176.954,SubSINK
Sub2,2464567.294,2596801.954,2467317.294,2585676.954,Sub12
Sub12,2469942.294,2601051.954,2470817.294,2593676.954,Sub13
Sub1,2436567.294,2599676.954,2433067.294,2594676.954,Sub2
Sub3,2481067.294,2601301.954,2483067.294,2594676.954,Sub5
Sub4,2455817.294,2588801.954,2458317.294,2576426.954,Sub5
Sub6,2445067.294,2592926.954,2452817.294,2585176.954,Sub7
Sub8,2457942.294,2593551.954,2461067.294,2587426.954,Sub11
Sub9,2471442.294,2592676.954,2467817.294,2585676.954,Sub11
Sub10,2435692.294,2595176.954,2436567.294,2591176.954,Sub11
已排序:
#####START_TOPOLOGY_BLOCK##########|###########|###########|###########|
Sub6,2445067.294,2592926.954,2452817.294,2585176.954,Sub7
Sub7,2453067.294,2598176.954,2453317.294,2596676.954,Sub12
Sub9,2471442.294,2592676.954,2467817.294,2585676.954,Sub11
Sub10,2435692.294,2595176.954,2436567.294,2591176.954,Sub11
Sub8,2457942.294,2593551.954,2461067.294,2587426.954,Sub11
Sub11,2462692.294,2607676.954,2461067.294,2605176.954,Sub12
Sub1,2436567.294,2599676.954,2433067.294,2594676.954,Sub2
Sub2,2464567.294,2596801.954,2467317.294,2585676.954,Sub12
Sub4,2455817.294,2588801.954,2458317.294,2576426.954,Sub5
Sub3,2481067.294,2601301.954,2483067.294,2594676.954,Sub5
Sub5,2454692.294,2603426.954,2456317.294,2596676.954,Sub12
Sub12,2469942.294,2601051.954,2470817.294,2593676.954,Sub13
Sub13,2449817.294,2601426.954,2450317.294,2593176.954,SubSINK
我怎样才能有效地做到这一点? 谢谢你的
问候 Rudy
河流拓扑的第二个例子
START_TOPOLOGY_BLOCK##########|###########|###########|###########|Sub16,2454692.294,2603426.954,2456317.294,2596676.954,Sub17 子7,2453067.294,2598176.954,2453317.294,2596676.954,子9 子8,2462692.294,2607676.954,2461067.294,2605176.954,子9 子4,2449817.294,2601426.954,2450317.294,2593176.954,子5 子1,2464567.294,2596801.954,2467317.294,2585676.954,子2 子14,2469942.294,2601051.954,2470817.294,2593676.954,子15 子19,2436567.294,2599676.954,2433067.294,2594676.954,子20 子13,2481067.294,2601301.954,2483067.294,2594676.954,子20 子10,2455817.294,2588801.954,2458317.294,2576426.954,子11 子6,2445067.294,2592926.954,2452817.294,2585176.954,子11 子17,2457942.294,2593551.954,2461067.294,2587426.954,子18 子15,2471442.294,2592676.954,2467817.294,2585676.954,子18 子9,2435692.294,2595176.954,2436567.294,2591176.954,子10 子2,2475817.294,2597426.954,2474067.294,2594176.954,子3 子18,2481442.294,2593801.954,2482567.294,2587926.954,子19 子12,2484817.294,2588051.954,2483817.294,2584676.954,子13 子21,2478067.294,2592801.954,2481317.294,2587676.954,SubSINK 子5,2437942.294,2589801.954,2437067.294,2587176.954,子6 子3,2439442.294,2589801.954,2439317.294,2589676.954,子5 子20,2435067.294,2583801.954,2441067.294,2574426.954,子21 子11,2476317.294,2590801.954,2476067.294,2590426.954,子12 子32,2473067.294,2587301.954,2468317.294,2583426.954,子31 子33,2469817.294,2557926.954,2461317.294,2549426.954,子31 子33,2475942.294,2590551.954,2475817.294,2590426.954,子31 子34,2477692.294,2582426.954,2474567.294,2573926.954,子26
I have a list of numbered Stream segments. And each one lists the next down stream stream segment. The last stream segment of course has no down stream segment referenced.
I need to order the entire river, starting from the topmost stream and progressing down stream. At junctions I need to jump to the top of the next branch, work down stream to the junction, then continue to the next branch. There may be multiple branches (any number) joined at a junction.
For example: Sub5 flows to Sub 12
SubSINK is the LAst stream segment.
UNSORTED:
#####START_TOPOLOGY_BLOCK##########|###########|###########|###########|
Sub5,2454692.294,2603426.954,2456317.294,2596676.954,Sub12
Sub7,2453067.294,2598176.954,2453317.294,2596676.954,Sub12
Sub11,2462692.294,2607676.954,2461067.294,2605176.954,Sub12
Sub13,2449817.294,2601426.954,2450317.294,2593176.954,SubSINK
Sub2,2464567.294,2596801.954,2467317.294,2585676.954,Sub12
Sub12,2469942.294,2601051.954,2470817.294,2593676.954,Sub13
Sub1,2436567.294,2599676.954,2433067.294,2594676.954,Sub2
Sub3,2481067.294,2601301.954,2483067.294,2594676.954,Sub5
Sub4,2455817.294,2588801.954,2458317.294,2576426.954,Sub5
Sub6,2445067.294,2592926.954,2452817.294,2585176.954,Sub7
Sub8,2457942.294,2593551.954,2461067.294,2587426.954,Sub11
Sub9,2471442.294,2592676.954,2467817.294,2585676.954,Sub11
Sub10,2435692.294,2595176.954,2436567.294,2591176.954,Sub11
SORTED:
#####START_TOPOLOGY_BLOCK##########|###########|###########|###########|
Sub6,2445067.294,2592926.954,2452817.294,2585176.954,Sub7
Sub7,2453067.294,2598176.954,2453317.294,2596676.954,Sub12
Sub9,2471442.294,2592676.954,2467817.294,2585676.954,Sub11
Sub10,2435692.294,2595176.954,2436567.294,2591176.954,Sub11
Sub8,2457942.294,2593551.954,2461067.294,2587426.954,Sub11
Sub11,2462692.294,2607676.954,2461067.294,2605176.954,Sub12
Sub1,2436567.294,2599676.954,2433067.294,2594676.954,Sub2
Sub2,2464567.294,2596801.954,2467317.294,2585676.954,Sub12
Sub4,2455817.294,2588801.954,2458317.294,2576426.954,Sub5
Sub3,2481067.294,2601301.954,2483067.294,2594676.954,Sub5
Sub5,2454692.294,2603426.954,2456317.294,2596676.954,Sub12
Sub12,2469942.294,2601051.954,2470817.294,2593676.954,Sub13
Sub13,2449817.294,2601426.954,2450317.294,2593176.954,SubSINK
How can I do this efficiently ??
Thank you
Regards Rudy
2nd Example of River Topology
START_TOPOLOGY_BLOCK##########|###########|###########|###########|
Sub16,2454692.294,2603426.954,2456317.294,2596676.954,Sub17
Sub7,2453067.294,2598176.954,2453317.294,2596676.954,Sub9
Sub8,2462692.294,2607676.954,2461067.294,2605176.954,Sub9
Sub4,2449817.294,2601426.954,2450317.294,2593176.954,Sub5
Sub1,2464567.294,2596801.954,2467317.294,2585676.954,Sub2
Sub14,2469942.294,2601051.954,2470817.294,2593676.954,Sub15
Sub19,2436567.294,2599676.954,2433067.294,2594676.954,Sub20
Sub13,2481067.294,2601301.954,2483067.294,2594676.954,Sub20
Sub10,2455817.294,2588801.954,2458317.294,2576426.954,Sub11
Sub6,2445067.294,2592926.954,2452817.294,2585176.954,Sub11
Sub17,2457942.294,2593551.954,2461067.294,2587426.954,Sub18
Sub15,2471442.294,2592676.954,2467817.294,2585676.954,Sub18
Sub9,2435692.294,2595176.954,2436567.294,2591176.954,Sub10
Sub2,2475817.294,2597426.954,2474067.294,2594176.954,Sub3
Sub18,2481442.294,2593801.954,2482567.294,2587926.954,Sub19
Sub12,2484817.294,2588051.954,2483817.294,2584676.954,Sub13
Sub21,2478067.294,2592801.954,2481317.294,2587676.954,SubSINK
Sub5,2437942.294,2589801.954,2437067.294,2587176.954,Sub6
Sub3,2439442.294,2589801.954,2439317.294,2589676.954,Sub5
Sub20,2435067.294,2583801.954,2441067.294,2574426.954,Sub21
Sub11,2476317.294,2590801.954,2476067.294,2590426.954,Sub12
Sub32,2473067.294,2587301.954,2468317.294,2583426.954,Sub31
Sub33,2469817.294,2557926.954,2461317.294,2549426.954,Sub31
Sub33,2475942.294,2590551.954,2475817.294,2590426.954,Sub31
Sub34,2477692.294,2582426.954,2474567.294,2573926.954,Sub26
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将段表示为图形数据结构。然后,熟悉的图算法(如 DFS、BFS 和拓扑排序)应该可以为您完成工作,具体取决于您的具体需求。
如果您可以用一个简单的示例或图片来澄清您的问题,以便清楚地了解您需要哪种排序顺序,我也许可以提供更具体的方向。
You need to represent your segments as a graph data structure. Then, familiar graph algorithms like DFS, BFS and topological sort should do the work for you, depending on what you need exactly.
If you could clarify your question with a simple example or a picture so it's clearly understood which sorting order you need, I may be able to provide a more specific direction.
以下简单的内容有望演示如何创建正确的河流图以及遍历它所需的所有方法。
测试
将打印:
编辑:使用第二个示例。首先请注意,您有多个水槽。如果这是故意的,那么处理森林也应该非常简单(比如让水槽返回所有森林,然后循环处理遍历)。但无论如何,前 22 行的输出是:
编辑 2:我的答案更多的是给出一些如何处理河树本身的想法。对于您的特定应用程序,您很可能可以找到更好的方法来实际处理这些点。但无论如何,您现在可以像这样访问它们:
您也可以完全忽略
class Point
并修改_insert
,例如:然后您将访问像这样的点:
Something simple along the following lines hopefully demonstrates how to create the proper river graph and all the methods needed to traverse on it.
And a test
will print:
Edit: With your second example. First note that you have more than 1 sink. If that's intentional, it should be quite straightforward to handle forests as well (like letting sink return all of them and then process traversing in a loop). But anyway with the first 22 rows the output is:
Edit 2: My answer is more to give some ideas how to handle the river tree itself. For your particular application you most probable can find better ways to actually handle the points. But anyway you can access them now like:
You may also ignore totally the
class Point
and modify_insert
like:Then you'll access points like: