How can I find all equal paths in a degenerate tree from a certain starting vertex?

I have degenerate tree

(it looks like an array or a double linked list). For example, this tree:

enter image description here

Each edge has some weight. I want to find all equal paths that start at each vertex.

Let the edges have the following weights (this is just an example):

a-b = 3

b-c = 1

c-d = 1

d-e = 1

Then:

  • The vertex A

    has no equal path (no vertex on the left side).
  • The vertex B

    has one equal pair. The path B-A

    is equal to the path B-E

    (3 == 3)

    .
  • The vertex C

    has one equal pair. The path B-C

    is equal to the path C-D

    (1 == 1)

    .
  • The vertex D

    has one equal pair. The path C-D

    is equal to the path D-E

    (1 == 1)

    .
  • The vertex E

    has no equal path (no vertex on the right side).

I am implementing a simple algorithm that works in O(n^2)

. But it's too slow for me.

+3


source to share





All Articles