def __init__(self, graph: DirectedGraph): self.matrix = [] for i in range(len(graph.get_nodes())): self.matrix.append([0 for j in range(len(graph.get_nodes()))]) for edge in graph.get_edges(): nodes_ids = edge.get_nodes_ids() self.matrix[nodes_ids[0].get_id()][nodes_ids[1].get_id()] = 1
def directed_graph_generator_np(n: int, p: float) -> DirectedGraph: vertices = list([v for v in range(n)]) e = list() for c in combinations(vertices, 2): tmp = random() if tmp < p: e.append((Node(c[0]), Node(c[1]))) tmp2 = random() if tmp2 < p: e.append((Node(c[1]), Node(c[0]))) graph = DirectedGraph() for v in vertices: graph.add_node(Node(v)) graph.add_edges_from(e) return graph
import os from graphlib.DirectedGraph import DirectedGraph from graphlib.representations.AdjacencyList import DirectedAdjacencyList from graphlib.representations.AdjacencyMatrix import DirectedAdjacencyMatrix directed_adjacency_list_graph = DirectedGraph.create_from_file( os.path.dirname(__file__) + "/files/directed_adjacency_list.json", "adjacency_list") print(directed_adjacency_list_graph) directed_adjacency_matrix_graph = DirectedGraph.create_from_file( os.path.dirname(__file__) + "/files/directed_adjacency_matrix.json", "adjacency_matrix") print(directed_adjacency_matrix_graph) directed_incidence_matrix_graph = DirectedGraph.create_from_file( os.path.dirname(__file__) + "/files/directed_incidence_matrix.json", "incidence_matrix") print(directed_incidence_matrix_graph) DirectedAdjacencyList(directed_incidence_matrix_graph).print()
import os from graphlib.DirectedGraph import DirectedGraph from graphlib.algorithms.Algorithms import Algorithms graph = DirectedGraph.create_from_file( os.path.dirname(__file__) + "/files/pagerank_list.json", "adjacency_list") dict = Algorithms().pagerank_randomwalk(graph) print("========= Błądzenie przypadkowe =========") for key, value in dict.items(): print(f"({key}) ==> ", value) dict2 = Algorithms().pagerank_randomwalk(graph) print("========= Metoda iteracyjna =========") for key, value in dict2.items(): print(f"({key}) ==> ", value)
def __init__(self, edges: List[Edge] = None, nodes: Set[Node] = None, is_weighted = False): DirectedGraph.__init__(self, edges, nodes, is_weighted) self.layers = list()
from graphlib.algorithms.Algorithms import Algorithms import os from graphlib.representations.AdjacencyList import DirectedAdjacencyList np_graph = Generator.directed_graph_generator_np(5, 0.7) np_graph.draw("graph_2.png") print("ilość spójnych składowych:", np_graph.SCCs(np_graph)) DirectedAdjacencyList(np_graph).print() print("------ example ------") # ex_g = DirectedGraph.create_from_file(os.path.dirname(__file__) + "/files/digraph_ex.json", "adjacency_list") ex_g = DirectedGraph.create_from_file( os.path.dirname(__file__) + "/files/example.json", "adjacency_list") DirectedAdjacencyList(ex_g).print() comp3 = ex_g.SCCs(ex_g) print("ilość spójnych składowych:", comp3) print("------ example ------") g = DirectedGraph() g.add_weighted_edge(Node(0), Node(1), weight=-1) g.add_weighted_edge(Node(1), Node(0), weight=4) g.add_weighted_edge(Node(0), Node(2), weight=-4) g.add_weighted_edge(Node(2), Node(1), weight=2) D = Algorithms().johnson(g) for i in range(len(D)): print(D[i])
def __init__(self, graph: DirectedGraph) -> None: self.list = {key: [] for key in range(len(graph.get_nodes()))} for edge in graph.get_edges(): nodes = edge.get_nodes_ids() self.list[nodes[0].get_id()].append(nodes[1].get_id())