(no title)
hermanschaaf | 6 years ago
Q: Given V (the number of vertices), E (the number of edges) and L (a list of the edges) for a connected undirected graph, determine whether the graph is a tree.
----
A: It can be done in O(1) by: return V == E + 1
The number of vertices in a tree is always one more than the number of edges. We also know that the graph is connected, so it has to be a tree.
No comments yet.