2023年12月21日 星期四

graph has loop ?

 #d = [[0,1],[1,2],[2,3],[3,4]] # no loop

d = [[0,1],[1,2],[2,3],[3,2]]

node = []

edge = []

for i in d:

    node.append(i[0])

    node.append(i[1])

    edge.append([i[0],i[1]])

  

node = set(node)

#edge = set(edge)

links = []

for i in node:

    t =str(i)

    for j in edge:

        if j[0]==i:

           t = t + str(j[1]) 

    links.append(t)


links1 = []

for i in links:

    t = i

    for j in edge:

        if i[-1] == str(j[0]):

            t = t + str(j[1])

    links1.append(t)

links2 = []

for i in links1:

    t = i

    for j in edge:

        if i[-1] == str(j[0]):

            t = t + str(j[1])

    links2.append(t)

    

for i in links2:

    if len(list(i))!=len(set(i)):

        print(i,':loop')

        

沒有留言: