def set_cost(self, vertex1, vertex2, cost) : if vertex1 not in self.__neighbours or vertex2 not in self.__neighbours : raise exceptionGraph("The vertices are invalid") if vertex2 not in self.__neighbours[vertex1] : raise exceptionGraph("The edge doesn't exist") if (vertex1, vertex2) in self.__dictionaryCosts.keys() : self.__dictionaryCosts[(vertex1, vertex2)] = cost else : self.__dictionaryCosts[(vertex2, vertex1)] = cost
def add_edge(self, vertex1, vertex2, cost) : if vertex1 not in self.__neighbours.keys() or vertex2 not in self.__neighbours.keys() : raise exceptionGraph("Vertices do not exist") if self.is_edge(vertex1, vertex2) : raise exceptionGraph("The edge already exists") if vertex1 == vertex2 : raise exceptionGraph("There can't be an edge from a vertex to itself") self.__neighbours[vertex2].append(vertex1) self.__neighbours[vertex1].append(vertex2) self.__dictionaryCosts[(vertex1, vertex2)] = cost
def remove_edge(self, vertex1, vertex2) : if vertex1 not in self.__neighbours.keys() or vertex2 not in self.__neighbours.keys() : raise exceptionGraph("Vertices do not exist") if not self.is_edge(vertex1, vertex2) : raise exceptionGraph("The edge doesn't exist") self.__neighbours[vertex2].remove(vertex1) self.__neighbours[vertex1].remove(vertex2) if (vertex1, vertex2) in self.__dictionaryCosts.keys() : self.__dictionaryCosts.pop((vertex1, vertex2)) else : self.__dictionaryCosts.pop((vertex2, vertex1))
def read_file(file): try: f = open(file, 'r') except exceptionGraph: raise exceptionGraph("The file is not available") firstLine = f.readline().strip().split() graph = Graph(0) number_of_vertices = int(firstLine[0]) line = f.readline() while line != "": edge = line.strip().split() vertex1 = int(edge[0]) vertex2 = int(edge[1]) if vertex1 not in graph.parse_vertices(): graph.add_vertex(vertex1) if vertex2 not in graph.parse_vertices() and vertex2 != -1: graph.add_vertex(vertex2) if len(edge) == 3 and vertex1 != vertex2 and not graph.is_edge( vertex1, vertex2): graph.add_edge(vertex1, vertex2, int(edge[2])) line = f.readline() if number_of_vertices != graph.get_nr_of_vertices(): for vertex in range(number_of_vertices): if vertex not in graph.parse_vertices(): graph.add_vertex(vertex) f.close() return graph
def validate_edge(edge): try: edge = edge.strip().split(",") vertex1 = int(edge[0]) vertex2 = int(edge[1]) return vertex1, vertex2 except exceptionGraph: raise exceptionGraph("The input is invalid\n")
def remove_vertex(self, vertex) : if vertex not in self.__neighbours.keys() : raise exceptionGraph("Vertex doesn't exist") self.__vertices -= 1 for neighbours in self.__neighbours[vertex] : self.__neighbours[neighbours].remove(vertex) if (neighbours, vertex) in self.__dictionaryCosts.keys() : self.__dictionaryCosts.pop((neighbours, vertex)) else : self.__dictionaryCosts.pop((vertex, neighbours)) self.__neighbours.pop(vertex)
def random_graph(number_of_vertices, number_of_edges): minimum = input("Minimum cost: ") minimum = validate_vertex(minimum) maximum = input("Maximum cost: ") maximum = validate_vertex(maximum) if number_of_edges > number_of_vertices * number_of_vertices: raise exceptionGraph("The nr of edges must be < than nrVertices^2") graph = Graph(number_of_vertices) for vertex in range(number_of_edges): cost = random.randint(minimum, maximum) vertex1 = random.randrange(0, number_of_vertices) vertex2 = random.randrange(0, number_of_vertices) while graph.is_edge(vertex1, vertex2) or vertex1 == vertex2: vertex1 = random.randrange(0, number_of_vertices) vertex2 = random.randrange(0, number_of_vertices) graph.add_edge(vertex1, vertex2, cost) return graph
def validate_vertex(vertex): try: vertex = int(vertex) return vertex except ValueError: raise exceptionGraph("Please add an integer\n")
def parse_neighbours(self, vertex) : if vertex not in self.__neighbours.keys() : raise exceptionGraph("Vertex does not exist") return self.__neighbours[vertex]
def get_vertex_degree(self, vertex) : if vertex not in self.__neighbours.keys() : raise exceptionGraph("Vertex does not exist") return len(self.__neighbours[vertex])
def is_edge(self, vertex1, vertex2) : if vertex1 not in self.__neighbours.keys() or vertex2 not in self.__neighbours.keys() : raise exceptionGraph("Vertices are not valid") return vertex2 in self.__neighbours[vertex1]
def add_vertex(self, vertex) : if vertex in self.__neighbours.keys() : raise exceptionGraph("Vertex already exists") self.__neighbours[vertex] = [] self.__vertices += 1