def __init__(self, size=5, *args, **kwargs): self.size = size super().__init__(*args, **kwargs) # create a linear topology g = graph_tools.Graph(directed=False) self.graph = g g.create_graph('lattice', 1, self.size) # save the positions of vertices as Vector object for v in range(1, self.size + 1): x, y = ((v - 0.5) / self.size * self.width, 0.5 * self.height) g.set_vertex_attribute(v, 'xy', V(x, y)) self.compute_edge_lengths()
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # create a grid topology g = graph_tools.Graph(directed=False) self.graph = g g.create_graph('lattice', 2, self.size) # save the positions of vertices as Vector object for j in range(1, self.size + 1): for i in range(1, self.size + 1): v = g._lattice_vertex(2, self.size, i, j) x, y = ((0.5 + i - 1) / self.size * self.width, (0.5 + j - 1) / self.size * self.height) g.set_vertex_attribute(v, 'xy', V(x, y)) self.compute_edge_lengths()
def __init__(self, npoints=100, *args, **kwargs): self.npoints = npoints super().__init__(*args, **kwargs) # create a Voronoi topology g = graph_tools.Graph(directed=False) self.graph = g g.create_graph('voronoi', self.npoints, self.width, self.height) # save the positions of vertices as Vector object for v in g.vertices(): x, y = g.get_vertex_attribute(v, 'pos').split(',') x, y = float(x), float(y) g.set_vertex_attribute(v, 'xy', V(x, y)) self.compute_edge_lengths()
import graph_tools # create a graph with four nodes and two edges g = graph_tools.Graph(directed=True) g.add_edge(1, 2) g.add_edge(2, 3) g.add_vertex(4) print(g) # find the all shortest paths from vertex 1 dist, prev = g.dijkstra(1) print(dist) # generate BA graph with 100 vertices g = graph_tools.Graph(directed=False).create_graph('barabasi', 100) # check if all vertices are mutually connected print(g.is_connected()) # compute the betweenness centrality of vertex 1 print(g.betweenness(1))
#!/usr/bin/env python3 import random import graph_tools N = 100 E = 200 G = graph_tools.Graph().create_random_graph(N, E) for v in G.vertices(): print('define v{} ellipse 3 3 yellow'.format(v)) for u, v in G.edges(): print('define - link v{} v{} 2 blue'.format(u, v)) print('spring /./') for p in range(N): n = 1 + random.randrange(N) c = 'heat{}'.format(random.randrange(100 + 1)) print('define p{} box 10 10 {} v{}'.format(p, c, n)) print('display') for _ in range(100): for p in range(N): n = 1 + random.randrange(N) print('animate p{} v{}'.format(p, n)) print('display')