File:PrimAlgDemo.gif
Page contents not supported in other languages.
Tools
Actions
General
In other projects
Appearance
Size of this preview: 314 × 323 pixels.
Original file (314 × 323 pixels, file size: 248 KB, MIME type: image/gif, looped, 58 frames, 29 s)
This is a file from the Wikimedia Commons. Information from its description page there is shown below.
Commons is a freely licensed media file repository. You can help.
Commons is a freely licensed media file repository. You can help.
Summary
| DescriptionPrimAlgDemo.gif |
English: A demo for Prim's algorithm to find minimum spanning tree on a 2D plane. |
| Date | |
| Source | Own work |
| Author | Shiyu Ji |
| GIF development 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)
Licensing
I, the copyright holder of this work, hereby publish it under the following license:
This file is licensed under the Creative Commons Attribution-Share Alike 4.0 International license.
- You are free:
- to share – to copy, distribute and transmit the work
- to remix – to adapt the work
- Under the following conditions:
- attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
- share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.
Captions
Add a one-line explanation of what this file represents
Items portrayed in this file
depicts
creator<\/a>"}},"text\/plain":{"en":{"":"creator"}}},"{\"value\":{\"entity-type\":\"property\",\"numeric-id\":4174,\"id\":\"P4174\"},\"type\":\"wikibase-entityid\"}":{"text\/html":{"en":{"":"Wikimedia username<\/a>"}},"text\/plain":{"en":{"":"Wikimedia username"}}},"{\"value\":\"Shiyu Ji\",\"type\":\"string\"}":{"text\/html":{"en":{"P4174":"Shiyu Ji<\/a>","P2093":"Shiyu Ji"}},"text\/plain":{"en":{"P4174":"Shiyu Ji","P2093":"Shiyu Ji"}}},"{\"value\":{\"entity-type\":\"property\",\"numeric-id\":2699,\"id\":\"P2699\"},\"type\":\"wikibase-entityid\"}":{"text\/html":{"en":{"":"URL<\/a>"}},"text\/plain":{"en":{"":"URL"}}},"{\"value\":\"https:\\\/\\\/commons.wikimedia.org\\\/wiki\\\/user:Shiyu_Ji\",\"type\":\"string\"}":{"text\/html":{"en":{"P2699":"https:\/\/commons.wikimedia.org\/wiki\/user:Shiyu_Ji<\/a>"}},"text\/plain":{"en":{"P2699":"https:\/\/commons.wikimedia.org\/wiki\/user:Shiyu_Ji"}}},"{\"value\":{\"entity-type\":\"property\",\"numeric-id\":2093,\"id\":\"P2093\"},\"type\":\"wikibase-entityid\"}":{"text\/html":{"en":{"":"author name string<\/a>"}},"text\/plain":{"en":{"":"author name string"}}}}" class="wbmi-entityview-statementsGroup wbmi-entityview-statementsGroup-P170 oo-ui-layout oo-ui-panelLayout oo-ui-panelLayout-framed">
copyright status<\/a>"}},"text\/plain":{"en":{"":"copyright status"}}},"{\"value\":{\"entity-type\":\"item\",\"numeric-id\":50423863,\"id\":\"Q50423863\"},\"type\":\"wikibase-entityid\"}":{"text\/html":{"en":{"P6216":"copyrighted<\/a>"}},"text\/plain":{"en":{"P6216":"copyrighted"}}}}" class="wbmi-entityview-statementsGroup wbmi-entityview-statementsGroup-P6216 oo-ui-layout oo-ui-panelLayout oo-ui-panelLayout-framed">
copyright license<\/a>"}},"text\/plain":{"en":{"":"copyright license"}}},"{\"value\":{\"entity-type\":\"item\",\"numeric-id\":18199165,\"id\":\"Q18199165\"},\"type\":\"wikibase-entityid\"}":{"text\/html":{"en":{"P275":"Creative Commons Attribution-ShareAlike 4.0 International<\/a>"}},"text\/plain":{"en":{"P275":"Creative Commons Attribution-ShareAlike 4.0 International"}}}}" class="wbmi-entityview-statementsGroup wbmi-entityview-statementsGroup-P275 oo-ui-layout oo-ui-panelLayout oo-ui-panelLayout-framed">
source of file<\/a>"}},"text\/plain":{"en":{"":"source of file"}}},"{\"value\":{\"entity-type\":\"item\",\"numeric-id\":66458942,\"id\":\"Q66458942\"},\"type\":\"wikibase-entityid\"}":{"text\/html":{"en":{"P7482":"original creation by uploader<\/a>"}},"text\/plain":{"en":{"P7482":"original creation by uploader"}}}}" class="wbmi-entityview-statementsGroup wbmi-entityview-statementsGroup-P7482 oo-ui-layout oo-ui-panelLayout oo-ui-panelLayout-framed">
inception<\/a>"}},"text\/plain":{"en":{"":"inception"}}},"{\"value\":{\"time\":\"+2016-12-24T00:00:00Z\",\"timezone\":0,\"before\":0,\"after\":0,\"precision\":11,\"calendarmodel\":\"http:\\\/\\\/www.wikidata.org\\\/entity\\\/Q1985727\"},\"type\":\"time\"}":{"text\/html":{"en":{"P571":"24 December 2016"}},"text\/plain":{"en":{"P571":"24 December 2016"}}}}" class="wbmi-entityview-statementsGroup wbmi-entityview-statementsGroup-P571 oo-ui-layout oo-ui-panelLayout oo-ui-panelLayout-framed">
24 December 2016
media type<\/a>"}},"text\/plain":{"en":{"":"media type"}}},"{\"value\":\"image\\\/gif\",\"type\":\"string\"}":{"text\/html":{"en":{"P1163":"image\/gif"}},"text\/plain":{"en":{"P1163":"image\/gif"}}}}" class="wbmi-entityview-statementsGroup wbmi-entityview-statementsGroup-P1163 oo-ui-layout oo-ui-panelLayout oo-ui-panelLayout-framed">
image/gif
File history
Click on a date/time to view the file as it appeared at that time.
| Date/Time | Thumbnail | Dimensions | User | Comment | |
|---|---|---|---|---|---|
| current | 12:52, 24 December 2016 | 314 × 323 (248 KB) | Shiyu Ji | User created page with UploadWizard |
File usage
The following 3 pages use this file:
Global file usage
The following other wikis use this file:
- Usage on ar.wikipedia.org
- Usage on hu.wikipedia.org
- Usage on it.wikipedia.org
- Usage on ka.wikipedia.org
- Usage on sr.wikipedia.org
- Usage on zh-min-nan.wikipedia.org
Retrieved from "https://en.wikipedia.org/wiki/File:PrimAlgDemo.gif"