graphinvent/MolecularGraph.py
def depth_first_search(self, node_ranking : list, node_init : int=0) -> list:
        """
        Starting from the specified `node_init` in the graph, uses a depth-first
        search (DFS) algorithm to find the longest branch nodes, returning an
        ordered list of the nodes traversed. Prioritizes the nodes based on the
        input `node_ranking`. The function uses the edge feature tensor to find
        adjacent nodes.

        Args:
        ----
            node_ranking (list) : Contains the ranking of all the nodes in the
                                  graph (e.g. the canonical RDKit node ranking,
                                  or a random ranking).
            node_init (int)     : Index of node to start the DFS from. Default 0.

        Returns:
        -------
            nodes_visited (list) : DFS ordering for nodes in the molecular graph.
        """
        nodes_visited     = [node_init]
        last_node_visited = node_init

        # loop until all nodes have been visited
        while len(nodes_visited) < self.n_nodes:

            neighbor_nodes = []
            for bond_type in range(self.constants.n_edge_features):
                neighbor_nodes.extend(list(
                    np.nonzero(self.edge_features[last_node_visited, :, bond_type])[0]
                ))
            new_neighbor_nodes = list(
                set(neighbor_nodes) - (set(neighbor_nodes) & set(nodes_visited))
            )

            if not new_neighbor_nodes:  # list is empty
                # backtrack if there are no "new" neighbor nodes i.e. reached end of branch
                current_node_idx  = nodes_visited.index(last_node_visited)
                last_node_visited = nodes_visited[current_node_idx - 1]
                continue

            node_importance = [node_ranking[neighbor_node] for
                               neighbor_node in new_neighbor_nodes]

            # get the most important of the neighboring nodes
            most_important_neighbor_node = node_importance.index(max(node_importance))

            # append the new, sorted neighboring nodes to list of visited nodes
            nodes_visited.extend([new_neighbor_nodes[most_important_neighbor_node]])

            # update the most recently visited node
            last_node_visited = new_neighbor_nodes[most_important_neighbor_node]

        return nodes_visited

        Starting from the specified `node_init` in the graph, uses a depth-first
        search (DFS) algorithm to find the longest branch nodes, returning an
        ordered list of the nodes traversed. Prioritizes the nodes based on the
        input `node_ranking`. The function uses the edge feature tensor to find
        adjacent nodes.

        Args:
        ----
            node_ranking (list) : Contains the ranking of all the nodes in the
                                  graph (e.g. the canonical RDKit node ranking,
                                  or a random ranking).
            node_init (int)     : Index of node to start the DFS from. Default 0.

        Returns:
        -------
            nodes_visited (list) : DFS ordering for nodes in the molecular graph.
        
Starting from the specified `node_init` in the graph, uses a depth-first
search (DFS) algorithm to find the longest branch nodes, returning an
ordered list of the nodes traversed. Prioritizes the nodes based on the
input `node_ranking`. The function uses the edge feature tensor to find
adjacent nodes.
Starting from the specified ` node_init ` in the graph uses a depth - first search ( DFS ) algorithm to find the longest branch nodes returning an ordered list of the nodes traversed . Prioritizes the nodes based on the input ` node_ranking ` . The function uses the edge feature tensor to find adjacent nodes .
def depth_first_search(self, node_ranking : list, node_init : int=0) -> list:
        nodes_visited     = [node_init]
        last_node_visited = node_init
        while len(nodes_visited) < self.n_nodes:
            neighbor_nodes = []
            for bond_type in range(self.constants.n_edge_features):
                neighbor_nodes.extend(list(
                    np.nonzero(self.edge_features[last_node_visited, :, bond_type])[0]
                ))
            new_neighbor_nodes = list(
                set(neighbor_nodes) - (set(neighbor_nodes) & set(nodes_visited))
            )
            if not new_neighbor_nodes:  
                current_node_idx  = nodes_visited.index(last_node_visited)
                last_node_visited = nodes_visited[current_node_idx - 1]
                continue
            node_importance = [node_ranking[neighbor_node] for
                               neighbor_node in new_neighbor_nodes]
            most_important_neighbor_node = node_importance.index(max(node_importance))
            nodes_visited.extend([new_neighbor_nodes[most_important_neighbor_node]])
            last_node_visited = new_neighbor_nodes[most_important_neighbor_node]
        return nodes_visited
def depth_first_search ( self , node_ranking : list , node_init : int = 0 ) -> list : nodes_visited = [ node_init ] last_node_visited = node_init while len ( nodes_visited ) < self . n_nodes : neighbor_nodes = [ ] for bond_type in range ( self . constants . n_edge_features ) : neighbor_nodes . extend ( list ( np . nonzero ( self . edge_features [ last_node_visited , : , bond_type ] ) [ 0 ] ) ) new_neighbor_nodes = list ( set ( neighbor_nodes ) - ( set ( neighbor_nodes ) & set ( nodes_visited ) ) ) if not new_neighbor_nodes : current_node_idx = nodes_visited . index ( last_node_visited ) last_node_visited = nodes_visited [ current_node_idx - 1 ] continue node_importance = [ node_ranking [ neighbor_node ] for neighbor_node in new_neighbor_nodes ] most_important_neighbor_node = node_importance . index ( max ( node_importance ) ) nodes_visited . extend ( [ new_neighbor_nodes [ most_important_neighbor_node ] ] ) last_node_visited = new_neighbor_nodes [ most_important_neighbor_node ] return nodes_visited
Starting from the specified `node_init` in the graph, uses a depth-first
search (DFS) algorithm to find the longest branch nodes, returning an
ordered list of the nodes traversed.
Starting from the specified ` node_init ` in the graph uses a depth - first search ( DFS ) algorithm to find the longest branch nodes returning an ordered list of the nodes traversed .
"""
        Starting from the specified `node_init` in the graph, uses a depth-first
        search (DFS) algorithm to find the longest branch nodes, returning an
        ordered list of the nodes traversed. Prioritizes the nodes based on the
        input `node_ranking`. The function uses the edge feature tensor to find
        adjacent nodes.

        Args:
        ----
            node_ranking (list) : Contains the ranking of all the nodes in the
                                  graph (e.g. the canonical RDKit node ranking,
                                  or a random ranking).
            node_init (int)     : Index of node to start the DFS from. Default 0.

        Returns:
        -------
            nodes_visited (list) : DFS ordering for nodes in the molecular graph.
        """ # loop until all nodes have been visited # list is empty # backtrack if there are no "new" neighbor nodes i.e. reached end of branch # get the most important of the neighboring nodes # append the new, sorted neighboring nodes to list of visited nodes # update the most recently visited node
nodes_visited (list) : DFS ordering for nodes in the molecular graph.
nodes_visited ( list ) : DFS ordering for nodes in the molecular graph .
self
node_ranking
list
Contains the ranking of all the nodes in the
graph .
Contains the ranking of all the nodes in the graph .
node_init
int
Index of node to start the DFS from. Default 0.
Index of node to start the DFS from . Default 0 .