예제 #1
0
    def test_shortest_path(self):
        graph = Graph()
        verticies = ["1", "2", "3", "4", "5"]
        edges = [
            ("1", "2"),
            ("1", "4"),
            ("2", "3"),
            ("2", "4"),
            ("2", "5"),
            ("3", "5"),
        ]

        # add verticies to path
        for vertex in verticies:
            graph.add_vertex(vertex)

        # add edges to graph
        for tuple_edge in edges:
            graph.add_edge(tuple_edge[0], tuple_edge[1])

        from_vertex = "1"
        to_vertex = "5"

        output = graph.breadth_first_search(from_vertex, to_vertex)
        expected_output = {'2': '1', '4': '1', '3': '2', '5': '2'}

        # output is used to calculate the shortest path
        self.assertEqual(output, expected_output)
    def new(cls, bot, update, args):

        param1 = args[0]
        # param2 = args[1]
        # user = User(chat=update.message.chat_id, screen_name='{}'.format(text),
        #             friends='', tweet_id='')

        api = cls.auth()

        user_info1 = api.GetUser(screen_name=param1)
        # user_info2 = api.GetUser(screen_name=param2)
        friends1 = api.GetFriends(screen_name=param1)
        # friends2 = api.GetFriends(screen_name=param2)
        # print([s.text for s in statuses])

        graph = Graph()
        graph2 = Graph()
        # db.session.add(user)
        # db.session.commit()

        user_ids = []
        for s in islice(friends1, 5):
            current_friend_id = s.AsDict().get('id')
            print(current_friend_id)

            graph.add_vertex(current_friend_id)
            graph.add_edge({user_info1.AsDict().get('id'), current_friend_id})

            # friends_of_current_friend = api.GetFriends(screen_name=s.AsDict().get('screen_name'))
            # for s in friends_of_current_friend:
            #     graph.add_edge({
            #         current_friend_id,
            #         s.AsDict().get('id')
            #     })
            # user_id = s.AsDict().get('id')
            # user_ids.append(user_id)

        # for s in friends2:
        #     graph.add_vertex(s.AsDict().get('id'))
        #     graph2.add_edge({
        #             user_info2.AsDict().get('id'),
        #             s.AsDict().get('id')
        #         })
        # user_id = s.AsDict().get('id')
        # user_ids.append(user_id)

        # for user_id in user_ids:
        #     current = api.GetUser(user_id=user_id)

        print(graph.edges())
        print(
            '\n ---------------------------------------------------------------------------------------- \n'
        )
        print(graph2.edges())

        bot.send_message(chat_id=update.message.chat_id,
                         text="abigo stoaki:" + str())
예제 #3
0
    def test_add_edge(self):
        vertex_a = "A"
        vertex_b = "B"

        graph = Graph()
        graph.add_vertex(vertex_a)
        graph.add_vertex(vertex_b)
        graph.add_edge(vertex_a, vertex_b)
        self.assertEqual(graph.num_edges, 1)

        vertex_a_obj = graph.vert_dict[vertex_a]
        vertex_b_obj = graph.vert_dict[vertex_b]

        # check if vertex_a & vertex_b are neighbours of each other.
        self.assertIn(vertex_a_obj, vertex_b_obj.neighbours)
        self.assertIn(vertex_b_obj, vertex_a_obj.neighbours)
예제 #4
0
    def test_eulerian(self):
        """
            Creates an undirected graph thatdoes contain a Eulerian
            cycle.  
        """

        vertex_a = "A"
        vertex_b = "B"
        vertex_c = "C"
        vertex_d = "D"
        vertex_e = "E"

        graph = Graph()
        graph.add_vertex(vertex_a)
        graph.add_vertex(vertex_b)
        graph.add_vertex(vertex_c)
        graph.add_vertex(vertex_d)
        graph.add_vertex(vertex_e)
        graph.add_edge(vertex_a, vertex_b)
        graph.add_edge(vertex_a, vertex_e)
        graph.add_edge(vertex_b, vertex_c)
        graph.add_edge(vertex_c, vertex_d)
        graph.add_edge(vertex_d, vertex_e)

        self.assertEqual(graph.is_eulerian(), True)