Exemplo n.º 1
0
def read_graph_from_file(filename):
    """
    Read in data from the specified filename, and create and return a graph
    object corresponding to that data.

    Arguments:
    filename (string): The relative path of the file to be processed

    Returns:
    Graph: A directed or undirected Graph object containing the specified
    vertices and edges
    """

    # Use 'open' to open the file
    f = open(filename, "r")

    # Use the first line (G or D) to determine whether graph is directed
    # and create a graph object
    first_line = f.readline().strip()
    graph = Graph(is_directed=False)

    # If undirected
    if first_line == "G":
        graph = Graph(is_directed=False)

    # If directed
    elif first_line == "D":
        graph = Graph()

    else:
        raise ValueError("Invalid graph type")

    # Use the second line to add the vertices to the graph
    vertices = f.readline().strip().split(",")
    for vertex in vertices:
        graph.add_vertex(vertex)

    # Use the 3rd+ line to add the edges to the graph
    for line in f:
        if line != '':
            # print(line)
            curr = line.replace('(', '')
            curr = curr.replace(')', '').strip()
            curr = curr.split(",")
            # print("Current line: {}".format(curr))

            if curr:
                vert1 = graph.get_vertex(curr[0])
                vert2 = graph.get_vertex(curr[1])
                # print("Vert 1: {} Vert 2: {}".format(vert1, vert2))

                graph.add_edge(vert1.get_id(), vert2.get_id())

    f.close()
    return graph
Exemplo n.º 2
0
class GraphTest(unittest.TestCase):
    V1_LABEL = 1
    V2_LABEL = 2
    V3_LABEL = 3
    V4_LABEL = 4
    V5_LABEL = 5

    E1_LABEL = 1
    E2_LABEL = 2
    E3_LABEL = 3

    if settings.INTEGER_VERTICES:
        v1 = 1
        v2 = 2
        v3 = 3
        v4 = 4
        v5 = 5
    else:
        v1 = Vertex(V1_LABEL)
        v2 = Vertex(V2_LABEL)
        v3 = Vertex(V3_LABEL)
        v4 = Vertex(V4_LABEL)
        v5 = Vertex(V5_LABEL)

    e1 = Edge(E1_LABEL, v1, v2, 3)
    e2 = Edge(E2_LABEL, v2, v1, 5)
    e3 = Edge(E3_LABEL, v4, v5, 10)

    def setUp(self):
        self.g = Graph()

    def test_get_vertex(self):
        self.g.add_vertex(self.v1)
        self.assertEqual(self.g.get_vertex(self.V1_LABEL), self.v1)

    def test_get_edge_ends(self):
        self.assertTupleEqual(self.g.get_edge_ends(self.e1), (self.v1, self.v2))

    def test_get_vertices_count(self):
        self.assertEqual(self.g.get_vertices_count(), 0)
        self.g.add_vertex(self.v1)
        self.assertEqual(self.g.get_vertices_count(), 1)
        self.g.add_vertex(self.v2)
        self.assertEqual(self.g.get_vertices_count(), 2)

    def test_get_vertex_position(self):
        if settings.INTEGER_VERTICES:
            return
        self.g.add_vertex(self.v1)
        self.g.add_vertex(self.v2)
        self.g.add_vertex(self.v3)
        self.assertEqual(self.g.get_vertex_position(self.v1), 0)
        self.assertEqual(self.g.get_vertex_position(self.v2), 1)
        self.assertEqual(self.g.get_vertex_position(self.v3), 2)