def createRandomGraph(numNodes, numEdges): print(f"numNodes: {numNodes}") graph = Graph() # Instantiate your graph all_edges = [] for i in range(numNodes): for j in range(i + 1, numNodes): all_edges.append((i, j)) random.shuffle(all_edges) edges = all_edges[:numEdges] if numEdges > len(all_edges): print("Too many edges") for edge in edges: print(f"edge: {edge}") for i in range(numNodes): graph.add_vertex(i) for edge in edges: graph.add_edge(edge[0], edge[1]) print(f"length: {len(all_edges)}") bg = BokehGraph(graph) bg.draw()
def createDefaultGraph(): 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_edge('0', '1') # graph.add_edge('2', '3') # graph.add_edge('2', '4') # graph.add_edge('3', '5') # graph.add_edge('5', '6') graph.add_vertex(5) graph.add_vertex(2) graph.add_vertex(6) graph.add_vertex(1) graph.add_vertex(4) graph.add_vertex(7) graph.add_vertex(3) graph.add_edge(5, 2) graph.add_edge(5, 6) graph.add_edge(2, 1) graph.add_edge(2, 4) graph.add_edge(4, 3) graph.add_edge(6, 7) print(graph.dfs_path(5, 7)) bg = BokehGraph(graph) # print(graph.bfs(5)) bg.draw()
def main(): 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) bokeh_graph = BokehGraph(graph) bokeh_graph.draw()
def createRandomGraph(numNodes, numEdges): graph = Graph() all_edges = [] for i in range(numNodes): for j in range(i + 1, numNodes): all_edges.append((i, j)) random.shuffle(all_edges) edges = all_edges[:numEdges] if numEdges > len(all_edges): print("Too many edges") for edge in edges: print(edge) for i in range(numNodes): graph.add_vertex(i) for edge in edges: graph.add_edge(edge[0], edge[1]) print(len(edges)) print(graph.vertices) bg = BokehGraph(graph) bg.graph.depth_first_search(3) bg.graph.breadth_first_search(3) bg.draw()
def createRandomGraph(numNodes, numEdges): graph = Graph() # Instantiate your graph all_edges = [] # O(n^2) for i in range(numNodes): for j in range(i + 1, numNodes): all_edges.append((i, j)) # O(n) random.shuffle(all_edges) edges = all_edges[:numEdges] # O(1) if numEdges > len(all_edges): print("Warning: Too many edges") # O(n) for i in range(numNodes): graph.add_vertex(i) # O(n) for edge in edges: graph.add_edge(edge[0], edge[1]) bg = BokehGraph(graph) bg.draw()
def createRandomGraph(numNodes, numEdges): graph = Graph() #instantiate a graph all_edges = [] # Big O(n^2) for i in range(numNodes): for j in range(i + 1, numNodes): all_edges.append((i, j)) # O(n) random.shuffle(all_edges) edges = all_edges[:numEdges] if numEdges > len(all_edges): print("Warning: Too many edges") #O(n) for edge in edges: print(edge) # naive_edges = [] # for i in range(numEdges): # naive_edges.append((random.randint(0, numNodes-1), random.randint(0, numNodes-1))) #O(n) for i in range(numNodes): graph.add_vertex(i) #O(n) for edge in edges: graph.add_edge(edge[0], edge[1]) bg = BokehGraph(graph) bg.draw()
def create_random_graph(num_nodes, num_edges): graph = Graph() edges = [] # dont want i to point to ever itself, thus i + 1. Big O of n^2 aka (O(n*(n-1) for i in range(num_nodes): for j in range(i + 1, num_nodes): edges.append((i, j)) # O(n) random.shuffle(edges) if num_edges > len(edges): print("WARNING: TOO MANY EDGES!") edges = edges[:num_edges] for i in range(num_nodes): graph.add_vertex(i) for edge in edges: print("RANDOM GRAPH EDGES:") print(edge) graph.add_edge(edge[0], edge[1]) print(len(edges)) bg = BokehGraph(graph) bg.draw()
def createRandomGraph(num_nodes, num_edges): graph = Graph() all_edges = [] for i in range(num_nodes): for j in range(i + 1, num_nodes): all_edges.append((i, j)) random.shuffle(all_edges) edges = all_edges[:num_edges] if num_edges > len(all_edges): print('Warning: max edges reached') # for edge in edges: # print(edge) for i in range(num_nodes): graph.add_vertex(i) for edge in edges: # print(edge) graph.add_edge(edge[0], edge[1]) # print(len(edges)) print(graph.vertices) bg = BokehGraph(graph) # bg.graph.dft_st(random.randint(0, num_nodes -1)) # bg.graph.bft(random.randint(0, num_nodes -1), 'green') bg.graph.connected() bg.draw()
def main(**kwargs): if style == 'default': graph = getDefaultGraph() elif style == 'random': graph = getRandomGraph() else: graph = getDefaultGraph() bokeh_graph = BokehGraph(graph) bokeh_graph.draw()
def createDefaultGraph(): graph = Graph() # Instantiate your graph for i in range(5): graph.add_vertex(str(i)) graph.add_edge('0', '1') graph.add_edge('0', '3') graph.add_edge('2', '4') bg = BokehGraph(graph) bg.draw()
def createDefaultGraph(): 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') bg = BokehGraph(graph) bg.draw()
def main(num_vertices, num_edges, draw_components=True): graph = Graph() for num in range(num_vertices): graph.add_vertex(num) for _ in range(num_edges): vertex1 = sample(graph.vertices.keys(), 1)[0] vertex2 = sample(graph.vertices.keys(), 1)[0] graph.add_edge(vertex1, vertex2) bokeh_graph = BokehGraph(graph, draw_components=draw_components) bokeh_graph.draw()
def createDefaultGraph(): graph = 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', '2') graph.add_edge('1', '3') bg = BokehGraph(graph) bg.draw()
def main(**kwargs): style = kwargs["style"] verts = kwargs["verts"] edges = kwargs["edges"] if style == "random": graph = randomGraph(verts, edges) else: graph = regularGraph() bokeh = BokehGraph(graph) bokeh.draw()
def main(**kwargs): style = kwargs["style"] numVerts = kwargs["num_verts"] numEdges = kwargs["num_edges"] if style == "default": graph = getDefaultGraph() elif style == "random": graph = getRandomGraph(numVerts, numEdges) else: graph = getDefaultGraph() bokeh_graph = BokehGraph(graph) bokeh_graph.draw()
def main(**kwargs): # one star give a list two give a dictionary print(kwargs) style = kwargs["style"] numVerts = kwargs["num_verts"] numEdges = kwargs["num_edges"] if style == "default": graph = getDefaultGraph() elif style == "random": graph = getRandomGraph(numVerts, numEdges) else: graph == getDefaultGraph() bokeh_graph = BokehGraph(graph) bokeh_graph.draw()
def getGraph(**kwargs): 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_edge(0, 1) graph.add_edge(1, 2) graph.add_edge(2, 3) graph.add_edge(3, 4) graph.add_edge(4, 5) graph.add_edge(5, 6) bokeh_graph = BokehGraph(graph) bokeh_graph.draw()
def getRandomGraph(numVerts, numEdges): graph = Graph() for vert_id in range(0, numVerts): graph.add_vertex(vert_id) allEdges = [] for i in range(0, numVerts): for j in range(0, numVerts): if i < j: allEdges.append((i, j)) random.shuffle(allEdges) randomEdges = allEdges[:numEdges] for edge in randomEdges: graph.add_edge(edge[0], edge[1]) bokeh_graph = BokehGraph(graph) bokeh_graph.draw()
def createRandomGraph(numNodes, numEdges): graph = Graph() all_edges = [] for i in range(numNodes): for j in range(i + 1, numNodes): all_edges.append((i, j)) random.shuffle(all_edges(i)) edges = all_edges[:numEdges] if numEdges > len(all_edges): print("Warning: Too many edges") # for edge in edges: # print(edge) # Add appropriate number of vertices for num in range(numNodes): # for num in range(num_vertices): # graph.add_vertex(Vertex(label=str(num))) graph.add_vertex(num) # Add random edges between vertices #for _ in range(num_edges): for edge in edges: # print(edge) graph.add_edge(edge[0], edge[1]) # vertices = sample(graph.vertices.keys(), 2) # TODO check if edge already exists # rand_vert = int(random.random() * num_vertices) # rand_edge = int(random.random() * num_edges) # print(rand_vert, rand_edge) # graph.add_edge(vertices[0], vertices[1]) # bokeh_graph = BokehGraph(graph, draw_components=draw_components) # bokeh_graph._get_connected_component_colors() # bokeh_graph.show() bg = BokehGraph(graph) bg.draw()
def createRandomGraph(numNodes, numEdges): graph = Graph() all_edges = [] for i in range(numNodes): for j in range(i+1, numNodes): all_edges.append((i, j)) random.shuffle(all_edges) edges = all_edges[:numEdges] if numEdges > len(all_edges): print('Warning: too many edges') for i in range(numNodes): graph.add_vertex(i) for edge in edges: graph.add_edge(edge[0], edge[1]) bg = BokehGraph(graph) bg.draw()
def createRandomGraph(numNodes): graph = Graph() edges = [] for i in range(numNodes): for j in range(i + 1, numNodes): edges.append((i , j)) random.shuffle(edges) edges=[edge for index, edge in enumerate(edges) if 1 < index < random.randrange(2, len(edges))] for i in range(numNodes): graph.add_vertex(i) for edge in edges: graph.add_edge(edge[0], edge[1]) bg = BokehGraph(graph) bg.draw()
def createDefaultGraph(): graph = Graph() # Instantiate your graph graph.add_vertex(5) graph.add_vertex(2) graph.add_vertex(6) graph.add_vertex(1) graph.add_vertex(4) graph.add_vertex(7) graph.add_vertex(3) graph.add_edge(5, 2) graph.add_edge(5, 6) graph.add_edge(2, 1) graph.add_edge(2, 4) graph.add_edge(4, 3) graph.add_edge(6, 7) print(graph.dfs_path(5, 6)) bg = BokehGraph(graph) bg.draw()
def createRandomGraph(numNodes, numEdges): graph = Graph() # Instantiate your graph all_edges = [] # O(n^2) for i in range(numNodes): for j in range(i + 1, numNodes): all_edges.append((i, j)) # O(n) random.shuffle(all_edges) edges = all_edges[:numEdges] # O(1) if numEdges > len(all_edges): print("Warning: Too many edges") # O(n) for edge in edges: print(edge) # O(n) for i in range(numNodes): graph.add_vertex(i) # O(n) for edge in edges: print(edge) graph.add_edge(edge[0], edge[1]) print(len(edges)) def middle_node(): return graph.vertices[int(numNodes / 2)].id bg = BokehGraph(graph) bg.draw() print(graph.bft(middle_node()))
def create_default_graph(): graph = Graph() # Instantiate your graph graph.add_vertex('5') graph.add_vertex('2') graph.add_vertex('6') graph.add_vertex('1') graph.add_vertex('4') graph.add_vertex('7') graph.add_vertex('3') graph.add_vertex('99') graph.add_edge('5', '2') graph.add_edge('5', '6') graph.add_edge('2', '1') graph.add_edge('2', '4') graph.add_edge('4', '3') graph.add_edge('6', '7') graph.bft('5') bg = BokehGraph(graph) bg.draw()
def main(vertices, edges): graph = 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_edge(0, 1) graph.add_edge(0, 3) graph.add_edge(2, 3) graph.add_edge(3, 4) graph.add_edge(3, 5) graph.add_edge(6, 8) bokeh_graph = BokehGraph(graph) bokeh_graph.draw()
def createRandomGraph(numnodes, num): graph = Graph() all_edges = [] # O(n^2) for i in range(numNodes): for j in range(i + 1, numNodes): """ Append every subsequent pair of nodes into all_edges as a tuple making an edge for each new node thus making a dense graph of maximal edges predicted by n(n-1)/2 """ all_edges.append((i, j)) # randomnize the edges # O(n) random.shuffle(all_edges) # edges is the array of all edges (which are tuples) # that do not exceeed the specifiied user input edges = all_edges[:numEdges] # O(1) if numEdges > len(all_edges): print("Too many edges") # O(n) for edge in edges: print(edge) # O(n) for i in range(numNodes): graph.add_vertex(i) # O(n) for edge in edges: print(edge) graph.add_edge(edge[0], edge[1]) print(len(edges)) bg = BokehGraph(graph) bg.draw()
def main(num_vertices=None, num_edges=None, connected=None): if num_vertices is None: num_vertices = random.randint(1, 20) else: num_vertices = int(num_vertices) if num_edges is None: num_edges = num_vertices elif int(num_edges) > num_vertices * (num_vertices - 1) / 2: num_edges = num_vertices * (num_vertices - 1) // 2 else: num_edges = int(num_edges) if connected == 'yes': connected = True else: connected = False all_edges = [] for i in range(num_vertices): for j in range(i + 1, num_vertices): all_edges.append((i, j)) random.shuffle(all_edges) edges = all_edges[:num_edges] graph = Graph() for num in range(num_vertices): graph.add_vertex(num) for edge in edges: graph.add_edge(edge[0], edge[1]) connected_components = graph.connected_components() bg = BokehGraph(graph, connected_components, connected) bg.draw()