1

I have written python code for doing random contranction algorithm, the problem I ran into is that the return of this function is None type. However, for each recurrence, I can print out the result. so I don't have a clue why this is happenning. also, it would be nice if you could point any improvements I can make to my codes to make it more efficient or friendly.
The input is:

[[1, 2, 3, 4, 7], [2, 1, 3, 4], [3, 1, 2, 4], [4, 1, 2, 3, 5], [5, 4, 6, 7, 8], [6, 5, 7, 8], [7, 1, 5, 6, 8], [8, 5, 6, 7]]

the output is None

def randomContractionA(adjacencylist):
    n=int(len(adjacencylist))
    print(adjacencylist)
    if n==2:
        return adjacencylist
    verticeChosenF=random.randint(0,n-1)
    verticeChosenS=random.randint(0,n-1)
    while verticeChosenF==verticeChosenS:
        verticeChosenS=random.randint(0,n-1)
    contractionedVertice=[]
    for vertice in adjacencylist[verticeChosenF][:]:
        contractionedVertice.append(vertice)
    for vertice in adjacencylist[verticeChosenS][1:]:
        if vertice in contractionedVertice:
            continue
        else:
            contractionedVertice.append(vertice)
    i=1
    for vertice in contractionedVertice[1:]:
        if vertice==contractionedVertice[0]:
            del contractionedVertice[i]
        i+=1
    del adjacencylist[verticeChosenF]
    if verticeChosenF<verticeChosenS:
        del adjacencylist[verticeChosenS-1]
    else:
        del adjacencylist[verticeChosenS]
    adjacencylist.append(contractionedVertice)
    randomContractionA(adjacencylist)
result1=randomContractionA(adjacencyList)
print(result1)
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
Jack Duan
  • 47
  • 5
  • 3
    Please fix the identation of your code. – Maurice Meyer Aug 06 '22 at 10:48
  • 2
    Don't *del* elements that you're iterating over. You'll end up in a whole heap of pain. Also, it would be a good idea to show sample input and expected output – DarkKnight Aug 06 '22 at 10:55
  • 1
    I will pile onto @Stuart's comment by adding two pieces of advice. (1) Read the very short paragraph at [Official Python tutorial / More control flow tools / "For" statements](https://docs.python.org/3/tutorial/controlflow.html#for-statements) which presents two alternatives to avoid deleting elements of a collection you're iterating on. (2) Do not use `del` to delete elements from a list; at least not until you have much more experience with python. It is very likely that the way you use it will always be wrong and that there will always be better alternatives. – Stef Aug 06 '22 at 11:54
  • 2
    Very important advice: when you begin writing a function that operates on a list, make it very clear in your head in advance whether your function is going to be modifying the list it's given, or return a different, modified, version of the list, without modifying the original. If you're unclear about this subtle difference, play around a little with `l.sort()` and `sorted(l)` to understand the difference. Both `l.sort()` and `sorted(l)` have the same goal of "sorting the list `l`", but in two different ways. When you begin writing a function it's very very important that you know which way. – Stef Aug 06 '22 at 11:57
  • @Stef thanks, but I'm still confused what the problem is with my codes, because in the loop I have used print(adjacencylist), and it print out the result I want in the loop, why would it be None once the recurence is over? – Jack Duan Aug 06 '22 at 13:33
  • @JackDuan Because you are missing a `return` statement. So your function doesn't return anything. Which is the same as returning `None` in python. – Stef Aug 07 '22 at 19:23

1 Answers1

1

You need to return the recursive calls to randomContractionA:

import random


def randomContractionA(adjacencylist):
    n=int(len(adjacencylist))
    #print(adjacencylist)
    if n==2:
        return adjacencylist
    verticeChosenF=random.randint(0,n-1)
    verticeChosenS=random.randint(0,n-1)
    while verticeChosenF==verticeChosenS:
        verticeChosenS=random.randint(0,n-1)
    contractionedVertice=[]
    for vertice in adjacencylist[verticeChosenF][:]:
        contractionedVertice.append(vertice)
    for vertice in adjacencylist[verticeChosenS][1:]:
        if vertice in contractionedVertice:
            continue
        else:
            contractionedVertice.append(vertice)
    i=1
    for vertice in contractionedVertice[1:]:
        if vertice==contractionedVertice[0]:
            del contractionedVertice[i]
        i+=1
    del adjacencylist[verticeChosenF]
    if verticeChosenF<verticeChosenS:
        del adjacencylist[verticeChosenS-1]
    else:
        del adjacencylist[verticeChosenS]
    adjacencylist.append(contractionedVertice)
    return randomContractionA(adjacencylist) # added return

adjacencyList = [[1, 2, 3, 4, 7], [2, 1, 3, 4], [3, 1, 2, 4], [4, 1, 2, 3, 5], [5, 4, 6, 7, 8], [6, 5, 7, 8], [7, 1, 5, 6, 8], [8, 5, 6, 7]]
result1=randomContractionA(adjacencyList)
print(result1)

Out:

[[2, 1, 3, 4, 5], [8, 5, 6, 7, 4, 2, 3, 1]]
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47