1
$\begingroup$

How can I find the minimal spanning tree of graph with edge values from 1 to 5 integers (no need to be unique) most effectively? I know I can use Kruskal algorithm, but how can I modify the algorithm to find it faster when I know there are edges with values only 1, 2, 3, 4 or 5? I cant figure it out how it could be faster when I know this limitation on edges.

$\endgroup$
1

1 Answer 1

1
$\begingroup$

Take a look at the complexity of Kruskal's algorithm. Since the edges are with integer weights, you can sort them in linear time. The entire algorithm will take a total of $O(|V|\alpha(|E|)+|E|)$. We need $O(|E|)$ to sort, and an additional $O(|V|\alpha(|E|))$ for Kruskal's algorithm.

$\endgroup$
2
  • $\begingroup$ so what is the modification? how can I fasten it when I know the values are 1-5 in comparison to e.g. graph with values 1-5000? $\endgroup$
    – Rikib1999
    May 15, 2021 at 12:13
  • $\begingroup$ Read the last paragraph in the link. It explains what needs to be done and what the complexity will be $\endgroup$
    – nir shahar
    May 15, 2021 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.