algorithm - tree graph - find how many pairs of vertices, for which the sum of edges on a path between them is C -
i've got weighted tree graph, weights positive. need algorithm solve following problem.
how many pairs of vertices there in graph, sum of weights of edges between them equals c?
i thought of solutions thats o(n^2)
for each vertex start dfs , stop when sum gets bigger c
. since number of edges n-1
, gives o(n^2)
solution.
but can better?
for undirected graph, in terms of theoretic asymptotic complexity - no, cannot better, since number of such pairs o(n^2)
.
as example, take 'sun/flower' graph:
g=(v[union]{x},e) e = { (x,v) | v in v } w(e) = 1 (for edges)
it easy see graph indeed tree.
however, number of pairs have distance of 2 (n-1)(n-2)
in omega(n^2)
, , algorithm finds of them omega(n^2)
in case.
Comments
Post a Comment