コード例 #1
0
    def test_add_vertex(self):
        """Tests the add_vertex method with an empty graph.
        """
        graph = Graph()

        self.assertFalse(graph.contains_vertex('A'), 'The graph does not contain A yet.')
        graph.add_vertex('A')
        self.assertTrue(graph.contains_vertex('A'), 'The graph now contains A.')
コード例 #2
0
    def test_add_vertex_existing_one(self):
        """Tests the add_vertex method in a graph where the vertex to add already exists.
        """
        data = { 'A' : ['B'] }
        graph = Graph(graph=data)
        connected = graph.connected_vertex('A')

        self.assertTrue(graph.contains_vertex('A'), 'Graph contains the vertex A.')
        self.assertEqual(connected[0], 'B', 'A has an edge to vertex B.')

        graph.add_vertex('A')
        connected = graph.connected_vertex('A')

        self.assertTrue(graph.contains_vertex('A'), 'Graph still contains vertex A.')
        self.assertEqual(connected[0], 'B', 'A still has an edge to vertex B.')
コード例 #3
0
ファイル: test_graph.py プロジェクト: nezaj/Reference
    def test_is_connected(self):
        # Returns False for an empty graph
        v, w = self.v, self.w
        g = Graph([v, w], [])
        eq_(g.is_connected(), False)

        # Returns True for a complete graph
        g.add_all_edges()
        eq_(g.is_connected(), True)

        # Returns False when a new vertex is added
        x = Vertex('x')
        g.add_vertex(x)
        eq_(g.is_connected(), False)

        # Returns True when edge is added to connect it
        e2 = Edge(w, x)
        g.add_edge(e2)
        eq_(g.is_connected(), True)
コード例 #4
0
def extract_connected_component(graph, needle):
    subgraph = Graph([needle])
    queue = [needle]

    step = 0
    while queue:
        vertex = queue.pop(0)
        step += 1
        if step % 10000 == 0:
            print(len(queue), repr(queue[0:min(5, len(queue))]))
            step = 0
        for (pred, obj) in graph.edges[vertex]:
            # examine all neighbours of vertex
            if not obj in subgraph.vertices:
                # we haven't visited this vertex before
                subgraph.add_vertex(obj)
                subgraph.add_edge(vertex, obj, label=pred)
                # we also needle to consider all new neighbours of `obj`
                queue.append(obj)
            elif (pred, obj) not in subgraph.edges[vertex]:
                # we already have `obj`, but not this edge
                subgraph.add_edge(vertex, obj, label=pred)

    return subgraph
コード例 #5
0
print("The maximum degree of the graph is:")
print(graph.Delta())

print("The minimum degree of the graph is:")
print(graph.delta())

print("Edges:")
print(graph.edges())

print("Degree Sequence: ")
ds = graph.degree_sequence()
print(ds)

fullfilling = [[2, 2, 2, 2, 1, 1], [3, 3, 3, 3, 3, 3], [3, 3, 2, 1, 1]]
non_fullfilling = [[4, 3, 2, 2, 2, 1, 1], [6, 6, 5, 4, 4, 2, 1], [3, 3, 3, 1]]

for sequence in fullfilling + non_fullfilling:
    print(sequence, Graph.erdoes_gallai(sequence))

print("Add vertex 'z':")
graph.add_vertex("z")
print(graph)

print("Add edge ('x','y'): ")
graph.add_edge(('x', 'y'))
print(graph)

print("Add edge ('a','d'): ")
graph.add_edge(('a', 'd'))
print(graph)
コード例 #6
0
from graphs import Graph

# Instantiate graphs (default not directed)
graph1 = Graph()
directed_graph1 = Graph(True)

# Instantiate vertices
vertex1 = Vertex("Value vertex 1")
vertex2 = Vertex("Value vertex 2")
vertex3 = Vertex("Value vertex 3")
vertex4 = Vertex("Value vertex 4")
vertex5 = Vertex("Value vertex 5")
vertex6 = Vertex("Value vertex 6")

# Adding vertex to graph1
graph1.add_vertex(vertex1)
graph1.add_vertex(vertex2)
graph1.add_vertex(vertex5)
graph1.add_vertex(vertex6)

# Adding vertex to directed_graph1
directed_graph1.add_vertex(vertex3)
directed_graph1.add_vertex(vertex4)

# Adding edges in graphs
graph1.add_edges(vertex1, vertex2, 5)
graph1.add_edges(vertex1, vertex5, 10)
graph1.add_edges(vertex1, vertex6, 15)
directed_graph1.add_edges(vertex3, vertex4)

print("\nVertices in non-directed Graph")
コード例 #7
0
ファイル: adjacency_list.py プロジェクト: bmuha1/python_dsa
#!/usr/bin/python3
# Implement adjacency list
from graphs import Vertex, Graph

if __name__ == '__main__':
    g = Graph()
    for i in range(6):
        g.add_vertex(i)

    g.add_edge(0, 1, 5)
    g.add_edge(0, 5, 2)
    g.add_edge(1, 2, 4)
    g.add_edge(2, 3, 9)
    g.add_edge(3, 4, 7)
    g.add_edge(3, 5, 3)
    g.add_edge(4, 0, 1)
    g.add_edge(5, 4, 8)
    g.add_edge(5, 2, 1)

    for v in g:
        for w in v.get_connections():
            print('({}, {})'.format(v.get_id(), w.get_id()))