Fájl:PrimAlgDemo.gif
Innen: testwiki
Ugrás a navigációhoz
Ugrás a kereséshez
PrimAlgDemo.gif (314 × 323 képpont, fájlméret: 248 KB, MIME-típus: image/gif, ismétlődik, 58 képkocka, 29 s)
Ez a fájl a Wikimedia Commons megosztott tárhelyről származik, és más projektek is használhatják. A fájl ottani leírólapjának másolata alább látható.
Összefoglaló
| LeírásPrimAlgDemo.gif |
English: A demo for Prim's algorithm to find minimum spanning tree on a 2D plane. |
| Dátum | |
| Forrás | A feltöltő saját munkája |
| Szerző | Shiyu Ji |
| GIF kód InfoField |
Python 3 Code
'''
Minimum Spanning Tree generation (SVG) for Prim's algorithm.
Firstly use this code to generate SVG frames.
Then transform to bitmaps and convert to GIF.
'''
# range size
N = 300
margin = 20
def norm(x, y):
return (x*x+y*y)**.5
def saveToSVG(nFrames, points, firmed, trying):
f = open('demo_'+'0'*(3-len(str(nFrames)))+str(nFrames)+'.svg', 'w')
f.write("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n")
for p in points:
f.write("<circle cx=\"" +str(p[0]+margin)+ "\" cy=\""+ str(N-p[1]+margin) +"\" r=\"5\" fill=\"white\" stroke=\"black\"/>\n")
for L in firmed:
f.write("<line x1=\"" +str(L[0][0]+margin)+ "\" y1=\""+ str(N-L[0][1]+margin) +"\" x2=\"" + str(L[1][0]+margin) + "\" y2=\"" + str(N-L[1][1]+margin) + "\" stroke=\"red\" stroke-width=\"5\"/>\n")
for L in trying:
f.write("<line x1=\"" +str(L[0][0]+margin)+ "\" y1=\""+ str(N-L[0][1]+margin) +"\" x2=\"" + str(L[1][0]+margin) + "\" y2=\"" + str(N-L[1][1]+margin) + "\" stroke=\"blue\" stroke-width=\"5\"/>\n")
f.write("</svg>\n")
f.close()
def generatePoints(n):
import random as r
r.seed(100)
res = []
for i in range(n):
pt = [r.randint(0,N) for _ in [0, 1]]
if [pt] not in res:
res += [pt]
return res
def prim(n, points):
n = len(points)
mst = []
S = set()
import heapq
heap = []
nframe = 0
while len(mst)<n-1:
if len(S)==0:
topV = 0
else:
while heap[0][2] in S:
heapq.heappop(heap)
topV = heap[0][2]
saveToSVG(nframe, points, mst, [[points[heap[0][1]], points[heap[0][2]]]])
nframe+=1
mst.append([points[heap[0][1]], points[topV]])
heapq.heappop(heap)
saveToSVG(nframe, points, mst, [])
nframe+=1
S.add(topV)
for i in range(n):
if i not in S:
L = norm(points[i][0]-points[topV][0], points[i][1]-points[topV][1])
heapq.heappush(heap, [L, topV, i])
return mst
# test 30 points temporarily
n = 30
pts = generatePoints(n)
prim(n, pts)
Licenc
Én, e mű szerzője a művemet az alábbi licenc alatt teszem közzé:
Ez a fájl a Creative Commons Nevezd meg! – Így add tovább! 4.0 Nemzetközi licenc alapján használható fel.
- A következőket teheted a művel:
- megoszthatod – szabadon másolhatod, terjesztheted, bemutathatod és előadhatod a művet
- feldolgozhatod – származékos műveket hozhatsz létre
- Az alábbi feltételekkel:
- Nevezd meg! – A szerzőt megfelelően fel kell tüntetned, hivatkozást kell létrehoznod a licencre és jelezned kell, ha a művön változtatást hajtottál végre. Ezt bármilyen észszerű módon megteheted, kivéve oly módon, ami azt sugallná hogy a jogosult támogat téged vagy a felhasználásod körülményeit.
- Így add tovább! – Ha megváltoztatod, átalakítod, feldolgozod ezt a művet, a közreműködésedet csak az eredetivel megegyező vagy hasonló licenc alatt terjesztheted.
Képaláírások
Adj meg egy egysoros magyarázatot arról, hogy mit mutat be ez a fájl
A fájl által ábrázolt elemek
mű tárgya
Valamilyen, Wikidata-elemmel nem rendelkező érték
24. december 2016
image/gif
Fájltörténet
Kattints egy időpontra, hogy a fájl akkori állapotát láthasd.
| Dátum/idő | Bélyegkép | Felbontás | Feltöltő | Megjegyzés | |
|---|---|---|---|---|---|
| aktuális | 2016. december 24., 13:52 | 314 × 323 (248 KB) | wikimediacommons>Shiyu Ji | User created page with UploadWizard |
Fájlhasználat
Az alábbi lap használja ezt a fájlt:
A lap eredeti címe: „https://hu.wiki.beta.math.wmflabs.org/wiki/Fájl:PrimAlgDemo.gif”
