def buildgraph(wordfile): d = {} g = Graph() wfile = open(wordfile, 'r') for line in wfile: word = line[:-1] for i in range(len(word)): bucket = word[:i] + '_' + word[i + 1:] if bucket in d: d[bucket].append(word) else: d[bucket] = [word] for bucket in d: for word1 in d[bucket]: for word2 in d[bucket]: if word1 != word2: g.addvertex(word1, word2) return g
def getRandomGraph(numVerts, numEdges): graph = Graph() #Instantiate your graph for vert_id in range(0, numVerts): graph.add_vertex(vert_id) #O(n^2) allEdges = [] for i in range(0, numVerts): for j in range(0, numVerts): if i < j: #it will only allow if the second number bigger than the first so that only one edge will be drawn between the elements 1 and 2, for example, instead of two allEdges.append((i, j)) #challenge question: max numbers of edges? n*(n-1)/2 # print(len(allEdges)) #Interview question: how would you shuffle and what is the big O of that? #O(1) random.shuffle(allEdges) #O(e) where e = # edges (and we know e < n^2) randomEdges = allEdges[:numEdges] for edge in randomEdges: graph.add_edge(edge[0], edge[1]) return graph
def build_graph(directed): g = Graph(directed) vertices = [] for val in ['a', 'b', 'c', 'd', 'e', 'f', 'g']: vertex = Vertex(val) vertices.append(vertex) g.add_vertex(vertex) for v in range(len(vertices)): v_idx = randrange(0, len(vertices) - 1) v1 = vertices[v_idx] v_idx = randrange(0, len(vertices) - 1) v2 = vertices[v_idx] g.add_edge(v1, v2, randrange(1, 10)) print_graph(g)
print("\n y value: ", y_value) graph.edge_renderer.data_source.data = dict(start=x_value, end=y_value) circ = [int(i) for i in x_value] x = [i for i in circ] y = [math.sin(i) for i in circ] # sinus shape graph_layout = dict(zip(x_value, zip(x, y))) graph.layout_provider = StaticLayoutProvider(graph_layout=graph_layout) plot.renderers.append(graph) output_file('graph.html') show(plot) graph = Graph() # Instantiate your graph graph.add_vertex('0') graph.add_vertex('1') graph.add_vertex('2') graph.add_vertex('3') graph.add_edge('0', '1') graph.add_edge('0', '3') graph.add_edge('1', '2') bg = BokehGraph(graph) bg.show() dft(graph.vertices, '0', []) bft(graph.vertices, '0') print(graph.vertices)
def getDefaultGraph(): graph = Graph() #Instantiate your graph graph.add_vertex(0) graph.add_vertex(1) graph.add_vertex(2) graph.add_vertex(3) graph.add_vertex(4) graph.add_vertex(5) graph.add_vertex(6) graph.add_vertex(7) graph.add_vertex(8) graph.add_vertex(9) graph.add_edge(0, 1) graph.add_edge(0, 3) graph.add_edge(1, 2) graph.add_edge(2, 5) graph.add_edge(2, 4) graph.add_edge(4, 9) graph.add_edge(3, 7) graph.add_edge(3, 6) graph.add_edge(7, 9) return graph
#! /usr/bin/python import sys from parser import Parser from graph1 import Graph parse = Parser() GGraph = Graph() def main(): while (True): data = parse.read_and_parse() if (data != None): if ((type(data) is str) and data == "g"): GGraph.output_graph() else: if (data[0] == "a"): GGraph.add_street(data[1], data[2]) elif (data[0] == "c"): GGraph.change_street(data[1], data[2]) elif (data[0] == "r"): GGraph.remove_street(data[1]) else: sys.stderr.write("Error: invalid command \n") if __name__ == '__main__': main()