How to determine if two nodes are connected?

前端 未结 11 1182
清酒与你
清酒与你 2020-12-09 03:31

I\'m concerned that this might be working on an NP-Complete problem. I\'m hoping someone can give me an answer as to whether it is or not. And I\'m looking for more of an an

11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-09 04:09

    I see you have got your answer that it's definitely not NP-Complete and this is a very old question as well.

    However, I'll just propose another approach to look at the problem. You could use disjoint sets for this. In most cases, for the given scenario, the approach will result in better time than doing a graph traversal (That includes constant time for a large chunk of tests). However, building the graph might take good amount of time, if union by rank or path compression is used.

    You can read about the Data Structure here.

提交回复
热议问题