def set_graph(self, withWeights=False): print "Please enter the number of vertices:\n" num_vertices = raw_input(">") print "Please enter the number of edges:\n" num_edges = raw_input(">") vertices = [] edges = [] for i in range(int(num_vertices)): print "Please enter the name of the vertex: ", i vertex_name = raw_input(">") vertices.append(Vertex(vertex_name)) for j in range(int(num_edges)): print "Please enter the name of the first vertex of edge\n" first_vertex = raw_input(">") print "Please enter the name of the second vertex of edge\n" second_vertex = raw_input(">") if withWeights: print "Please enter edge weight\n" edge_weight = raw_input(">") edges.append( Edge(Vertex(first_vertex), Vertex(second_vertex), edge_weight)) else: edges.append(Edge(Vertex(first_vertex), Vertex(second_vertex))) return Graph(vertices=vertices, edges=edges)
def handle_shortest_path(self, g): print "Enter the start vertex" start_vertex = raw_input(">") print "Enter the end vertex" end_vertex = raw_input(">") print "Your answer is ", g.find_shortest_path(Vertex(start_vertex), Vertex(end_vertex))
def test_contains_cycle(self): graph = self.createSampleGraph() path = [] a_vertex = Vertex('A') c_vertex = Vertex('C') f_vertex = Vertex('F') path.append(Edge(a_vertex, c_vertex, 1)) path.append(Edge(c_vertex, f_vertex, 2)) self.assertFalse( graph.doesNotCreateCycle(path, Edge(f_vertex, c_vertex, 2)))
def test_get_visited_nodes_from_edge(self): graph = self.create_undirected_graph() path = [ Edge(Vertex('A'), Vertex('C')), Edge(Vertex('B'), Vertex('C')), Edge(Vertex('C'), Vertex('A')) ] visited_nodes = graph.get_visited_nodes_from_edges(path) correct_nodes = set([Vertex('A'), Vertex('B'), Vertex('C')]) self.assertEqual(visited_nodes, correct_nodes)
def test_dfs(self): graph = self.createSampleGraph() self.assertEquals([ Vertex('A'), Vertex('C'), Vertex('F'), Vertex('E'), Vertex('B'), Vertex('D') ], graph.dfs(Vertex('A')))
def createSampleGraph(self): vertices = [] a_vertex = Vertex('A') b_vertex = Vertex('B') c_vertex = Vertex('C') d_vertex = Vertex('D') e_vertex = Vertex('E') f_vertex = Vertex('F') vertices.append(a_vertex) vertices.append(b_vertex) vertices.append(c_vertex) vertices.append(d_vertex) vertices.append(e_vertex) vertices.append(f_vertex) edges = [] edges.append(Edge(a_vertex, c_vertex, 1)) edges.append(Edge(a_vertex, b_vertex, 3)) edges.append(Edge(b_vertex, d_vertex, 2)) edges.append(Edge(b_vertex, e_vertex, 2)) edges.append(Edge(c_vertex, f_vertex, 2)) edges.append(Edge(c_vertex, a_vertex, 2)) edges.append(Edge(d_vertex, b_vertex, 2)) edges.append(Edge(e_vertex, f_vertex, 2)) edges.append(Edge(e_vertex, b_vertex, 2)) edges.append(Edge(f_vertex, c_vertex, 2)) edges.append(Edge(f_vertex, e_vertex, 2)) graph = Graph(vertices, edges) return graph
def test_prims_algo(self): graph = self.create_undirected_graph() a_vertex = Vertex('A') b_vertex = Vertex('B') c_vertex = Vertex('C') d_vertex = Vertex('D') e_vertex = Vertex('E') f_vertex = Vertex('F') expected_path = graph.prims_algorithm(Vertex('A')) correct_path = [ Edge(a_vertex, c_vertex, 1), Edge(c_vertex, f_vertex, 5), Edge(f_vertex, e_vertex, 2), Edge(e_vertex, b_vertex, 1), Edge(b_vertex, d_vertex, 4) ] self.assertEqual(expected_path, correct_path)
def handle_bfs(self, g): print "Please enter what vertex you want to start from:\n" start = raw_input(">") print "Your answer is ", g.bfs(Vertex(start))
def handle_prims(self, g): print "Please enter what vertex you want to start from:\n" start = raw_input(">") print "Your path is ", g.prims_algorithm(Vertex(start))
numberOfVertices = int(input("How many Vertices: ")) # we loop to add each vertex into a list to to insert the vertices in the alphabetical order verticesList = [] i = 0 while i < numberOfVertices: verLabel = input("Enter vertex number %d: " % (i + 1)) if verLabel not in verticesList: verticesList.append(verLabel) i += 1 else: print("This vertex is already inserted") verticesList.sort() # to sort the list # adding all vertices into the graph for i in verticesList: graph.addVertex(Vertex(i)) print() print("All vertices added successfully...") # now we should add the edges print() numberOfEdges = int(input("How many Edges: ")) for i in range(0, numberOfEdges): print() addV = True while addV: ver1 = input("Enter Source label: ") if ver1 not in verticesList: print("This character is not in the graph") else: addV = False
def test_shortest_path(self): graph = self.create_undirected_graph() expected_path = graph.find_shortest_path(Vertex('A'), Vertex('E')) correct_path = [Vertex('A'), Vertex('B'), Vertex('E')] self.assertEqual(expected_path, correct_path)
def test_get_lowest_weight_edge(self): graph = self.createSampleGraph() path = [] edge = graph.getLowestWeightEdge(Vertex('A'), path) self.assertEqual(Edge(Vertex('A'), Vertex('C'), 1), edge)
def test_edge_equality(self): edge1 = Edge(Vertex('A'), Vertex('B'), 2) edge2 = Edge(Vertex('A'), Vertex('B'), 2) self.assertEqual(edge1, edge2)
def test_negative_kruskals_algo(self): vertices = [Vertex('A'), Vertex('B'), Vertex('C')] edges = [ Edge(Vertex('A'), Vertex('B'), 2), Edge(Vertex('B'), Vertex('C'), 1), Edge(Vertex('A'), Vertex('C'), 10) ] graph = Graph(vertices, edges) correct_path = graph.kruskals_algorithm() incorrect_path = [ Edge(Vertex('B'), Vertex('C'), 1), Edge(Vertex('A'), Vertex('B'), 10) ] self.assertNotEqual(correct_path, incorrect_path)
def test_positive_kruskals_algo(self): vertices = [Vertex('A'), Vertex('B'), Vertex('C')] edges = [ Edge(Vertex('A'), Vertex('B'), 2), Edge(Vertex('B'), Vertex('C'), 1), Edge(Vertex('A'), Vertex('C'), 10) ] graph = Graph(vertices, edges) expected_path = graph.kruskals_algorithm() correct_path = [ Edge(Vertex('B'), Vertex('C'), 1), Edge(Vertex('A'), Vertex('B'), 2) ] self.assertEqual(expected_path, correct_path)