Exemple #1
0
def undigraph_to_mixedgraph(undiGraph):
    mixedGraph = gum.MixedGraph()
    for nodeId in undiGraph.nodes():
        mixedGraph.addNodeWithId(nodeId)
    for x, y in undiGraph.edges():
        mixedGraph.addEdge(x, y)
    return mixedGraph
Exemple #2
0
def mixedgraph_deepcopy(mixedGraph):
    copy = gum.MixedGraph()
    for nodeId in mixedGraph.nodes():
        copy.addNodeWithId(nodeId)
    for x, y in mixedGraph.arcs():
        copy.addArc(x, y)
    for x, y in mixedGraph.edges():
        copy.addEdge(x, y)
    return copy
Exemple #3
0
    def testCopyConstructor(self):
        mixed_graph = gum.MixedGraph()

        mixed_graph.addNodes(4)

        mixed_graph.addArc(0, 2)
        mixed_graph.addEdge(1, 2)
        mixed_graph.addArc(2, 3)

        self.assertEquals(mixed_graph.parents(2), {0})
        self.assertEquals(mixed_graph.children(2), {3})
        self.assertEquals(mixed_graph.neighbours(2), {1})
        self.assertEquals(mixed_graph.adjacents(2), {0, 1, 3})

        copy = mixed_graph
        self.assertEqual(mixed_graph, copy)

        copy = gum.MixedGraph(mixed_graph)
        self.assertEqual(mixed_graph, copy)
Exemple #4
0
    def testConstructorFromDG(self):
        dg = gum.DiGraph()

        dg.addNodes(4)

        dg.addArc(0, 2)
        dg.addArc(1, 2)
        dg.addArc(2, 3)

        mixed_graph = gum.MixedGraph()

        mixed_graph.addNodes(4)

        mixed_graph.addArc(0, 2)
        mixed_graph.addArc(1, 2)
        mixed_graph.addArc(2, 3)

        mg = gum.MixedGraph(dg)

        self.assertEqual(mixed_graph, mg)
Exemple #5
0
 def testPath(self):
     mg = gum.MixedGraph()
     mg.addNodes(5)
     self.assertEqual(mg.mixedUnorientedPath(1, 4), [])
     self.assertEqual(mg.mixedOrientedPath(1, 4), [])
     mg.addArc(0, 1)
     mg.addEdge(1, 2)
     mg.addArc(3, 2)
     self.assertEqual(mg.mixedUnorientedPath(0, 3), [0, 1, 2, 3])
     self.assertEqual(mg.mixedOrientedPath(0, 3), [])
     self.assertEqual(mg.mixedOrientedPath(0, 2), [0, 1, 2])
Exemple #6
0
    def testConstructorFromUG(self):
        ug = gum.UndiGraph()

        ug.addNodes(4)

        ug.addEdge(0, 2)
        ug.addEdge(1, 2)
        ug.addEdge(2, 3)

        mixed_graph = gum.MixedGraph()

        mixed_graph.addNodes(4)

        mixed_graph.addEdge(0, 2)
        mixed_graph.addEdge(1, 2)
        mixed_graph.addEdge(2, 3)

        mg = gum.MixedGraph(ug)

        self.assertEqual(mixed_graph, mg)
Exemple #7
0
def dag_to_cpdag(dag):
    cpdag = gum.MixedGraph()

    for node in dag.nodes():
        cpdag.addNodeWithId(node)

    for arc in dag.arcs():
        cpdag.addArc(arc[0], arc[1])

    v = []
    while True:
        v.clear()
        for x in dag.topologicalOrder():
            for y in cpdag.children(x):
                if not strongly_protected(cpdag, x, y):
                    v.append((x, y))
        for arc in v:
            cpdag.eraseArc(arc[0], arc[1])
            cpdag.addEdge(arc[0], arc[1])
        if len(v) <= 0:
            break

    return cpdag
Exemple #8
0
def read_graph(file_name):
    print("Loading file {}".format(file_name))
    dot_graph = dot.graph_from_dot_file(file_name)
    isUndiGraph = False

    # Cleaning nodes
    for node in dot_graph.get_nodes():
        name = node.get_name()
        if name in ['node', 'edge']:
            if name == 'edge':
                if node.get_attributes() and node.get_attributes(
                )['dir'] == 'none':
                    isUndiGraph = True
            dot_graph.del_node(node)

    # Getting node names
    node_name_map = {}
    for i, node in enumerate(dot_graph.get_nodes()):
        node_name_map[node.get_name()] = i
    nodeId = max(node_name_map.values()) + 1
    for edge in dot_graph.get_edges():
        source = edge.get_source()
        destination = edge.get_destination()
        if source not in node_name_map.keys():
            node_name_map[source] = nodeId
            nodeId += 1
        if destination not in node_name_map.keys():
            node_name_map[destination] = nodeId
            nodeId += 1

    edges = []
    arcs = []
    for edge in dot_graph.get_edges():
        if (isUndiGraph or (edge.get_attributes()
                            and edge.get_attributes()['dir'] == 'none')):
            edges.append(
                gum.Edge(node_name_map[edge.get_source()],
                         node_name_map[edge.get_destination()]))
        else:
            arcs.append(
                gum.Arc(node_name_map[edge.get_source()],
                        node_name_map[edge.get_destination()]))

    if not edges:  # DAG
        graph = gum.DAG()
        for node_name in node_name_map:
            graph.addNodeWithId(node_name_map[node_name])
        for arc in arcs:
            graph.addArc(arc.tail(), arc.head())

    elif not arcs:  # UndiGraph
        graph = gum.UndiGraph()
        for node_name in node_name_map:
            graph.addNodeWithId(node_name_map[node_name])
        for edge in edges:
            graph.addEdge(edge.first(), edge.second())

    else:  # MixedGraph
        graph = gum.MixedGraph()
        for node_name in node_name_map:
            graph.addNodeWithId(node_name_map[node_name])
        for edge in edges:
            graph.addEdge(edge.first(), edge.second())
        for arc in arcs:
            graph.addArc(arc.tail(), arc.head())

    # Since python3.7, dict are insertion ordered so
    # just returning values should be fine but we never know !
    return graph, list(node_name_map.keys())
Exemple #9
0
 def testAddNodes(self):
     self._testAddNodes(gum.DiGraph())
     self._testAddNodes(gum.UndiGraph())
     self._testAddNodes(gum.MixedGraph())
     self._testAddNodes(gum.DAG())