Skip to content
DeepForgeby svx
All problems
EasyGraph Algorithmsgraph-100

Common Neighbors Count

Problem

Count the common neighbors of nodes u and v in an undirected graph with n nodes.

Return the size of the intersection of their neighbor sets. Self-loops, if present, are ignored. edges is a list of [u, v] pairs.

Hint

Common neighbors are the classic simplest link prediction feature.

Test cases

case 1
in: [3,[[0,1],[1,2]],0,2]
expected: 1
case 2
in: [4,[[0,1],[1,2],[2,3],[3,0]],0,1]
expected: 0
case 3
in: [4,[[0,1],[0,2],[0,3]],1,2]
expected: 1
case 4
in: [4,[[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]],0,1]
expected: 2
Starter codePython
def common_neighbors(n, edges, u, v):
    # Your code here
    pass

4 test cases · run your code against them in the editor