Example #1
0
def build_directed_acyclic_graph():
    graph = Graph()
    graph.add_edge('u', 'v')
    graph.add_edge('v', 'y')
    graph.add_edge('y', 'x')
    graph.add_edge('u', 'x')
    graph.add_edge('w', 'y')
    graph.add_edge('w', 'z')
    return graph, graph.get_vertex('u')
def createFirstGraph():
    g = Graph(8)

    # create nodes
    n1 = Node("a")
    n2 = Node("b")
    n3 = Node("c")
    n4 = Node("d")
    n5 = Node("e")
    n6 = Node("f")
    n7 = Node("g")
    n8 = Node("h")

    # Create connections
    # n1
    n1.addAdjacent(n2)
    # n2
    n2.addAdjacent(n1)
    n2.addAdjacent(n3)
    n2.addAdjacent(n4)
    # n3
    n3.addAdjacent(n2)
    # n4
    n4.addAdjacent(n2)
    n4.addAdjacent(n5)
    # n5
    n5.addAdjacent(n4)
    n5.addAdjacent(n6)
    n5.addAdjacent(n7)
    # n6
    n6.addAdjacent(n5)
    # n7
    n7.addAdjacent(n5)
    n7.addAdjacent(n8)
    # n8
    n8.addAdjacent(n7)

    nodes = [n1, n2, n3, n4, n5, n6, n7, n8]
    for n in nodes:
        g.addNode(n)

    return g
    def test_add_vertex(self):
        graph = Graph()
        graph.add_vertex("A")
        graph.add_vertex("B")

        self.assertEqual(2, len(graph.get_vertices()))
        self.assertIsInstance(graph.get_vertex('A'), Vertex)
def read_file(filename):
    """Read the txt file containg graph information and return them
    in a list

    Args: 
        filename (txt): takes a text file to read from

    Returns:
        graph elements (tuple): Tuple of graph object, list of vertices and edges
    """
    edges = []
    with open(filename, 'r') as file:
        for counter, line in enumerate(file):

            # get type of the graph
            if counter == 0:
                graph_type = line.strip()
                # create undirected graph
                if graph_type == 'G':
                    graph = Graph(directed=False)
                elif graph_type == 'D':
                    graph = Graph(directed=True)
                else:
                    raise ValueError(
                        "Graph type is not specified correctly, type can be 'G' or 'D'!"
                    )

            # get vertices
            elif counter == 1:
                vertices = line.strip().split(',')

            # grab all the edges
            else:

                edge = line.strip('()\n').split(',')
                if len(edge) > 3 or len(edge) < 2:
                    raise Exception("Edges must contain 2 or 3 values!")
                edges.append(edge)

    return graph, vertices, edges
Example #5
0
def construct_undirected_graph():
    """
     1 -> 2
     ^    |
     |    v
      --- 3 ---> 4
          |
          V
          5

    """
    graph = Graph()
    graph_node_1 = GraphNode(1)
    graph_node_2 = GraphNode(2)
    graph_node_3 = GraphNode(3)
    graph_node_4 = GraphNode(4)
    graph_node_5 = GraphNode(5)
    graph_node_1.add_node(graph_node_2)
    graph_node_2.add_node(graph_node_3)
    graph_node_3.add_node(graph_node_1)
    graph_node_3.add_node(graph_node_4)
    graph_node_4.add_node(graph_node_5)
    graph.add_nodes([graph_node_1, graph_node_2, graph_node_3])
    return graph
def create_second_graph():
    g = Graph(12)

    # create nodes
    n1 = Node("a")
    n2 = Node("b")
    n3 = Node("c")
    n4 = Node("d")
    n5 = Node("e")
    n6 = Node("f")
    n7 = Node("g")
    n8 = Node("h")
    n9 = Node("i")
    n10 = Node("j")
    n11 = Node("k")
    n12 = Node("l")

    # create connections
    n1.addAdjacent(n2)  #n1
    n2.addAdjacent(n3)  #n2
    n3.addAdjacent(n4)  #n3
    n3.addAdjacent(n5)
    n3.addAdjacent(n6)
    n4.addAdjacent(n7)  #n4
    n5.addAdjacent(n8)  #n5
    n6.addAdjacent(n9)  #n6
    n8.addAdjacent(n11)  #n8
    n9.addAdjacent(n10)  #n9
    n11.addAdjacent(n12)  #n11

    # add nodes to graph
    nodes = [n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12]
    for n in nodes:
        g.addNode(n)

    return g
Example #7
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        assert(self.get_cfgs('task_type').lower() == 'training'.lower()),\
            "training task %s created for a non-training scenario:%s" % (
                self.get_name(), self.scenario_name)

        self.dataset_name = self.get_cfgs('dataset_name')
        self.graph_name = self.get_cfgs('graph_name')

        # Datasets used
        self.train_set_name = self.get_cfgs('train_set_name')
        if self.has_pseudo_labels:
            self.train_set_name = self.get_cfgs('pseudo_set_name',
                                                default=self.train_set_name)
        self.val_set_name = self.get_cfgs('val_set_name')
        self.test_set_name = self.get_cfgs('test_set_name')

        self.pi_model = self.get_cfgs('pi_model')
        self.validate_when_epoch_is_devisable_by = self.get_cfgs(
            'validate_when_epoch_is_devisable_by')

        self.dataset = Dataset_Factory().get_dataset(
            dataset_name=self.dataset_name,
            batch_size_multiplier=self.get_cfgs('batch_size_multiplier', 1),
        )

        experiment_names = [self.train_set_name, self.val_set_name]
        if self.test_set_name:
            experiment_names.append(self.test_set_name)

        for experiment_name in experiment_names:
            # Make sure that we have all the expected datasets
            if experiment_name not in self.dataset.experiments:
                raise ValueError(
                    f'The set \'{experiment_name}\' cannot be found in dataset "{self.dataset_name}"'
                )

            self.graphs[experiment_name.lower()] = \
                Graph(graph_name=self.graph_name,
                      experiment_set=self.dataset.experiments[experiment_name],
                      task_cfgs=self.task_cfgs,
                      scene_cfgs=self.scene_cfgs,
                      scenario_cfgs=self.scenario_cfgs)
    def test_add_edge(self):
        graph = Graph()
        graph.add_vertex("A")
        graph.add_vertex("B")
        graph.add_vertex("C")

        graph.add_edge("A", "C")
        graph.add_edge("A", "C", 3)

        self.assertEqual(3, len(graph.get_vertices()))
        self.assertEqual(2, len(graph.edge_list))
        graph.add_vertex("E")
        graph.add_vertex("F")
        graph.add_edge("E", "F")

        self.assertEqual(5, graph.num_vertices)
        self.assertEqual(3, graph.num_edges)
        self.assertCountEqual(["A", "B", "C", "E", "F"], graph.get_vertices())
Example #9
0
def build_undirected_edge_weighted_graph():
    graph = Graph()
    graph.add_edge('a', 'b', cost=4, edge_type='undirected')
    graph.add_edge('b', 'c', cost=8, edge_type='undirected')
    graph.add_edge('c', 'd', cost=7, edge_type='undirected')
    graph.add_edge('d', 'e', cost=9, edge_type='undirected')
    graph.add_edge('e', 'f', cost=10, edge_type='undirected')
    graph.add_edge('f', 'g', cost=2, edge_type='undirected')
    graph.add_edge('g', 'h', cost=1, edge_type='undirected')
    graph.add_edge('a', 'h', cost=8, edge_type='undirected')
    graph.add_edge('b', 'h', cost=11, edge_type='undirected')
    graph.add_edge('h', 'i', cost=7, edge_type='undirected')
    graph.add_edge('i', 'c', cost=2, edge_type='undirected')
    graph.add_edge('i', 'g', cost=6, edge_type='undirected')
    graph.add_edge('c', 'f', cost=4, edge_type='undirected')
    graph.add_edge('d', 'f', cost=14, edge_type='undirected')
    return graph, graph.get_vertex('a')
Example #10
0
def build_undirected_graph():
    graph = Graph()
    graph.add_edge('u', 'v', 'undirected')
    graph.add_edge('v', 'y', 'undirected')
    graph.add_edge('y', 'x', 'undirected')
    graph.add_edge('x', 'v', 'undirected')
    graph.add_edge('u', 'x', 'undirected')
    graph.add_edge('w', 'z', 'undirected')
    graph.add_edge('z', 'z', 'undirected')
    return graph, graph.get_vertex('u')
Example #11
0
def build_directed_edge_weighted_graph():
    graph = Graph()
    graph.add_edge('s', 't', cost=10)
    graph.add_edge('s', 'y', cost=5)
    graph.add_edge('t', 'y', cost=2)
    graph.add_edge('y', 't', cost=3)
    graph.add_edge('t', 'x', cost=1)
    graph.add_edge('y', 'z', cost=2)
    graph.add_edge('y', 'x', cost=9)
    graph.add_edge('z', 's', cost=7)
    graph.add_edge('z', 'x', cost=6)
    graph.add_edge('x', 'z', cost=4)
    return graph, graph.get_vertex('s')