Ticket #11908: trac_11908-rev.patch

File trac_11908-rev.patch, 3.3 KB (added by ncohen, 8 years ago)
  • sage/graphs/generic_graph.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1368014561 -7200
    # Node ID b853b21bf7e0a97ea829513c1a21d8f714c28bbb
    # Parent  c2f49f4b695494d8f6ffd1174fb3e52e585746a5
    Fix tree plotting again -- reviewer's patch
    
    diff --git a/sage/graphs/generic_graph.py b/sage/graphs/generic_graph.py
    a b  
    1403414034        else:
    1403514035            root = tree_root
    1403614036
     14037        pos = {}
     14038
     14039        # The children and parent of each vertex
    1403714040        children = {root:self.neighbors(root)}
    14038 
    14039         # always make a copy of the children because they get eaten
    14040         stack = [[u for u in children[root]]]
     14041        parent = {u:root for u in children[root]}
     14042
     14043        # stack[i] is the list of children of stick[i] which have not been given
     14044        # a position yet.
     14045        stack = [list(children[root])]
    1404114046        stick = [root]
    14042         parent = dict([(u,root) for u in children[root]])
    14043         pos = {}
     14047
     14048        # obstruction[y] is the smallest value of x to which a vertex at height
     14049        # y can be assigned. All vertices at height y which have already been
     14050        # assigned are on the left of (x-1,y).
    1404414051        obstruction = [0.0]*self.num_verts()
    1404514052
    1404614053        if tree_orientation in ['down', 'left']:
     
    1407114078
    1407214079        while stack:
    1407314080            C = stack[-1]
     14081
     14082            # If all the children of stick[-1] have been given a position
    1407414083            if len(C) == 0:
    1407514084                p = stick.pop()
    1407614085                stack.pop()
    1407714086                cp = children[p]
    1407814087                y = o*len(stack)
     14088
    1407914089                if len(cp) == 0:
     14090                    # If p has no children, we draw it at the leftmost position
     14091                    # which has not been forbidden
    1408014092                    x = obstruction[y]
    1408114093                    pos[p] = x, y
    1408214094                else:
     14095                    # If p has children, we put v on a vertical line going
     14096                    # through the barycenter of its children
    1408314097                    x = sum([pos[c][0] for c in cp])/len(cp)
    1408414098                    pos[p] = x, y
    1408514099                    ox = obstruction[y]
    1408614100                    if x < ox:
    1408714101                        slide(p, ox-x)
    1408814102                        x = ox
     14103
     14104                # If the vertex to the right of p has not children, we want it
     14105                # at distance 1 from p
    1408914106                obstruction[y] = x+1
    14090                 continue
    14091 
    14092             t = C.pop()
    14093             pt = parent[t]
    14094 
    14095             ct = [u for u in self.neighbors(t) if u != pt]
    14096             for c in ct:
    14097                 parent[c] = t
    14098             children[t] = ct
    14099 
    14100             stack.append([c for c in ct])
    14101             stick.append(t)
     14107
     14108            # Otherwise, we take one of the children and add it to the
     14109            # stack. Note that this vertex is removed from the list C.
     14110            else:
     14111                t = C.pop()
     14112
     14113                pt = parent[t]
     14114                ct = [u for u in self.neighbors(t) if u != pt]
     14115                children[t] = ct
     14116
     14117                for c in ct:
     14118                    parent[c] = t
     14119
     14120                stack.append([c for c in ct])
     14121                stick.append(t)
    1410214122
    1410314123        if tree_orientation in ['right', 'left']:
    14104             return dict([[point,[pos[point][1],pos[point][0]]] for point in pos])
     14124            return {p:(py,px) for p,(px,py) in pos.iteritems()}
    1410514125
    1410614126        return pos
    1410714127