예제 #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
파일: Graph.py 프로젝트: elahi-arman/Python
    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
파일: Graph.py 프로젝트: elahi-arman/Python
    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'))