Advertisement
Guest User

animate_dfs.py

a guest
Jul 3rd, 2019
1,124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.97 KB | None | 0 0
  1. from animator import Animator
  2.  
  3. def toAnimLabel (num):
  4.     return "v" + str(num + 1)
  5.            
  6. class DFSAnimator:    
  7.     def __init__ (self, inFileName, outDir, outFileName):
  8.         fi = open(inFileName, "rt")
  9.         (vertexc, edgec) = map(int, fi.readline().split())
  10.         self.vertexc = vertexc
  11.         self.edgec = edgec
  12.  
  13.         self.initCoords = [(0, 0) for i in range(vertexc)]
  14.         self.finalCoords = [(0, 0) for i in range(vertexc)]
  15.         for i in range(vertexc):
  16.             (ix, iy, tx, ty) = map(int, fi.readline().split())
  17.             self.initCoords[i] = [ix, iy]
  18.             self.finalCoords[i] = [tx, ty]
  19.         xlMargin = min(min(i[0] for i in self.initCoords),
  20.                        min(t[0] for t in self.finalCoords)) - 1
  21.         xrMargin = max(max(i[0] for i in self.initCoords),
  22.                        max(t[0] for t in self.finalCoords)) + 1
  23.         ylMargin = min(min(i[1] for i in self.initCoords),
  24.                        min(t[1] for t in self.finalCoords)) - 1
  25.         yrMargin = max(max(i[1] for i in self.initCoords),
  26.                        max(t[1] for t in self.finalCoords)) + 1
  27.  
  28.         self.animator = Animator(outDir, outFileName, xlMargin, xrMargin, ylMargin, yrMargin)
  29.            
  30.         for i in range(vertexc):
  31.             self.animator.addVertex(toAnimLabel(i), label = str(i + 1))
  32.             self.animator.setVertexPos(toAnimLabel(i), self.initCoords[i][0], self.initCoords[i][1])
  33.             self.animator.setVertexAttr(toAnimLabel(i), "style", "filled")
  34.             self.animator.setVertexAttr(toAnimLabel(i), "fillcolor", "white")
  35.  
  36.         self.adj = [[] for i in range(vertexc)]
  37.         for i in range(edgec):
  38.             (u, v) = map(int, fi.readline().split())
  39.             self.adj[u].append(v)
  40.             self.adj[v].append(u)
  41.             self.animator.addEdge(toAnimLabel(u), toAnimLabel(v), 0)
  42.  
  43.         fi.close()
  44.  
  45.     def drawBuildDFS (self, vertex, visited):
  46.         visited[vertex] = True
  47.         self.animator.setVertexAttr(toAnimLabel(vertex), "fillcolor", "red")
  48.         self.animator.sleep(10)
  49.         for nxt in self.adj[vertex]:
  50.             if not visited[nxt]:
  51.                 self.animator.setVertexAttr(toAnimLabel(vertex), "fillcolor", "gray")
  52.                 self.animator.fillEdge(toAnimLabel(vertex), toAnimLabel(nxt))
  53.                 self.drawBuildDFS(nxt, visited)
  54.                 self.animator.setVertexAttr(toAnimLabel(vertex), "fillcolor", "red")
  55.                 self.animator.sleep(10)
  56.         self.animator.setVertexAttr(toAnimLabel(vertex), "fillcolor", "gray")
  57.            
  58.     def drawBuild (self, root):
  59.         visited = [False for i in range(self.vertexc)]
  60.         self.drawBuildDFS(root, visited)
  61.  
  62.         targets = {}
  63.         for i in range(self.vertexc):
  64.             targets[toAnimLabel(i)] = self.finalCoords[i]
  65.  
  66.         self.animator.moveVertices(targets)
  67.         self.animator.sleep(50)
  68.    
  69. dfsanim = DFSAnimator("graph.txt", "frames", "frame")
  70. dfsanim.drawBuild(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement