import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import csv
from scipy.ndimage.measurements import label
import time

maze=cv2.imread('cleaned_skeleton_ROADS_robot.pgm',0)

img=mpimg.imread('cleaned_skeleton_ROADS_robot.pgm')
img2=mpimg.imread('ROADS_INTERSECTIONS.pgm')





#vytvorenie spojitych podgrafov
structure = np.ones((3, 3), dtype=np.int)
labeled, ncomponents = label(maze, structure)
print("Printujem pocet komponentov:",ncomponents)

#vytvorenie poli s grafmi, ocislovanie mien listov
List_of_Graphs = {}
pocet_grafov=ncomponents
for i in range(1,ncomponents+1):
    List_of_Graphs["graf"+str(i)] = []
print(List_of_Graphs)
#vytvorenie poli s grafmi, ocislovanie mien listov
#vytvorenie spojitych podgrafov







#priradit indexi do grafov podla ocislovania a vypis
for x in range(1,ncomponents+1):
    for i in range(len(labeled)):
        for j in range(len(labeled[i])):
            if(labeled[i][j]==x):
                List_of_Graphs["graf"+str(x)].append([i,j])

for g in List_of_Graphs:
    print(g,":",List_of_Graphs[g])
#priradit indexi do grafov podla ocislovania a vypis


#vytvorenie poli s vrcholmi grafov
ArrayofNodes = {}
for i in range(1,ncomponents+1):
    ArrayofNodes["graf"+str(i)+"_nodes"] = []
print(ArrayofNodes)
#vytvorenie poli s vrcholmi grafov


#nacitanie vrcholov zo subora
nodes = []
with open('finded_nodes.csv', 'r') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        nodes.append([int(val) for val in row])
#nacitanie vrcholov zo subora






#kontrola kruznic v mape, ktore nemaju vrchol!!!!!!--------------------------------------------------------------
for a in nodes:
    for b in range(1,pocet_grafov+1):
        if(a in List_of_Graphs["graf"+str(b)]):
            ArrayofNodes["graf"+str(b)+"_nodes"].append([a[0],a[1]])
print(ArrayofNodes)


count=0
print("Zaciname------------------------------------------")
for z in ArrayofNodes:
    count=count+1
    if((len(ArrayofNodes[z])==0)):
        ArrayofNodes[z].append(List_of_Graphs["graf"+str(count)][0])
        nodes.append(List_of_Graphs["graf"+str(count)][0])

for z in ArrayofNodes:
     print(z, ":",ArrayofNodes[z])

#zobrazenie grafov vrcholov podla farieb
# imgplot = plt.imshow(img,alpha=0.5)
#
# for z in range(1,ncomponents+1):
#     ArrayofNodes["graf"+str(z)+"_nodes"]=np.asarray(ArrayofNodes["graf"+str(z)+"_nodes"])
#
#
#
# for z in range(1,ncomponents+1):
#     rgb = np.random.rand(3,)
#     if((len(ArrayofNodes["graf"+str(z)+"_nodes"])!=0)):
#         plt.scatter(ArrayofNodes["graf"+str(z)+"_nodes"][:,[1]],ArrayofNodes["graf"+str(z)+"_nodes"][:,[0]], color=rgb, s=40)
# #zobrazenie grafov vrcholov podla farieb
#
#
# plt.show()
#kontrola kruznic v mape, ktore nemaju vrchol!!!!!!-----------------------------------------------------------

#kod pre click and give graph------------------------------------------------------------------------------
#vrcholy=[]
#pixel_click=[38,156]
#pixel_click=[1395,149]
#zistime kde je pixel v ktorom grafe
# for graph in List_of_Graphs:
#         if(pixel_click in List_of_Graphs[graph]):
#             print(graph,pixel_click)
#             print(ArrayofNodes[str(graph)+"_nodes"])
#             vrcholy=ArrayofNodes[str(graph)+"_nodes"]
#
#
# print(vrcholy)
# nodes=vrcholy
#kod pre click and give graph-------------------------------------------------------------------------------


#vytvorenie pola, v ktorom budu ulozene bridge hrany
Bridges_Edges=[]
#vytvorenie pola, v ktorom budu ulozene bridge hrany

#vytvorenie pola, v ktorom budu ulozene volitelne hrany
Customizable_Edges=[]
#vytvorenie pola, v ktorom budu ulozene volitelne hrany

#vytvorenie grafu pre hrany
GraphEdges=[]
#vytvorenie grafu pre hrany


#vytvorenie grafu pre susediace vrcholy
GraphNodes = {}
#vytvorenie grafu pre susediace vrcholy


#nacitanie rozmerov mapy
row=maze.shape[0]-1
col=maze.shape[1]-1
#nacitanie rozmerov mapy

#pomocna premenna na pocitanie vrcholov ci vsetky preslo
total_count_of_nodes=0
#pomocna premenna na pocitanie vrcholov ci vsetky preslo


#vypis vsetchych vrcholov pre kontrolu aj kruznicovych
print("Printim vrcholy!!!\n",nodes)
print("Dlzka vrcholov:",len(nodes))
#vypis vsetchych vrcholov pre kontrolu aj kruznicovych
#time.sleep(20)

#zaciatok hlavneho cyklu, ktory prechadza vrcholy
for a in nodes:

    GraphNodes[a[0],a[1]]=[]

    current=[]
    frontier=[]
    visited=[]
    roads=[]


    #input("Press Enter to continue...")
    frontier.append(a)
    current_node=a
    print("Vkladame UZOLLLLLLLLLLLLLLL: ",frontier)



    r=frontier[0][0]
    c=frontier[0][1]

    #inicializacie premennych a poli
    cesta1=[]
    cesta2=[]
    cesta3=[]
    cesta4=[]
    cesta5=[]
    cesta6=[]
    cesta7=[]
    cesta8=[]
    cesta_kruznica=[]

    node_finded_in_cesta1=[]
    node_finded_in_cesta2=[]
    node_finded_in_cesta3=[]
    node_finded_in_cesta4=[]
    node_finded_in_cesta5=[]
    node_finded_in_cesta6=[]
    node_finded_in_cesta7=[]
    node_finded_in_cesta8=[]


    cesta1_nodes=0
    cesta2_nodes=0
    cesta3_nodes=0
    cesta4_nodes=0
    cesta5_nodes=0
    cesta6_nodes=0
    cesta7_nodes=0
    cesta8_nodes=0

    cells_added_in_cesta1=0
    cells_added_in_cesta2=0
    cells_added_in_cesta3=0
    cells_added_in_cesta4=0
    cells_added_in_cesta5=0
    cells_added_in_cesta6=0
    cells_added_in_cesta7=0
    cells_added_in_cesta8=0

    finded_node_cesta1=0
    finded_node_cesta2=0
    finded_node_cesta3=0
    finded_node_cesta4=0
    finded_node_cesta5=0
    finded_node_cesta6=0
    finded_node_cesta7=0
    finded_node_cesta8=0

    kruznica=0
    #inicializacie premennych a poli


    #zacatie ciest pre vrchol
    #horna stred cesta
    if((r-1)>=0 and maze[r-1][c]==1):
        if([r-1,c] in nodes):
            GraphNodes[a[0],a[1]].append([r-1,c])
        if([r-1,c] not in nodes):
            frontier.append([r-1,c])
            cesta1.append([r-1,c])
            roads.append([r-1,c])
    #horna napravo cesta
    if((r-1)>=0 and (c+1)<=col and maze[r-1][c+1]==1):
        if([r-1,c+1] in nodes):
            GraphNodes[a[0],a[1]].append([r-1,c+1])
        if([r-1,c+1] not in nodes):
            frontier.append([r-1,c+1])
            cesta2.append([r-1,c+1])
            roads.append([r-1,c+1])
            cesta2_nodes=0
    #stred napravo cesta
    if((c+1)<=col and maze[r][c+1]==1):
        if([r,c+1] in nodes):
            GraphNodes[a[0],a[1]].append([r,c+1])
        if([r,c+1] not in nodes):
            frontier.append([r,c+1])
            cesta3.append([r,c+1])
            roads.append([r,c+1])
            cesta3_nodes=0
    #dole napravo cesta
    if((r+1)<=row and (c+1)<=col and maze[r+1][c+1]==1):
        if([r+1,c+1] in nodes):
            GraphNodes[a[0],a[1]].append([r+1,c+1])
        if([r+1,c+1] not in nodes):
            frontier.append([r+1,c+1])
            cesta4.append([r+1,c+1])
            roads.append([r+1,c+1])
            cesta4_nodes=0
    #dole stred cesta
    if((r+1)<=row and maze[r+1][c]==1):
        if([r+1,c] in nodes):
            GraphNodes[a[0],a[1]].append([r+1,c])
        if([r+1,c] not in nodes):
            frontier.append([r+1,c])
            cesta5.append([r+1,c])
            roads.append([r+1,c])
            cesta5_nodes=0
    #dole stred nalavo
    if((r+1)<=row and (c-1)>=0 and maze[r+1][c-1]==1):
        if([r+1,c-1] in nodes):
            GraphNodes[a[0],a[1]].append([r+1,c-1])
        if([r+1,c-1] not in nodes):
            frontier.append([r+1,c-1])
            cesta6.append([r+1,c-1])
            roads.append([r+1,c-1])
            cesta6_nodes=0
    #stred nalavo cesta
    if((c-1)>=0 and maze[r][c-1]==1):
        if([r,c-1] in nodes):
            GraphNodes[a[0],a[1]].append([r,c-1])
        if([r,c-1] not in nodes):
            frontier.append([r,c-1])
            cesta7.append([r,c-1])
            roads.append([r,c-1])
            cesta7_nodes=0
    #horna nalavo cesta
    if((r-1)>=0 and (c-1)>=0 and maze[r-1][c-1]==1):
        if([r-1,c-1] in nodes):
            GraphNodes[a[0],a[1]].append([r-1,c-1])
        if([r-1,c-1] not in nodes):
            frontier.append([r-1,c-1])
            cesta8.append([r-1,c-1])
            roads.append([r-1,c-1])
            cesta8_nodes=0
    #zacatie ciest pre vrchol


    #vypisanie, ktore pixely su v jednotlivych cestach na zaciatku
    # print("Cesta 1:",cesta1)
    # print("Cesta 2:",cesta2)
    # print("Cesta 3:",cesta3)
    # print("Cesta 4:",cesta4)
    # print("Cesta 5:",cesta5)
    # print("Cesta 6:",cesta6)
    # print("Cesta 7:",cesta7)
    # print("Cesta 8:",cesta8)
    #vypisanie, ktore pixely su v jednotlivych cestach na zaciatku



    prechod=0


    print("Frontier je: ",frontier)
    visited.append(frontier[0])

    print("Vyhadzujem z frontiera: ",frontier.pop(0))
    print("Vo visited je: ",visited)
    print("V current je: ",current)
    print("Vo frontieri je: ",frontier)
    finded_node=len(roads)
    print("Printujem kolko vrcholov musi byt najdenych----------------------------------------------------------------------: ",finded_node)




    #cyklus hladania pixelov ciest do vrcholov
    while(finded_node!=0):

        # print("Prechod: ",prechod)
        # print("Vo frontieri pred prechodom je: ",frontier)
        # print("V current pred prechodom je: ",current)
        # print("Vo visited pred prechodom je: ",visited)
        current=frontier[0]
        r=current[0]
        c=current[1]
        print("Current tu je: ",current)

        #do ktorej cesty mame pridavat!!!-----------------------------------------------------------------------------------------------------------1
        if(cesta1_nodes==0):
            if(current in cesta1):
                #horna stred cesta hotovo opravene
                if((r-1)>=0 and ([r-1,c] not in visited) and maze[r-1][c]==1 and ([r-1,c] not in cesta2) and ([r-1,c] not in cesta3) and ([r-1,c] not in cesta4) and ([r-1,c] not in cesta5) and
                ([r-1,c] not in cesta6) and ([r-1,c] not in cesta7) and ([r-1,c] not in cesta8)):
                    if([r-1,c] in nodes):
                        finded_node=finded_node-1
                        cesta1_nodes=cesta1_nodes+1
                        print("Nasiel som vrchol horna stred!")
                        node_finded_in_cesta1=[r-1,c]
                    else:
                        frontier.append([r-1,c])
                        cesta1.append([r-1,c])
                        roads.append([r-1,c])
                        cells_added_in_cesta1=cells_added_in_cesta1+1

                #horna napravo cesta hotovo opravene
                if((r-1)>=0 and (c+1)<=col and ([r-1,c+1] not in visited)  and maze[r-1][c+1]==1 and ([r-1,c+1] not in cesta2) and ([r-1,c+1] not in cesta3) and ([r-1,c+1] not in cesta4) and ([r-1,c+1] not in cesta5) and
                ([r-1,c+1] not in cesta6) and ([r-1,c+1] not in cesta7) and ([r-1,c+2] not in cesta8)):
                    if([r-1,c+1] in nodes):
                        finded_node=finded_node-1
                        print("Nasiel som vrchol horna napravo!")
                        cesta1_nodes=cesta1_nodes+1
                        node_finded_in_cesta1=[r-1,c+1]
                    else:
                        frontier.append([r-1,c+1])
                        cesta1.append([r-1,c+1])
                        roads.append([r-1,c+1])
                        cells_added_in_cesta1=cells_added_in_cesta1+1


                #stred napravo cesta hotovo opravene
                if((c+1)<=col and ([r,c+1] not in visited) and maze[r][c+1]==1 and ([r,c+1] not in cesta2) and ([r,c+1] not in cesta3) and ([r,c+1] not in cesta4) and ([r,c+1] not in cesta5) and
                ([r,c+1] not in cesta6) and ([r,c+1] not in cesta7) and ([r,c+1] not in cesta8)):
                    if([r,c+1] in nodes):
                        finded_node=finded_node-1
                        cesta1_nodes=cesta1_nodes+1
                        print("Nasiel som vrchol stred napravo!")
                        node_finded_in_cesta1=[r,c+1]
                    else:
                        frontier.append([r,c+1])
                        cesta1.append([r,c+1])
                        roads.append([r,c+1])
                        cells_added_in_cesta1=cells_added_in_cesta1+1

                #dole napravo cesta hotovo opravene
                if((r+1)<=row and (c+1)<=col and  ([r+1,c+1] not in visited) and maze[r+1][c+1]==1 and ([r+1,c+1] not in cesta2) and ([r+1,c+1] not in cesta3) and ([r+1,c+1] not in cesta4) and ([r+1,c+1] not in cesta5) and
                ([r+1,c+1] not in cesta6) and ([r+1,c+1] not in cesta7) and ([r+1,c+1] not in cesta8)):
                    if([r+1,c+1] in nodes):
                        finded_node=finded_node-1
                        cesta1_nodes=cesta1_nodes+1
                        print("Nasiel som vrchol dole napravo!")
                        node_finded_in_cesta1=[r+1,c+1]
                    else:
                        frontier.append([r+1,c+1])
                        cesta1.append([r+1,c+1])
                        roads.append([r+1,c+1])
                        cells_added_in_cesta1=cells_added_in_cesta1+1

                #dole stred cesta hotovo opravene
                if((r+1)<=row and ([r+1,c] not in visited) and maze[r+1][c]==1 and ([r+1,c] not in cesta2) and ([r+1,c] not in cesta3) and ([r+1,c] not in cesta4) and ([r+1,c] not in cesta5) and
                ([r+1,c] not in cesta6) and ([r+1,c] not in cesta7) and ([r+1,c] not in cesta8)):
                    if([r+1,c] in nodes):
                        finded_node=finded_node-1
                        cesta1_nodes=cesta1_nodes+1
                        print("Nasiel som vrchol stred dole!")
                        node_finded_in_cesta1=[r+1,c]

                    else:
                        frontier.append([r+1,c])
                        cesta1.append([r+1,c])
                        roads.append([r+1,c])
                        cells_added_in_cesta1=cells_added_in_cesta1+1

                #dole stred nalavo hotovo opravene
                if((r+1)<=row and (c-1)>=0 and ([r+1,c-1] not in visited) and maze[r+1][c-1]==1 and ([r+1,c-1] not in cesta2) and ([r+1,c-1] not in cesta3) and ([r+1,c-1] not in cesta4) and ([r+1,c-1] not in cesta5) and
                ([r+1,c-1] not in cesta6) and ([r+1,c-1] not in cesta7) and ([r+1,c-1] not in cesta8)):
                    if([r+1,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta1_nodes=cesta1_nodes+1
                        print("Nasiel som vrchol dole stred nalavo!")
                        node_finded_in_cesta1=[r+1,c-1]
                    else:
                        frontier.append([r+1,c-1])
                        cesta1.append([r+1,c-1])
                        roads.append([r+1,c-1])
                        cells_added_in_cesta1=cells_added_in_cesta1+1

                #stred nalavo cesta hotovo opravene
                if((c-1)>=0 and ([r,c-1] not in visited) and maze[r][c-1]==1 and ([r,c-1] not in cesta2) and ([r,c-1] not in cesta3) and ([r,c-1] not in cesta4) and ([r,c-1] not in cesta5) and
                ([r,c-1] not in cesta6) and ([r,c-1] not in cesta7) and ([r,c-1] not in cesta8)):
                    if([r,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta1_nodes=cesta1_nodes+1
                        print("Nasiel som vrchol stred nalavo!")
                        node_finded_in_cesta1=[r,c-1]
                    else:
                        frontier.append([r,c-1])
                        cesta1.append([r,c-1])
                        roads.append([r,c-1])
                        cells_added_in_cesta1=cells_added_in_cesta1+1

                #horna nalavo cesta hotovo opravene
                if((r-1)>=0 and (c-1)>=0 and ([r-1,c-1] not in visited) and maze[r-1][c-1]==1 and ([r-1,c-1] not in cesta2) and ([r-1,c-1] not in cesta3) and ([r-1,c-1] not in cesta4) and ([r-1,c-1] not in cesta5) and
                ([r-1,c-1] not in cesta6) and ([r-1,c-1] not in cesta7) and ([r-1,c-1] not in cesta8)):
                    if([r-1,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta1_nodes=cesta1_nodes+1
                        print("Nasiel som vrchol horna nalavo!")
                        node_finded_in_cesta1=[r-1,c-1]
                    else:
                        frontier.append([r-1,c-1])
                        cesta1.append([r-1,c-1])
                        roads.append([r-1,c-1])
                        cells_added_in_cesta1=cells_added_in_cesta1+1
            if (cesta1_nodes==1):
                print("Nasiel som vrchol pri ceste 1!")

                for t in range(cells_added_in_cesta1):
                    cesta1.pop(-1)
                    frontier.pop(-1)


        #do ktorej cesty mame pridavat!!!----------------------------------------------------------------------------------------------------------------------------2
        if(cesta2_nodes==0):
            if(current in cesta2):
                #horna stred cesta
                if((r-1)>=0 and ([r-1,c] not in visited) and maze[r-1][c]==1 and ([r-1,c] not in cesta1) and ([r-1,c] not in cesta3) and ([r-1,c] not in cesta4) and ([r-1,c] not in cesta5) and
                ([r-1,c] not in cesta6) and ([r-1,c] not in cesta7) and ([r-1,c] not in cesta8)):
                    if([r-1,c] in nodes):
                        finded_node=finded_node-1
                        cesta2_nodes=cesta2_nodes+1
                        print("Nasiel som vrchol horna stred!")
                        node_finded_in_cesta2=[r-1,c]
                    else:
                        frontier.append([r-1,c])
                        cesta2.append([r-1,c])
                        roads.append([r-1,c])
                        cells_added_in_cesta2=cells_added_in_cesta2+1

                #horna napravo cesta
                if((r-1)>=0 and  (c+1)<=col and ([r-1,c+1] not in visited)  and maze[r-1][c+1]==1 and ([r-1,c+1] not in cesta1) and ([r-1,c+1] not in cesta3) and ([r-1,c+1] not in cesta4) and ([r-1,c+1] not in cesta5) and
                ([r-1,c+1] not in cesta6) and ([r-1,c+1] not in cesta7) and ([r-1,c+2] not in cesta8)):
                    if([r-1,c+1] in nodes):
                        finded_node=finded_node-1
                        print("Nasiel som vrchol horna napravo!")
                        cesta2_nodes=cesta2_nodes+1
                        node_finded_in_cesta2=[r-1,c+1]
                    else:
                        frontier.append([r-1,c+1])
                        cesta2.append([r-1,c+1])
                        roads.append([r-1,c+1])
                        cells_added_in_cesta2=cells_added_in_cesta2+1


                #stred napravo cesta
                if((c+1)<=col and ([r,c+1] not in visited) and maze[r][c+1]==1 and ([r,c+1] not in cesta1) and ([r,c+1] not in cesta3) and ([r,c+1] not in cesta4) and ([r,c+1] not in cesta5) and
                ([r,c+1] not in cesta6) and ([r,c+1] not in cesta7) and ([r,c+1] not in cesta8)):
                    if([r,c+1] in nodes):
                        finded_node=finded_node-1
                        cesta2_nodes=cesta2_nodes+1
                        print("Nasiel som vrchol stred napravo!")
                        node_finded_in_cesta2=[r,c+1]
                    else:
                        frontier.append([r,c+1])
                        cesta2.append([r,c+1])
                        roads.append([r,c+1])
                        cells_added_in_cesta2=cells_added_in_cesta2+1

                #dole napravo cesta
                if((r+1)<=row and (c+1)<=col and  ([r+1,c+1] not in visited) and maze[r+1][c+1]==1 and ([r+1,c+1] not in cesta1) and ([r+1,c+1] not in cesta3) and ([r+1,c+1] not in cesta4) and ([r+1,c+1] not in cesta5) and
                ([r+1,c+1] not in cesta6) and ([r+1,c+1] not in cesta7) and ([r+1,c+1] not in cesta8)):
                    if([r+1,c+1] in nodes):
                        finded_node=finded_node-1
                        cesta2_nodes=cesta2_nodes+1
                        print("Nasiel som vrchol dole napravo!")
                        node_finded_in_cesta2=[r+1,c+1]
                    else:
                        frontier.append([r+1,c+1])
                        cesta2.append([r+1,c+1])
                        roads.append([r+1,c+1])
                        cells_added_in_cesta2=cells_added_in_cesta2+1

                #dole stred cesta
                if((r+1)<=row and ([r+1,c] not in visited) and maze[r+1][c]==1 and ([r+1,c] not in cesta1) and ([r+1,c] not in cesta3) and ([r+1,c] not in cesta4) and ([r+1,c] not in cesta5) and
                ([r+1,c] not in cesta6) and ([r+1,c] not in cesta7) and ([r+1,c] not in cesta8)):
                    if([r+1,c] in nodes):
                        finded_node=finded_node-1
                        cesta2_nodes=cesta2_nodes+1
                        print("Nasiel som vrchol stred dole!")
                        node_finded_in_cesta2=[r+1,c]

                    else:
                        frontier.append([r+1,c])
                        cesta2.append([r+1,c])
                        roads.append([r+1,c])
                        cells_added_in_cesta2=cells_added_in_cesta2+1

                #dole stred nalavo
                if((r+1)<=row and (c-1)>=0 and ([r+1,c-1] not in visited) and maze[r+1][c-1]==1 and ([r+1,c-1] not in cesta1) and ([r+1,c-1] not in cesta3) and ([r+1,c-1] not in cesta4) and ([r+1,c-1] not in cesta5) and
                ([r+1,c-1] not in cesta6) and ([r+1,c-1] not in cesta7) and ([r+1,c-1] not in cesta8)):
                    if([r+1,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta2_nodes=cesta2_nodes+1
                        print("Nasiel som vrchol dole stred nalavo!")
                        node_finded_in_cesta2=[r+1,c-1]
                    else:
                        frontier.append([r+1,c-1])
                        cesta2.append([r+1,c-1])
                        roads.append([r+1,c-1])
                        cells_added_in_cesta2=cells_added_in_cesta2+1

                #stred nalavo cesta
                if((c-1)>=0 and ([r,c-1] not in visited) and maze[r][c-1]==1 and ([r,c-1] not in cesta1) and ([r,c-1] not in cesta2) and ([r,c-1] not in cesta4) and ([r,c-1] not in cesta5) and
                ([r,c-1] not in cesta6) and ([r,c-1] not in cesta7) and ([r,c-1] not in cesta8)):
                    if([r,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta2_nodes=cesta2_nodes+1
                        print("Nasiel som vrchol stred nalavo!")
                        node_finded_in_cesta2=[r,c-1]
                    else:
                        frontier.append([r,c-1])
                        cesta2.append([r,c-1])
                        roads.append([r,c-1])
                        cells_added_in_cesta2=cells_added_in_cesta2+1

                #horna nalavo cesta
                if((r-1)>=0 and (c-1)>=0 and ([r-1,c-1] not in visited) and maze[r-1][c-1]==1 and ([r-1,c-1] not in cesta1) and ([r-1,c-1] not in cesta3) and ([r-1,c-1] not in cesta4) and ([r-1,c-1] not in cesta5) and
                ([r-1,c-1] not in cesta6) and ([r-1,c-1] not in cesta7) and ([r-1,c-1] not in cesta8)):
                    if([r-1,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta2_nodes=cesta2_nodes+1
                        print("Nasiel som vrchol horna nalavo!")
                        node_finded_in_cesta2=[r-1,c-1]
                    else:
                        frontier.append([r-1,c-1])
                        cesta2.append([r-1,c-1])
                        roads.append([r-1,c-1])
                        cells_added_in_cesta2=cells_added_in_cesta2+1

            if (cesta2_nodes)==1:
                print("Nasiel som vrchol pri ceste 2!")

                for t in range(cells_added_in_cesta2):
                    cesta2.pop(-1)
                    frontier.pop(-1)

        #do ktorej cesty mame pridavat!!!------------------------------------------------------------------------------------------------------------------------------3
        if(cesta3_nodes==0):
            if(current in cesta3):
                print(cesta5)
                #horna stred cesta
                if((r-1)>=0 and ([r-1,c] not in visited) and maze[r-1][c]==1 and ([r-1,c] not in cesta1) and ([r-1,c] not in cesta2) and ([r-1,c] not in cesta4) and ([r-1,c] not in cesta5) and
                ([r-1,c] not in cesta6) and ([r-1,c] not in cesta7) and ([r-1,c] not in cesta8)):
                    if([r-1,c] in nodes):
                        finded_node=finded_node-1
                        cesta3_nodes=cesta3_nodes+1
                        print("Nasiel som vrchol horna stred!")
                        node_finded_in_cesta3=[r-1,c]
                    else:
                        frontier.append([r-1,c])
                        cesta3.append([r-1,c])
                        roads.append([r-1,c])
                        cells_added_in_cesta3=cells_added_in_cesta3+1

                #horna napravo cesta
                if((r-1)>=0 and  (c+1)<=col and ([r-1,c+1] not in visited)  and maze[r-1][c+1]==1 and ([r-1,c+1] not in cesta1) and ([r-1,c+1] not in cesta2) and ([r-1,c+1] not in cesta4) and ([r-1,c+1] not in cesta5) and
                ([r-1,c+1] not in cesta6) and ([r-1,c+1] not in cesta7) and ([r-1,c+2] not in cesta8)):
                    if([r-1,c+1] in nodes):
                        finded_node=finded_node-1
                        print("Nasiel som vrchol horna napravo!")
                        cesta3_nodes=cesta3_nodes+1
                        node_finded_in_cesta3=[r-1,c+1]
                    else:
                        frontier.append([r-1,c+1])
                        cesta3.append([r-1,c+1])
                        roads.append([r-1,c+1])
                        cells_added_in_cesta3=cells_added_in_cesta3+1


                #stred napravo cesta
                if((c+1)<=col and ([r,c+1] not in visited) and maze[r][c+1]==1 and ([r,c+1] not in cesta1) and ([r,c+1] not in cesta2) and ([r,c+1] not in cesta4) and ([r,c+1] not in cesta5) and
                ([r,c+1] not in cesta6) and ([r,c+1] not in cesta7) and ([r,c+1] not in cesta8)):
                    if([r,c+1] in nodes):
                        finded_node=finded_node-1
                        cesta3_nodes=cesta3_nodes+1
                        print("Nasiel som vrchol stred napravo!")
                        node_finded_in_cesta3=[r,c+1]
                    else:
                        frontier.append([r,c+1])
                        cesta3.append([r,c+1])
                        roads.append([r,c+1])
                        cells_added_in_cesta3=cells_added_in_cesta3+1

                #dole napravo cesta
                if((r+1)<=row and (c+1)<=col and  ([r+1,c+1] not in visited) and maze[r+1][c+1]==1 and ([r+1,c+1] not in cesta1) and ([r+1,c+1] not in cesta2) and ([r+1,c+1] not in cesta4) and ([r+1,c+1] not in cesta5) and
                ([r+1,c+1] not in cesta6) and ([r+1,c+1] not in cesta7) and ([r+1,c+1] not in cesta8)):
                    if([r+1,c+1] in nodes):
                        finded_node=finded_node-1
                        cesta3_nodes=cesta3_nodes+1
                        print("Nasiel som vrchol dole napravo!")
                        node_finded_in_cesta3=[r+1,c+1]
                    else:
                        frontier.append([r+1,c+1])
                        cesta3.append([r+1,c+1])
                        roads.append([r+1,c+1])
                        cells_added_in_cesta3=cells_added_in_cesta3+1

                #dole stred cesta
                if((r+1)<=row and ([r+1,c] not in visited) and maze[r+1][c]==1 and ([r+1,c] not in cesta1) and ([r+1,c] not in cesta2) and ([r+1,c] not in cesta4) and ([r+1,c] not in cesta5) and
                ([r+1,c] not in cesta6) and ([r+1,c] not in cesta7) and ([r+1,c] not in cesta8)):
                    if([r+1,c] in nodes):
                        finded_node=finded_node-1
                        cesta3_nodes=cesta3_nodes+1
                        print("Nasiel som vrchol stred dole!")
                        node_finded_in_cesta3=[r+1,c]
                    else:
                        frontier.append([r+1,c])
                        cesta3.append([r+1,c])
                        roads.append([r+1,c])
                        cells_added_in_cesta3=cells_added_in_cesta3+1

                #dole stred nalavo
                if((r+1)<=row and (c-1)>=0 and ([r+1,c-1] not in visited) and maze[r+1][c-1]==1 and ([r+1,c-1] not in cesta1) and ([r+1,c-1] not in cesta2) and ([r+1,c-1] not in cesta4) and ([r+1,c-1] not in cesta5) and
                ([r+1,c-1] not in cesta6) and ([r+1,c-1] not in cesta7) and ([r+1,c-1] not in cesta8)):
                    #print("Nemalo by vojst!!!!!!!!!!!!!: ",r+1,c-1)
                    if([r+1,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta3_nodes=cesta3_nodes+1
                        print("Nasiel som vrchol dole stred nalavo!")
                        node_finded_in_cesta3=[r+1,c-1]
                    else:
                        frontier.append([r+1,c-1])
                        cesta3.append([r+1,c-1])
                        roads.append([r+1,c-1])
                        cells_added_in_cesta3=cells_added_in_cesta3+1

                #stred nalavo cesta
                if((c-1)>=0 and ([r,c-1] not in visited) and maze[r][c-1]==1 and ([r,c-1] not in cesta1) and ([r,c-1] not in cesta2) and ([r,c-1] not in cesta4) and ([r,c-1] not in cesta5) and
                ([r,c-1] not in cesta6) and ([r,c-1] not in cesta7) and ([r,c-1] not in cesta8)):
                    if([r,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta3_nodes=cesta3_nodes+1
                        print("Nasiel som vrchol stred nalavo!")
                        node_finded_in_cesta3=[r,c-1]
                    else:
                        frontier.append([r,c-1])
                        cesta3.append([r,c-1])
                        roads.append([r,c-1])
                        cells_added_in_cesta3=cells_added_in_cesta3+1

                #horna nalavo cesta
                if((r-1)>=0 and (c-1)>=0 and ([r-1,c-1] not in visited) and maze[r-1][c-1]==1 and ([r-1,c-1] not in cesta1) and ([r-1,c-1] not in cesta2) and ([r-1,c-1] not in cesta4) and ([r-1,c-1] not in cesta5) and
                ([r-1,c-1] not in cesta6) and ([r-1,c-1] not in cesta7) and ([r-1,c-1] not in cesta8)):
                    if([r-1,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta3_nodes=cesta3_nodes+1
                        print("Nasiel som vrchol horna nalavo!")
                        node_finded_in_cesta3=[r-1,c-1]
                    else:
                        frontier.append([r-1,c-1])
                        cesta3.append([r-1,c-1])
                        roads.append([r-1,c-1])
                        cells_added_in_cesta3=cells_added_in_cesta3+1

            if (cesta3_nodes)==1:
                print("Nasiel som vrchol pri ceste 3!")

                for t in range(cells_added_in_cesta3):
                    cesta3.pop(-1)
                    frontier.pop(-1)

        #do ktorej cesty mame pridavat!!!-------------------------------------------------------------------------------------------------------------------------4
        if(cesta4_nodes==0):
            if(current in cesta4):
                #horna stred cesta
                if((r-1)>=0 and ([r-1,c] not in visited) and maze[r-1][c]==1 and ([r-1,c] not in cesta1) and ([r-1,c] not in cesta2) and ([r-1,c] not in cesta3) and ([r-1,c] not in cesta5) and
                ([r-1,c] not in cesta6) and ([r-1,c] not in cesta7) and ([r-1,c] not in cesta8)):
                    if([r-1,c] in nodes):
                        finded_node=finded_node-1
                        cesta4_nodes=cesta4_nodes+1
                        print("Nasiel som vrchol horna stred!")
                        node_finded_in_cesta4=[r-1,c]
                    else:
                        frontier.append([r-1,c])
                        cesta4.append([r-1,c])
                        roads.append([r-1,c])
                        cells_added_in_cesta4=cells_added_in_cesta4+1

                #horna napravo cesta
                if((r-1)>=0 and  (c+1)<=col and ([r-1,c+1] not in visited)  and maze[r-1][c+1]==1 and ([r-1,c+1] not in cesta1) and ([r-1,c+1] not in cesta2) and ([r-1,c+1] not in cesta3) and ([r-1,c+1] not in cesta5) and
                ([r-1,c+1] not in cesta6) and ([r-1,c+1] not in cesta7) and ([r-1,c+2] not in cesta8)):
                    if([r-1,c+1] in nodes):
                        finded_node=finded_node-1
                        print("Nasiel som vrchol horna napravo!")
                        cesta4_nodes=cesta4_nodes+1
                        node_finded_in_cesta4=[r-1,c+1]
                    else:
                        frontier.append([r-1,c+1])
                        cesta4.append([r-1,c+1])
                        roads.append([r-1,c+1])
                        cells_added_in_cesta4=cells_added_in_cesta4+1


                #stred napravo cesta
                if((c+1)<=col and ([r,c+1] not in visited) and maze[r][c+1]==1 and ([r,c+1] not in cesta1) and ([r,c+1] not in cesta2) and ([r,c+1] not in cesta3) and ([r,c+1] not in cesta5) and
                ([r,c+1] not in cesta6) and ([r,c+1] not in cesta7) and ([r,c+1] not in cesta8)):
                    if([r,c+1] in nodes):
                        finded_node=finded_node-1
                        cesta4_nodes=cesta4_nodes+1
                        print("Nasiel som vrchol stred napravo!")
                        node_finded_in_cesta4=[r,c+1]
                    else:
                        frontier.append([r,c+1])
                        cesta4.append([r,c+1])
                        roads.append([r,c+1])
                        cells_added_in_cesta4=cells_added_in_cesta4+1

                #dole napravo cesta
                if((r+1)<=row and (c+1)<=col and  ([r+1,c+1] not in visited) and maze[r+1][c+1]==1 and ([r+1,c+1] not in cesta1) and ([r+1,c+1] not in cesta2) and ([r+1,c+1] not in cesta3) and ([r+1,c+1] not in cesta5) and
                ([r+1,c+1] not in cesta6) and ([r+1,c+1] not in cesta7) and ([r+1,c+1] not in cesta8)):
                    if([r+1,c+1] in nodes):
                        finded_node=finded_node-1
                        cesta4_nodes=cesta4_nodes+1
                        print("Nasiel som vrchol dole napravo!")
                        node_finded_in_cesta4=[r+1,c+1]
                    else:
                        frontier.append([r+1,c+1])
                        cesta4.append([r+1,c+1])
                        roads.append([r+1,c+1])
                        cells_added_in_cesta4=cells_added_in_cesta4+1

                #dole stred cesta
                if((r+1)<=row and ([r+1,c] not in visited) and maze[r+1][c]==1 and ([r+1,c] not in cesta1) and ([r+1,c] not in cesta2) and ([r+1,c] not in cesta3) and ([r+1,c] not in cesta5) and
                ([r+1,c] not in cesta6) and ([r+1,c] not in cesta7) and ([r+1,c] not in cesta8)):
                    if([r+1,c] in nodes):
                        finded_node=finded_node-1
                        cesta4_nodes=cesta4_nodes+1
                        print("Nasiel som vrchol stred dole!")
                        node_finded_in_cesta4=[r+1,c]

                    else:
                        frontier.append([r+1,c])
                        cesta4.append([r+1,c])
                        roads.append([r+1,c])
                        cells_added_in_cesta4=cells_added_in_cesta4+1

                #dole stred nalavo
                if((r+1)<=row and (c-1)>=0 and ([r+1,c-1] not in visited) and maze[r+1][c-1]==1 and ([r+1,c-1] not in cesta1) and ([r+1,c-1] not in cesta2) and ([r+1,c-1] not in cesta3) and ([r+1,c-1] not in cesta5) and
                ([r+1,c-1] not in cesta6) and ([r+1,c-1] not in cesta7) and ([r+1,c-1] not in cesta8)):
                    if([r+1,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta4_nodes=cesta4_nodes+1
                        print("Nasiel som vrchol dole stred nalavo!")
                        node_finded_in_cesta4=[r+1,c-1]
                    else:
                        frontier.append([r+1,c-1])
                        cesta4.append([r+1,c-1])
                        roads.append([r+1,c-1])
                        cells_added_in_cesta4=cells_added_in_cesta4+1

                #stred nalavo cesta
                if((c-1)>=0 and ([r,c-1] not in visited) and maze[r][c-1]==1 and ([r,c-1] not in cesta1) and ([r,c-1] not in cesta2) and ([r,c-1] not in cesta3) and ([r,c-1] not in cesta5) and
                ([r,c-1] not in cesta6) and ([r,c-1] not in cesta7) and ([r,c-1] not in cesta8)):
                    if([r,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta4_nodes=cesta4_nodes+1
                        print("Nasiel som vrchol stred nalavo!")
                        node_finded_in_cesta4=[r,c-1]
                    else:
                        frontier.append([r,c-1])
                        cesta4.append([r,c-1])
                        roads.append([r,c-1])
                        cells_added_in_cesta4=cells_added_in_cesta4+1

                #horna nalavo cesta
                if((r-1)>=0 and (c-1)>=0 and ([r-1,c-1] not in visited) and maze[r-1][c-1]==1 and  ([r-1,c-1] not in cesta1) and ([r-1,c-1] not in cesta2) and ([r-1,c-1] not in cesta3) and ([r-1,c-1] not in cesta5) and
                ([r-1,c-1] not in cesta6) and ([r-1,c-1] not in cesta7) and ([r-1,c-1] not in cesta8)):
                    if([r-1,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta4_nodes=cesta4_nodes+1
                        print("Nasiel som vrchol horna nalavo!")
                        node_finded_in_cesta4=[r-1,c-1]
                    else:
                        frontier.append([r-1,c-1])
                        cesta4.append([r-1,c-1])
                        roads.append([r-1,c-1])
                        cells_added_in_cesta4=cells_added_in_cesta4+1

            if (cesta4_nodes)==1:
                print("Nasiel som vrchol pri ceste 4!")

                for t in range(cells_added_in_cesta4):
                    cesta4.pop(-1)
                    frontier.pop(-1)


        #do ktorej cesty mame pridavat!!!------------------------------------------------------------------------------------------------------------------------------5
        if(cesta5_nodes==0):
            if(current in cesta5):
                #horna stred cesta
                if((r-1)>=0 and ([r-1,c] not in visited) and maze[r-1][c]==1 and ([r-1,c] not in cesta1) and ([r-1,c] not in cesta2) and ([r-1,c] not in cesta3) and ([r-1,c] not in cesta4) and
                ([r-1,c] not in cesta6) and ([r-1,c] not in cesta7) and ([r-1,c] not in cesta8)):
                    if([r-1,c] in nodes):
                        finded_node=finded_node-1
                        cesta5_nodes=cesta5_nodes+1
                        print("Nasiel som vrchol horna stred!")
                        node_finded_in_cesta5=[r-1,c]
                    else:
                        frontier.append([r-1,c])
                        cesta5.append([r-1,c])
                        roads.append([r-1,c])
                        cells_added_in_cesta5=cells_added_in_cesta5+1

                #horna napravo cesta
                if((r-1)>=0 and  (c+1)<=col and ([r-1,c+1] not in visited)  and maze[r-1][c+1]==1 and ([r-1,c+1] not in cesta1) and ([r-1,c+1] not in cesta2) and ([r-1,c+1] not in cesta3) and ([r-1,c+1] not in cesta4) and
                ([r-1,c+1] not in cesta6) and ([r-1,c+1] not in cesta7) and ([r-1,c+2] not in cesta8)):
                    if([r-1,c+1] in nodes):
                        finded_node=finded_node-1
                        print("Nasiel som vrchol horna napravo!")
                        cesta5_nodes=cesta5_nodes+1
                        node_finded_in_cesta5=[r-1,c+1]
                    else:
                        frontier.append([r-1,c+1])
                        cesta5.append([r-1,c+1])
                        roads.append([r-1,c+1])
                        cells_added_in_cesta5=cells_added_in_cesta5+1


                #stred napravo cesta
                if((c+1)<=col and ([r,c+1] not in visited) and maze[r][c+1]==1 and ([r,c+1] not in cesta1) and ([r,c+1] not in cesta2) and ([r,c+1] not in cesta3) and ([r,c+1] not in cesta4) and
                ([r,c+1] not in cesta6) and ([r,c+1] not in cesta7) and ([r,c+1] not in cesta8)):
                    if([r,c+1] in nodes):
                        finded_node=finded_node-1
                        cesta5_nodes=cesta5_nodes+1
                        print("Nasiel som vrchol stred napravo!")
                        node_finded_in_cesta5=[r,c+1]
                    else:
                        frontier.append([r,c+1])
                        cesta5.append([r,c+1])
                        roads.append([r,c+1])
                        cells_added_in_cesta5=cells_added_in_cesta5+1

                #dole napravo cesta
                if((r+1)<=row and (c+1)<=col and  ([r+1,c+1] not in visited) and maze[r+1][c+1]==1 and ([r+1,c+1] not in cesta1) and ([r+1,c+1] not in cesta2) and ([r+1,c+1] not in cesta3) and ([r+1,c+1] not in cesta4) and
                ([r+1,c+1] not in cesta6) and ([r+1,c+1] not in cesta7) and ([r+1,c+1] not in cesta8)):
                    if([r+1,c+1] in nodes):
                        finded_node=finded_node-1
                        cesta5_nodes=cesta5_nodes+1
                        print("Nasiel som vrchol dole napravo!")
                        node_finded_in_cesta5=[r+1,c+1]
                    else:
                        frontier.append([r+1,c+1])
                        cesta5.append([r+1,c+1])
                        roads.append([r+1,c+1])
                        cells_added_in_cesta5=cells_added_in_cesta5+1

                #dole stred cesta
                if((r+1)<=row and ([r+1,c] not in visited) and maze[r+1][c]==1 and ([r+1,c] not in cesta1) and ([r+1,c] not in cesta2) and ([r+1,c] not in cesta3) and ([r+1,c] not in cesta4) and
                ([r+1,c] not in cesta6) and ([r+1,c] not in cesta7) and ([r+1,c] not in cesta8)):
                    if([r+1,c] in nodes):
                        finded_node=finded_node-1
                        cesta5_nodes=cesta5_nodes+1
                        print("Nasiel som vrchol stred dole!")
                        node_finded_in_cesta5=[r+1,c]

                    else:
                        frontier.append([r+1,c])
                        cesta5.append([r+1,c])
                        roads.append([r+1,c])
                        cells_added_in_cesta5=cells_added_in_cesta5+1

                #dole stred nalavo
                if((r+1)<=row and (c-1)>=0 and ([r+1,c-1] not in visited) and maze[r+1][c-1]==1 and ([r+1,c-1] not in cesta1) and ([r+1,c-1] not in cesta2) and ([r+1,c-1] not in cesta3) and ([r+1,c-1] not in cesta4) and
                ([r+1,c-1] not in cesta6) and ([r+1,c-1] not in cesta7) and ([r+1,c-1] not in cesta8)):
                    if([r+1,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta5_nodes=cesta5_nodes+1
                        print("Nasiel som vrchol dole stred nalavo!")
                        node_finded_in_cesta5=[r+1,c-1]
                    else:
                        frontier.append([r+1,c-1])
                        cesta5.append([r+1,c-1])
                        roads.append([r+1,c-1])
                        cells_added_in_cesta5=cells_added_in_cesta5+1

                #stred nalavo cesta
                if((c-1)>=0 and ([r,c-1] not in visited) and maze[r][c-1]==1 and ([r,c-1] not in cesta1) and ([r,c-1] not in cesta2) and ([r,c-1] not in cesta3) and ([r,c-1] not in cesta4) and
                ([r,c-1] not in cesta6) and ([r,c-1] not in cesta7) and ([r,c-1] not in cesta8)):
                    if([r,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta5_nodes=cesta5_nodes+1
                        print("Nasiel som vrchol stred nalavo!")
                        node_finded_in_cesta5=[r,c-1]
                    else:
                        frontier.append([r,c-1])
                        cesta5.append([r,c-1])
                        roads.append([r,c-1])
                        cells_added_in_cesta5=cells_added_in_cesta5+1

                #horna nalavo cesta
                if((r-1)>=0 and (c-1)>=0 and ([r-1,c-1] not in visited) and maze[r-1][c-1]==1 and ([r-1,c-1] not in cesta1) and ([r-1,c-1] not in cesta2) and ([r-1,c-1] not in cesta3) and ([r-1,c-1] not in cesta4) and
                ([r-1,c-1] not in cesta6) and ([r-1,c-1] not in cesta7) and ([r-1,c-1] not in cesta8)):
                    if([r-1,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta5_nodes=cesta5_nodes+1
                        print("Nasiel som vrchol horna nalavo!")
                        node_finded_in_cesta5=[r-1,c-1]
                    else:
                        frontier.append([r-1,c-1])
                        cesta5.append([r-1,c-1])
                        roads.append([r-1,c-1])
                        cells_added_in_cesta5=cells_added_in_cesta5+1

            if (cesta5_nodes)==1:
                print("Nasiel som vrchol pri ceste 5!")

                for t in range(cells_added_in_cesta5):
                    cesta5.pop(-1)
                    frontier.pop(-1)


        #do ktorej cesty mame pridavat!!!---------------------------------------------------------------------------------------------------------------------------6
        if(cesta6_nodes==0):
            if(current in cesta6):
                #horna stred cesta
                if((r-1)>=0 and ([r-1,c] not in visited) and maze[r-1][c]==1 and ([r-1,c] not in cesta1) and ([r-1,c] not in cesta2) and ([r-1,c] not in cesta3) and ([r-1,c] not in cesta4) and
                ([r-1,c] not in cesta5) and ([r-1,c] not in cesta7) and ([r-1,c] not in cesta8)):
                    if([r-1,c] in nodes):
                        finded_node=finded_node-1
                        cesta6_nodes=cesta6_nodes+1
                        print("Nasiel som vrchol horna stred!")
                        node_finded_in_cesta6=[r-1,c]
                    else:
                        frontier.append([r-1,c])
                        cesta6.append([r-1,c])
                        roads.append([r-1,c])
                        cells_added_in_cesta6=cells_added_in_cesta6+1

                #horna napravo cesta
                if((r-1)>=0 and  (c+1)<=col and ([r-1,c+1] not in visited)  and maze[r-1][c+1]==1 and ([r-1,c+1] not in cesta1) and ([r-1,c+1] not in cesta2) and ([r-1,c+1] not in cesta3) and ([r-1,c+1] not in cesta4) and
                ([r-1,c+1] not in cesta5) and ([r-1,c+1] not in cesta7) and ([r-1,c+2] not in cesta8)):
                    if([r-1,c+1] in nodes):
                        finded_node=finded_node-1
                        print("Nasiel som vrchol horna napravo!")
                        cesta6_nodes=cesta6_nodes+1
                        node_finded_in_cesta6=[r-1,c+1]
                    else:
                        frontier.append([r-1,c+1])
                        cesta6.append([r-1,c+1])
                        roads.append([r-1,c+1])
                        cells_added_in_cesta6=cells_added_in_cesta6+1


                #stred napravo cesta
                if((c+1)<=col and ([r,c+1] not in visited) and maze[r][c+1]==1 and ([r,c+1] not in cesta1) and ([r,c+1] not in cesta2) and ([r,c+1] not in cesta3) and ([r,c+1] not in cesta4) and
                ([r,c+1] not in cesta5) and ([r,c+1] not in cesta7) and ([r,c+1] not in cesta8)):
                    if([r,c+1] in nodes):
                        finded_node=finded_node-1
                        cesta6_nodes=cesta6_nodes+1
                        print("Nasiel som vrchol stred napravo!")
                        node_finded_in_cesta6=[r,c+1]
                    else:
                        frontier.append([r,c+1])
                        cesta6.append([r,c+1])
                        roads.append([r,c+1])
                        cells_added_in_cesta6=cells_added_in_cesta6+1

                #dole napravo cesta
                if((r+1)<=row and (c+1)<=col and  ([r+1,c+1] not in visited) and maze[r+1][c+1]==1 and ([r+1,c+1] not in cesta1) and ([r+1,c+1] not in cesta2) and ([r+1,c+1] not in cesta3) and ([r+1,c+1] not in cesta4) and
                ([r+1,c+1] not in cesta5) and ([r+1,c+1] not in cesta7) and ([r+1,c+1] not in cesta8)):
                    if([r+1,c+1] in nodes):
                        finded_node=finded_node-1
                        cesta6_nodes=cesta6_nodes+1
                        print("Nasiel som vrchol dole napravo!")
                        node_finded_in_cesta6=[r+1,c+1]
                    else:
                        frontier.append([r+1,c+1])
                        cesta6.append([r+1,c+1])
                        roads.append([r+1,c+1])
                        cells_added_in_cesta6=cells_added_in_cesta6+1

                #dole stred cesta
                if((r+1)<=row and ([r+1,c] not in visited) and maze[r+1][c]==1 and ([r+1,c] not in cesta1) and ([r+1,c] not in cesta2) and ([r+1,c] not in cesta3) and ([r+1,c] not in cesta4) and
                ([r+1,c] not in cesta5) and ([r+1,c] not in cesta7) and ([r+1,c] not in cesta8)):
                    if([r+1,c] in nodes):
                        finded_node=finded_node-1
                        cesta6_nodes=cesta6_nodes+1
                        print("Nasiel som vrchol stred dole!")
                        node_finded_in_cesta6=[r+1,c]

                    else:
                        frontier.append([r+1,c])
                        cesta6.append([r+1,c])
                        roads.append([r+1,c])
                        cells_added_in_cesta6=cells_added_in_cesta6+1

                #dole stred nalavo
                if((r+1)<=row and (c-1)>=0 and ([r+1,c-1] not in visited) and maze[r+1][c-1]==1 and ([r+1,c-1] not in cesta1) and ([r+1,c-1] not in cesta2) and ([r+1,c-1] not in cesta3) and ([r+1,c-1] not in cesta4) and
                ([r+1,c-1] not in cesta5) and ([r+1,c-1] not in cesta7) and ([r+1,c-1] not in cesta8)):
                    if([r+1,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta6_nodes=cesta6_nodes+1
                        print("Nasiel som vrchol dole stred nalavo!")
                        node_finded_in_cesta6=[r+1,c-1]
                    else:
                        frontier.append([r+1,c-1])
                        cesta6.append([r+1,c-1])
                        roads.append([r+1,c-1])
                        cells_added_in_cesta6=cells_added_in_cesta6+1

                #stred nalavo cesta
                if((c-1)>=0 and ([r,c-1] not in visited) and maze[r][c-1]==1 and ([r,c-1] not in cesta1) and ([r,c-1] not in cesta2) and ([r,c-1] not in cesta3) and ([r,c-1] not in cesta4) and
                ([r,c-1] not in cesta5) and ([r,c-1] not in cesta7) and ([r,c-1] not in cesta8)):
                    if([r,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta6_nodes=cesta6_nodes+1
                        print("Nasiel som vrchol stred nalavo!")
                        node_finded_in_cesta6=[r,c-1]
                    else:
                        frontier.append([r,c-1])
                        cesta6.append([r,c-1])
                        roads.append([r,c-1])
                        cells_added_in_cesta6=cells_added_in_cesta6+1

                #horna nalavo cesta
                if((r-1)>=0 and (c-1)>=0 and ([r-1,c-1] not in visited) and maze[r-1][c-1]==1 and ([r-1,c-1] not in cesta1) and ([r-1,c-1] not in cesta2) and ([r-1,c-1] not in cesta3) and ([r-1,c-1] not in cesta4) and
                ([r-1,c-1] not in cesta5) and ([r-1,c-1] not in cesta7) and ([r-1,c-1] not in cesta8)):
                    if([r-1,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta6_nodes=cesta6_nodes+1
                        print("Nasiel som vrchol horna nalavo!")
                        node_finded_in_cesta6=[r-1,c-1]
                    else:
                        frontier.append([r-1,c-1])
                        cesta6.append([r-1,c-1])
                        roads.append([r-1,c-1])
                        cells_added_in_cesta6=cells_added_in_cesta6+1

            if (cesta6_nodes)==1:
                print("Nasiel som vrchol pri ceste 6!")

                for t in range(cells_added_in_cesta6):
                    cesta6.pop(-1)
                    frontier.pop(-1)


        #do ktorej cesty mame pridavat!!!-------------------------------------------------------------------------------------------------------------------------7
        if(cesta7_nodes==0):
            if(current in cesta7):
                #horna stred cesta
                if((r-1)>=0 and ([r-1,c] not in visited) and maze[r-1][c]==1 and ([r-1,c] not in cesta1) and ([r-1,c] not in cesta2) and ([r-1,c] not in cesta3) and ([r-1,c] not in cesta4) and
                ([r-1,c] not in cesta5) and ([r-1,c] not in cesta6) and ([r-1,c] not in cesta8)):
                    if([r-1,c] in nodes):
                        finded_node=finded_node-1
                        cesta7_nodes=cesta7_nodes+1
                        print("Nasiel som vrchol horna stred!")
                        node_finded_in_cesta7=[r-1,c]
                    else:
                        frontier.append([r-1,c])
                        cesta7.append([r-1,c])
                        roads.append([r-1,c])
                        cells_added_in_cesta7=cells_added_in_cesta7+1

                #horna napravo cesta
                if((r-1)>=0 and  (c+1)<=col and ([r-1,c+1] not in visited)  and maze[r-1][c+1]==1 and ([r-1,c+1] not in cesta1) and ([r-1,c+1] not in cesta2) and ([r-1,c+1] not in cesta3) and ([r-1,c+1] not in cesta4) and
                ([r-1,c+1] not in cesta5) and ([r-1,c+1] not in cesta6) and ([r-1,c+2] not in cesta8)):
                    if([r-1,c+1] in nodes):
                        finded_node=finded_node-1
                        print("Nasiel som vrchol horna napravo!")
                        cesta7_nodes=cesta7_nodes+1
                        node_finded_in_cesta7=[r-1,c+1]
                    else:
                        frontier.append([r-1,c+1])
                        cesta7.append([r-1,c+1])
                        roads.append([r-1,c+1])
                        cells_added_in_cesta7=cells_added_in_cesta7+1


                #stred napravo cesta
                if((c+1)<=col and ([r,c+1] not in visited) and maze[r][c+1]==1 and ([r,c+1] not in cesta1) and ([r,c+1] not in cesta2) and ([r,c+1] not in cesta3) and ([r,c+1] not in cesta4) and
                ([r,c+1] not in cesta5) and ([r,c+1] not in cesta6) and ([r,c+1] not in cesta8)):
                    if([r,c+1] in nodes):
                        finded_node=finded_node-1
                        cesta7_nodes=cesta7_nodes+1
                        print("Nasiel som vrchol stred napravo!")
                        node_finded_in_cesta7=[r,c+1]
                    else:
                        frontier.append([r,c+1])
                        cesta7.append([r,c+1])
                        roads.append([r,c+1])
                        cells_added_in_cesta7=cells_added_in_cesta7+1

                #dole napravo cesta
                if((r+1)<=row and (c+1)<=col and  ([r+1,c+1] not in visited) and maze[r+1][c+1]==1 and ([r+1,c+1] not in cesta1) and ([r+1,c+1] not in cesta2) and ([r+1,c+1] not in cesta3) and ([r+1,c+1] not in cesta4) and
                ([r+1,c+1] not in cesta5) and ([r+1,c+1] not in cesta6) and ([r+1,c+1] not in cesta8)):
                    if([r+1,c+1] in nodes):
                        finded_node=finded_node-1
                        cesta7_nodes=cesta7_nodes+1
                        print("Nasiel som vrchol dole napravo!")
                        node_finded_in_cesta7=[r+1,c+1]
                    else:
                        frontier.append([r+1,c+1])
                        cesta7.append([r+1,c+1])
                        roads.append([r+1,c+1])
                        cells_added_in_cesta7=cells_added_in_cesta7+1

                #dole stred cesta
                if((r+1)<=row and ([r+1,c] not in visited) and maze[r+1][c]==1 and ([r+1,c] not in cesta1) and ([r+1,c] not in cesta2) and ([r+1,c] not in cesta3) and ([r+1,c] not in cesta4) and
                ([r+1,c] not in cesta5) and ([r+1,c] not in cesta6) and ([r+1,c] not in cesta8)):
                    if([r+1,c] in nodes):
                        finded_node=finded_node-1
                        cesta7_nodes=cesta7_nodes+1
                        print("Nasiel som vrchol stred dole!")
                        node_finded_in_cesta7=[r+1,c]
                    else:
                        frontier.append([r+1,c])
                        cesta7.append([r+1,c])
                        roads.append([r+1,c])
                        cells_added_in_cesta7=cells_added_in_cesta7+1

                #dole stred nalavo
                if((r+1)<=row and (c-1)>=0 and ([r+1,c-1] not in visited) and maze[r+1][c-1]==1 and ([r+1,c-1] not in cesta1) and ([r+1,c-1] not in cesta2) and ([r+1,c-1] not in cesta3) and ([r+1,c-1] not in cesta4) and
                ([r+1,c-1] not in cesta5) and ([r+1,c-1] not in cesta6) and ([r+1,c-1] not in cesta8)):
                    if([r+1,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta7_nodes=cesta7_nodes+1
                        print("Nasiel som vrchol dole stred nalavo!")
                        node_finded_in_cesta7=[r+1,c-1]
                    else:
                        frontier.append([r+1,c-1])
                        cesta7.append([r+1,c-1])
                        roads.append([r+1,c-1])
                        cells_added_in_cesta7=cells_added_in_cesta7+1

                #stred nalavo cesta
                if((c-1)>=0 and ([r,c-1] not in visited) and maze[r][c-1]==1 and ([r,c-1] not in cesta1) and ([r,c-1] not in cesta2) and ([r,c-1] not in cesta3) and ([r,c-1] not in cesta4) and
                ([r,c-1] not in cesta5) and ([r,c-1] not in cesta6) and ([r,c-1] not in cesta8)):
                    if([r,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta7_nodes=cesta7_nodes+1
                        print("Nasiel som vrchol stred nalavo!")
                        node_finded_in_cesta7=[r,c-1]
                    else:
                        frontier.append([r,c-1])
                        cesta7.append([r,c-1])
                        roads.append([r,c-1])
                        cells_added_in_cesta7=cells_added_in_cesta7+1

                #horna nalavo cesta
                if((r-1)>=0 and (c-1)>=0 and ([r-1,c-1] not in visited) and maze[r-1][c-1]==1 and ([r-1,c-1] not in cesta1) and ([r-1,c-1] not in cesta2) and ([r-1,c-1] not in cesta3) and ([r-1,c-1] not in cesta4) and
                ([r-1,c-1] not in cesta5) and ([r-1,c-1] not in cesta6) and ([r-1,c-1] not in cesta8)):
                    if([r-1,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta7_nodes=cesta7_nodes+1
                        print("Nasiel som vrchol horna nalavo!")
                        node_finded_in_cesta7=[r-1,c-1]
                    else:
                        frontier.append([r-1,c-1])
                        cesta7.append([r-1,c-1])
                        roads.append([r-1,c-1])
                        cells_added_in_cesta7=cells_added_in_cesta7+1

            if (cesta7_nodes)==1:
                print("Nasiel som vrchol pri ceste 7!")

                for t in range(cells_added_in_cesta7):
                    cesta7.pop(-1)
                    frontier.pop(-1)


        #do ktorej cesty mame pridavat!!!---------------------------------------------------------------------------------------------------------------------------8
        if(cesta8_nodes==0):
            if(current in cesta8):
                #horna stred cesta
                if((r-1)>=0 and ([r-1,c] not in visited) and maze[r-1][c]==1 and ([r-1,c] not in cesta1) and ([r-1,c] not in cesta2) and ([r-1,c] not in cesta3) and ([r-1,c] not in cesta4) and
                ([r-1,c] not in cesta5) and ([r-1,c] not in cesta6) and ([r-1,c] not in cesta7)):
                    if([r-1,c] in nodes):
                        finded_node=finded_node-1
                        cesta8_nodes=cesta8_nodes+1
                        print("Nasiel som vrchol horna stred!")
                        node_finded_in_cesta8=[r-1,c]
                    else:
                        frontier.append([r-1,c])
                        cesta8.append([r-1,c])
                        roads.append([r-1,c])
                        cells_added_in_cesta8=cells_added_in_cesta8+1

                #horna napravo cesta
                if((r-1)>=0 and  (c+1)<=col and ([r-1,c+1] not in visited)  and maze[r-1][c+1]==1 and ([r-1,c+1] not in cesta1) and ([r-1,c+1] not in cesta2) and ([r-1,c+1] not in cesta3) and ([r-1,c+1] not in cesta4) and
                ([r-1,c+1] not in cesta5) and ([r-1,c+1] not in cesta6) and ([r-1,c+2] not in cesta7)):
                    if([r-1,c+1] in nodes):
                        finded_node=finded_node-1
                        print("Nasiel som vrchol horna napravo!")
                        cesta8_nodes=cesta8_nodes+1
                        node_finded_in_cesta8=[r-1,c+1]
                    else:
                        frontier.append([r-1,c+1])
                        cesta8.append([r-1,c+1])
                        roads.append([r-1,c+1])
                        cells_added_in_cesta8=cells_added_in_cesta8+1


                #stred napravo cesta
                if((c+1)<=col and ([r,c+1] not in visited) and maze[r][c+1]==1 and ([r,c+1] not in cesta1) and ([r,c+1] not in cesta2) and ([r,c+1] not in cesta3) and ([r,c+1] not in cesta4) and
                ([r,c+1] not in cesta5) and ([r,c+1] not in cesta6) and ([r,c+1] not in cesta7)):
                    if([r,c+1] in nodes):
                        finded_node=finded_node-1
                        cesta8_nodes=cesta8_nodes+1
                        print("Nasiel som vrchol stred napravo!")
                        node_finded_in_cesta8=[r,c+1]
                    else:
                        frontier.append([r,c+1])
                        cesta8.append([r,c+1])
                        roads.append([r,c+1])
                        cells_added_in_cesta8=cells_added_in_cesta8+1

                #dole napravo cesta
                if((r+1)<=row and (c+1)<=col and  ([r+1,c+1] not in visited) and maze[r+1][c+1]==1 and ([r+1,c+1] not in cesta1) and ([r+1,c+1] not in cesta2) and ([r+1,c+1] not in cesta3) and ([r+1,c+1] not in cesta4) and
                ([r+1,c+1] not in cesta5) and ([r+1,c+1] not in cesta6) and ([r+1,c+1] not in cesta7)):
                    if([r+1,c+1] in nodes):
                        finded_node=finded_node-1
                        cesta8_nodes=cesta8_nodes+1
                        print("Nasiel som vrchol dole napravo!")
                        node_finded_in_cesta8=[r+1,c+1]
                    else:
                        frontier.append([r+1,c+1])
                        cesta8.append([r+1,c+1])
                        roads.append([r+1,c+1])
                        cells_added_in_cesta8=cells_added_in_cesta8+1

                #dole stred cesta
                if((r+1)<=row and ([r+1,c] not in visited) and maze[r+1][c]==1 and ([r+1,c] not in cesta1) and ([r+1,c] not in cesta2) and ([r+1,c] not in cesta3) and ([r+1,c] not in cesta4) and
                ([r+1,c] not in cesta5) and ([r+1,c] not in cesta6) and ([r+1,c] not in cesta7)):
                    if([r+1,c] in nodes):
                        finded_node=finded_node-1
                        cesta8_nodes=cesta8_nodes+1
                        print("Nasiel som vrchol stred dole!")
                        node_finded_in_cesta8=[r+1,c]
                    else:
                        frontier.append([r+1,c])
                        cesta8.append([r+1,c])
                        roads.append([r+1,c])
                        cells_added_in_cesta8=cells_added_in_cesta8+1

                #dole stred nalavo
                if((r+1)<=row and (c-1)>=0 and ([r+1,c-1] not in visited) and maze[r+1][c-1]==1 and ([r+1,c-1] not in cesta1) and ([r+1,c-1] not in cesta2) and ([r+1,c-1] not in cesta3) and ([r+1,c-1] not in cesta4) and
                ([r+1,c-1] not in cesta5) and ([r+1,c-1] not in cesta6) and ([r+1,c-1] not in cesta7)):
                    if([r+1,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta8_nodes=cesta8_nodes+1
                        print("Nasiel som vrchol dole stred nalavo!")
                        node_finded_in_cesta8=[r+1,c-1]
                    else:
                        frontier.append([r+1,c-1])
                        cesta8.append([r+1,c-1])
                        roads.append([r+1,c-1])
                        cells_added_in_cesta8=cells_added_in_cesta8+1

                #stred nalavo cesta
                if((c-1)>=0 and ([r,c-1] not in visited) and maze[r][c-1]==1 and ([r,c-1] not in cesta1) and ([r,c-1] not in cesta2) and ([r,c-1] not in cesta3) and ([r,c-1] not in cesta4) and
                ([r,c-1] not in cesta5) and ([r,c-1] not in cesta6) and ([r,c-1] not in cesta7)):
                    if([r,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta8_nodes=cesta8_nodes+1
                        print("Nasiel som vrchol stred nalavo!")
                        node_finded_in_cesta8=[r,c-1]
                    else:
                        frontier.append([r,c-1])
                        cesta8.append([r,c-1])
                        roads.append([r,c-1])
                        cells_added_in_cesta8=cells_added_in_cesta8+1

                #horna nalavo cesta
                if((r-1)>=0 and (c-1)>=0 and ([r-1,c-1] not in visited) and maze[r-1][c-1]==1 and ([r-1,c-1] not in cesta1) and ([r-1,c-1] not in cesta2) and ([r-1,c-1] not in cesta3) and ([r-1,c-1] not in cesta4) and
                ([r-1,c-1] not in cesta5) and ([r-1,c-1] not in cesta6) and ([r-1,c-1] not in cesta7)):
                    if([r-1,c-1] in nodes):
                        finded_node=finded_node-1
                        cesta8_nodes=cesta8_nodes+1
                        print("Nasiel som vrchol horna nalavo!")
                        node_finded_in_cesta8=[r-1,c-1]
                    else:
                        frontier.append([r-1,c-1])
                        cesta8.append([r-1,c-1])
                        roads.append([r-1,c-1])
                        cells_added_in_cesta8=cells_added_in_cesta8+1

            if (cesta8_nodes==1):
                print("Nasiel som vrchol pri ceste 8!")
                for t in range(cells_added_in_cesta8):
                    cesta8.pop(-1)
                    frontier.pop(-1)


        #---------------------------------------------------------------------------------------------------
        cells_added_in_cesta1=0
        cells_added_in_cesta2=0
        cells_added_in_cesta3=0
        cells_added_in_cesta4=0
        cells_added_in_cesta5=0
        cells_added_in_cesta6=0
        cells_added_in_cesta7=0
        cells_added_in_cesta8=0
        #---------------------------------------------------------------------------------------------------

        #vykreslenie krok po kroku co sa deje
        # imgplot = plt.imshow(img,alpha=0.5)
        # plt.scatter(current[1],current[0], color='green', s=40)
        # plt.show()
        #vykreslenie krok po kroku co sa deje


        visited.append(current)
        if(len(frontier)!=0):
            frontier.pop(0)
        #print("TU to blbne: ")
        prechod=prechod+1




        #print("Po prechode udaje:-------------------------------------------------------------------------------------")


        #kontrola kruzniceeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
        if(len(frontier)==0 and finded_node>0):
            print("Kruznica!!!!")
            finded_node=0
            kruznica=kruznica+1
        #kontrola kruzniceeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee


        # print("Kolko vrcholov musi byt najdenych po prechode!: ",finded_node)
        # print("Vo frontieri po prechode je: ",frontier)
        # print("V current po prechode je: ",current)
        # print("Vo visited po prechode je: ",visited)




    # print("Visited na konci je: ",visited)
    # print("Frontier na konci je: ",frontier)
    # print("Current je na konci: ",current)



    #vyrobenie kruznicovej cesty
    if(kruznica>0):
        if(len(cesta1)>0 and cesta1_nodes==0):
            if(len(cesta_kruznica)>0):
                cesta1.reverse()
            cesta_kruznica=cesta_kruznica+cesta1
            print("Pripajam cestu 1!!!!!!!!!!!!!!!")
            cesta1=[]
        if(len(cesta2)>0 and cesta2_nodes==0):
            if(len(cesta_kruznica)>0):
                cesta2.reverse()
            cesta_kruznica=cesta_kruznica+cesta2
            print("Pripajam cestu 2!!!!!!!!!!!!!!!")
            cesta2=[]
        if(len(cesta3)>0 and cesta3_nodes==0):
            if(len(cesta_kruznica)>0):
                cesta3.reverse()
            cesta_kruznica=cesta_kruznica+cesta3
            print("Pripajam cestu 3!!!!!!!!!!!!!!!")
            cesta3=[]
        if(len(cesta4)>0 and cesta4_nodes==0):
            if(len(cesta_kruznica)>0):
                cesta4.reverse()
            cesta_kruznica=cesta_kruznica+cesta4
            print("Pripajam cestu 4!!!!!!!!!!!!!!!")
            cesta4=[]
        if(len(cesta5)>0 and cesta5_nodes==0):
            if(len(cesta_kruznica)>0):
                cesta5.reverse()
            cesta_kruznica=cesta_kruznica+cesta5
            print("Pripajam cestu 5!!!!!!!!!!!!!!!")
            cesta5=[]
        if(len(cesta6)>0 and cesta6_nodes==0):
            if(len(cesta_kruznica)>0):
                cesta6.reverse()
            cesta_kruznica=cesta_kruznica+cesta6
            print("Pripajam cestu 6!!!!!!!!!!!!!!!")
            cesta6=[]
        if(len(cesta7)>0 and cesta7_nodes==0):
            if(len(cesta_kruznica)>0):
                cesta7.reverse()
            cesta_kruznica=cesta_kruznica+cesta7
            print("Pripajam cestu 7!!!!!!!!!!!!!!")
            cesta7=[]
        if(len(cesta8)>0 and cesta8_nodes==0):
            if(len(cesta_kruznica)>0):
                cesta8.reverse()
            cesta_kruznica=cesta_kruznica+cesta8
            print("Pripajam cestu 8!!!!!!!!!!!!!!!")
            cesta8=[]
    #vyrobenie kruznicovej cesty


    #---------------vypis ciest na konci prechodu pre jednotlivy vrchol----------------------------------
    #print("\nCESTY NA KONCIIII------------------------------------------------------\n")
    # print("Cesta 1 na konci je: ",cesta1,"a spaja vrchol:",a,"s vrcholom",node_finded_in_cesta1)
    # print("Cesta 2 na konci je: ",cesta2,"a spaja vrchol:",a,"s vrcholom",node_finded_in_cesta2)
    # print("Cesta 3 na konci je: ",cesta3,"a spaja vrchol:",a,"s vrcholom",node_finded_in_cesta3)
    # print("Cesta 4 na konci je: ",cesta4,"a spaja vrchol:",a,"s vrcholom",node_finded_in_cesta4)
    # print("Cesta 5 na konci je: ",cesta5,"a spaja vrchol:",a,"s vrcholom",node_finded_in_cesta5)
    # print("Cesta 6 na konci je: ",cesta6,"a spaja vrchol:",a,"s vrcholom",node_finded_in_cesta6)
    # print("Cesta 7 na konci je: ",cesta7,"a spaja vrchol:",a,"s vrcholom",node_finded_in_cesta7)
    # print("Cesta 8 na konci je: ",cesta8,"a spaja vrchol:",a,"s vrcholom",node_finded_in_cesta8)
    # print("Cesta KRUZNICA na konci je: ",cesta_kruznica,"a spaja vrchol:",a,"s vrcholom",a)
    #---------------vypis ciest na konci prechodu pre jednotlivy vrchol----------------------------------




    #zistujem ci vsetky nody presli, vypis poctu vrcholov
    total_count_of_nodes=total_count_of_nodes+1
    print("Vrchol v poradi: ",total_count_of_nodes,"z poctu vrcholov",len(nodes),"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
    #zistujem ci vsetky nody presli, vypis poctu vrcholov




    #Pridavanie ciest do grafu ciest 1.index je vrchol odkial ide a 2.index je kam smeruje cesta------------------------------------------------------------------------
    if(len(cesta1)!=0):
        final_cesta1 = [a] + cesta1 + [node_finded_in_cesta1]
        GraphEdges.append(final_cesta1)
    if(len(cesta2)!=0):
        final_cesta2 = [a] + cesta2 + [node_finded_in_cesta2]
        GraphEdges.append(final_cesta2)
    if(len(cesta3)!=0):
        final_cesta3 = [a] + cesta3 + [node_finded_in_cesta3]
        GraphEdges.append(final_cesta3)
    if(len(cesta4)!=0):
        final_cesta4 = [a] + cesta4 + [node_finded_in_cesta4]
        GraphEdges.append(final_cesta4)
    if(len(cesta5)!=0):
        final_cesta5 = [a] + cesta5 + [node_finded_in_cesta5]
        GraphEdges.append(final_cesta5)
    if(len(cesta6)!=0):
        final_cesta6 = [a] + cesta6 + [node_finded_in_cesta6]
        GraphEdges.append(final_cesta6)
    if(len(cesta7)!=0):
        final_cesta7 = [a] + cesta7 + [node_finded_in_cesta7]
        GraphEdges.append(final_cesta7)
    if(len(cesta8)!=0):
        final_cesta8 = [a] + cesta8 + [node_finded_in_cesta8]
        GraphEdges.append(final_cesta8)
    if(len(cesta_kruznica)!=0):
        final_cesta_kruznica = [a] + cesta_kruznica + [a]
        GraphEdges.append(final_cesta_kruznica)
        GraphEdges.append(final_cesta_kruznica[::-1])

    # for g in GraphEdges:
    #   print("Hrany medzi vrcholmi:",g)
    #Pridavanie ciest do grafu ciest 1.index je vrchol odkial ide a 2.index je kam smeruje cesta------------------------------------------------------------------------




    #pridavanie do grafu susedne vrcholy---------------------------------------------
    if(cesta1_nodes!=0):
        GraphNodes[a[0],a[1]].append(node_finded_in_cesta1)
    if(cesta2_nodes!=0):
        GraphNodes[a[0],a[1]].append(node_finded_in_cesta2)
    if(cesta3_nodes!=0):
        GraphNodes[a[0],a[1]].append(node_finded_in_cesta3)
    if(cesta4_nodes!=0):
        GraphNodes[a[0],a[1]].append(node_finded_in_cesta4)
    if(cesta5_nodes!=0):
        GraphNodes[a[0],a[1]].append(node_finded_in_cesta5)
    if(cesta6_nodes!=0):
        GraphNodes[a[0],a[1]].append(node_finded_in_cesta6)
    if(cesta7_nodes!=0):
        GraphNodes[a[0],a[1]].append(node_finded_in_cesta7)
    if(cesta8_nodes!=0):
        GraphNodes[a[0],a[1]].append(node_finded_in_cesta8)
    if(len(cesta_kruznica)!=0):
        GraphNodes[a[0],a[1]].append(a)

    # for x in GraphNodes:
      # print("Vrchol a vrcholy susediace:",x,":",GraphNodes[x])
    #pridavanie do grafu susedne vrcholy---------------------------------------------




    final_roads=[]
    final_roads=cesta1+cesta2+cesta3+cesta4+cesta5+cesta6+cesta7+cesta8+cesta_kruznica




#zobrazovanie ciest
    #zobrazovanie---------------------------------------------
    array=(np.argwhere(img2 == 255)) #-uzly zaznacit

    final_roads=np.asarray(final_roads)
    cesta1=np.asarray(cesta1)
    cesta2=np.asarray(cesta2)
    cesta3=np.asarray(cesta3)
    cesta4=np.asarray(cesta4)
    cesta5=np.asarray(cesta5)
    cesta6=np.asarray(cesta6)
    cesta7=np.asarray(cesta7)
    cesta8=np.asarray(cesta8)
    cesta_kruznica=np.asarray(cesta_kruznica)


    # #odkopirovat pre kontrolu!!!!!!!!!!!!!
    # imgplot = plt.imshow(img,alpha=0.5)
    # imgplot = plt.imshow(img2,alpha=0.5)
    #
    # # #vytvor scatter pre kazdu cestu inou farbou!!!!!!!!!!!!!!!!!!!!
    # if(len(cesta1)>0):
    #          plt.scatter(cesta1[:,[1]],cesta1[:,[0]], color='green', s=40)
    #          plt.scatter(node_finded_in_cesta1[1],node_finded_in_cesta1[0], color='red', s=40)
    #          plt.scatter(current_node[1],current_node[0], color='blue', s=40)
    # if(len(cesta2)>0):
    #          plt.scatter(cesta2[:,[1]],cesta2[:,[0]], color='#EDB120', s=40)
    #          plt.scatter(node_finded_in_cesta2[1],node_finded_in_cesta2[0], color='red', s=40)
    #          plt.scatter(current_node[1],current_node[0], color='blue', s=40)
    #
    # if(len(cesta3)>0):
    #          plt.scatter(cesta3[:,[1]],cesta3[:,[0]], color='cyan', s=40)
    #          plt.scatter(node_finded_in_cesta3[1],node_finded_in_cesta3[0], color='red', s=40)
    #          plt.scatter(current_node[1],current_node[0], color='blue', s=40)
    #
    # if(len(cesta4)>0):
    #          plt.scatter(cesta4[:,[1]],cesta4[:,[0]], color='#A2142F', s=40)
    #          plt.scatter(node_finded_in_cesta4[1],node_finded_in_cesta4[0], color='red', s=40)
    #          plt.scatter(current_node[1],current_node[0], color='blue', s=40)
    # if(len(cesta5)>0):
    #          plt.scatter(cesta5[:,[1]],cesta5[:,[0]], color='yellow', s=40)
    #          plt.scatter(node_finded_in_cesta5[1],node_finded_in_cesta5[0], color='red', s=40)
    #          plt.scatter(current_node[1],current_node[0], color='blue', s=40)
    # if(len(cesta6)>0):
    #          plt.scatter(cesta6[:,[1]],cesta6[:,[0]], color='black', s=40)
    #          plt.scatter(node_finded_in_cesta6[1],node_finded_in_cesta6[0], color='red', s=40)
    #          plt.scatter(current_node[1],current_node[0], color='blue', s=40)
    # if(len(cesta7)>0):
    #          plt.scatter(cesta7[:,[1]],cesta7[:,[0]], color='#0072BD', s=40)
    #          plt.scatter(node_finded_in_cesta7[1],node_finded_in_cesta7[0], color='red', s=40)
    #          plt.scatter(current_node[1],current_node[0], color='blue', s=40)
    # if(len(cesta8)>0):
    #          plt.scatter(cesta8[:,[1]],cesta8[:,[0]], color='#D95319', s=40)
    #          plt.scatter(node_finded_in_cesta8[1],node_finded_in_cesta8[0], color='red', s=40)
    #          plt.scatter(current_node[1],current_node[0], color='blue', s=40)
    # if(len(cesta_kruznica)>0):
    #          plt.scatter(cesta_kruznica[:,[1]],cesta_kruznica[:,[0]], color='#77AC30', s=40)
    #          plt.scatter(a[1],a[0], color='red', s=40)
    #          plt.scatter(current_node[1],current_node[0], color='blue', s=40)
    #     #vytvor scatter pre kazdu cestu inou farbou!!!!!!!!!!!!!!!!!!!!
    #
    #
    #     # if(len(final_roads)>0):
    #     #     plt.scatter(array[:,[1]],array[:,[0]], color='red', s=40)
    #     #     plt.scatter(final_roads[:,[1]],final_roads[:,[0]], color='green', s=40)
    #     #     plt.scatter(current_node[1],current_node[0], color='blue', s=40)
    #
    # plt.show()
    # # #odkopirovat pre kontrolu!!!!!!!!!!!!!
#zobrazovanie ciest



#vypis grafu s hranami
count_hrany=0
for u in GraphEdges:
    count_hrany=count_hrany+1
    print("Hrany medzi vrcholmi",u,"poradie hrany:",count_hrany)
#vypis grafu s hranami


# #zobrazenie hran vysledkych
# for edge in GraphEdges:
#     if(edge[0]==[454,72]):
#         imgplot = plt.imshow(img,alpha=0.5)
#         imgplot = plt.imshow(img2,alpha=0.5)
#         edge=np.asarray(edge)
#         plt.scatter(edge[:,1],edge[:,0], color='red', s=40)
#         plt.show()
# #zobrazenie hran vysledkych
#
#















#vypis grafu s vrcholmi
#print("GRAF KTORY POZOSTAVA Z VRCHOLOV JE:",GraphNodes)
pocitanie=0
for x in GraphNodes:
  pocitanie=pocitanie+1
  print("Vrchol v poradi:",pocitanie,"!!!!",x,":",GraphNodes[x])
#vypis grafu s vrcholmi




#najdeme bridges hrany v grafe
for e in GraphEdges:
    maze_copy_bridges=maze.copy()
    structure = np.ones((3, 3), dtype=np.int)
    labeled, ncomponents = label(maze_copy_bridges, structure)
    pocet_komponentov_pred=ncomponents
    for t in range(1,len(e)-1):
        maze_copy_bridges[e[t][0]][e[t][1]]=0
    structure = np.ones((3, 3), dtype=np.int)
    labeled, ncomponents = label(maze_copy_bridges, structure)
    pocet_komponentov_po=ncomponents
    if(pocet_komponentov_pred!=pocet_komponentov_po):
        #print("Nasiel som bridge!!!!!",pocet_komponentov_pred,pocet_komponentov_po)
        Bridges_Edges.append(e)
    #print("---------------------------------------------")
for e in Bridges_Edges:
    print(e)
#najdeme bridges hrany v grafe

#vykres hran bridges
# imgplot = plt.imshow(maze,cmap='gray')
#
# for t in Bridges_Edges:
#     rgb = np.random.rand(3,)
#     for x in range(1,len(t)-1):
#         plt.scatter(t[x][1],t[x][0], color=rgb, s=40)
#
# plt.show()
# # #vykres hran bridges


#customizable edges

copy_of_All_EDGES_without_bridges=[]
copy_of_ALL_EDGES_help=GraphEdges.copy()
for edge in copy_of_ALL_EDGES_help:
    if(edge not in Bridges_Edges):
        copy_of_All_EDGES_without_bridges.append(edge)

print(copy_of_All_EDGES_without_bridges)


Pixels_of_customizable_help = []
with open('Skeleton1-SkeletonFinal.csv', 'r') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        Pixels_of_customizable_help.append([int(val) for val in row])
print(Pixels_of_customizable_help)

kontrola=0
Pixels_of_Customizable=[]
#deleting nodes from pixels
for pixel in Pixels_of_customizable_help:
        if(pixel in nodes):
            kontrola=kontrola+1
        else:
            Pixels_of_Customizable.append(pixel)
print(kontrola,len(Pixels_of_customizable_help),len(Pixels_of_Customizable))


# kontrolus=0
# for pixel in Pixels_of_Customizable:
#     kontrolus=0
#     for edge in copy_of_All_EDGES_without_bridges:
#         if((pixel in edge) and (edge not in Customizable_Edges)):
#             kontrolus=kontrolus+1
#             Customizable_Edges.append(edge)
#             print(edge)
#             print(pixel,":",kontrolus)


#skuska noveho programu
for edge in copy_of_All_EDGES_without_bridges:
    pocet_rovnajucich=0
    pocet_policok=len(edge)-2
    for index in range(1,len(edge)-1):
        if(edge[index] in Pixels_of_Customizable):
            pocet_rovnajucich=pocet_rovnajucich+1

    if(pocet_rovnajucich==pocet_policok):
        Customizable_Edges.append(edge)

#skuska noveho programu

#customizable edges

#vyrobenie obrazku pre kontrolu-------------------------------------------------------------------------------------------KONTROLA


img20=cv2.imread('dilation_for_ROADS_robot.pgm',cv2.IMREAD_COLOR)
img22=cv2.imread('cleaned_skeleton_ROADS_robot.pgm',0)
help=img20.copy()
for i in range(len(img22)):
    for j in range(len(img22[i])):
        if(img22[i][j]!=0):
            help[i][j]=[0,0,255]
cv2.imwrite('dilat3-skeleton3.png', help)

img30=cv2.imread('dilation_for_ROADS_robot.pgm',0)
img40=cv2.imread('dilat3-skeleton3.png',cv2.IMREAD_COLOR)

for i in range(len(img30)):
    for j in range(len(img30[i])):
        if(img30[i][j]==1 and img40[i][j][2]!=255):
            img40[i][j]=[255,255,255]
cv2.imwrite('dilat3-skeleton3.png', img40)
######################################################################

img90=cv2.imread('dilat3-skeleton3.png',cv2.IMREAD_COLOR)
for c in Bridges_Edges:
    for f in range(1,len(c)-1):
        img90[c[f][0],c[f][1]]=[129,0,0]

cv2.imwrite('spolocneDVA.png', img90)

img80=cv2.imread('spolocneDVA.png',cv2.IMREAD_COLOR)
for t in Customizable_Edges:
    for x in range(1,len(t)-1):
        img80[t[x][0],t[x][1]]=[0,255,0]

cv2.imwrite('spolocneDVA.png', img80)


######################################################################
img10=mpimg.imread('spolocneDVA.png')



imgplot = plt.imshow(img10, cmap='gray',extent=[0,19,0,124])
plt.title("Bridges a voliteľné hrany")
plt.xlabel("Súradnica x [m]")
plt.ylabel("Súradnica y [m]")
plt.ylim(90,100)




plt.show()









#vyrobenie obrazku pre kontrolu-------------------------------------------------------------------------------------------KONTROLA

#vykreslenie pre kontrolu bridges and customizable
#imgplot = plt.imshow(maze,cmap='gray')

imgplot = plt.imshow(img,alpha=0.5)
imgplot = plt.imshow(img2,alpha=0.5)

for t in Customizable_Edges:
    for x in range(1,len(t)-1):
        plt.scatter(t[x][1],t[x][0], color='red', s=40)

for c in Bridges_Edges:
    for f in range(1,len(c)-1):
        plt.scatter(c[f][1],c[f][0], color='blue', s=40)

plt.show()
#



#vykreslenie pre kontrolu bridges and customizable
#final!!!
