Home > AI > Data Structure > Graph > Adjacency Matrix >

write adjacent matrix

def getAdjacentMatrix(n, a):
    '''
    Arguments
        n: 5
        a: [[1, 2], [4, 5], [2, 3]]
    '''
    m = [[0 for x in range(n)] for y in range(n)]
    for l in a:
        c1 = l[0]-1
        c2 = l[1]-1
        m[c1][c2] += 1
        m[c2][c1] += 1
        
    return m
    
    
re = getAdjacentMatrix(3, [[1, 2], [2, 3]])
print(re)
        
Related posts:
    No posts found.

Leave a Reply