def test_eval(size, edges, start_vertice, expected): graph = GraphMatrix(size, digraph=True) for x, y, w in edges: graph.add_edge(x, y, weight=w) dijkstra = Dijkstra(graph) result = dijkstra.run(start_vertice) assert result == expected
def test_assert_vertice_exists_bigger_range(): try: graph = GraphMatrix(3) graph.neighbourhood(3) assert False except ValueError: assert True
def test_assert_vertice_exists_two_not_in_range(): try: graph = GraphMatrix(3) graph.add_edge(-1, 3) assert False except ValueError: assert True
def add_edges(graph: GraphMatrix, edges_list: List[List[int]]) -> GraphMatrix: if len(edges_list[0]) == 3: for x, y, z in edges_list: graph.add_edge(x, y, weight=z) else: for x, y in edges_list: graph.add_edge(x, y) return graph
def test_path(edges, size, begin, end, expected): graph = GraphMatrix(size) for edge in edges: graph.add_edge(edge[0], edge[1]) print(graph) print(begin) print(end) assert graph.has_path(begin, end) == expected
def __init__(self, graph: GraphMatrix, list_vertices: List[int] = None): num_vertices: int = graph.number_of_vertices() self.marked: List[bool] = [False] * num_vertices self.id: List = [None] * num_vertices self.count: int = 0 if not list_vertices: list_vertices = graph.get_vertices() for vertice in list_vertices: if not self.marked[vertice]: self.__dfs(graph, vertice) self.count += 1
def __init__(self, graph: GraphMatrix, list_of_vertices: List[int] = None): if not graph.digraph: raise ValueError("this graph is not directed") self.color: List[Color] = [Color.WHITE] * graph.number_of_vertices() self.discovered_vertice_time: List = [None ] * graph.number_of_vertices() self.final_vertice_time: List = [None] * graph.number_of_vertices() self.predecessor: List = [None] * graph.number_of_vertices() self.count: int = 0 if not list_of_vertices: list_of_vertices = graph.get_vertices() for vertice in list_of_vertices: if self.color[vertice] != Color.BLACK: self.run(graph, vertice)
def test_number_of_edges(input, expected): graph = GraphMatrix(10) edges = [] for i in range(input): edge1 = random.randint(0, 9) edge2 = random.randint(0, 9) edge = sorted([edge1, edge2]) if edge not in edges: edges.append(edge) for edge in edges: graph.add_edge(edge[0], edge[1]) expected = len(edges) print(edges) print(graph) assert graph.number_of_edges() == expected
def main(num_vertices, edges, do_warshall, topological_sorting, strong_components, dijkstra): graph = GraphMatrix(num_vertices, digraph=True) if edges: edges = ast.literal_eval(edges) graph = add_edges(graph, edges) _print_graph(graph) if do_warshall: warshall = Warshall(graph) print(warshall) if topological_sorting: print("Topological Sorting:") temp = TopologicalSorting(graph) print(temp.run()) if strong_components: print("\nStrong Components:") temp = StrongComponents(graph) print(temp) if dijkstra >= 0: vertice = dijkstra str_dijkstra(graph, vertice)
def run(self, graph: GraphMatrix, vertice: int) -> None: self.color[vertice] = Color.GRAY self.count += 1 self.discovered_vertice_time[vertice] = self.count for connected_vertice in graph.out_neighbourhood(vertice): if self.color[connected_vertice] == Color.WHITE: self.predecessor[connected_vertice] = vertice self.run(graph, connected_vertice) self.color[vertice] = Color.BLACK self.count += 1 self.final_vertice_time[vertice] = self.count
def run(self, graph: GraphMatrix, vertice: int) -> None: self.color[vertice] = Color.GRAY self.count += 1 self.discovered_vertice_time[vertice] = self.count for connected_vertice in graph.out_neighbourhood(vertice): if self.color[connected_vertice] == Color.WHITE: self.predecessor[connected_vertice] = vertice self.run(graph, connected_vertice) # if DFS finds a gray vertice, than it is not DAG elif self.color[connected_vertice] == Color.GRAY: self.dag = False self.color[vertice] = Color.BLACK self.count += 1 self.final_vertice_time[vertice] = self.count
def test_neighbourhood(edges, vertice, expected): graph = GraphMatrix(3) for edge in edges: graph.add_edge(edge[0], edge[1]) assert graph.neighbourhood(vertice) == expected
def test_num_edges(size_graph, edges): graph = GraphMatrix(size_graph, digraph=True) for edge in edges: graph.add_edge(edge[0], edge[1]) assert graph.number_of_edges() == len(edges)
def test_assert_positive_vertice_raises_exception(): with pytest.raises(ValueError) as e: graph = GraphMatrix(-1)
def test_eval(size, edges, expected): graph = GraphMatrix(size, digraph=True) for x, y in edges: graph.add_edge(x, y) warshall = Warshall(graph) assert warshall.run() == expected
def test_edges_acessible(edges, size, expected): graph = GraphMatrix(size) for edge in edges: graph.add_edge(edge[0], edge[1]) assert graph.has_acessible_edges() == expected
def test_euler_path(edges, size, expected): graph = GraphMatrix(size) for edge in edges: graph.add_edge(edge[0], edge[1]) assert graph.has_open_euler_path() == expected
def test_vertices_with_edges(edges, size, expected): graph = GraphMatrix(size) for edge in edges: graph.add_edge(edge[0], edge[1]) assert graph.vertices_with_edges() == expected
def test_num_loops_graph(edges, expected): graph = GraphMatrix(3) for edge in edges: graph.add_edge(edge[0], edge[1]) assert graph.number_of_loops_graph() == expected
def _print_graph(graph: GraphMatrix) -> None: print("Graph:") print(graph) print("Edge Weight:") print(graph.str_weight())
def test_asser_positive_vertice_zero(): try: graph = GraphMatrix(0) except ValueError: assert False
def test_number_of_vertice_four(): assert GraphMatrix(4).number_of_vertices() == 4
def test_number_of_vertice_one(): assert GraphMatrix(1).number_of_vertices() == 1
def test_number_of_vertice_zero(): assert GraphMatrix(0).number_of_vertices() == 0
def test_degree(edges, vertice, expected): graph = GraphMatrix(3) for edge in edges: graph.add_edge(edge[0], edge[1]) assert graph.degree(vertice) == expected
def __dfs(self, graph: GraphMatrix, start_vertice: int) -> None: self.marked[start_vertice] = True self.id[start_vertice] = self.count for adjacent_vertice in graph.out_neighbourhood(start_vertice): if not self.marked[adjacent_vertice]: self.__dfs(graph, adjacent_vertice)
def test_max_degree(edges, expected): graph = GraphMatrix(3) for edge in edges: graph.add_edge(edge[0], edge[1]) assert graph.max_degree() == expected
def __init__(self, graph: GraphMatrix): self.graph = graph self.__inf = float("Inf") self.__num_vertices = graph.number_of_vertices() self.__reset()
def test_assert_positive_vertice_one(): try: graph = GraphMatrix(1) except ValueError: assert False