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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:12:56+00:00 2026-06-14T02:12:56+00:00

I can’t figure out why my implementation of Reverse Cuthill–McKee algorithm is returning at

  • 0

I can’t figure out why my implementation of Reverse Cuthill–McKee algorithm is returning
at the wrong place.
Basically, it produces the correct result, but when I run the code, it returns a different value.

Here is a full code with a graph, could some one please explain this behavior?

import numpy as np

A=np.diag(np.ones(8))

nzc=[[4],[2,5,7],[1,4],[6],[0,2],[1,7],[3],[1,5]]

for i in range(len(nzc)):
    for j in nzc[i]:
        A[i,j]=1

# define the Result queue
R = ["C"]*A.shape[0]


def getDegree(Graph):
    """
    find the degree of each node. That is the number
    of neighbours or connections.
    (number of non-zero elements) in each row minus 1.
    Graph is a Cubic Matrix.
    """
    degree = [0]*Graph.shape[0]
    for row in range(Graph.shape[0]):
        degree[row] = len(np.flatnonzero(Graph[row]))-1
    return degree
    
# find the starting point, the parent
degree = getDegree(A)

adj = getAdjcncy(A)
degree2=degree[::]
adj2 = adj[::]
digar=np.array(degree2)
pivots = list(np.where(digar == digar.min())[0])

def main_loop2(deg,start, adj,R):
    degcp=deg[:]
    digar=np.array(deg)
    # use np.where here to get indecies of minimums
    if start not in R:
        R.append(start)
        degcp.pop(start)
        if start in pivots:
           pivots.pop(start)
           print "p: ", pivots
    Q=adj[start]
    for idx, item in enumerate(Q):
        if item not in R:
            R.append(item)
            print "R", R
    print "starts", adj[R[-1]]
    Q=adj[R[-1]]
    if set(Q).issubset(set(R)) and len(R) < len(degcp) :
         print "will now look for new pivot"
         p = pivots[0]
         pivots.pop(0)
         print "pivots", pivots
         print "starting with" , p
         main_loop2(deg,p,adj,R)
         return 'this is never reached'
    elif len(R) < len(degcp):
         main_loop2(deg,R[-1],adj,R)
         return 'wrong'
    else:
         print "will return R"
         print type(R)
         return R      
    
inl=[]
Res = main_loop2(degree2,0, adj,inl)

print(Res)

output:

Degrees: [1, 3, 2, 1, 2, 2, 1, 2]
0
p:  [3, 6]
R [0, 4]
starts [0, 2]
R  2 degcp 7
R [0, 4, 2]
starts [1, 4]
R  3 degcp 8
R [0, 4, 2, 1]
starts [2, 5, 7]
R  4 degcp 8
R [0, 4, 2, 1, 5]
R [0, 4, 2, 1, 5, 7]
starts [1, 5]
will now look for new pivot
pivots [6]
starting with 3
R [0, 4, 2, 1, 5, 7, 3, 6]
starts [3]
will return R
<type 'list'>
wrong

So the question is:
Why is the function working OK at all cycles of the recursion, but in the last one,
it returns the value wrong?
The output shows that the last else is only reached once, yet, a list is not returned, and I am quite helpless here.
I would appreciate if someone shed some light on this.

  • 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-06-14T02:12:57+00:00Added an answer on June 14, 2026 at 2:12 am

    The top level of your recursive call goes into here:

    elif len(R) < len(degcp):
         main_loop2(deg,R[-1],adj,R)
         return 'wrong'
    

    Whatever happens further inside the recursive call to main_loop2 doesn’t matter; when it comes back out, it will execute the return 'wrong' statement. You need to actually return the value the innermost call produces, all the way back out to the top level – it’s not going to magically happen for you.

    Did you mean to return main_loop2(...)?

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

Sidebar

Related Questions

Can't seem to figure out what's wrong with the simple getJSON call below. It's
Can't figure out how to do this in a pretty way : I have
Can someone please help me figure out why IE9 won't load my google map
Can anyone please explain to me how to figure out the type of the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can anyone help me as what is going wrong here? Am not able to
Can any one help me in sorting this out in sed/awk/perl Input file Start
Can anyone help me trying to find out why this doesn't work. The brushes
Can any one tell me why AES encryption Algorithm is more efficient to use
Can anyone let me know how can we change the value of kendo combobox

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.