当前位置:   article > 正文

python 深度优先最短路径,广度优先最短路径,dijkstra最短路径及可视化_以添图的方式实现最短路径可视化

以添图的方式实现最短路径可视化

简要说明

通过使用python实现图的可视化,以及广度优先的最短距离,深度优先的最短路径,dijkstra的最短路径

dijkstra最短路径

在这里插入图片描述

深度优先最短路径

在这里插入图片描述
深度优先忘记标路径的边及方向

广度优先最短路径

在这里插入图片描述
这个广度优先可能有点BUG,有时候是对的有时候有错,因为刚检查完,忙着复习期末考试,没时间调了

操作说明

1.需要安装graphviz,这是可视化的库

2.需要安装opencv,因为要读写图片

3.当开始运行程序时,首先要点击“初始化画图”然后再点击“画图”才显示出图片

这是初始化图后的显示
在这里插入图片描述

4.当你每次要操作其他功能的时候,都要先“清空”然后“初始化画图”,及画图,这样一轮循环。比如你先填如初始节点“a”,目的节点“v”,点击“dijkstra最短路径”(dijkstra是我最后加的文本)

在这里插入图片描述

然后当你要再要选择广度优先或者深度优先填入节点时,首先要点击“清空”,然后再“初始化画图”,再“画图”,就是重新初始化,然后就可以进行其他的操作,每次都要循环这个操作

点击“清空”

跟原来一样,没变化

在这里插入图片描述

然后“初始化画图”,以及画图

在这里插入图片描述

这样就重新初始化成功了。可以进行别的操作

可以进行节点的添加,和边的添加,但没有删除功能

5.添加节点“yyy”,然后点击“添加节点”,然后“画图”

在这里插入图片描述

图中出现“yyy”的节点,这是可视化graphviz自动分配的位置,下一步添加边和权值,输入要连接的节点名称和权值,然后点击“添加边”,以及“画图”

在这里插入图片描述

如果想删除或恢复原来的状态就进行重新初始化操作,跟上面提到的一样

代码

#好像没什么注释,有时间再补吧

import cv2
import copy
import time
import graphviz as gv
import tkinter
from tkinter import *
tk=tkinter.Tk()

g = gv.Graph('1',format='png',edge_attr ={},node_attr={"fixedsize":"shape"},engine='neato')

g1=gv.Graph("2",format='pdf',engine='neato')
tk.geometry("1500x800")

e=tkinter.Entry(tk)
e.place(x=0,y=0)
edge=tkinter.Entry(tk)
edge1=tkinter.Entry(tk)
edge2=tkinter.Entry(tk)
breath=tkinter.Entry(tk,width=10)
breath1=tkinter.Entry(tk,width=10)
breath2=tkinter.Entry(tk,width=10)
depth=tkinter.Entry(tk,width=10)
depth1=tkinter.Entry(tk,width=10)
depth2=tkinter.Entry(tk,width=10)
djstla=tkinter.Entry(tk,width=10)
djstla1=tkinter.Entry(tk,width=10)
edge.place(x=0,y=60)
edge1.place(x=150,y=60)
edge2.place(x=300,y=60)
breath2.place(x=100,y=290)
breath.place(x=0,y=190)
breath1.place(x=100,y=190)

depth.place(x=0,y=340)
depth1.place(x=100,y=340)
depth2.place(x=100,y=240)
djstla.place(x=200,y=90)
djstla1.place(x=300,y=90)
menu0=Menu(tk)#参数是父级控件
list1=['a','b','c','d','e','f','g','h','l','m','n','o','p','q','r','s','t','u','v','w','x','a1','b1','c1','d1','e1','f1','g1','a2','b2']
length=30
adrect=[[0,25,-1,60,-1,-1,-1,20,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
        [25,0,50,30,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
        [-1,50,0,-1,12,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
        [60,30,-1,0,30,10,50,-1,-1,-1,-1,-1,-1,-1,45,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
        [-1,-1,12,30,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
        [-1,-1,-1,10,-1,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
        [-1,-1,-1,50,-1,-1,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
        [20,25,-1,60,-1,-1,-1,0,25,-1,60,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
        [-1,-1,-1,-1,-1,-1,-1,25,0,50,30,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
        [-1,-1,-1,-1,-1,-1,-1,-1,50,0,-1,12,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
        [-1,-1,-1,-1,-1,-1,-1,60,30,-1,0,12,10,50,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
        [-1,-1,-1,-1,-1,-1,-1,-1,-1,12,12,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,20,-1,-1,-1,-1,-1,-1,-1,-1],
        [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,10,-1,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
        [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,50,-1,-1,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
        [-1,-1,-1,45,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,25,-1,60,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
        [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,25,0,50,30,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
        [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,50,0,-1,12,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
        [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,60,30,-1,0,12,10,50,-1,-1,-1,-1,-1,-1,-1,-1,-1],
        [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,12,12,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,5,-1],
        [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,10,-1,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
        [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,50,-1,-1,0,-1,-1,-1,-1,-1,-1,-1,-1,-1],
        [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,20,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,25,-1,60,-1,-1,-1,-1,-1],
        [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,25,0,50,30,-1,-1,-1,-1,-1],
        [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,50,0,-1,12,-1,-1,-1,-1],
        [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,60,30,-1,0,12,10,50,-1,-1],
        [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,12,12,0,-1,-1,-1,10],
        [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,10,-1,0,-1,-1,-1],
        [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,50,-1,-1,0,-1,-1],
        [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,5,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,2],
        [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,10,-1,-1,2,-1],
        ]#初始化邻近矩阵
list2={}

j=0
for i in list1:
    list2[i]=j
    j=j+1
print(list2)

global minshu
minshu=9999999
def lab():
    print("1")
for x in ['文件','编辑','视图']:
    menu0.add_command(label=x,command=lab)#添加3个一级菜单
tk['menu'] = menu0
def addpoint():
     a=e.get()
     if(a not in list1):
        g.node(name=a)

        list1.append(a)
        list3 = []
        for i in range(len(list1)-1):
            adrect[i].append(-1)
        for j in range(len(list1)-1):
            list3.append(-1)
        list3.append(0)
        adrect.append(list3)
        list2.update({a:len(list1)-1})
     print(list1)
     print(adrect)
     print(list2)

button=tkinter.Button(tk,text="添加节点",command=addpoint)
button.place(x=200,y=0)
def addedge():
    a=edge.get()
    b=edge1.get()
    c=edge2.get()
    if( adrect[list2[a]][list2[b]]==-1):
        adrect[list2[a]][list2[b]]=int(c)
        adrect[list2[b]][list2[a]]=int(c)
    g.edge(a,b,c)

    print(adrect)
lab1=tkinter.Label(tk,text="边的权值")
lab1.place(x=350,y=30)
lab2=tkinter.Label(tk,text="第一个点")
lab2.place(x=200,y=30)
lab3=tkinter.Label(tk,text="第二个点")
lab3.place(x=50,y=30)
button2=tkinter.Button(tk,text="  添加边  ",command=addedge)
button2.place(x=500,y=50)

def drawpicture():
    # g.view()
    g.render("1.gv",view=False)
    img = cv2.imread("1.gv.png")
    #h = cv2.resize(img, (600, 750))
    cv2.imwrite("img1.png", img)
    render=tkinter.PhotoImage(file="./img1.png")
    la=tkinter.Label(tk,image=render,width=800,height=800)
    la.place(x=700,y=0)
    tk.mainloop()
button1=tkinter.Button(tk,text="  画图  ",command=drawpicture)
button1.place(x=620,y=50)
def firstdrawpicture():
    #g.attr('graph',{"size": "7,7", "ratio ": "expand"})
    g.attr(size='7,7')
    #g.attr('graph',mindist="0.2")
    g.attr('node', fontsize='10',style='filled')
    g.attr('node',width='0.4',height='0.4')

    g.attr('node',fixedsize='shape', fontsize = '16')
    #g.attr('edge',len="5", fontsize = '10')
    g.node(name='a')
    g.node(name='b')
    g.node(name='a2')
    g.node(name='b2')
    g.node(name='c')
    g.node(name='d')
    g.node(name='e')
    g.node(name='f')
    g.node(name='g')
    g.node(name='a1')
    g.node(name='b1')
    g.node(name='c1')
    g.node(name='d1')
    g.node(name='e1')
    g.node(name='f1')
    g.node(name='g1')
    g.node(name='h')
    g.node(name='l')
    g.node(name='m')
    g.node(name='n')
    g.node(name='o')
    g.node(name='p')
    g.node(name='q')
    g.node(name='r')
    g.node(name='s')
    g.node(name='t')
    g.node(name='u')
    g.node(name='v')
    g.node(name='w')
    g.node(name='x')
    g.edge('a','b',label='25')
    g.edge('b','c',label='50')
    g.edge('b', 'd',label='30')
    g.edge('a', 'd',label='60')
    g.edge('d', 'g',label='50')
    g.edge('d', 'e',label='30')
    g.edge('d', 'f',label='10')
    g.edge('c', 'e', label='12')
    g.edge('a1', 'b1', label='25')
    g.edge('b1', 'c1', label='50')
    g.edge('b1', 'd1', label='30')
    g.edge('a1', 'd1', label='60')
    g.edge('d1', 'g1', label='50')
    g.edge('d1', 'e1', label='12')
    g.edge('d1', 'f1', label='10')
    g.edge('c1', 'e1', label='12')
    g.edge('h', 'l', label='25')
    g.edge('l', 'm', label='50')
    g.edge('l', 'n', label='30')
    g.edge('h', 'n', label='60')
    g.edge('n', 'q', label='50')
    g.edge('n', 'o', label='12')
    g.edge('n', 'p', label='10')
    g.edge('m', 'o', label='12')
    g.edge('a', 'h', label='20')

    g.edge('r', 's', label='25')
    g.edge('s', 't', label='50')
    g.edge('s', 'u', label='30')
    g.edge('r', 'u', label='60')
    g.edge('u', 'x', label='50')
    g.edge('u', 'v', label='12')
    g.edge('u', 'w', label='10')
    g.edge('t', 'v', label='12')
    g.edge('d','r',label='45')
    g.edge('a2', 'b2', label='2')
    g.edge('o', 'a1', label='20')
    g.edge('b2', 'e1', label='10')
    g.edge('a2', 'v', label='5')

    g.render("1.gv", view=False)

button3=tkinter.Button(tk,text="初始化画图",command=firstdrawpicture)
button3.place(x=620,y=0)
#queue=[]
#res=[]
#visited=[False]*(len(list1))
def bfs(queue,res,visited,dis,b,res1):
    global  minp
    global ggh,path11
    ggh={}
    ggh[queue[0]]=0
    minp=9999999
    path11={}
    #gr=queue
    while len(queue) > 0:
        key= queue.pop(0)
        res1.append(key)
        #print("ffff",queue)
        #gr.append(key)
        if(key==b):
            print(dis)
            break
        print(key)
        i=list2[key]
        for j in range(len(list1)):
            if adrect[i][j]> 0 and visited[j] == False:
                res.append(list1[j])
                ggh[list1[j]]=adrect[i][j]
                visited[j] = True
                for jj in range(len(list1)):
                    if (adrect[list2[list1[j]]][jj] > 0 and visited[jj] == True):
                        ggh[list1[j]] = adrect[list2[list1[j]]][jj] + ggh[list1[jj]]
                        path11[list1[j]]=list1[jj]
                for ii in range(len(list1)):
                    if(adrect[list2[list1[j]]][ii]>0 and visited[ii]==True):
                        if(adrect[list2[list1[j]]][ii]+ggh[list1[ii]]<ggh[list1[j]]):
                            ggh[list1[j]]=adrect[list2[list1[j]]][ii]+ggh[list1[ii]]
                            path11[list1[j]] = list1[ii]
                dis=dis+adrect[i][j]
                queue.append(list1[j])
                print(queue)


def breath_serach():
    queue = []
    res = []
    res1=[]
    visited = [False] * (len(list1))
    start = time.time()
    a=breath.get()
    b=breath1.get()
    if len(list1) <= 0:
        return res
    else:
        queue.append(a)  # 节点名称
        visited[list2[a]] = True#节点所在的位置号
        res.append(a)
        bfs(queue,res,visited,0,b,res1)
    print(res)
    print(res1)
    print(ggh)
    print(path11)
    rr=[]
    def router(b):
        if(b!=a):
            for ji in path11:
                if(b==ji):
                    rr.append(b)
                    router(path11[b])
    router(b)
    rr.append(a)
    rr.reverse()
    print(rr)
    end=time.time()
    zuiduanluji=ggh[b]
    print(zuiduanluji)
    label10 = tkinter.Label(tk, text="最短路径:"+str(zuiduanluji))
    label10.place(x=0, y=220)
    label11 = tkinter.Label(tk, text="运行时间" + str(end-start))
    label11.place(x=100, y=220)
    '''
    str1=''
    for jk in res:
        j=0
        str1=str(str1)+jk
        j=j+1
    #   if(j>=len(res)-2):
            break
        else:
            str1 = str1 + '->'
        print(str1)
    label1=tkinter.Label(tk, text=str1)
    label1.place(x=0,y=270)'''
    '''list4 = {}
    j = 0
    for ii in res:
        list4[j] = ii
        j = j + 1'''

    for i in range(len(rr) - 1):
        g.attr('edge', dir='forward')
        g.edge(rr[i], rr[i + 1], color='red')
        g.attr('edge', dir='forward')
        g.node(rr[i], shape='doublecircle', style='filled', color='red')
    g.node(rr[len(rr) - 1], shape='doublecircle', style='filled', color='red')
    g.render("2.gv", view=False)
    #img = cv2.imread("2.gv.png")
    #h = cv2.resize(img, (300, 565))
    # cv2.imwrite("img.png", h)
    #render = tkinter.PhotoImage(file="./img.png")
    render = tkinter.PhotoImage(file="./2.gv.png")
    la = tkinter.Label(tk, image=render, width=800, height=800)
    la.place(x=700, y=0)
    tk.mainloop()

button4=tkinter.Button(tk,text="广度优先搜素最短路径",command=breath_serach)
button4.place(x=200,y=190)
def bfs2(queue,res,visited):
    while len(queue) > 0:
        key= queue.pop(0)
        i=list2[key]
        for j in range(len(list1)):
            if adrect[i][j]> 0 and visited[j] == False:
                res.append(list1[j])
                visited[j] = True
                queue.append(list1[j])
def breath_serach1():
    queue = []
    res = []
    visited = [False] * (len(list1))
    a=breath2.get()
    if len(list1) <= 0:
        return res
    else:
        queue.append(a)  # 节点名称
        visited[list2[a]] = True#节点所在的位置号
        res.append(a)
        bfs2(queue,res,visited)
    print(res)
    str1=''
    for jk in res:
        j=0
        str1=str(str1)+jk
        j=j+1
        if(j>=len(res)-2):
            break
        else:
            str1 = str1 + '->'
        print(str1)
    label1=tkinter.Label(tk, text=str1)
    label1.place(x=0,y=320)
    list4 = {}
    j = 0
    for ii in res:
        list4[j] = ii
        j = j + 1

    for i in range(len(res) - 1):
        g.attr('edge', dir='forward')
        g.edge(list4[i], list4[i + 1], color='red')

        g.attr('edge', dir='forward')
    g.node(list4[0], shape='doublecircle', style='filled', color='red')
    g.render("2.gv", view=False)
    # img = cv2.imread("2.gv.png")
    # h = cv2.resize(img, (300, 565))
    # cv2.imwrite("img.png", h)
    # render = tkinter.PhotoImage(file="./img.png")
    render = tkinter.PhotoImage(file="./2.gv.png")
    la = tkinter.Label(tk, image=render, width=800, height=800)
    la.place(x=700, y=0)
    tk.mainloop()

button41=tkinter.Button(tk,text="广度优先搜索",command=breath_serach1)
button41.place(x=200,y=290)
global book,path,min,minn,hhh,pp,dis,aaa,yyy,pk
minn=[]
hhh=[]
aaa=[]
min1 = 9999999
def dfss(cur,dis,aim,book,path,pk):
    global min1,pp,yyy

    if(aim==cur):
        #for i in range(list2['a'],len(list1)):
            #if( path[i]):
        #print(path)
        #print(dis)
        if min1>dis:
            min1=dis
            print(path)
            print(pk)
            pp=copy.copy(path)

            #print(pp)
            #print(path)

            minn.append(min1)
            jp=sorted(minn)
            yyy=jp[0]
            while len(minn)>0:
                minn.pop()
            #print(minn)

    global flag
    tt = 0
    for h  in range(list2['a'],len(list1)):

        if(adrect[list2[cur]][h]>0  and book[h]==0):
            book[h]=1
            path[h]=list1[h]
            #print(tt)
            pk[tt]=list1[h]
            '''for hk in range(len(pk)):
                if(pk[hk]==list1[h]):
                    flag=hk
                    print(flag)'''
            #print(pk)
            #print(book)
            #print(path)
            dfss(list1[h],dis+adrect[list2[cur]][h],aim,book,path,pk)
            book[h]=0
            path[h]=0
            pk[tt]=0
        tt=tt+1

    #print(book)
    #print(path)

def dfs1():
    global book,path,pp,pk

    pk= [0] * len(list1)
    book = [0] * len(list1)
    path = [0] * len(list1)
    a=depth.get()
    book[list2[a]]=1
    pk[list2[a]]=1
    b=depth1.get()
    path[list2[a]]=a
    start=time.time()

    dfss(a,0,b,book,path,pk)
    #aaa=sorted(minn)
    #print(minn)
    print(yyy)
    hp=yyy
    end=time.time()
    #while len(aaa)>0:
     #   aaa.pop()
    #for ii in minn:
    #    aaa.append(ii)
    #print(sorted(aaa))
    #hp=aaa[0]
    str1="最短路径"
    #print(minn[aaa[0]])
    label1 = tkinter.Label(tk, text=str1+str(hp)+"  ")
    label1.place(x=0, y=370)
    label12 = tkinter.Label(tk, text="运行时间:" + str(end-start))
    label12.place(x=100, y=370)
    #print(pp)

    while len(hhh)>0:
        hhh.pop()
        #print(hhh)

    for jj in pp:
        if(jj!=0):
            hhh.append(jj)
    print(hhh)
    ght = {}
    ghtt=[]
    jkp=0
    for ty in hhh:
        ght[ty] = jkp
        ghtt.append(ty)
        jkp = jkp + 1
    print(ght)
    print(ghtt)
    qw={}
    qw1={}
    gr={}
    for ih in hhh:
        qw1[ih]=0
    qw[a]=0
    global ghq
    ghq=copy.deepcopy(hhh)
    def gy(a):
        visted=[False]*len(hhh)
        while(len(ghq)>0):
            key = ghq.pop(0)
            visted[ght[key]]=True
            for ij in range(len(hhh)):
                if(adrect[list2[key]][list2[hhh[ij]]]>0 and visted[ij]==False):
                    if(qw1[hhh[ij]]>0):
                        qw1[hhh[ij]]=qw1[hhh[ij]]
                    else:
                        qw1[hhh[ij]]=adrect[list2[key]][list2[hhh[ij]]]
                if(qw1[hhh[ij]]>qw1[key]+adrect[list2[key]][list2[hhh[ij]]] and visted[ij]==False):
                    qw1[hhh[ij]]=qw1[key]+adrect[list2[key]][list2[hhh[ij]]]
                    gr[hhh[ij]]=key
                if(adrect[list2[key]][list2[hhh[ij]]]>0 and visted[ij]==False):
                    qw1[hhh[ij]]=qw1[key]+adrect[list2[key]][list2[hhh[ij]]]
                    gr[hhh[ij]] = key

    gy(a)
    print(qw1)
    print(gr)
    routlist=[]
    if(b in gr.keys()):
        def rout(b):
            if(b!=a):
                for key in gr:
                    if(b==key):
                        print(gr[key])
                        routlist.append(gr[key])
                rout(gr[b])
        rout(b)
        routlist.insert(0,b)
        routlist.reverse()
        print(routlist)
    else:
        def rout(a):
            if(b!=a):
                for key in gr:
                    if(a==key):
                        print(gr[key])
                        routlist.append(gr[key])
                rout(gr[a])
        rout(a)
        routlist.insert(0,a)
        print(routlist)

    # print(pk)
    #for i in hhh:
       # for j in hhh:
          #  if(adrect[list2[i]][list2[j]]>0):
            #    g.edge(list1[list2[i]], list1[list2[j]], color='red')
    for i in range(len(hhh) - 1):
        #g.attr('edge', dir='forward')
       # g.edge(routlist[i], routlist[i + 1], color='red')
        #g.attr('edge', dir='forward')
        g.node(hhh[i], shape='doublecircle',style='filled',color='red')
    g.node(hhh[len(hhh)-1],shape='doublecircle',style='filled',color='red')
    g.render("2.gv", view=False)
    render = tkinter.PhotoImage(file="./2.gv.png")
    la = tkinter.Label(tk, image=render, width=800, height=800)
    la.place(x=700, y=0)
    tk.mainloop()

def dfs(res,visited,a,juli,temp):

    res.append(a)
    juli[a]=temp
    visited[list2[a]] = True
    for j in range(len(list1)):
        if adrect[list2[a]][j] > 0 and visited[j] == False:
            temp1=temp+adrect[list2[a]][j]
            dfs(res,visited,list1[j],juli,temp1)
def depth_serach():
    res=[]
    juli={}
    temp=0
    visited=[False]*len(list1)
    a=depth2.get()

    dfs(res,visited,a,juli,temp)
    print(juli)

    print(res)
    str1 = ''
    for jk in res:
        j = 0
        str1 = str(str1) + jk
        j = j + 1
        if (j >= len(res) - 2):
            break
        else:
            str1 = str1 + '->'
        print(str1)
    label1 = tkinter.Label(tk, text=str1)
    label1.place(x=0, y=270)
    list4 = {}
    j = 0
    for ii in res:
        list4[j] = ii
        j = j + 1

    for i in range(len(res)-1):
        g.attr('edge', dir='forward')
        g.edge(list4[i],list4[i+1],color='red')

        g.attr('edge', dir='forward')
    g.node(list4[0],shape='doublecircle',style='filled',color='red')
    g.render("2.gv", view=False)
    #img = cv2.imread("2.gv.png")
    #h = cv2.resize(img, (300, 565))
    #cv2.imwrite("img.png", h)
    #render = tkinter.PhotoImage(file="./img.png")
    render = tkinter.PhotoImage(file="./2.gv.png")
    la = tkinter.Label(tk, image=render, width=800, height=800)
    la.place(x=700, y=0)
    tk.mainloop()

button5=tkinter.Button(tk,text="深度度优先搜索最短路径",command=dfs1)
button5.place(x=200,y=340)

button7=tkinter.Button(tk,text="深度度优先搜索",command=depth_serach)
button7.place(x=200,y=240)
def clear():
    global min1,dis
    min1=9999999
    dis=0
    #book=[0]*len(list1)
    #path=[0]*len(list1)
    g.clear(keep_attrs = False )

    del adrect[length:len(list1)]
    for i in range(len(adrect)):
        del adrect[i][length:len(list1)]
    x=len(list1)
    a=[]
    for j in range(length,x):
        a.append(list1[j])
    for i in a:
        list2.pop(i)
    del list1[length:x]
    print(adrect)
    print(list1)
    print(list2)

button6=tkinter.Button(tk,text="清空",command=clear)
button6.place(x=620,y=100)
def min_distance():
    start = time.time()
    a=djstla.get()
    b=djstla1.get()
    hh=adrect
    distances={}
    for i in range(len(list1)):
        distances[list1[i]]={}
    print(distances)
    for i in range(len(list1)):
        for j in range(len(list1)):
            if(hh[i][j]>0):
                distances[list1[i]][list1[j]]=hh[i][j]
    print(distances)
    nodes=list1
    unvisited={node:9999999 for node in nodes}
    current=a
    currentdistance=0
    visited={}
    router={}
    routlist=[]
    while True:
        key_tos={}
        for neighbor,distance in distances[current].items():
            if(neighbor not in unvisited):
                continue
            newdistance=currentdistance+distance
            if(unvisited[neighbor]==9999999 or unvisited[neighbor]>newdistance):
                unvisited[neighbor]=newdistance
                key_tos [neighbor]=current
        print(key_tos)
        for key in key_tos:
            router[key]=key_tos[key]
        print(router)
        visited[current]=currentdistance
        del unvisited[current]
        if not unvisited:
            break
        after=[node for node in unvisited.items() ]#将unvisited转为列表元祖
        current,currentdistance=sorted(after,key=lambda x:x[1])[0]#选路径最短的
    print(visited)
    print(router)
    def rout(b):
        if(b!=a):
            for key in router:
                if(b==key):
                     print(router[key])
                     routlist.append(router[key])
            rout(router[b])
    rout(b)
    routlist.insert(0,b)
    routlist.reverse()
    print(routlist)

    for i in visited:
        if(i==b):
            print(visited[i])
    end = time.time()
    print(str(end-start))
    label5 = tkinter.Label(tk, text="dijkstra最短距离为:"+str(visited[b])+"   ")
    label5.place(x=200, y=120)
    label6 = tkinter.Label(tk, text="运行时间:" + str(end-start)+ "   ")
    label6.place(x=200, y=150)
    list4 = {}
    j = 0
    for ii in routlist:
        list4[j] = ii
        j = j + 1
    for i in range(len(routlist) - 1):
        g.attr('edge', dir='forward')
        g.edge(list4[i], list4[i + 1], color='red')
        g.attr('edge', dir='forward')
        g.node(list4[i], shape='doublecircle',style='filled',color='red')
    g.node(list4[len(routlist)-1],shape='doublecircle',style='filled',color='red')
    g.render("2.gv", view=False)
    #img = cv2.imread("2.gv.png")
    # img = cv2.imread("2.gv.png")
   # h = cv2.resize(img, (550, 750))
    #cv2.imwrite("img.png", h,[int(cv2.IMWRITE_PNG_COMPRESSION), 0])
    #cv2.imwrite("123.png", img, [int(cv2.IMWRITE_PNG_COMPRESSION), 0])
    #render = tkinter.PhotoImage(file="./img.png")
    render = tkinter.PhotoImage(file="./2.gv.png")
    la = tkinter.Label(tk, image=render, width=800, height=800)
    la.place(x=700, y=0)
    tk.mainloop()
button7=tkinter.Button(tk,text="dijkstra最短路径",command=min_distance)
button7.place(x=500,y=90)
tkinter.mainloop()






  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551
  • 552
  • 553
  • 554
  • 555
  • 556
  • 557
  • 558
  • 559
  • 560
  • 561
  • 562
  • 563
  • 564
  • 565
  • 566
  • 567
  • 568
  • 569
  • 570
  • 571
  • 572
  • 573
  • 574
  • 575
  • 576
  • 577
  • 578
  • 579
  • 580
  • 581
  • 582
  • 583
  • 584
  • 585
  • 586
  • 587
  • 588
  • 589
  • 590
  • 591
  • 592
  • 593
  • 594
  • 595
  • 596
  • 597
  • 598
  • 599
  • 600
  • 601
  • 602
  • 603
  • 604
  • 605
  • 606
  • 607
  • 608
  • 609
  • 610
  • 611
  • 612
  • 613
  • 614
  • 615
  • 616
  • 617
  • 618
  • 619
  • 620
  • 621
  • 622
  • 623
  • 624
  • 625
  • 626
  • 627
  • 628
  • 629
  • 630
  • 631
  • 632
  • 633
  • 634
  • 635
  • 636
  • 637
  • 638
  • 639
  • 640
  • 641
  • 642
  • 643
  • 644
  • 645
  • 646
  • 647
  • 648
  • 649
  • 650
  • 651
  • 652
  • 653
  • 654
  • 655
  • 656
  • 657
  • 658
  • 659
  • 660
  • 661
  • 662
  • 663
  • 664
  • 665
  • 666
  • 667
  • 668
  • 669
  • 670
  • 671
  • 672
  • 673
  • 674
  • 675
  • 676
  • 677
  • 678
  • 679
  • 680
  • 681
  • 682
  • 683
  • 684
  • 685
  • 686
  • 687
  • 688
  • 689
  • 690
  • 691
  • 692
  • 693
  • 694
  • 695
  • 696
  • 697
  • 698
  • 699
  • 700
  • 701
  • 702
  • 703
  • 704
  • 705
  • 706
  • 707
  • 708
  • 709
  • 710
  • 711
  • 712
  • 713
  • 714
  • 715
  • 716
  • 717
  • 718
  • 719
  • 720
  • 721
  • 722
  • 723
  • 724
  • 725
  • 726
  • 727
  • 728
  • 729
  • 730
  • 731
  • 732
  • 733
  • 734
  • 735
  • 736
  • 737
  • 738
  • 739
  • 740
  • 741
  • 742
  • 743
  • 744
  • 745
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/969997
推荐阅读
相关标签
  

闽ICP备14008679号