2

A graph $G = (V,E)$ is called an almost tree if it is connected and has most $n + c$ edges where $n = |V|$ and $c$ is a small constant number. How would I go about designing an algorithm for a given almost tree graph $G$ with distinct edge weight costs that computes a minimum spanning tree of $G$ in time $O(n)$?

David Richerby
  • 82,470
  • 26
  • 145
  • 239
Shandy
  • 123
  • 1
  • 4

2 Answers2

1

Here is the description of a desired algorithm in Python style pseudocode.

Generate a spanning tree T of G
Put all remaining edges that are not in T in a list L
For each edge e in L:
  Find the cycle C in the graph that is T plus edge e.
  If e is lighter than the heaviest edge e' in C:
    add e to T and remove e' from T

It requires $O(n)$ operations to generate a spanning tree. It requires $O(n)$ operations to find a cycle when you add an edge to a tree and $O(n)$ operations to find the heaviest edge in a cycle. Since there were total $c+1$ edges remaining, where $c+1$ is a constant, the for loop requires $O(n)$ operations. Adding up other operations of small time, we find that the time-complexity of the algorithm is $O(n)$.

The correctness of the above algorithm has been proved in my answer to turn MST of G to MST of G with one new edge. Note that initially T is its own MST.

John L.
  • 39,205
  • 4
  • 34
  • 93
0

I am writing a new answer because I can't comment yet.

I'm new to graph problems, but I think it is possible to optimise a bit Jonh L.'s answer by finding the cycle in a way that is O(logn) instead of O(n).

Of course, the complexity of the whole algorithm itself remains the same ((c + 1)logn + n is still O(n)), but it's just something I wanted to mention since I took the time study this problem a bit.

YiannisHa
  • 1
  • 1