#10. Basics of Graph
Basics of Graph
Description
In this assignment, you will model a simple social network using a Graph. The network consists of users, represented as nodes, and friendships, represented as undirected edges. You will implement a system that processes a series of commands to build and query this social network.
The social network will be represented using an Adjacency List. An adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a vertex in the graph. For U users, you can think of this as an array of lists, where the i-th list contains all the users who are friends with user i.
You will process a sequence of commands, each representing one of the following operations:
add u v: Add a friendship between useruand userv. Since friendships are mutual, this is an undirected edge.degree u: Count the total number of friends useruhas.isfriend u v: Check if useruand uservare friends.count_greater x: Count how many users in the network have strictly more thanxfriends.
Format
Input
- The first line contains two integers,
UandN, whereU() is the number of users andN() is the number of operations. Users are identified by integers from0toU-1. - The next
Nlines each contain an operation. The operation will be one of the following:add u v: The stringaddfollowed by two space-separated integersuandv().degree u: The stringdegreefollowed by a single space and an integeru().isfriend u v: The stringisfriendfollowed by two space-separated integersuandv().count_greater x: The stringcount_greaterfollowed by a single space and an integerx().
Output
- For each
degreeoperation, print the integer result on a new line. - For each
isfriendoperation, printyesornoon a new line. - For each
count_greateroperation, print the integer count on a new line. - The
addoperation should not produce any output.
Samples
6 8
add 0 1
add 0 2
add 1 2
add 1 3
add 4 5
isfriend 0 1
degree 1
count_greater 1
yes
3
3
Explanation:
- After the
addoperations, the friendships are: (0,1), (0,2), (1,2), (1,3), (4,5). - The number of friends (degree) for each user is:
degree(0)= 2degree(1)= 3degree(2)= 2degree(3)= 1degree(4)= 1degree(5)= 1
isfriend 0 1: Users 0 and 1 are directly connected. Output:yes.degree 1: User 1 has 3 friends (0, 2, and 3). Output:3.count_greater 1: We need to count users with more than 1 friend. These are user 0 (2 friends), user 1 (3 friends), and user 2 (2 friends). The total count is 3. Output:3.
10 10
add 0 1
add 1 2
add 2 3
add 1 4
degree 1
degree 5
isfriend 1 3
isfriend 0 2
count_greater 0
count_greater 2
4
0
no
no
5
1
Explanation:
- After the
addoperations, the friendships are: (0,1), (1,2), (2,3), (1,4). - The degrees are:
deg(0)=1,deg(1)=4,deg(2)=2,deg(3)=1,deg(4)=1. All other users have 0 friends. degree 1: User 1 is friends with 0, 2, 3, and 4. Output:4.degree 5: User 5 has no friends. Output:0.isfriend 1 3: User 1 and 3 are not directly connected. Output:no.isfriend 0 2: User 0 and 2 are not directly connected. Output:no.count_greater 0: Users 0, 1, 2, 3, 4 all have more than 0 friends. Count is 5. Output:5.count_greater 2: Only user 1 (degree 4) has a degree strictly greater than 2. Count is 1. Output:1.
Hint
- An adjacency list is a good way to store the graph. You can use a
vectorofvectors, or amapwhere keys are user IDs and values are lists of their friends. - Remember that the graph is undirected. When you
add u v, you must update the adjacency lists for bothuandv. - For the
degreeoperation, the size of a user's adjacency list is their degree. - For the
isfriendoperation, you can check ifvis inu's adjacency list. A simple loop through the list is sufficient. - For the
count_greateroperation, you will need to iterate through allUusers, find the degree of each, and count how many have a degree greater thanx.
Limitation
1s, 256MiB for each test case.