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)
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
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)
from bokeh.plotting import figure from bokeh.models import GraphRenderer, StaticLayoutProvider, Circle, ColumnDataSource, Label, LabelSet from bokeh.palettes import Spectral8 from graph1 import Graph 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_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('3', '7') graph.add_edge('3', '6') print(graph.vertices) # 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')