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 6172157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:20:36+00:00 2026-05-23T23:20:36+00:00

I need to calculate something whose value is given by the following inneficient pseudo

  • 0

I need to calculate something whose value is given by the following inneficient pseudo python code:

def foo(a,b):
   tmp = 0
   for i in graph.nodes():
       for j in graph.nodes():
          tmp += c
          if (i and j are neighbors):
              tmp += a - c
          elif (i and j are next-neighbors):
              tmp += b - c
   return tmp

In other words, given all the pairs of nodes, foo = a * (E = number of edges) + b * (EE = number of pairs who are not neighbors but have a common neighbor) + c * ( N(N-1)/2 – EE – E).

I tried to think on some kind of breadth first search but I couldn’t.

EDIT: more info

  • the graph is not static. I constantly add and remove edges, so I can’t calculate this only once. I must constantly update the “list of next neighbors”.
  • I store the graph as an adjacency matrix.
  • 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-23T23:20:37+00:00Added an answer on May 23, 2026 at 11:20 pm

    Here’s a rough idea. But first, some assumptions: 1. undirected graph 2. constant vertex-count

    Your graph changes constantly. So you need to keep the number of neighbours (edges) and second-neighbours updated efficiently. The first is trivial, so let’s look at second neighbours.

    Per @Patrick’s suggestion, let’s work with A^2, and let’s see how it can be efficiently updated without recomputing it from scratch every time. A^2_ij is the number of length-two paths from i to j, just keep in mind that it’ll have diagonal entries as well because there’s always a length-two path from a vertex to itself. If you have A^2, you can easily count the number of second-neighbours, so let’s keep all of A^2 updated in memory while the graph changes.

    How to update A^2 efficiently:

    When you add/remove an edge, you change A by a matrix B that has only two entries. Suppose we’re adding (not removing) an edge. Then (A+B)^2 = A^2 + AB + BA + B^2.

    Assume the edge added was (i,j).

    • B^2 is trivial: (B^2)_ii = (B^2)_jj = 1, the rest is 0.
    • AB = transpose(BA) so we only need to compute one of the two, say BA. It turns out that row i of BA is row j of A and row j of BA is row i of A, the rest is zero. So again, it’s fast to compute.

    Removing edges will be similar.

    You only need the count of second-neighbours, so instead of counting how many non-zero non-diagonal entries A^2 has at each step, with some extra work you can count by how much the number of non-zero entries changes in A^2 when you add AB + BA.

    EDIT

    This whole thing explained in a different language:

    Let’s keep track of the number of length-two paths between any two vertices in a matrix M. When we add (remove) an edge between i and j, the number of length-two paths will increase (decrease) by one between i and all neighbours of j as well as j and all neighbours of i, so M is easy to update in O(n) time after every change to the graph.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to calculate the time complexity of the following code: for (i =
I need to calculate the distance (in meters and miles) between two coordinates given
I need to calculate word frequencies of a given set of adjectives in a
I'm stuck in a problem in which I need to calculate something like: ((500!)/(20!x20!x20!x20!...))
I need to calculate Math.exp() from java very frequently, is it possible to get
I need to calculate averages, standard deviations, medians etc for a bunch of numerical
I need to calculate the number of FULL month in SQL, i.e. 2009-04-16 to
I need to calculate date (year, month, day) which is (for example) 18 working
I need to calculate the within and between run variances from some data as
I need to calculate the quantity of nights (stay at a hotel) from the

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.