def bipartite(V1, V2, E): ''' * Returns a random simple bipartite graph on V1 and V2 vertices * with E edges. * @param V1 the number of vertices in one partition * @param V2 the number of vertices in the other partition * @param E the number of edges * @return a random simple bipartite graph on V1 and V2 vertices, * containing a total of E edges * @raises ValueError if no such simple bipartite graph exists ''' if E > V1 * V2: raise ValueError('Too many edges') if E < 0: raise ValueError('Too few edges') # Modification question #5 if V1 < 0 or V2 < 0: raise ValueError("Vertex value cannot be less then 0") G = Graph(V1 + V2) vertices = [i for i in range(V1 + V2)] rand.shuffle(vertices) edges = [] while G.E() < E: i = rand.randrange(V1) j = V1 + rand.randrange(V2) e = (vertices[i], vertices[j]) if e not in edges: edges.append(e) G.add_edge(e) return G
def bipartite_with_probability(V1, V2, p): ''' Returns a random simple bipartite graph on V1 and V2 vertices, containing each possible edge with probability p. @param V1 the number of vertices in one partition @param V2 the number of vertices in the other partition @param p the probability that the graph contains an edge with one endpoint in either side @return a random simple bipartite graph on V1 and V2 vertices, containing each possible edge with probability p @raises ValueError if proba bility is not between 0 and 1 ''' if p < 0.0 or p > 1.0: raise ValueError('Probability must be between 0 and 1') # Modification question #5 if V1 < 0 or V2 < 0: raise ValueError("Vertex value cannot be less then 0") vertices = [i for i in range(V1 + V2)] rand.shuffle(vertices) G = Graph(V1 + V2) for i in range(V1): for j in range(V2): if utils.bernoulli(p): G.add_edge((vertices[i], vertices[V1 + j])) return G
def simple(V, E): ''' Returns a random simple graph containing V vertices and E edges. @param V the number of vertices @param E the number of edges @return a random simple graph on V vertices, containing a total of E edges @raises ValueError if no such simple graph exists ''' if E > V * (V - 1) / 2: raise ValueError("Too many edges") if E < 0: raise ValueError("Too few edges") # Modification question #5 if V <= 0: raise ValueError("A simple graph must have at least one vertex") G = Graph(V) edges = [] while G.E() < E: v = rand.randrange(V) w = rand.randrange(V) e = (v, w) if v != w and e not in edges: edges.append(e) G.add_edge(e) return G
def regular(V, k): ''' Returns a uniformly random k-regular graph on V vertices (not necessarily simple). The graph is simple with probability only about e^(-k^2/4), which is tiny when k = 14. @param V the number of vertices in the graph @param k degree of each vertex @return a uniformly random k-regular graph on V vertices. ''' if V * k % 2 != 0: raise ValueError("Number of vertices * k must be even") # Modification question #5 if V <= 0: raise ValueError("A regular graph must have at least one vertex") G = Graph(V) # create k copies of each vertex vertices = [0 for _ in range(V * k)] for v in range(V): for j in range(k): vertices[v + V * j] = v # pick a random perfect matching rand.shuffle(vertices) for i in range(V * k // 2): G.add_edge((vertices[2 * i], vertices[2 * i + 1])) return G
def regular(V, k): ''' Returns a uniformly random k-regular graph on V vertices (not necessarily simple). The graph is simple with probability only about e^(-k^2/4), which is tiny when k = 14. @param V the number of vertices in the graph @param k degree of each vertex @return a uniformly random k-regular graph on V vertices. ''' if k >= V: raise ValueError("Too many edges") if k < 0: raise ValueError("number of edges must be positive") if V < 3: raise ValueError("number of vertices must be greater or equal to 3") if V*k % 2 != 0: raise ValueError("Number of vertices * k must be even") G = Graph(V) # create k copies of each vertex vertices = [0 for _ in range(V*k)] for v in range(V): for j in range(k): vertices[v + V*j] = v # pick a random perfect matching rand.shuffle(vertices) while(regularHasSameEdges(vertices)): rand.shuffle(vertices) for i in range(V*k//2): G.add_edge((vertices[2*i], vertices[2*i + 1])) return G
def path(V): ''' Returns a path graph on V vertices. @param V the number of vertices in the path @return a path graph on V vertices ''' G = Graph(V) vertices = [i for i in range(V)] rand.shuffle(vertices) for i in range(V - 1): G.add_edge((vertices[i], vertices[i + 1])) return G
def test_graph_vertex_add(self): g = Graph() v1 = Vertex(Point(1, 2)) v2 = Vertex(Point(2, 1)) g.add_vertex(v1) g.add_vertex(v2) g.add_edge(v1, v2) g.add_edge(v2, v1) self.assertEqual(len(g.edges), 1)
def cycle(V): ''' Returns a cycle graph on V vertices. @param V the number of vertices in the cycle @return a cycle graph on V vertices ''' G = Graph(V) vertices = [i for i in range(V)] rand.shuffle(vertices) for i in range(V - 1): G.add_edge((vertices[i], vertices[i + 1])) G.add_edge((vertices[V - 1], vertices[0])) return G
def star(V): ''' Returns a star graph on V vertices. @param V the number of vertices in the star @return a star graph on V vertices: a single vertex connected to every other vertex ''' if V <= 0: raise ValueError("Number of vertices must be at least 1") G = Graph(V) vertices = [i for i in range(V)] rand.shuffle(vertices) # connect vertices[0] to every other vertex for i in range(V): G.add_edge((vertices[0], vertices[i])) return G
def filter(): ## canciones[between]=0,20&sexo[equal]=mujer filters = parse_filters(request.args.items()) gph = Graph() people, friendships = friend.filter(filters) statistics = GraphStatistics(gph, people) for k, p in people.iteritems(): gph.add_node(p, 'usuario') for f in friendships: gph.add_edge([f['usuario_1'], f['usuario_2']], songs=f['canciones']) st = statistics.compute() return jsonify({ 'statistics': st, 'nodes': gph.nodes, 'edges': gph.edges })
def eulerianCycle(V, E): ''' Returns an Eulerian cycle graph on V vertices. @param V the number of vertices in the cycle @param E the number of edges in the cycle @return a graph that is an Eulerian cycle on V vertices and E edges @raises ValueError if either V <= 0 or E <= 0 ''' if E <= 0: raise ValueError("An Eulerian cycle must have at least one edge") if V <= 0: raise ValueError("An Eulerian cycle must have at least one vertex") G = Graph(V) vertices = [rand.randrange(V) for i in range(E)] for i in range(E - 1): G.add_edge((vertices[i], vertices[i + 1])) G.add_edge((vertices[E - 1], vertices[0])) return G
def eulerianPath(V, E): ''' Returns an Eulerian path graph on V vertices. @param V the number of vertices in the path @param E the number of edges in the path @return a graph that is an Eulerian path on V vertices and E edges @raises ValueError if either V <= 0 or E < 0 ''' if E < 0: raise ValueError("negative number of edges") if V <= 0: raise ValueError("An Eulerian path must have at least one vertex") G = Graph(V) vertices = [] for i in range(E + 1): vertices[i] = rand.randrange(V) for i in range(E): G.add_edge((vertices[i], vertices[i + 1])) return G
def simple_with_probability(V, p): ''' Returns a random simple graph on V vertices, with an edge between any two vertices with probability p. This is sometimes referred to as the Erdos-Renyi random graph model. @param V the number of vertices @param p the probability of choosing an edge @return a random simple graph on V vertices, with an edge between any two vertices with probability p @raises ValueError if probability is not between 0 and 1 ''' if p < 0.0 or p > 1.0: raise ValueError('Probability must be between 0 and 1') G = Graph(V) for v in range(V): for w in range(v+1,V,1): if utils.bernoulli(p): G.add_edge((v , w)) return G
def wheel(V): ''' Returns a wheel graph on V vertices. @param V the number of vertices in the wheel @return a wheel graph on V vertices: a single vertex connected to every vertex in a cycle on V-1 vertices ''' if V <= 1: raise ValueError("Number of vertices must be at least 2") G = Graph(V) vertices = [i for i in range(V)] rand.shuffle(vertices) # simple cycle on V-1 vertices for i in range(V - 1): G.add_edge((vertices[i], vertices[i + 1])) G.add_edge((vertices[V - 1], vertices[1])) # connect vertices[0] to every vertex on cycle for i in range(V): G.add_edge((vertices[0], vertices[i])) return G
def test_stripe(self): p1 = Vertex(Point(7, 0)) p2 = Vertex(Point(2, 2.5)) p3 = Vertex(Point(12, 3)) p4 = Vertex(Point(8, 5)) p5 = Vertex(Point(0, 7)) p6 = Vertex(Point(13, 8)) p7 = Vertex(Point(6, 11)) g = Graph() g.add_vertex(p1) g.add_vertex(p2) g.add_vertex(p3) g.add_vertex(p4) g.add_vertex(p5) g.add_vertex(p6) g.add_vertex(p7) g.add_edge(p1, p2) g.add_edge(p1, p3) g.add_edge(p2, p3) g.add_edge(p7, p6) g.add_edge(p3, p6) g.add_edge(p4, p6) g.add_edge(p4, p5) g.add_edge(p4, p7) g.add_edge(p5, p7) g.add_edge(p2, p5) dot = Point(11.5, 5.5) ans = list(stripe(g, dot)) self.assertEqual( ans[0], [ (-math.inf, 0.0), (0.0, 2.5), (2.5, 3.0), (3.0, 5.0), (5.0, 7.0), (7.0, 8.0), (8.0, 11.0), (11.0, math.inf), ], ) self.assertTrue( TestAlgorithms.fragmentation_eq( ans[1], { (-math.inf, 0.0): [], (0.0, 2.5): [Edge(p1, p2), Edge(p1, p3)], (2.5, 3.0): [Edge(p1, p3), Edge(p2, p3), Edge(p2, p5)], (3.0, 5.0): [Edge(p2, p5), Edge(p3, p6)], (5.0, 7.0): [ Edge(p2, p5), Edge(p4, p5), Edge(p4, p7), Edge(p4, p6), Edge(p3, p6), ], (7.0, 8.0): [ Edge(p5, p7), Edge(p4, p7), Edge(p4, p6), Edge(p3, p6), ], (8.0, 11.0): [Edge(p5, p7), Edge(p4, p7), Edge(p7, p6)], (11.0, math.inf): [], }, )) self.assertEqual(ans[2], (5.0, 7.0)) self.assertEqual(ans[3], [Edge(p4, p6), Edge(p3, p6)])