Beispiel #1
0
 def __init__(self, graph: DiGraph = None):
     if graph is None:
         self._graph = DiGraph()
     else:
         self._graph = graph
     self._parent = {}
Beispiel #2
0
 def test_add_element(self):
     G = DiGraph()
     G.add_node(0)
     self.assertFalse(G.add_edge(0, 1, 13))
     G.add_node(1)
     self.assertTrue(G.add_edge(0, 1, 13))
Beispiel #3
0
 def __init__(self, Graph_Algo: DiGraph = DiGraph()):
     self.Graph_Algo = Graph_Algo
     self.predecessor = {}
     for node_key in self.Graph_Algo.graph.keys():
         self.predecessor[node_key] = None
 def __init__(self, graph: DiGraph = DiGraph()):
     self.graph = graph
Beispiel #5
0
 def test_remove_node(self):
     G = DiGraph()
     self.assertFalse(G.remove_node(0))
     G.add_node(0)
     self.assertTrue(G.remove_node(0))
Beispiel #6
0
    def test_connected_component(self):
        #  print("check0 connected_component ")
        """
                 empty graph
                                   """

        graph = DiGraph()
        g_algo = GraphAlgo(graph)
        my_list = []  # non exiting node
        self.assertEqual(
            my_list,
            g_algo.connected_component(89))  # empty graph return empty list

        print("check1 connected_component ")
        """
          0->1->2->3
             1<-
                           """

        graph1 = DiGraph()
        g_algo1 = GraphAlgo(graph1)
        graph1.add_node(0)
        graph1.add_node(1)
        graph1.add_node(2)
        graph1.add_node(3)
        graph1.add_edge(1, 2, 11)
        graph1.add_edge(2, 1, 11)
        graph1.add_edge(2, 3, 5)
        graph1.add_edge(1, 0, 3)
        my_list = [0]
        self.assertEqual(my_list, g_algo1.connected_component(0))
        my_list = [1, 2]
        self.assertEqual(my_list, g_algo1.connected_component(1))
        my_list = [2, 1]
        self.assertEqual(my_list, g_algo1.connected_component(2))
        my_list = [3]
        self.assertEqual(my_list, g_algo1.connected_component(3))

        #  print("check2 connected_component ")
        """
    
                    0------>1------>2<------>3
                    ↑     / |       |        ↑
                    |    /  |       |        |
                    |   /   |       |        |
                    |  /    |       |        |
                    | ↓     ↓       ↓        ↓
                    7------>6<----->5<-------4
                           (7)
    
               """

        g_algo2 = GraphAlgo(first_graph())
        my_list = [0, 1, 7]
        self.assertEqual(my_list, g_algo2.connected_component(0))
        my_list = [1, 7, 0]
        self.assertEqual(my_list, g_algo2.connected_component(1))
        my_list = [2, 3, 4]
        self.assertEqual(my_list, g_algo2.connected_component(2))
        my_list = [3, 2, 4]
        self.assertEqual(my_list, g_algo2.connected_component(3))
        my_list = [4, 3, 2]
        self.assertEqual(my_list, g_algo2.connected_component(4))
        my_list = [5, 6]
        self.assertEqual(my_list, g_algo2.connected_component(5))
        my_list = [6, 5]
        self.assertEqual(my_list, g_algo2.connected_component(6))
        my_list = [7, 0, 1]
        self.assertEqual(my_list, g_algo2.connected_component(7))

        #  print("check3 connected_component ")
        """
                             5
                           ↑  ↑
                     (9) /     \(6)
                       /        \
                     ↓ (2)  (11) ↓
                   6<----->3<---->4
                   ↑     ↑  ↑     ↑
                   |    /    \    |
              (14) |   /      \   | (15)
                   |  / (9)(10 \  |
                   | ↓          ↓ ↓
                   1<------------>2
                          (7)
    
              """

        g_algo3 = GraphAlgo(second_graph())
        my_list = [1, 2, 4, 3, 6, 5]
        self.assertEqual(my_list, g_algo3.connected_component(1))
        my_list = [2, 1, 3, 4, 5, 6]
        self.assertEqual(my_list, g_algo3.connected_component(2))
        my_list = [3, 1, 2, 4, 5, 6]
        self.assertEqual(my_list, g_algo3.connected_component(3))
        my_list = [4, 2, 1, 3, 6, 5]
        self.assertEqual(my_list, g_algo3.connected_component(4))
        my_list = [5, 4, 2, 1, 3, 6]
        self.assertEqual(my_list, g_algo3.connected_component(5))
        my_list = [6, 1, 2, 4, 3, 5]
        self.assertEqual(my_list, g_algo3.connected_component(6))

        #  print("check4 connected_component ")
        """
                               5
    
                        (2)    (11)
                     6<----->3<---->4
                     ↑     ↑  ↑     ↑
                     |    /    \    |
                (14) |   /      \   | (15)
                     |  / (9)(10 \  |
                     ↓ ↓          ↓ ↓
                     1<------------>2
                            (7)
    
                """

        g_algo4 = GraphAlgo(three_graph())
        my_list = [1, 2, 4, 3, 6]
        self.assertEqual(my_list, g_algo4.connected_component(1))
        my_list = [2, 1, 3, 4, 6]
        self.assertEqual(my_list, g_algo4.connected_component(2))
        my_list = [3, 1, 2, 4, 6]
        self.assertEqual(my_list, g_algo4.connected_component(3))
        my_list = [4, 2, 1, 3, 6]
        self.assertEqual(my_list, g_algo4.connected_component(4))
        my_list = [5]
        self.assertEqual(my_list, g_algo4.connected_component(5))
        my_list = [6, 1, 2, 4, 3]
        self.assertEqual(my_list, g_algo4.connected_component(6))

        #  print("check5 connected_component ")
        """
        4<-0->1  2->3  4->0  5
         
                       """
        graph4 = DiGraph()
        g_algo5 = GraphAlgo(graph4)
        graph4.add_node(0)
        graph4.add_node(1)
        graph4.add_node(2)
        graph4.add_node(3)
        graph4.add_node(4)
        graph4.add_node(5)
        graph4.add_edge(0, 1, 11)
        graph4.add_edge(0, 4, 11)
        graph4.add_edge(2, 3, 5)
        graph4.add_edge(4, 0, 3)
        my_list = [0, 4]
        self.assertEqual(my_list, g_algo5.connected_component(0))
        my_list = [1]
        self.assertEqual(my_list, g_algo5.connected_component(1))
        my_list = [2]
        self.assertEqual(my_list, g_algo5.connected_component(2))
        my_list = [3]
        self.assertEqual(my_list, g_algo5.connected_component(3))
        my_list = [4, 0]
        self.assertEqual(my_list, g_algo5.connected_component(4))
        my_list = [5]
        self.assertEqual(my_list, g_algo5.connected_component(5))
Beispiel #7
0
 def test_nodeSize(self):
     graph = DiGraph()
     ran = 10000
     for i in range(ran):
         graph.add_node(i)
     self.assertEqual(ran, graph.v_size())
Beispiel #8
0
 def test_v_size(self):
     graph = DiGraph()
     for i in range(10):
         graph.add_node(i)
     self.assertEqual(10, graph.node_size)
Beispiel #9
0
 def test_v_size(self):
     g0 = DiGraph()
     for i in range(0, 10):
         g0.add_node(i)
     self.assertEqual(10, g0.v_size())
Beispiel #10
0
 def test_add_edge(self):
     graph = DiGraph()
     graph.add_node(1)
     graph.add_node(2)
     graph.add_edge(1, 2, 0.4)
     self.assertEqual(0.4, graph.E[1][2])
Beispiel #11
0
 def test_empty_graph(self):
     graph = DiGraph()
     self.assertEqual(0, graph.v_size())
     self.assertEqual(0, graph.e_size())
     self.assertEqual(0, graph.get_mc())
Beispiel #12
0
 def test_add_node(self):
     graph = DiGraph()
     graph.add_node(0)
     self.assertIn(0, graph.V)
 def setUp(self):
     self.graph = DiGraph()
        self.assertEqual([], self.ga.connected_component(10))
        for j in range(1, 9):
            print(self.ga.connected_component(j))

    def test_connected_components(self):
        self.ga.graph = g1
        sccs = self.ga.connected_components()
        self.assertEqual([[1, 2, 5], [8, 3, 4], [6, 7]], sccs)

    def test_plot_graph(self):
        self.ga.graph = rg
        self.ga.plot_graph()


# ========================== Graph 1 ===========================
g1 = DiGraph()
for i in range(1, 9):
    g1.add_node(i)
g1.add_edge(1, 2, 1)
g1.add_edge(2, 3, 1)
g1.add_edge(3, 4, 1)
g1.add_edge(4, 8, 1)
g1.add_edge(8, 4, 1)
g1.add_edge(4, 3, 1)
g1.add_edge(8, 7, 1)
g1.add_edge(6, 7, 1)
g1.add_edge(7, 6, 1)
g1.add_edge(2, 6, 1)
g1.add_edge(5, 6, 1)
g1.add_edge(2, 5, 1)
g1.add_edge(5, 1, 1)
Beispiel #15
0
 def __init__(self):
     self.g = DiGraph()
     self.graph = GraphAlgo(self.g)
Beispiel #16
0
 def test_copy(self):
     graph = DiGraph(self.graph)
     self.assertEqual(graph, self.graph)
Beispiel #17
0
    def test_shortest_path(self):
        #  print("second_graph")
        g_algo = GraphAlgo(second_graph())
        x, y = g_algo.shortest_path(1, 5)  # check1
        self.assertTrue(x == 20)
        ans_list1 = [1, 3, 6, 5]
        self.assertEqual(y, ans_list1)

        x, y = g_algo.shortest_path(2, 5)  # check2
        self.assertTrue(x == 21)
        ans_list1 = [2, 4, 5]
        self.assertEqual(y, ans_list1)

        #  print("three_graph")
        g_algo2 = GraphAlgo(three_graph())
        x, y = g_algo2.shortest_path(1, 5)  # check3 node5 not connected
        self.assertTrue(x == math.inf)
        self.assertTrue(y == [])
        x, y = g_algo2.shortest_path(5,
                                     5)  # check4 node5 not connected and solo
        self.assertTrue(x == 0)
        ans_list1 = [5]
        self.assertEqual(y, ans_list1)
        x, y = g_algo2.shortest_path(1, 6)  # check5
        self.assertTrue(x == 11)
        ans_list1 = [1, 3, 6]
        self.assertEqual(y, ans_list1)
        x, y = g_algo2.shortest_path(1, 3)  # check6
        self.assertTrue(x == 9)
        ans_list1 = [1, 3]
        self.assertEqual(y, ans_list1)
        x, y = g_algo2.shortest_path(1, 4)  # check7
        self.assertTrue(x == 20)
        ans_list1 = [1, 3, 4]
        self.assertEqual(y, ans_list1)
        g_algo2.get_graph().remove_edge(
            1, 3)  # remove edge between node1 and node3
        x, y = g_algo2.shortest_path(1, 4)  # check8
        self.assertTrue(x == 22)
        ans_list1 = [1, 2, 4]
        self.assertEqual(y, ans_list1)
        x, y = g_algo2.shortest_path(1, 1)  # check9
        self.assertTrue(x == 0)
        ans_list1 = [1]
        self.assertEqual(y, ans_list1)

        #  print("graph4")
        """
        0<-1->2->3
           1<-
        """
        graph4 = DiGraph()
        g_algo3 = GraphAlgo(graph4)
        graph4.add_node(0)
        graph4.add_node(1)
        graph4.add_node(2)
        graph4.add_node(3)
        graph4.add_edge(1, 2, 11)
        graph4.add_edge(2, 1, 11)
        graph4.add_edge(2, 3, 5)
        graph4.add_edge(1, 0, 3)
        x, y = g_algo3.shortest_path(2, 0)
        self.assertTrue(x == 14)  # check10
        ans_list1 = [2, 1, 0]
        self.assertEqual(y, ans_list1)
        x, y = g_algo3.shortest_path(3, 2)  # no way between this vertex
        self.assertTrue(x == math.inf)
        self.assertTrue(y == [])
        graph4.remove_node(2)  # remove node2
        x, y = g_algo3.shortest_path(1, 2)  # node not exist
        self.assertTrue(x == math.inf)
        self.assertTrue(y == [])
        x, y = g_algo3.shortest_path(2, 2)  # node not exist
        self.assertTrue(x == math.inf)
        self.assertTrue(y == [])
        x, y = g_algo3.shortest_path(1, 7)  # node not exist
        self.assertTrue(x == math.inf)
        self.assertTrue(y == [])
        x, y = g_algo3.shortest_path(10, 11)  # node not exist
        self.assertTrue(x == math.inf)
        self.assertTrue(y == [])
Beispiel #18
0
def buildGraph(size: int) -> DiGraph:
    g = DiGraph()
    for i in range(size):
        g.add_node(i)
    return g
Beispiel #19
0
    def test_connected_components(self):
        #  print("check0 connected_components ")
        graph1 = DiGraph()
        g_algo6 = GraphAlgo(graph1)
        my_list = []
        self.assertEqual(my_list,
                         g_algo6.connected_components())  # empty graph

        #  print("check1 connected_components ")
        """
          0->1->2->3
             1<-
                           """
        graph4 = DiGraph()
        g_algo7 = GraphAlgo(graph4)
        graph4.add_node(0)
        graph4.add_node(1)
        graph4.add_node(2)
        graph4.add_node(3)
        graph4.add_edge(1, 2, 11)
        graph4.add_edge(2, 1, 11)
        graph4.add_edge(2, 3, 5)
        graph4.add_edge(1, 0, 3)
        my_list = [[1, 2], [3], [0]]
        self.assertEqual(my_list, g_algo7.connected_components())

        #  print("check2 connected_components ")
        """
    
                    0------>1------>2<------>3
                    ↑     / |       |        ↑
                    |    /  |       |        |
                    |   /   |       |        |
                    |  /    |       |        |
                    | ↓     ↓       ↓        ↓
                    7------>6<----->5<-------4
                           (7)
    
               """
        g_algo8 = GraphAlgo(first_graph())
        my_list = [[0, 1, 7], [2, 3, 4], [5, 6]]
        self.assertEqual(my_list, g_algo8.connected_components())

        #  print("check3 connected_components ")
        """
                             5
                           ↑  ↑
                     (9) /     \(6)
                       /        \
                     ↓ (2)  (11) ↓
                   6<----->3<---->4
                   ↑     ↑  ↑     ↑
                   |    /    \    |
              (14) |   /      \   | (15)
                   |  / (9)(10 \  |
                   | ↓          ↓ ↓
                   1<------------>2
                          (7)
    
              """
        g_algo9 = GraphAlgo(second_graph())
        my_list = [[1, 2, 4, 3, 6, 5]]
        self.assertEqual(my_list, g_algo9.connected_components())

        #  print("check4 connected_components ")
        """
                               5
    
                        (2)    (11)
                     6<----->3<---->4
                     ↑     ↑  ↑     ↑
                     |    /    \    |
                (14) |   /      \   | (15)
                     |  / (9)(10 \  |
                     ↓ ↓          ↓ ↓
                     1<------------>2
                            (7)
    
                """
        g_algo10 = GraphAlgo(three_graph())
        my_list = [[5], [1, 2, 4, 3, 6]]
        self.assertEqual(my_list, g_algo10.connected_components())

        #  print("check5 connected_components ")
        """
        4<-0->1  2->3  4->0  5
    
                       """
        graph4 = DiGraph()
        g_algo11 = GraphAlgo(graph4)
        graph4.add_node(0)
        graph4.add_node(1)
        graph4.add_node(2)
        graph4.add_node(3)
        graph4.add_node(4)
        graph4.add_node(5)
        graph4.add_edge(0, 1, 11)
        graph4.add_edge(0, 4, 11)
        graph4.add_edge(2, 3, 5)
        graph4.add_edge(4, 0, 3)
        my_list = [[5], [2], [3], [0, 4], [1]]
        self.assertEqual(my_list, g_algo11.connected_components())
Beispiel #20
0
 def build_empty_graph(self):
     graph = DiGraph()
     return graph
    def plot_limit(self):

        x_pos = []
        y_pos = []
        for node in self.get_graph().get_all_v().values():
            if node.pos != None:
                x_pos.append(node.pos.x)
                y_pos.append(node.pos.y)
        if len(x_pos) != 0:
            return (min(x_pos), min(y_pos), max(x_pos), max(y_pos))
        else:
            return (0, 0, self.get_graph().v_size(), self.get_graph().v_size())


if __name__ == '__main__':
    g = DiGraph()  # creates an empty directed graph
    for n in range(4):
        g.add_node(node_id=n)

    g.add_edge(0, 1, 1)
    g.add_edge(1, 2, 0)
    g.add_edge(2, 1, 1)
    # g.add_edge(1, 0, 1.1)
    # g.add_edge(1, 2, 1.3)
    # g.add_edge(2, 3, 1.1)
    # g.add_edge(1, 3, 1.9)
    # # print(g.all_out_edges_of_node(1))
    # print(g.edges)
    g.remove_edge(1, 3)
    g.add_edge(1, 3, 10)
Beispiel #22
0
class GraphAlgo(GraphAlgoInterface):
    graph = DiGraph()
    gl = {}

    def __init__(self, graph: DiGraph = None):
        self.graph = graph

    """This abstract class represents an interface of a graph."""

    def get_graph(self) -> GraphInterface:
        return self.graph

    """
        return: the directed graph on which the algorithm works on.
    """

    def load_from_json(self, file_name: str) -> bool:
        with open(file_name) as file:
            self.graph = DiGraph()
            graph_file = json.load(file)
            node_loader = graph_file.get("Nodes")
            edge_loader = graph_file.get("Edges")
            for itr in node_loader:
                if itr.get("pos") is None:
                    self.graph.add_node(itr.get("id"))
                else:
                    geo = str(itr.get("pos")).split(",")
                    self.graph.add_node(itr.get("id"))
                    self.graph.nodes[itr.get("id")].set_location(float(geo[0]), float(geo[1]), 0)

            for itrs in edge_loader:
                src = itrs.get("src")
                dest = itrs.get("dest")
                weight = itrs.get("w")
                self.graph.add_edge(src, dest, weight)

            return True

    """
        Loads a graph from a json file.
        @param file_name: The path to the json file
        @returns True if the loading was successful, False o.w.
        """

    def save_to_json(self, file_name: str) -> bool:
        with open(file_name, 'w') as file:
            if self.graph is not None:
                files = dict()
                files["Nodes"] = list()
                files["Edges"] = list()
                for nod in self.graph.nodes.values():
                    if nod.geo_location is not None:
                        files["Nodes"].append({"pos": nod.geo_location, "id": nod.key})
                    else:
                        files["Nodes"].append({"id": nod.key})
                for save in self.graph.edges.keys():
                    for key, items in self.graph.all_out_edges_of_node(save).items():
                        files["Edges"].append({"src": save, "w": items, "dest": key})
                json.dump(files, file)
                return True
            else:
                return False

    """
        Saves the graph in JSON format to a file
        @param file_name: The path to the out file
        @return: True if the save was successful, False o.w.
        """

    def min_neighbor(self, id1) -> int:
        neigh = self.graph.all_in_edges_of_node(id1)
        all_nodes = self.graph.get_all_v()
        min_weight = all_nodes[id1].get_weight()
        min_key = id1
        for key in neigh.keys():
            if all_nodes[key].get_weight() != -1:
                if all_nodes[key].get_weight() < min_weight and \
                        all_nodes[key].get_weight() + neigh[key] == all_nodes[id1].get_weight():
                    min_key = key
                    min_weight = all_nodes[key].get_weight()
        return min_key

    def shortest_path_dist(self, id1: int, id2: int) -> (float):
        if id1 not in self.graph.nodes.keys() or id2 not in self.graph.nodes.keys():
            return -1
        if id1 == id2:
            return 0
        all_edge = self.graph.all_out_edges_of_node(id1)
        if all_edge is None:
            return -1
        total_edge = self.graph.edges
        all_node = self.graph.get_all_v()
        for node in all_node.keys():
            all_node[node].set_weight(-1)
        all_node[id1].set_weight(0)
        adjacent = PriorityQueue()
        for nod in all_edge.keys():
            if nod != id1:
                all_node[nod].set_weight(all_edge[nod])
                adjacent.put(PrioritizedNode(all_node[nod].get_weight(), all_node[nod]))
        while adjacent.qsize() != 0:
            current_adjacent = adjacent.get()
            current_node = current_adjacent.get_node()
            if current_node == all_node[id2]:
                return all_node[id2].get_weight()
            all_edge = self.graph.all_out_edges_of_node(current_node.get_key())
            if all_edge is not None:
                for key in all_edge.keys():
                    if all_node[key].get_weight() == -1 or all_edge[key] + current_node.get_weight() < all_node[
                        key].get_weight():
                        all_node[key].set_weight(all_edge[key] + current_node.get_weight())
                        adjacent.put(PrioritizedNode(all_node[key].get_weight(), all_node[key]))
        return all_node[id2].get_weight()

    def shortest_path(self, id1: int, id2: int) -> (float, list):
        if self.graph is not None:
            dis = self.shortest_path_dist(id1, id2)
            if dis == -1:
                return math.inf, []
            path = [id1]
            if dis == 0:
                return 0, path
            path.remove(id1)
            current_node = id2
            while current_node != id1:
                path.append(current_node)
                current_node = self.min_neighbor(current_node)
            path.append(current_node)
            final_path = list(reversed(path))
            return dis, final_path
        else:
            return math.inf, None

        """
        Returns the shortest path from node id1 to node id2 using Dijkstra's Algorithm
        @param id1: The start node id
        @param id2: The end node id
        @return: The distance of the path, a list of the nodes ids that the path goes through
        Example:
#      >>> from GraphAlgo import GraphAlgo
#       >>> g_algo = GraphAlgo()
#        >>> g_algo.addNode(0)
#        >>> g_algo.addNode(1)
#        >>> g_algo.addNode(2)
#        >>> g_algo.addEdge(0,1,1)
#        >>> g_algo.addEdge(1,2,4)
#        >>> g_algo.shortestPath(0,1)
#        (1, [0, 1])
#        >>> g_algo.shortestPath(0,2)
#        (5, [0, 1, 2])
        Notes:
        If there is no path between id1 and id2, or one of them dose not exist the function returns (float('inf'),[])
        More info:
        https://en.wikipedia.org/wiki/Dijkstra's_algorithm
        """

    def bfs(self, id1: int) -> (list, dict):
        node = self.graph.nodes[id1]
        list1 = []
        list2 = {}
        q = deque()
        node.set_tag(2)
        list2[node.key] = node
        list1.append(node.key)
        self.gl[id1] = id1
        if node.get_key() in self.graph.edges.keys():
            for key in self.graph.edges[node.get_key()].keys():
                temp = self.graph.nodes[key]
                temp.set_tag(1)
                list2[key] = temp
                q.append(temp.key)
        while q:
            nodes = q.popleft()
            if nodes in self.graph.edges.keys():
                for keys in self.graph.edges[nodes].keys():
                    temp3 = self.graph.nodes[keys]
                    if temp3.get_tag() == 0:
                        temp3.set_tag(1)
                        list2[temp3.get_key()] = temp3
                        q.append(temp3.key)
        if node.get_key() in self.graph.opposite_edges.keys():
            for nod in self.graph.opposite_edges[node.key].keys():
                temp4 = self.graph.nodes[nod]
                if temp4.get_tag() == 1:
                    q.append(temp4.key)
        while q:
            node = q.popleft()
            if self.graph.nodes[node].get_tag() == 1:
                self.graph.nodes[node].set_tag(2)
                list2[node] = self.graph.nodes[node]
                list1.append(node)
                self.gl[node] = node
                for i in self.graph.opposite_edges[node].keys():
                    temp3 = self.graph.nodes[i]
                    q.append(temp3.key)
        return list1, list2

    def connected_component(self, id1: int) -> list:
        if self.graph is None:
            return []
        if id1 not in self.graph.nodes.keys():
            return []
        lista = self.bfs(id1)
        for key in lista[1]:
            lista[1][key].set_tag(0)
        return lista[0]

    def connected_components(self) -> List[list]:
        self.gl.clear()
        if self.graph is None:
            return []
        ans = []
        for key in self.graph.nodes:
            if key not in self.gl:
                ans.append(self.connected_component(key))
        return ans

    def plot_graph(self) -> None:
        """
Beispiel #23
0
 def test_get_graph(self):
     graph = DiGraph()
     for i in range(0, 3):
         graph.add_node(i)
     graph_algo = GraphAlgo(graph)
     self.assertEqual(graph_algo.get_graph(), graph)
Beispiel #24
0
    G = nx.DiGraph()
    with open(filename) as f:
        data = json.load(f)

    for node in data['Nodes']:
        G.add_node(node['id'], pos=(node['pos']))

    for edge in data['Edges']:
        G.add_edge(edge['src'], edge['dest'], weight=edge['w'])
    return G


# nx.draw(G, with_labels=1)
# plt.show()

g = DiGraph()
g_a = GraphAlgo()
g_a.__init__(g)
print("shortest path comparison")
file_name = '../data/G_10000_80000_1.json'
print("this is the 10000_80000 graph shortest path comparison")
start_time = time.time()
c = nx.single_source_dijkstra(from_json(file_name), 226, 2797)
end_time1 = (time.time() - start_time)
print("--- %s seconds --- Networkx" % end_time1)
print(c)
g_a.load_from_json(file_name)
start_time = time.time()
c = g_a.shortest_path(226, 2797)
print("--- %s seconds --- python" % (time.time() - start_time))
print(c)
Beispiel #25
0
 def test_get_all_v(self):
     G = DiGraph()
     G.add_node(0)
     G.add_node(1)
     self.assertEqual(len(G.get_all_v()), 2)
Beispiel #26
0
 def test_AddEdge(self):
     g0 = DiGraph()
     node0 = NodeData(0, (0, 1, 2))
     node1 = NodeData(1, (2.4, 7.1, 3.5))
     g0.add_edge(node0.getkey(), node1.getkey(), 4)
     self.assertEqual(g0.e_size(), 1)
Beispiel #27
0
 def __init__(self, g: DiGraph = DiGraph()):
     if g is None:
         self.graph = DiGraph()
     else:
         self.graph = g
Beispiel #28
0
        self.ga.graph = graph_2
        self.assertEqual([[1, 2, 4, 3, 5, 0], [6]], self.ga.connected_components())
        self.ga.graph = graph_3
        self.assertEqual([[8, 9, 7], [5], [6], [2, 1, 0], [4, 3]], self.ga.connected_components())

    def test_plot_graph(self):
        # plot graph without positions
        self.ga.graph = graph_2
        self.ga.plot_graph()
        # plot graph with position
        self.ga.graph = graph_3
        self.ga.plot_graph()


# graph creator, |V|=7, |E|=19
graph_2 = DiGraph()
for i in range(7):
    graph_2.add_node(i)
graph_2.add_edge(0, 1, 1)
graph_2.add_edge(0, 2, 1)
graph_2.add_edge(0, 3, 1)
graph_2.add_edge(0, 4, 1)
graph_2.add_edge(0, 5, 1)
graph_2.add_edge(0, 6, 1)
graph_2.add_edge(1, 0, 8)
graph_2.add_edge(1, 2, 1)
graph_2.add_edge(1, 6, 9)
graph_2.add_edge(2, 1, 1)
graph_2.add_edge(2, 4, 1)
graph_2.add_edge(3, 4, 1)
graph_2.add_edge(3, 5, 1)