Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6128097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:35:17+00:00 2026-05-23T16:35:17+00:00

Is there an efficient way to calculate the matrix score for common neighbors(CC) and

  • 0

Is there an efficient way to calculate the matrix score for common neighbors(CC) and preferential attachment(PA) in python? I’m using igraph to calculate score matrixes for other methods such as jaccard’s coefficient (Graph.similarity_jaccard()), dice (Graph.similarity_dice) and adamic/adar (Graph.similarity_inverse_log_weighted()), but I haven’t found any function to compute score matrixes for CC and PA.

Currently I’m doing:

#Preferential attachment score between nodes i and j in a graph g
len(g.neighbors(i))*len(g.neighbors(j))

#Common neighbors score between nodes i and j in a graph g
len(g.neighbors(i) and g.neighbors(j))

but I have to do this for all edges(i,j) in the network which in my case is really large.

If anyone knows any mathematical matrix operation that generates such score matrixes I’m looking for, I would appreciate as well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:35:17+00:00Added an answer on May 23, 2026 at 4:35 pm

    There is no direct function for this in igraph. However, note that len(g.neighbors(i)) is simply the degree of vertex i, so you can call g.degree(), treat it as a 1D matrix using NumPy, then multiply it with its own transpose s.t. a column vector is multiplied by a row vector from the right. This gives you the preferential attachment score matrix:

    from numpy import matrix
    d = matrix(g.degree())
    pref_score_matrix = d.T*d
    

    The common neighbors score is trickier using matrix notation, and I wouldn’t operate with matrices anyway if your graph is really large (even with only 2000 vertices, you would have a matrix with 4 million elements). I would simply cache set representations of g.neighbors(i) for all possible values in a list, and then use that to calculate the common neighbor scores as sets can be intersected efficiently:

    neisets = [set(g.neighbors(i)) for i in xrange(g.vcount())]
    for v1, v2 in g.get_edgelist():
        common_neis = neisets[v1].intersection(neisets[v2])
        print "%d --> %d: %d" % (v1, v2, len(common_neis))
    

    If you really want to stick to matrices, you can construct an n by n matrix consisting of zeros only using the zeros function from NumPy and then loop over the vertices once. Get all the neighbors of vertex v and note that any pair of neighbors of v have one neighbor in common: v itself:

    from itertools import combinations
    from numpy import zeros
    
    n = g.vcount()
    common_neis = zeros(n, n)
    for v in xrange(g.vcount()):
        neis = g.neighbors(v)
        for u, w in combinations(neis, 2):
            # v is a common neighbor of u and w
            common_neis[u, w] += 1
            common_neis[w, u] += 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have many 100x100 grids, is there an efficient way using numpy to calculate
Is there a more efficient way to clamp real numbers than using if statements
Is there an efficient way to version store procedures written in PL/SQL? (I only
Is there an efficient way to clone an object yet leave out specified properties?
Is there a more efficient way to list files from a bucket in Amazon
Is there are more efficient way than the following for selecting the third parent?
I'm curious if there is an efficient way to wait for the front page
If I have a standard for loop is there a more efficient way to
I'm wondering if there's a super-efficient way of confirming that an Image object references
Is there any way we could do efficient paging with petapoco and mvc3. At

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.