timeit

Програмиране с Python

Курс във Факултета по Математика и Информатика към СУ

Решение на Социална мрежа от Ивета Чампоева

Обратно към всички решения

Към профила на Ивета Чампоева

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 8 успешни тест(а)
  • 0 неуспешни тест(а)

Код

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import uuid
import datetime
from collections import deque
import math


class UserAlreadyExistsError(Exception):
    pass


class UserDoesNotExistError(Exception):
    pass


class UserNotConnectedError(Exception):
    pass


class Post:
    def __init__(self, author, content):
        self.author = author
        self.published_at = datetime.datetime.now()
        self.content = content

    def __lt__(self, post):
        return self.published_at < post.published_at


class User:
    def __init__(self, full_name):
        self.full_name = full_name
        self.uuid = uuid.uuid4()
        self.posts = deque([], 50)

    def add_post(self, post_content):
        post = Post(self.uuid, post_content)
        self.posts.append(post)

    def get_post(self):
        for post in self.posts:
            yield post


class SocialGraph:
    def __init__(self):
        self.__graph = {}

    def check_existance(func):
        def does_it_exist(self, *user_uuid):
            if any([user not in self.__graph.keys() for user in user_uuid
                    if isinstance(user, uuid.UUID)]):
                raise UserDoesNotExistError
            else:
                return func(self, *user_uuid)
        return does_it_exist

    def add_user(self, user):
        if user.uuid not in self.__graph.keys():
            self.__graph[user.uuid] = [user]
        else:
            raise UserAlreadyExistsError

    @check_existance
    def get_user(self, user_uuid):
        return self.__graph[user_uuid][0]

    @check_existance
    def delete_user(self, user_uuid):
        for user in self.followers(user_uuid):
            self.unfollow(user, user_uuid)
        self.__graph.pop(user_uuid)

    @check_existance
    def follow(self, follower, followee):
        if all([followee not in self.__graph[follower],
               followee is not follower]):
            self.__graph[follower] += [followee]

    @check_existance
    def unfollow(self, follower, followee):
        if followee in self.__graph[follower]:
            self.__graph[follower].remove(followee)

    @check_existance
    def is_following(self, follower, followee):
        return followee in self.__graph[follower]

    @check_existance
    def followers(self, user_uuid):
        following_users = [uuid for uuid, following in
                           self.__graph.items() if user_uuid in following]
        return set(following_users)

    @check_existance
    def following(self, user_uuid):
        return set(self.__graph[user_uuid][1:])

    @check_existance
    def friends(self, user_uuid):
        social_friends = [person for person in self.__graph[user_uuid][1:]
                          if user_uuid in self.following(person)]
        return set(social_friends)

    def shortest_path(self, start_uuid, end_uuid, path=[]):
        path = path + [start_uuid]
        if end_uuid in self.__graph[start_uuid]:
            return len(path)
        newpath = [self.shortest_path(user, end_uuid, path)
                   for user in self.__graph[start_uuid][1:]
                   if user not in path and
                   self.shortest_path(user, end_uuid, path)]
        if newpath:
            return min(newpath)
        return 0

    @check_existance
    def min_distance(self, start_uuid, end_uuid):
        if not self.following(start_uuid):
            return math.inf
        shortest = self.shortest_path(start_uuid, end_uuid)
        if shortest:
            return shortest
        raise UserNotConnectedError

    @check_existance
    def generate_feed(self, user_uuid, offset=0, limit=10):
        return sorted([post for user in self.__graph[user_uuid][1:]
                       for post in self.get_user(user).posts],
                      reverse=True)[offset:offset + limit]

    def longest_path(self, user_uuid, path=[], depth=0):
        path.append(user_uuid)
        if all([user in path for user in self.__graph[user_uuid][1:]]) or\
                not self.following(user_uuid):
            return depth
        path = path + [user for user in self.__graph[user_uuid][1:]
                       if user not in path]
        longest = [self.longest_path(user, path, depth + 1)
                   for user in self.__graph[user_uuid][1:]]
        return max(longest)

    @check_existance
    def max_distance(self, user_uuid):
        if not self.following(user_uuid):
            return math.inf
        return self.longest_path(user_uuid)

    def nth_layer_list(self, user_uuid, n, path=[], depth=0):
        path.append(user_uuid)
        if depth == n:
            return user_uuid
        nth_followees = []
        for user in self.__graph[user_uuid][1:]:
            if user not in path:
                followee = self.nth_layer_list(user, n, path, depth+1)
                if followee and isinstance(followee, list):
                    nth_followees = nth_followees + followee
                elif followee:
                    nth_followees.append(followee)
        return nth_followees

    @check_existance
    def nth_layer_followings(self, user_uuid, n):
        if n == 0:
            return []
        if not self.following(user_uuid):
            return math.inf
        return self.nth_layer_list(user_uuid, n)

Лог от изпълнението

........
----------------------------------------------------------------------
Ran 8 tests in 0.065s

OK

История (4 версии и 1 коментар)

Ивета обнови решението на 14.04.2016 23:58 (преди над 1 година)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import uuid
import datetime


class UserAlreadyExistsError(Exception):
    pass


class UserDoesNotExistError(Exception):
    pass


class UserNotConnectedError(Exception):
    pass


class Post:
    def __init__(self, author, content):
        self.author = author
        self.published_at = datetime.datetime.now()
        self.content = content

    def __lt__(self, post):
        return self.published_at < post.published_at


class User:
    def __init__(self, full_name):
        self.full_name = full_name
        self.uuid = uuid.uuid4()
        self.__number_of_posts = 0
        self.posts = []

    def add_post(self, post_content):
        post = Post(self.uuid, post_content)
        if self.__number_of_posts < 50:
            self.__number_of_posts += 1
        else:
            self.posts.pop(0)
        self.posts.append(post)

    def get_post(self):
        for post in self.posts:
            yield post


class SocialGraph:
    def __init__(self):
        self.__graph = {}

    def add_user(self, user):
        if user.uuid not in self.__graph.keys():
            self.__graph[user.uuid] = [user]
        else:
            raise UserAlreadyExistsError

    def get_user(self, user_uuid):
        try:
            return self.__graph[user_uuid][0]
        except KeyError:
            raise UserDoesNotExistError

    def delete_user(self, user_uuid):
        try:
            self.__graph.pop(user_uuid)
        except KeyError:
            raise UserDoesNotExistError

    def follow(self, follower, followee):
        try:
            if followee not in self.__graph[follower]:
                self.__graph[follower] += [followee]
        except KeyError:
            raise UserDoesNotExistError

    def unfollow(self, follower, followee):
        try:
            if followee in self.__graph[follower]:
                self.__graph[follower].remove(followee)
        except KeyError:
            raise UserDoesNotExistError

    def is_following(self, follower, followee):
        try:
            return followee in self.__graph[follower]
        except KeyError:
            raise UserDoesNotExistError

    def followers(self, user_uuid):
        try:
            following_users = []
            for uuid, following in self.__graph.items():
                if user_uuid in following:
                    following_users += [uuid]
            return set(following_users)
        except KeyError:
            raise UserDoesNotExistError

    def following(self, user_uuid):
        try:
            return set(self.__graph[user_uuid][1:])
        except KeyError:
            raise UserDoesNotExistError

    def friends(self, user_uuid):
        try:
            social_friends = []
            for person in self.__graph[user_uuid][1:]:
                if user_uuid in self.following(person):
                    social_friends += [person]
            return set(social_friends)
        except KeyError:
            raise UserDoesNotExistError

    def shortest_path(self, start_uuid, end_uuid, path=[]):
        try:
            path = path + [start_uuid]
            if end_uuid in self.__graph[start_uuid]:
                return len(path)
            newpath = []
            for user in self.__graph[start_uuid][1:]:
                value = self.shortest_path(user, end_uuid, path)
                if value:
                    newpath.append(value)
            if newpath:
                return min(newpath)
            return 0
        except KeyError:
            raise UserDoesNotExistError

    def min_distance(self, start_uuid, end_uuid):
        shortest = self.shortest_path(start_uuid, end_uuid)
        if shortest:
            return shortest
        raise UserNotConnectedError

    def generate_feed(self, user_uuid, offset=0, limit=10):
        try:
            return sorted([post for user in self.__graph[user_uuid][1:]
                          for post in self.get_user(user).posts],
                          reverse=True)[offset:offset + limit]
        except KeyError:
            raise UserDoesNotExistError

    def max_distance(self, user_uuid, path=[], depth=0):
        try:
            if not path:
                path.append(user_uuid)
            if any([user in path for user in self.__graph[user_uuid][1:]]) or\
                    not self.following(user_uuid):
                return depth
            path = path + self.__graph[user_uuid][1:]
            longest = 0
            for user in self.__graph[user_uuid][1:]:
                newpath = self.max_distance(user, path, depth + 1)
                if newpath > longest:
                    longest = newpath
            return longest
        except KeyError:
            raise UserDoesNotExistError

    def nth_layer_followings(self, user_uuid, n, path=[], depth=-1):
        try:
            path += [user_uuid]
            depth += 1
            if depth == n:
                return user_uuid
            if not self.following(user_uuid):
                return None
            nth_followees = []
            for user in self.__graph[user_uuid][1:]:
                if user not in path:
                    followee = self.nth_layer_followings(user, n, path, depth)
                    if followee and isinstance(followee, list):
                        nth_followees += followee
                    elif followee:
                        nth_followees += [followee]
            if nth_followees:
                return nth_followees
            return None
        except KeyError:
            raise UserDoesNotExistError

Ивета обнови решението на 15.04.2016 00:18 (преди над 1 година)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import uuid
import datetime


class UserAlreadyExistsError(Exception):
    pass


class UserDoesNotExistError(Exception):
    pass


class UserNotConnectedError(Exception):
    pass


class Post:
    def __init__(self, author, content):
        self.author = author
        self.published_at = datetime.datetime.now()
        self.content = content

    def __lt__(self, post):
        return self.published_at < post.published_at


class User:
    def __init__(self, full_name):
        self.full_name = full_name
        self.uuid = uuid.uuid4()
        self.__number_of_posts = 0
        self.posts = []

    def add_post(self, post_content):
        post = Post(self.uuid, post_content)
        if self.__number_of_posts < 50:
            self.__number_of_posts += 1
        else:
            self.posts.pop(0)
        self.posts.append(post)

    def get_post(self):
        for post in self.posts:
            yield post


class SocialGraph:
    def __init__(self):
        self.__graph = {}

    def add_user(self, user):
        if user.uuid not in self.__graph.keys():
            self.__graph[user.uuid] = [user]
        else:
            raise UserAlreadyExistsError

    def get_user(self, user_uuid):
        try:
            return self.__graph[user_uuid][0]
        except KeyError:
            raise UserDoesNotExistError

    def delete_user(self, user_uuid):
        try:
            self.__graph.pop(user_uuid)
        except KeyError:
            raise UserDoesNotExistError

    def follow(self, follower, followee):
        try:
            if followee not in self.__graph[follower]:
                self.__graph[follower] += [followee]
        except KeyError:
            raise UserDoesNotExistError

    def unfollow(self, follower, followee):
        try:
            if followee in self.__graph[follower]:
                self.__graph[follower].remove(followee)
        except KeyError:
            raise UserDoesNotExistError

    def is_following(self, follower, followee):
        try:
            return followee in self.__graph[follower]
        except KeyError:
            raise UserDoesNotExistError

    def followers(self, user_uuid):
        try:
            following_users = [uuid for uuid, following in
                               self.__graph.items() if user_uuid in following]
            return set(following_users)
        except KeyError:
            raise UserDoesNotExistError

    def following(self, user_uuid):
        try:
            return set(self.__graph[user_uuid][1:])
        except KeyError:
            raise UserDoesNotExistError

    def friends(self, user_uuid):
        try:
            social_friends = [person for person in self.__graph[user_uuid][1:]
                              if user_uuid in self.following(person)]
            return set(social_friends)
        except KeyError:
            raise UserDoesNotExistError

    def shortest_path(self, start_uuid, end_uuid, path=[]):
        try:
            path = path + [start_uuid]
            if end_uuid in self.__graph[start_uuid]:
                return len(path)
            newpath = []
            for user in self.__graph[start_uuid][1:]:
                value = self.shortest_path(user, end_uuid, path)
                if value:
                    newpath.append(value)
            if newpath:
                return min(newpath)
            return 0
        except KeyError:
            raise UserDoesNotExistError

    def min_distance(self, start_uuid, end_uuid):
        shortest = self.shortest_path(start_uuid, end_uuid)
        if shortest:
            return shortest
        raise UserNotConnectedError

    def generate_feed(self, user_uuid, offset=0, limit=10):
        try:
            return sorted([post for user in self.__graph[user_uuid][1:]
                          for post in self.get_user(user).posts],
                          reverse=True)[offset:offset + limit]
        except KeyError:
            raise UserDoesNotExistError

    def max_distance(self, user_uuid, path=[], depth=0):
        try:
            if not path:
                path.append(user_uuid)
            if any([user in path for user in self.__graph[user_uuid][1:]]) or\
                    not self.following(user_uuid):
                return depth
            path = path + self.__graph[user_uuid][1:]
            longest = 0
            for user in self.__graph[user_uuid][1:]:
                newpath = self.max_distance(user, path, depth + 1)
                if newpath > longest:
                    longest = newpath
            return longest
        except KeyError:
            raise UserDoesNotExistError

    def nth_layer_followings(self, user_uuid, n, path=[], depth=-1):
        try:
            path += [user_uuid]
            depth += 1
            if depth == n:
                return user_uuid
            if not self.following(user_uuid):
                return None
            nth_followees = []
            for user in self.__graph[user_uuid][1:]:
                if user not in path:
                    followee = self.nth_layer_followings(user, n, path, depth)
                    if followee and isinstance(followee, list):
                        nth_followees += followee
                    elif followee:
                        nth_followees += [followee]
            return nth_followees
        except KeyError:
            raise UserDoesNotExistError

Ивета обнови решението на 17.04.2016 22:11 (преди над 1 година)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import uuid
import datetime
from collections import deque


class UserAlreadyExistsError(Exception):
    pass


class UserDoesNotExistError(Exception):
    pass


class UserNotConnectedError(Exception):
    pass


class Post:
    def __init__(self, author, content):
        self.author = author
        self.published_at = datetime.datetime.now()
        self.content = content

    def __lt__(self, post):
        return self.published_at < post.published_at


class User:
    def __init__(self, full_name):
        self.full_name = full_name
        self.uuid = uuid.uuid4()
        self.posts = deque([], 50)

    def add_post(self, post_content):
        post = Post(self.uuid, post_content)
        self.posts.append(post)

    def get_post(self):
        for post in self.posts:
            yield post


class SocialGraph:
    def __init__(self):
        self.__graph = {}

    def check_existance(func):
        def does_it_exist(self, *user_uuid):
            if any([user not in self.__graph.keys() for user in user_uuid
                    if isinstance(user, uuid.UUID)]):
                raise UserDoesNotExistError
            else:
                return func(self, *user_uuid)
        return does_it_exist

    def add_user(self, user):
        if user.uuid not in self.__graph.keys():
            self.__graph[user.uuid] = [user]
        else:
            raise UserAlreadyExistsError

    @check_existance
    def get_user(self, user_uuid):
        return self.__graph[user_uuid][0]

    @check_existance
    def delete_user(self, user_uuid):
        self.__graph.pop(user_uuid)

    @check_existance
    def follow(self, follower, followee):
        if followee not in self.__graph[follower]:
            self.__graph[follower] += [followee]

    @check_existance
    def unfollow(self, follower, followee):
        if followee in self.__graph[follower]:
            self.__graph[follower].remove(followee)

    @check_existance
    def is_following(self, follower, followee):
        return followee in self.__graph[follower]

    @check_existance
    def followers(self, user_uuid):
        following_users = [uuid for uuid, following in
                           self.__graph.items() if user_uuid in following]
        return set(following_users)

    @check_existance
    def following(self, user_uuid):
        return set(self.__graph[user_uuid][1:])

    @check_existance
    def friends(self, user_uuid):
        social_friends = [person for person in self.__graph[user_uuid][1:]
                          if user_uuid in self.following(person)]
        return set(social_friends)

    def shortest_path(self, start_uuid, end_uuid, path=[]):
        path = path + [start_uuid]
        if end_uuid in self.__graph[start_uuid]:
            return len(path)
        newpath = [self.shortest_path(user, end_uuid, path)
                   for user in self.__graph[start_uuid][1:]
                   if user not in path and
                   self.shortest_path(user, end_uuid, path)]
        if newpath:
            return min(newpath)
        return 0

    @check_existance
    def min_distance(self, start_uuid, end_uuid):
        shortest = self.shortest_path(start_uuid, end_uuid)
        if shortest:
            return shortest
        raise UserNotConnectedError

    @check_existance
    def generate_feed(self, user_uuid, offset=0, limit=10):
        return sorted([post for user in self.__graph[user_uuid][1:]
                       for post in self.get_user(user).posts],
                      reverse=True)[offset:offset + limit]

    @check_existance
    def max_distance(self, user_uuid, path=[], depth=0):
        if not path:
            path.append(user_uuid)
        if all([user in path for user in self.__graph[user_uuid][1:]]) or\
                not self.following(user_uuid):
            return depth
        path = path + [user for user in self.__graph[user_uuid][1:]
                       if user not in path]
        longest = [self.max_distance(user, path, depth + 1)
                   for user in self.__graph[user_uuid][1:]]
        return max(longest)

    @check_existance
    def nth_layer_followings(self, user_uuid, n, path=[], depth=0):
        if n == 0:
            return []
        path.append(user_uuid)
        if depth == n:
            return user_uuid
        if not self.following(user_uuid):
            return None
        nth_followees = []
        for user in self.__graph[user_uuid][1:]:
            if user not in path:
                followee = self.nth_layer_followings(user, n, path, depth+1)
                if followee and isinstance(followee, list):
                    nth_followees = nth_followees + followee
                elif followee:
                    nth_followees.append(followee)
        return nth_followees

Ивета обнови решението на 18.04.2016 16:53 (преди над 1 година)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import uuid
import datetime
from collections import deque
import math


class UserAlreadyExistsError(Exception):
    pass


class UserDoesNotExistError(Exception):
    pass


class UserNotConnectedError(Exception):
    pass


class Post:
    def __init__(self, author, content):
        self.author = author
        self.published_at = datetime.datetime.now()
        self.content = content

    def __lt__(self, post):
        return self.published_at < post.published_at


class User:
    def __init__(self, full_name):
        self.full_name = full_name
        self.uuid = uuid.uuid4()
        self.posts = deque([], 50)

    def add_post(self, post_content):
        post = Post(self.uuid, post_content)
        self.posts.append(post)

    def get_post(self):
        for post in self.posts:
            yield post


class SocialGraph:
    def __init__(self):
        self.__graph = {}

    def check_existance(func):
        def does_it_exist(self, *user_uuid):
            if any([user not in self.__graph.keys() for user in user_uuid
                    if isinstance(user, uuid.UUID)]):
                raise UserDoesNotExistError
            else:
                return func(self, *user_uuid)
        return does_it_exist

    def add_user(self, user):
        if user.uuid not in self.__graph.keys():
            self.__graph[user.uuid] = [user]
        else:
            raise UserAlreadyExistsError

    @check_existance
    def get_user(self, user_uuid):
        return self.__graph[user_uuid][0]

    @check_existance
    def delete_user(self, user_uuid):
        for user in self.followers(user_uuid):
            self.unfollow(user, user_uuid)
        self.__graph.pop(user_uuid)

    @check_existance
    def follow(self, follower, followee):
        if all([followee not in self.__graph[follower],
               followee is not follower]):
            self.__graph[follower] += [followee]

    @check_existance
    def unfollow(self, follower, followee):
        if followee in self.__graph[follower]:
            self.__graph[follower].remove(followee)

    @check_existance
    def is_following(self, follower, followee):
        return followee in self.__graph[follower]

    @check_existance
    def followers(self, user_uuid):
        following_users = [uuid for uuid, following in
                           self.__graph.items() if user_uuid in following]
        return set(following_users)

    @check_existance
    def following(self, user_uuid):
        return set(self.__graph[user_uuid][1:])

    @check_existance
    def friends(self, user_uuid):
        social_friends = [person for person in self.__graph[user_uuid][1:]
                          if user_uuid in self.following(person)]
        return set(social_friends)

    def shortest_path(self, start_uuid, end_uuid, path=[]):
        path = path + [start_uuid]
        if end_uuid in self.__graph[start_uuid]:
            return len(path)
        newpath = [self.shortest_path(user, end_uuid, path)
                   for user in self.__graph[start_uuid][1:]
                   if user not in path and
                   self.shortest_path(user, end_uuid, path)]
        if newpath:
            return min(newpath)
        return 0

    @check_existance
    def min_distance(self, start_uuid, end_uuid):
        if not self.following(start_uuid):
            return math.inf
        shortest = self.shortest_path(start_uuid, end_uuid)
        if shortest:
            return shortest
        raise UserNotConnectedError

    @check_existance
    def generate_feed(self, user_uuid, offset=0, limit=10):
        return sorted([post for user in self.__graph[user_uuid][1:]
                       for post in self.get_user(user).posts],
                      reverse=True)[offset:offset + limit]

    def longest_path(self, user_uuid, path=[], depth=0):
        path.append(user_uuid)
        if all([user in path for user in self.__graph[user_uuid][1:]]) or\
                not self.following(user_uuid):
            return depth
        path = path + [user for user in self.__graph[user_uuid][1:]
                       if user not in path]
        longest = [self.longest_path(user, path, depth + 1)
                   for user in self.__graph[user_uuid][1:]]
        return max(longest)

    @check_existance
    def max_distance(self, user_uuid):
        if not self.following(user_uuid):
            return math.inf
        return self.longest_path(user_uuid)

    def nth_layer_list(self, user_uuid, n, path=[], depth=0):
        path.append(user_uuid)
        if depth == n:
            return user_uuid
        nth_followees = []
        for user in self.__graph[user_uuid][1:]:
            if user not in path:
                followee = self.nth_layer_list(user, n, path, depth+1)
                if followee and isinstance(followee, list):
                    nth_followees = nth_followees + followee
                elif followee:
                    nth_followees.append(followee)
        return nth_followees

    @check_existance
    def nth_layer_followings(self, user_uuid, n):
        if n == 0:
            return []
        if not self.following(user_uuid):
            return math.inf
        return self.nth_layer_list(user_uuid, n)