# -*- coding: utf-8 -*- import networkx as nx from functions import * from algorithms import prims_algorithm from drawing import * graph_data = open('test-graph/G2.txt', 'r') G = nx.read_weighted_edgelist(graph_data, nodetype=int) T = prims_algorithm(G, 3, show_graph=True, show_cost=True)
'test-graphs/G2.txt' (file name): The name of the folder/file that will be read graph_data (file): The txt file that will be used to create graph nodetype (bool): Used to set the nodetype to int G (Graph): The graph of the spanning tree show_graph (bool): A flag used to print MST plot show_cost (bool): A flag used to print cost of MST return: plot: a drawing of the MST if show_graph = True float: total cost of the MST if show_cost = True """ import networkx as nx from functions import * from algorithms import prims_algorithm from drawing import * #Opens the file named G3.txt in read mode graph_data = open('test-graphs/G3.txt', 'r') #Reads the txt and creates a weighted graph, G G = nx.read_weighted_edgelist(graph_data, nodetype=int) #Calls the prims_algorithm function and prompts the user to enter a starting #node T = prims_algorithm(G, int(input('Enter a node to begin with: ')), show_graph=True, show_cost=True)
int(random.random() * HEIGHT))) points.append((int(random.random() * WIDTH), int(random.random() * HEIGHT))) points.append((int(random.random() * WIDTH), int(random.random() * HEIGHT))) points.append((int(random.random() * WIDTH), int(random.random() * HEIGHT))) points.append((int(random.random() * WIDTH), int(random.random() * HEIGHT))) points.append((int(random.random() * WIDTH), int(random.random() * HEIGHT))) elif event.key == pg.K_q: lines = [] connected_graph = algorithms.prims_algorithm(points) for point in connected_graph[1:]: p0 = int(point.x), int(point.y) p1 = int(point.nearest.x), int(point.nearest.y) lines.append((p0, p1)) elif event.key == pg.K_w: points = [] screen.fill((255, 255, 255)) for point in points: pg.draw.circle(screen, (255, 0, 0), point, 5, 0) for p0, p1 in lines: pg.draw.line(screen, (0, 128, 128), p0, p1, 2)
def check_power_up_collisions(self): collided_powerups = pg.sprite.groupcollide( self.player, self.power_up_sprites, False, True, collided=pg.sprite.collide_circle) for value in collided_powerups.values(): # All the code for the powerups is done right here instead of in the powerup # mainly because the powerups have effects which need data from the game. for power_up in value: if power_up.effect == "HealthUp": self.player_sprite.health += 1 elif power_up.effect == "Impervious": self.player_sprite.impervious = 1 self.player_sprite.invunticks = 300 elif power_up.effect == "Score": self.score += 10 elif power_up.effect == "Circle": # Summon a circle of missiles around the player. x, y = self.player_sprite.rect.center missile_locations = algorithms.circle(x, y, 30) missile_velocities = algorithms.circle(0, 0, 30) angle_missiles = math.pi * 2 / len(missile_velocities) degrees_90 = math.pi / 2 new_missiles = [] for i, loc, vel in zip(range(len(missile_velocities)), missile_locations, missile_velocities): x, y = loc vx, vy = vel # By dividing by 3 we give them 10 velocity. # Also the angles are 90 degrees out of phase and backwards because of reasons new_missiles.append( Missile(x, y, -angle_missiles * i - degrees_90, vx / 3, vy / 3)) self.missile_sprites.add(new_missiles) elif power_up.effect == "Span": # Rather complicated, Create a spanning tree over the map which shoots every asteroid at once. points_to_hit = [] new_shards = [] for asteroid in self.asteroid_sprites: points_to_hit.append(asteroid.rect.center) if asteroid.get_hit(): self.score += 1 new_shards.extend(asteroid.split()) self.asteroid_sprites.add(new_shards) points_to_hit.append(power_up.rect.center) spanning_tree = algorithms.prims_algorithm(points_to_hit) lines = [] for point in spanning_tree[1:]: lines.append( Line(point.x, point.y, point.nearest.x, point.nearest.y)) self.line_sprites.add(lines)
def main(): # Declare sentinel values choice = -1 graph_file = 'test-graphs/filename' # Declare default option values opt_plot = True opt_cost = True vertex = 1 found_file = False # Loop only on option/file choices and input error. Terminate on run and quit. while not choice == 6 and not choice == 7: if found_file: print() print("Currently selected graph file: ") print(graph_file) print() show_menu(opt_plot, opt_cost, vertex, graph_file) choice = try_menu_input("Input 1 through 7: ") # Select file from list if choice == 1: filechoice = -1 print("Choose a file: ") filelist = get_files_in_test_path() show_filelist(filelist) # Loop only when input is invalid while filechoice < 1 or filechoice > len(filelist): filechoice = try_menu_input("Input 1 through " + str(len(filelist)) + ": ") # If invalid input if filechoice < 1 or filechoice > len(filelist): print("Choice must be between 1 through " + str(len(filelist)) + ". Please try again. ") print() show_filelist(filelist) graph_file = 'test-graphs/' + filelist[filechoice - 1] found_file = True # Request a full file path input if choice == 2: graph_file = input("Input full C:/path/to/file\n") found_file = path.exists(graph_file) if not found_file: print("Error, file not found.") print() # Toggle show plot if choice == 3: opt_plot = not opt_plot # Toggle show cost if choice == 4: opt_cost = not opt_cost # Update starting vertex if choice == 5: vertex = try_menu_input("Input starting vertex: ") # The try_menu_input function defaults to -1 for an invalid input. # Instead, set vertex back to the default. if vertex == -1: vertex = 1 # Invalid input if choice < 0 or choice > 7: print("Input must be 1 through 7. ") # If not exiting the program if not choice == 7: # Open input file graph_data = open(graph_file, 'r') # Read graph data G = nx.read_weighted_edgelist(graph_data, nodetype=int) # Close input file graph_data.close() # Perform Prim's on graph with selected options T = prims_algorithm(G, vertex, show_graph=opt_plot, show_cost=opt_cost) # Uses regex to find just the filename without extension of the input file. graph_file = re.search("[ \w-]+?(?=\.)", graph_file).group() # Generate string for outfile path outfilename = 'test-graphs/trees/' + graph_file + 'tree.txt' print("Save output tree to " + outfilename) if path.exists(outfilename): print("WARNING: output file exsists and will be overwritten.") choice = input("Input yes/no : ") if choice[0].lower() == 'y' or choice[0] == '1': outfile = open(outfilename, 'w') outdata = generate_output_tree(T) outfile.write(outdata) outfile.close()