Exemplo n.º 1
0
    def test_graph(self):
        """
        1          5
        | \      /  \
        |  3 -- 4   6
        | /     \  /
        2        7
        :return:
        """
        g = nx.Graph()
        g.add_nodes_from([1, 2, 3, 4, 5, 6, 7])
        g.add_edges_from([(1, 2), (2, 3), (3, 1), (3, 4), (4, 5), (5, 6),
                          (6, 7), (7, 4)])

        g.node[1]['infected'] = True
        g.node[2]['infected'] = True
        g.node[3]['infected'] = True
        g.node[4]['infected'] = True
        g.node[5]['infected'] = False
        g.node[6]['infected'] = False
        g.node[7]['infected'] = False

        g_i = nx.Graph()
        g_i.add_nodes_from([1, 2, 3, 4])
        g_i.add_edges_from([(1, 2), (2, 3), (3, 1), (3, 4)])

        sz = AlgorithmSZ()
        source_estimation = sz.run(g, g_i)
        self.assertEqual(source_estimation, 3)
        print("Source of rumor is %s" % source_estimation)
Exemplo n.º 2
0
    def test_graph(self):
        """
        1          5
        | \      /  \
        |  3 -- 4   6
        | /     \  /
        2        7
        :return:
        """
        g = nx.Graph()
        g.add_nodes_from([1, 2, 3, 4, 5, 6, 7])
        g.add_edges_from([(1, 2), (2, 3), (3, 1), (3, 4), (4,5), (5, 6), (6, 7), (7, 4)])

        g.node[1]['infected'] = True
        g.node[2]['infected'] = True
        g.node[3]['infected'] = True
        g.node[4]['infected'] = True
        g.node[5]['infected'] = False
        g.node[6]['infected'] = False
        g.node[7]['infected'] = False

        sz = AlgorithmSZ()
        source_estimation = sz.run(g, v=4)
        self.assertEqual(source_estimation, 3)
        print("Source of rumor is %s" % source_estimation)
Exemplo n.º 3
0
    def test_less_elementary(self):
        """
              1
             / \
            2  3
           / \
          4  5
        :return:
        """
        g = nx.Graph()
        g.add_nodes_from([1, 2, 3, 4, 5])
        g.add_edges_from([(1, 2), (1, 3), (2, 4), (2, 5)])

        g.node[1]['infected'] = False
        g.node[3]['infected'] = False
        g.node[4]['infected'] = True
        g.node[5]['infected'] = True
        g.node[2]['infected'] = True

        g_i = nx.Graph()
        g_i.add_nodes_from([2, 4, 5])
        g_i.add_edges_from([(2, 4), (2, 5)])

        sz = AlgorithmSZ()
        source_estimation = sz.run(g, g_i)
        self.assertEqual(source_estimation, 2)
        print("Source of rumor is %s" % source_estimation)
Exemplo n.º 4
0
    def test_elementary(self):
        """
            1
          /  \
         2   3
        :return:
        """
        g = nx.Graph()
        g.add_nodes_from([1, 2, 3])
        g.add_edges_from([(1, 2), (1, 3)])  # FIXME do you need to add also (2, 1) and (3, 1) ?

        g.node[1]['infected'] = True
        g.node[2]['infected'] = True
        g.node[3]['infected'] = True

        sz = AlgorithmSZ()
        # Test various v options (root of the spanning tree)
        source_estimation = sz.run(g, v=1)
        self.assertEqual(source_estimation, 1)
        source_estimation2 = sz.run(g, v=2)
        self.assertEqual(source_estimation2, 1)
        source_estimation3 = sz.run(g, v=3)
        self.assertEqual(source_estimation3, 1)

        print("Source of rumor is %s" % source_estimation)
Exemplo n.º 5
0
    def test_less_elementary(self):
        """
              1
             / \
            2  3
           / \
          4  5
        :return:
        """
        g = nx.Graph()
        g.add_nodes_from([1, 2, 3, 4, 5])
        g.add_edges_from([(1, 2), (1, 3), (2, 4), (2, 5)])

        g.node[1]['infected'] = False
        g.node[3]['infected'] = False
        g.node[4]['infected'] = True
        g.node[5]['infected'] = True
        g.node[2]['infected'] = True

        sz = AlgorithmSZ()
        source_estimation = sz.run(g, v=2)
        self.assertEqual(source_estimation, 2)
        source_estimation2 = sz.run(g, v=4)
        self.assertEqual(source_estimation2, 2)
        source_estimation3 = sz.run(g, v=5)
        self.assertEqual(source_estimation3, 2)
        print("Source of rumor is %s" % source_estimation)
Exemplo n.º 6
0
    def test_wheel_graph(self):
        g = nx.wheel_graph(10)
        nx.set_node_attributes(g, 'infected', {n: False for n in g.nodes()})
        g.node[0]['infected'] = True
        g.node[1]['infected'] = True
        g.node[5]['infected'] = True

        sz = AlgorithmSZ()
        source_estimation = sz.run(g, g)
        print("Source of rumor is %s" % source_estimation)
Exemplo n.º 7
0
    def test_wheel_graph(self):
        g = nx.wheel_graph(10)
        nx.set_node_attributes(g, 'infected', {n: False for n in g.nodes()})
        g.node[0]['infected'] = True
        g.node[1]['infected'] = True
        g.node[5]['infected'] = True

        sz = AlgorithmSZ()
        source_estimation = sz.run(g, v=1)
        print("Source of rumor is %s" % source_estimation)
Exemplo n.º 8
0
    def test_other_graph(self):
        g = nx.chvatal_graph()
        nx.set_node_attributes(g, 'infected', {n: False for n in g.nodes()})
        g.node[0]['infected'] = True

        g_i = nx.Graph()
        g_i.add_nodes_from([0])

        sz = AlgorithmSZ()
        source_estimation = sz.run(g, g_i)
        print("Source of rumor is %s" % source_estimation)
Exemplo n.º 9
0
    def test_other_graph(self):
        g = nx.chvatal_graph()
        nx.set_node_attributes(g, 'infected', {n: False for n in g.nodes()})
        g.node[0]['infected'] = True

        sz = AlgorithmSZ()
        source_estimation = sz.run(g, v=0)
        print("Source of rumor is %s" % source_estimation)

        nx.draw_networkx(g, node_color=['b' if g.node[n]['infected'] else 'r' for n in g])
        plt.show()
Exemplo n.º 10
0
    def test_other_graph(self):
        g = nx.chvatal_graph()
        nx.set_node_attributes(g, 'infected', {n: False for n in g.nodes()})
        g.node[0]['infected'] = True

        g_i = nx.Graph()
        g_i.add_nodes_from([0])

        sz = AlgorithmSZ()
        source_estimation = sz.run(g, g_i)
        print("Source of rumor is %s" % source_estimation)
Exemplo n.º 11
0
    def test_random_graph(self):
        g = nx.fast_gnp_random_graph(10, 0.5)
        nx.set_node_attributes(g, 'infected', {n: False for n in g.nodes()})
        g.node[1]['infected'] = True
        g.node[2]['infected'] = True
        g.node[3]['infected'] = True
        g.node[4]['infected'] = True

        # FIXME Cannot assert anything because graph is random !
        sz = AlgorithmSZ()
        source_estimation = sz.run(g, g)
        print("Source of rumor is %s" % source_estimation)
Exemplo n.º 12
0
    def test_random_graph(self):
        g = nx.fast_gnp_random_graph(10, 0.5)
        nx.set_node_attributes(g, 'infected', {n: False for n in g.nodes()})
        g.node[1]['infected'] = True
        g.node[2]['infected'] = True
        g.node[3]['infected'] = True
        g.node[4]['infected'] = True

        # FIXME Cannot assert anything because graph is random !
        sz = AlgorithmSZ()
        source_estimation = sz.run(g, v=4)
        print("Source of rumor is %s" % source_estimation)
Exemplo n.º 13
0
    def test_elementary(self):
        """
            1
          /  \
         2   3
        :return:
        """
        g = nx.Graph()
        g.add_nodes_from([1, 2, 3])
        g.add_edges_from([(1, 2), (1, 3)])  # FIXME do you need to add also (2, 1) and (3, 1) ?

        g.node[1]['infected'] = True
        g.node[2]['infected'] = True
        g.node[3]['infected'] = True

        sz = AlgorithmSZ()
        source_estimation = sz.run(g, g)
        self.assertEqual(source_estimation, 1)

        print("Source of rumor is %s" % source_estimation)
Exemplo n.º 14
0
    def test_elementary(self):
        """
            1
          /  \
         2   3
        :return:
        """
        g = nx.Graph()
        g.add_nodes_from([1, 2, 3])
        g.add_edges_from([
            (1, 2), (1, 3)
        ])  # FIXME do you need to add also (2, 1) and (3, 1) ?

        g.node[1]['infected'] = True
        g.node[2]['infected'] = True
        g.node[3]['infected'] = True

        sz = AlgorithmSZ()
        source_estimation = sz.run(g, g)
        self.assertEqual(source_estimation, 1)

        print("Source of rumor is %s" % source_estimation)