Check if edge is included in SOME MST in linear time (non-distinct values)

后端 未结 4 531
醉梦人生
醉梦人生 2021-01-31 19:24

I am working on an algorithm to check if a given edge is included in one of all possible mst\'s.

For this question, we are considering non-distinct values and our edge e

4条回答
  •  时光取名叫无心
    2021-01-31 20:14

    We will solve this using MST cycle property, which says that, "For any cycle C in the graph, if the weight of an edge e of C is larger than the weights of all other edges of C, then this edge cannot belong to an MST."

    Now, run the following O(E+V) algorithm to test if the edge E connecting vertices u and v will be a part of some MST or not.

    Step 1

    Run dfs from one of the end-points(either u or v) of the edge E considering only those edges that have weight less than that of E.

    Step 2

    Case 1 If at the end of this dfs, the vertices u and v get connected, then edge E cannot be a part of some MST. This is because in this case there definitely exists a cycle in the graph with the edge E having the maximum weight and it cannot be a part of the MST(from the cycle property).

    Case 2 But if at the end of the dfs u and v stay disconnected, then edge E must be the part of some MST as in this case E is always not the maximum weight edge in all the cycles that it is a part of.

提交回复
热议问题