I don't understand why my python code for random contraction algorithm return None type

70 Views Asked by At

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)
1

There are 1 best solutions below

2
On

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]]