Is there an effective way to determine if a leaf node is accessible from another arbitrary node in a direct-access acyclic graph?

Wikipedia: Directional acyclic graph

Not sure if the leaf node is still the correct terminology, since it is not really a tree (each node can have several children, as well as several parents), and also I'm actually trying to find all the root nodes (which are actually just a matter of semantics if you change the direction of all the edges that they would be nodes of the sheet).

Right now, we are just looking at the entire graph (reachable from the specified node), but it turns out to be somewhat expensive, so I wonder if there is a better algorithm for this. One thing that I think of is that we keep track of the nodes that have already been visited (when following a different path), and do not double-check them.

Are there any other algorithmic optimizations?

We also thought about saving the list of root nodes, that this node is a descendant, but it seems that saving such a list will be quite expensive if we need to check if it changes every time a node is added, moved or deleted.

Edit:

This is more than just looking for a single node, but rather looking for ALL nodes that are endpoints.

. node . (, , , , OutOfMemory)

Edit2:

, , ( ) (, ) ( ).

+3
3

, , , , - .

, , . , node node. , , , ! , , . .

, node "" , , . , , node. , , , .

, , , , , . , , , , , , , .

EDIT: . , , root/leaf . DepthFirstSearch (DFS) node, . , node. - , , "", , "" . , node . BreadthFirst , , . , , .

, . node , . , , . , , - . ( , , ).

, , , . A node , . . , node, , , (, , ). , , , , - , . . http://en.wikipedia.org/wiki/Graph_(data_structure)

+3

() .

Python:

def reachable(nodes, edges, start, end):
  color = {}
  for n in nodes:
    color[n] = False
  q = [start]
  while q:
    n = q.pop()
    if color[n]:
      continue
    color[n] = True
    for adj in edges[n]:
      q.append(adj)
  return color[end]
+2

x - f (x), Ri, 1 (. 0) , "x can (resp ) Ri.

"" U, R , x U, x U. , < = D Ri.

U f x U.

y: y U, . y, f (x) x (, ), . f (y) .

0
source

Source: https://habr.com/ru/post/1709312/


All Articles