def test_unweighted_remove_node_node(): graph = Graph() nodes = [["a","b"],["b","c"],["a","c"],["c","a"]] graph.make_unweighted_from_list(nodes) graph.remove_node(Node("a")) test_graph = {Node("b"):[Edge("b","c")], Node("c"):[]} assert compare_graphs(test_graph, graph.graph) == True
def test_cyclic_graph_no_path(): graph = Graph() nodes = [["a","b"],["b","a"],["c"]] graph.make_unweighted_from_list(nodes) expected_path = [] actual_path = graph.depth_first_search(start=Node("a"), target=Node("c")) assert actual_path == expected_path
def test_passes_with_unused_negative_weights(): graph = Graph() nodes = [["a","b", 4], ["c", "d", -1]] graph.make_weighted_from_list(nodes) actual_path = graph.dijkstras_with_target(start=Node("a"), target=Node("b")) expected_path = ["a", "b"] assert actual_path == expected_path
def test_cyclic_graph(): graph = Graph() nodes = [["a","b"], ["b","c"], ["b", "a"], ["b", "z"], ["c", "d"], ["c", "e"], ["e", "a"]] graph.make_unweighted_from_list(nodes) expected_path = ["a", "b", "c", "e"] actual_path = graph.dijkstras_with_target(start=Node("a"), target=Node("e")) assert actual_path == expected_path
def test_with_disconnected_nodes(): graph = Graph() nodes = [["a"], ["b"]] graph.make_unweighted_from_list(nodes) actual_path = graph.dijkstras_with_target(start=Node("a"), target=Node("b")) expected_path = [] assert actual_path == expected_path
def generate_prims_maze(self) -> List[Edge]: edge_queue = PriorityQueue(self.ascending_edge_order) visited = {} T = list() for x in range(0, int(self.columns / 2) + 1): for y in range(0, int(self.rows / 2) + 1): visited[Node(2 * x, 2 * y)] = False for node in visited: for neighbour in self.range_2_neighbours_of_(node): if not visited[neighbour]: edge_queue.enqueue( Edge(node, neighbour, random.randrange(1000))) while not edge_queue.is_empty() and visited[ edge_queue.first().node_b]: edge_queue.dequeue() if not edge_queue.is_empty(): edge = edge_queue.first() node_sr = Node(int((edge.node_b.x + edge.node_a.x) / 2), int((edge.node_a.y + edge.node_b.y) / 2)) T.append(Edge(edge.node_a, node_sr, 0)) T.append(Edge(node_sr, edge.node_b, 0)) visited[edge.node_b] = True return T
def test_visited_reset(): graph = get_default_graph() for node in graph.graph: assert node.visited == False actual_path = graph.depth_first_search(start=Node("b"), target=Node("a")) for node in graph.graph: assert node.visited == False
def test_straight_line_graph(): # a straight line graph is one that has only one path at each node graph = Graph() nodes = [["a","b",1],["b","c",1]] graph.make_weighted_from_list(nodes) expected_path = ["a","b","c"] actual_path = graph.dijkstras_with_target(Node("a"),Node("c")) assert actual_path == expected_path
def test_cyclic_graph_large(): graph = Graph() nodes = [["a", "b"], ["b", "c"], ["c", "d"], ["d", "e"], ["d", "a"], ["a", "d"], ["e", "z"], ["z", "a"]] graph.make_unweighted_from_list(nodes) expected_path = ["a", "d", "e", "z"] actual_path = graph.breadth_first_search(start=Node("a"), target=Node("z")) assert actual_path == expected_path
def test_weighted_list_of_int(): graph = Graph() nodes = [[1, 2, 99], [2, 3, 26], [3, 5, 130], [5, 1, 2], [2, 5, 0]] graph.make_unweighted_from_list(nodes) test_graph = { Node(1):[Edge(1,2,99)], Node(2):[Edge(2,3,26), Edge(2,5,0)], Node(3):[Edge(3,5,130)], Node(5):[Edge(5,1,2)]} assert compare_graphs(test_graph, graph.graph) == True
def test_unweighted_directed(): graph = Graph() nodes = [["a","b"],["b","c"],["a","c"]] graph.make_unweighted_from_list(nodes) test_graph = { Node("a"):[Edge("a","b"),Edge("a","c")], Node("b"):[Edge("b","c")], Node("c"):[] } assert compare_graphs(test_graph, graph.graph) == True
def test_weighted_undirected(): graph = Graph() nodes = [["a","b",1],["b","c",2],["a","c",3]] graph.make_weighted_from_list(nodes, directed=False) test_graph = { Node("a"):[Edge("a", "b",1), Edge("a","c",3)], Node("b"):[Edge("b","a",1), Edge("b","c",2)], Node("c"):[Edge("c","b",2), Edge("c","a",3)] } assert compare_graphs(test_graph, graph.graph) == True
def test_unweighted_list_of_int(): graph = Graph() nodes = [[1, 2], [2, 3], [3, 5], [5, 1], [2,5]] graph.make_unweighted_from_list(nodes) test_graph = { Node(1):[Edge(1,2)], Node(2):[Edge(2,3), Edge(2,5)], Node(3):[Edge(3,5)], Node(5):[Edge(5,1)]} assert compare_graphs(test_graph, graph.graph) == True
def test_unweighted_edges_set(): test_graph = { Node("a"):[Edge("a","b"),Edge("a","c")], Node("b"):[Edge("b","c")], Node("c"):[] } graph = Graph() nodes = [["a","b"],["b","c"],["a","c"]] graph.make_unweighted_from_list(nodes) for node in graph.graph: assert graph.graph[node] == test_graph[node.name]
def test_weighted_modify_weight(): graph = Graph() nodes = [["a","b",1],["b","c",2],["a","c",3]] graph.make_weighted_from_list(nodes) graph.modify_weight(["a","b",1], 78) test_graph = { Node("a"):[Edge("a", "b",78), Edge("a","c",3)], Node("b"):[Edge("b","c",2)], Node("c"):[] } assert compare_graphs(test_graph, graph.graph) == True
def test_weighted_add_node(): graph = Graph() nodes = [["a","b",5],["b","c",4],["a","c",3]] graph.make_weighted_from_list(nodes) graph.add_node(["b","b",2]) graph.add_node(["b","d",1]) test_graph = { Node("a"):[Edge("a","b",5),Edge("a","c",3)], Node("b"):[Edge("b","c",4),Edge("b","b",2),Edge("b","d",1)], Node("c"):[], Node("d"):[] } assert compare_graphs(test_graph, graph.graph) == True
def test_strongly_connected_graph(): graph = Graph() nodes = [] nodes_to_add = set(["a","b","c","d","e","f","g","h","i"]) for node in nodes_to_add: for other_node in nodes_to_add: if other_node != node: nodes.append([node,other_node]) graph = Graph() graph.make_unweighted_from_list(nodes, directed=False) actual_path = graph.dijkstras_with_target(start = Node("f"), target = Node("i")) expected_path = ["f","i"] assert actual_path == expected_path
def test_unweighted_add_node(): # make the same graph as one of the ones above # add a node and assert its equal graph = Graph() nodes = [["a","b"],["b","c"],["a","c"]] graph.make_unweighted_from_list(nodes) graph.add_node(["b","b"]) test_graph = { Node("a"):[Edge("a","b"),Edge("a","c")], Node("b"):[Edge("b","c"),Edge("b","b")], Node("c"):[] } assert compare_graphs(test_graph, graph.graph) == True
def add_sybils(graph, sybil_edges, group): nodes_dic = {node.name: node for node in graph.nodes()} edges = [] for edge in sybil_edges: for node_name in edge: if node_name not in nodes_dic: nodes_dic[node_name] = Node(node_name, 'Sybil', groups=set([group, group + 'b'])) elif nodes_dic[node_name].node_type != 'Sybil': nodes_dic[node_name].groups.add('Attackers') nodes_dic[node_name].node_type = 'Attacker' edges.append((nodes_dic[edge[0]], nodes_dic[edge[1]])) graph.add_edges_from(edges)
def range_2_neighbours_of_(self, node: Node) -> List[Node]: neighbours = list() # up if node.x > 1: neighbours.append(Node(node.x - 2, node.y)) # down if node.x < int(self.columns): neighbours.append(Node(node.x + 2, node.y)) # left if node.y > 1: neighbours.append(Node(node.x, node.y - 2)) # right if node.y < int(self.rows): neighbours.append(Node(node.x, node.y + 2)) return neighbours
def test_large_vs_known_implementation(): """ Creates a large random graph and runs dijkstra's algorithm with this implementation as well as a known good implementation I grabbed from github. Compares that the results returned are the same """ import random size = 1000 my_graph = Graph() my_nodes = [] other_graph = OtherGraph() for i in range(size): start_node = i end_node = random.randint(0, size) length = random.randrange(0, 10000) my_nodes.append([start_node, end_node, length]) other_graph.add_edge(start_node, end_node, length) my_graph.make_weighted_from_list(my_nodes) my_prev, my_unvisited = my_graph.dijkstra(start=Node(0)) other_visited, other_prev = known_dijkstras_implementation(other_graph, 0) unvisited_diff = my_unvisited.keys() - other_visited.keys() visited_diff = other_visited.keys() - my_unvisited.keys() assert my_prev == other_prev and my_unvisited.keys() == unvisited_diff and other_visited.keys() == visited_diff
def add_sybils(graph, sybil_edges, group): nodes_dic = {node.name: node for node in graph.nodes()} edges = [] for edge in sybil_edges: for node_name in edge: if node_name not in nodes_dic: nodes_dic[node_name] = Node(node_name, 'Sybil', groups=set([group, group + '-2'])) edges.append((nodes_dic[edge[0]], nodes_dic[edge[1]])) graph.add_edges_from(edges)
def test_weighted_remove_node(): graph = Graph() nodes = [["a","b",1],["b","b",99],["c","b",2],["d","b",3],["e","b",4],["f","z","a"]] graph.make_weighted_from_list(nodes) graph.remove_node(Node("b")) test_graph = { Node("a"):[], Node("c"):[], Node("d"):[], Node("e"):[], Node("f"):[Edge("f","z","a")], Node("z"): [] } assert compare_graphs(test_graph, graph.graph) == True
def add_sybils(graph, sybils_file_name): nodes_dic = {node.name: node for node in graph.nodes()} input_file = open(sybils_file_name, 'rb') edges = [] for i, row in enumerate(input_file): edge = row.strip().split() edge = [ int(node_name) if node_name.isdigit() else node_name for node_name in edge ] for node_name in edge: if node_name not in nodes_dic: nodes_dic[node_name] = Node(node_name, 'Sybil', groups=set(['sybils'])) edges.append((nodes_dic[edge[0]], nodes_dic[edge[1]])) graph.add_edges_from(edges) sybils = [node for node in nodes_dic.values() if node.node_type == 'Sybil'] for sybil in sybils: for neighbour in graph.neighbors(sybil): if neighbour.node_type != 'Sybil': # neighbour.node_type = 'Attacker' neighbour.groups.add('sybils')
from graphs.graph import Graph from graphs.node import Node g = Graph() n1 = Node(1) n2 = Node(2) n3 = Node(3) n4 = Node(4) n5 = Node(5) n6 = Node(6) n7 = Node(7) n8 = Node(8) n1.neighbours.append(n2) n1.neighbours.append(n3) n1.neighbours.append(n4) n2.neighbours.append(n5) n2.neighbours.append(n6) n3.neighbours.append(n7) n3.neighbours.append(n8) # code bellow makes sure that graph is not directional. (so every edge is bi-directional) n2.neighbours.append(n1) n3.neighbours.append(n1) n4.neighbours.append(n1) n5.neighbours.append(n2) n6.neighbours.append(n2) n7.neighbours.append(n3) n8.neighbours.append(n3) result = g.dfs(n1) for r in result:
from graphs.graph import Graph from graphs.node import Node g = Graph() n1 = Node(1) n2 = Node(2) n3 = Node(3) n4 = Node(4) n5 = Node(5) n6 = Node(6) n7 = Node(7) n8 = Node(8) n9 = Node(9) n10 = Node(10) # first component (region) n1.neighbours.append(n2) n2.neighbours.append(n3) n3.neighbours.append(n4) n4.neighbours.append(n1) # second component n6.neighbours.append(n7) # third component is just node 5 # fourth component n8.neighbours.append(n9) n9.neighbours.append(n10) # code bellow makes sure that graph is not directional. (so every edge is bi-directional)
def test_name_set(): node = Node() node.name = "node" assert node.name == "node"
def test_edges_set(): node = Node() node.edges = [["a", "b"], ["b", "c"]] assert node.edges == [["a", "b"], ["b", "c"]]
def __init__(self, matrix: Matrix, columns: int, rows: int): self.two_dim_list: List[List[AbtractBlock]] = matrix.two_dim_list self.columns = columns self.rows = rows self.nodes: List[List[Node]] = [[Node(y, x) for x in range(rows)] for y in range(columns)]
def test_add_single_node(): nodes = [["a"]] graph = Graph() graph.make_unweighted_from_list(nodes) test_graph = {Node("a"):[]} assert compare_graphs(test_graph, graph.graph)