通过使用python实现图的可视化,以及广度优先的最短距离,深度优先的最短路径,dijkstra的最短路径
这个广度优先可能有点BUG,有时候是对的有时候有错,因为刚检查完,忙着复习期末考试,没时间调了
#好像没什么注释,有时间再补吧
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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,-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()