Пример #1
0
    def test_recognise_edges(self):
        graph = DAG()
        graph.add_node('B')
        graph.add_node('C')
        graph.add_edge('B', 'C')

        self.assertRaises(ValueError, graph.add_edge('B', 'C'))
Пример #2
0
    def test_add_edges(self):
        graph = DAG()
        graph.add_node('B')
        graph.add_node('C')
        graph.add_edge('B', 'C')

        self.assertEqual(graph.graph, {'B': ['C'], 'C': []})
Пример #3
0
    def test_add_nodes(self):
        graph = DAG()
        graph.add_node('A')
        self.assertEqual(graph.graph, {'A': []})

        graph.add_node('B')
        graph.add_node('C')
        self.assertEqual(graph.graph, {'A': [], 'B': [], 'C': []})
Пример #4
0
    def BFS(self, start):
        """
        Creates a BFS DAG and returns list of triples (in, out, weight)

        Positional Arguments:
            start - key in vertex

        Return:
            [] if start does not exist

        """

        leaves, dirtyNodes = ({}, {})
        upEdges, queue = ([], [])
        dag = DAG()

        try:
            queue.append(start)
            dirtyNodes[start] = 1
        except KeyError:
            return []

        for node in queue:
            dag.add_vertex(node)
            weight = dirtyNodes[node] + 1
            notDirtyNeighbors = []

            for edge in self.vertices[node]:
                if edge.nextVertex not in dirtyNodes:
                    notDirtyNeighbors.append(edge.nextVertex)
                elif dirtyNodes[edge.nextVertex] == weight:
                    dag.add_edge(node, edge.nextVertex)
                    upEdges.append((edge.nextVertex, node))

            queue.extend(notDirtyNeighbors)

            for neighbor in notDirtyNeighbors:
                dirtyNodes[neighbor] = weight
                dag.add_edge(node, neighbor)
                upEdges.append((neighbor, node))

        return dag
Пример #5
0
    def BFS(self, start):
        """
        Creates a BFS DAG and returns list of triples (in, out, weight)

        Positional Arguments:
            start - key in vertex

        Return:
            [] if start does not exist

        """

        leaves, dirtyNodes = ({}, {})
        upEdges, queue = ([], [])
        dag = DAG()

        try:
            queue.append(start)
            dirtyNodes[start] = 1
        except KeyError:
            return []

        for node in queue:
            dag.add_vertex(node)
            weight = dirtyNodes[node] + 1
            notDirtyNeighbors = []

            for edge in self.vertices[node]:
                if edge.nextVertex not in dirtyNodes:
                    notDirtyNeighbors.append(edge.nextVertex)
                elif dirtyNodes[edge.nextVertex] == weight:
                    dag.add_edge(node, edge.nextVertex)
                    upEdges.append((edge.nextVertex, node))

            queue.extend(notDirtyNeighbors)

            for neighbor in notDirtyNeighbors:
                dirtyNodes[neighbor] = weight
                dag.add_edge(node, neighbor)
                upEdges.append((neighbor, node))

        return dag
Пример #6
0
 def test_complex_lca(self):
     graph = DAG()
     graph.add_node('A')
     graph.add_node('B')
     graph.add_node('C')
     graph.add_node('D')
     graph.add_node('E')
     graph.add_node('F')
     graph.add_node('G')
     graph.add_node('H')
     graph.add_node('I')
     graph.add_edge('A', 'B')
     graph.add_edge('B', 'C')
     graph.add_edge('B', 'D')
     graph.add_edge('C', 'F')
     graph.add_edge('C', 'E')
     graph.add_edge('D', 'F')
     graph.add_edge('D', 'H')
     graph.add_edge('B', 'G')
     graph.add_edge('H', 'I')
     graph.add_edge('C', 'I')
     self.assertEqual(graph.dfs_wrapper(graph.graph, 'A', 'A'), 'A')
     self.assertEqual(graph.dfs_wrapper(graph.graph, 'F', 'E'), 'C')
     self.assertEqual(graph.dfs_wrapper(graph.graph, 'I', 'G'), 'B')
     self.assertEqual(graph.dfs_wrapper(graph.graph, 'F', 'I'), 'C')
Пример #7
0
 def test_basic_lca(self):
     graph = DAG()
     graph.add_node('A')
     print(graph.dfs_wrapper(graph.graph, 'A', 'A'))
Пример #8
0
 def test_complex(self):
     graph = DAG()
     graph.add_node('A')
     graph.add_node('B')
     graph.add_node('C')
     graph.add_node('D')
     graph.add_node('E')
     graph.add_node('F')
     graph.add_node('G')
     graph.add_node('H')
     graph.add_node('I')
     graph.add_edge('A', 'B')
     graph.add_edge('B', 'C')
     graph.add_edge('B', 'D')
     graph.add_edge('C', 'F')
     graph.add_edge('C', 'E')
     graph.add_edge('D', 'F')
     graph.add_edge('D', 'H')
     graph.add_edge('B', 'G')
     graph.add_edge('H', 'I')
     graph.add_edge('C', 'I')
     self.assertEqual(
         graph.graph, {
             'A': ['B'],
             'B': ['C', 'D', 'G'],
             'C': ['F', 'E', 'I'],
             'D': ['F', 'H'],
             'E': [],
             'F': [],
             'G': [],
             'H': ['I'],
             'I': []
         })
Пример #9
0
 def test_recognise_nodes(self):
     graph = DAG()
     graph.add_node('A')
     self.assertFalse(graph.add_node('A'))