Example #1
0
 def test_children(self):
     """
     FUNCTION: children, in graph.py.
     """
     """Use the function to determine the children of each node"""
     children = []
     for i in range(0, self.n):
         children.append(graph.children(self.dag, i))
     """
     Assert that the function returns the expected children for each node.
     """
     assert children[0] == [1, 2]
     assert children[1] == [3]
     assert children[2] == [3]
     assert children[3] == []
    def test_children(self):
        """
        FUNCTION: children, in graph.py.
        """
        """Use the function to determine the children of each node"""
        children = []
        for i in range(0, self.n):
            children.append(graph.children(self.dag, i))

        """
        Assert that the function returns the expected children for each node.
        """
        assert children[0] == [1, 2]
        assert children[1] == [3]
        assert children[2] == [3]
        assert children[3] == []
Example #3
0
    def dfs_visit(self, u, adj_mat, directed):
        """
        Performs a depth first serach visit on a node in a graph.

        Parameters
        ----------
        -'u': Int
            The index of the node to visit.
        -'adj_mat': Numpy ndarray
            Adjacency matrix. If adj_mat[i, j] = 1, there exists
            a directed edge from node i to node j.
        -'directed': Int
            Equals 1 if it is a directed graph, 0 otherwise.
        """
        self.pre.append(u)
        self.color[0, u] = self.gray
        self.time_stamp = self.time_stamp + 1
        self.d[0, u] = self.time_stamp

        if directed == 1:
          ns = graph.children(adj_mat, u)
        else:
          ns = graph.neighbours(adj_mat, u)
          ns = np.unique(ns)
          ns = np.setdiff1d(np.array(ns), np.array([self.pred[0, u]]))
          ns = ns.tolist()

        for v in ns:
            if self.color[0, v] == self.white:
                self.pred[0, v] = u
                self.dfs_visit(v, adj_mat, directed)
            elif self.color[0, v] == self.gray:
                self.cycle = 1

        self.color[0, u] = self.black
        self.post.append(u)
        self.time_stamp = self.time_stamp + 1
        self.f[0, u] = self.time_stamp
Example #4
0
    def dfs_visit(self, u, adj_mat, directed):
        """
        Performs a depth first serach visit on a node in a graph.

        Parameters
        ----------
        -'u': Int
            The index of the node to visit.
        -'adj_mat': Numpy ndarray
            Adjacency matrix. If adj_mat[i, j] = 1, there exists
            a directed edge from node i to node j.
        -'directed': Int
            Equals 1 if it is a directed graph, 0 otherwise.
        """
        self.pre.append(u)
        self.color[0, u] = self.gray
        self.time_stamp = self.time_stamp + 1
        self.d[0, u] = self.time_stamp

        if directed == 1:
          ns = graph.children(adj_mat, u)
        else:
          ns = graph.neighbours(adj_mat, u)
          ns = np.unique(ns)
          ns = np.setdiff1d(np.array(ns), np.array([self.pred[0, u]]))
          ns = ns.tolist()

        for v in ns:
            if self.color[0, v] == self.white:
                self.pred[0, v] = u
                self.dfs_visit(v, adj_mat, directed)
            elif self.color[0, v] == self.gray:
                self.cycle = 1

        self.color[0, u] = self.black
        self.post.append(u)
        self.time_stamp = self.time_stamp + 1
        self.f[0, u] = self.time_stamp