Skip to content
DeepForgeby svx
All problems
MediumGraph Algorithmsgraph-107

MST Uniqueness Check

Problem

Decide whether the minimum spanning tree of an undirected weighted graph with n nodes is unique.

Build one MST with Kruskal, then for every non-tree edge compare its weight to the maximum edge weight on the tree path between its endpoints. If any weight is equal, swapping produces a second MST, so the answer is False. edges is a list of [u, v, w] triples; return False for disconnected graphs.

Hint

Equal weights along a cycle alternative are exactly what creates multiple MSTs.

Test cases

case 1
in: [4,[[0,1,1],[1,2,2],[2,3,3]]]
expected: true
case 2
in: [3,[[0,1,1],[1,2,1],[0,2,1]]]
expected: false
case 3
in: [3,[[0,1,1],[1,2,2],[0,2,3]]]
expected: true
case 4
in: [4,[[0,1,1],[1,2,1],[2,3,1],[3,0,1]]]
expected: false
case 5
in: [2,[[0,1,1],[0,1,1]]]
expected: false
Starter codePython
def is_mst_unique(n, edges):
    # Your code here
    pass

5 test cases · run your code against them in the editor