Builds a new graph with the edges reversed.

        Returns:
            :class:`stacker.dag.DAG`: The transposed graph.
199457
def transpose(self):
        
        graph = self.graph
        transposed = DAG()
        for node, edges in graph.items():
            transposed.add_node(node)
        for node, edges in graph.items():
            # for each edge A -> B, transpose it so that B -> A
            for edge in edges:
                transposed.add_edge(edge, node)
        return transposed
557692
def reverse(graph):
    """"""
    rev_graph = [[] for node in graph]
    for node in range(len(graph)):
        for neighbor in graph[node]:
            rev_graph[neighbor].append(node)
    return rev_graph
1359298
def graph(self):
        

        if self._local_transition_graph is not None:
            return self._local_transition_graph

        g = networkx.DiGraph()
        if self.startpoint is not None:
            g.add_node(self.startpoint)
        for block in self._local_blocks.values():
            g.add_node(block)
        for src, dst, data in self.transition_graph.edges(data=True):
            if 'type' in data:
                if data['type']  == 'transition' and ('outside' not in data or data['outside'] is False):
                    g.add_edge(src, dst, **data)
                elif data['type'] == 'fake_return' and 'confirmed' in data and \
                        ('outside' not in data or data['outside'] is False):
                    g.add_edge(src, dst, **data)

        self._local_transition_graph = g

        return g
378993
def digraph_walker_backwards(graph, element, call_back):
    """"""
    call_back(graph, element)
    for predecessor in graph.predecessors(element):
        call_back(graph, predecessor)
    for predecessor in graph.predecessors(element):
        digraph_walker_backwards(graph, predecessor, call_back)
459354
def permute_graph(G, order):
  '''
  '''
  adj = G.matrix('dense')
  adj = adj[np.ix_(order, order)]
  return Graph.from_adj_matrix(adj)
238925
def dict_to_nx(graph, oriented=False):
    """"""
    nxg = nx.DiGraph() if oriented else nx.Graph()
    for node, succs in graph.items():
        for succ in succs:
            nxg.add_edge(node, succ)
    return nxg
80443
def _raw(cls, vertices, edges, heads, tails):
        
        self = object.__new__(cls)
        self._vertices = vertices
        self._edges = edges
        self._heads = heads
        self._tails = tails

        # For future use, map each vertex to its outward and inward edges.
        # These could be computed on demand instead of precomputed.
        self._out_edges = collections.defaultdict(set)
        self._in_edges = collections.defaultdict(set)
        for edge in self._edges:
            self._out_edges[self._tails[edge]].add(edge)
            self._in_edges[self._heads[edge]].add(edge)
        return self
1204533
def reversed(self):
        """"""
        new_cub = CubicBezier(self.end, self.control2, self.control1,
                              self.start)
        if self._length_info['length']:
            new_cub._length_info = self._length_info
            new_cub._length_info['bpoints'] = (
                self.end, self.control2, self.control1, self.start)
        return new_cub
228257
def inverse(self):
        
        csg = self.clone()
        map(lambda p: p.flip(), csg.polygons)
        return csg
233233
def get_graph(self, parse_direction=False):
        
        g = nx.DiGraph()

        for way_id, (tags, nodes) in self.ways.items():
            # If oneway is '-1', reverse the way and treat as a normal oneway
            if tags.get('oneway') == '-1':
                nodes = reversed(nodes)
                tags['oneway'] = 'yes'
            oneway = tags.get('oneway') == 'yes'

            for n0, n1 in tools.pairwise(nodes):
                g.add_edge(n0, n1, attr_dict=tags)
                if parse_direction:
                    g[n0][n1]['_direction'] = 'forward'
                if not oneway:
                    g.add_edge(n1, n0, attr_dict=tags)
                    if parse_direction:
                        g[n1][n0]['_direction'] = 'backward'

                g.node[n0].update(self._node_properties(n0))
            g.node[n1].update(self._node_properties(n1))

        return g
506877
def node_to_edge(edges, directed=True):
    
    outgoing = defaultdict(set)
    incoming = defaultdict(set) if directed else outgoing
    nodes = set()
    for i, edge in enumerate(edges):
        a, b, = edge[:2]
        outgoing[a].add(i)
        incoming[b].add(i)
        nodes.add(a)
        nodes.add(b)
    nodes = sorted(nodes)
    if directed:
        return outgoing, incoming, nodes
    return outgoing, nodes
1116209
def scc(graph):
    '''  '''
    order = []
    vis = {vertex: False for vertex in graph}

    graph_transposed = {vertex: [] for vertex in graph}

    for (v, neighbours) in graph.iteritems():
        for u in neighbours:
            add_edge(graph_transposed, u, v)

    for v in graph:
        if not vis[v]:
            dfs_transposed(v, graph_transposed, order, vis)

    vis = {vertex: False for vertex in graph}
    vertex_scc = {}

    current_comp = 0
    for v in reversed(order):
        if not vis[v]:
            # Each dfs will visit exactly one component
            dfs(v, current_comp, vertex_scc, graph, vis)
            current_comp += 1

    return vertex_scc
661703
def invert_graph(include_graph):
    

    file_index = include_graph.file_index
    dep_extractor = include_graph.dep_extractor

    inverted_graph = IncludeGraph(file_index, dep_extractor)
    inverted_graph.active = True

    for node, edges in include_graph:
        inverted_graph.add_node(node)

        for edge in edges:
            inverted_graph.add_edge(edge.to, node, edge.settings['type'])

    return inverted_graph
967228
def depgraph_to_dotsrc(dep_graph, show_cycles, nodot, reverse):
    
    if show_cycles:
        dotsrc = cycles2dot(dep_graph, reverse=reverse)
    elif not nodot:
        dotsrc = dep2dot(dep_graph, reverse=reverse)
    else:
        dotsrc = None
    return dotsrc
394034
def as_graph(self) -> Digraph:  # pragma: no cover
        """"""
        if Digraph is None:
            raise ImportError('The graphviz package is required to draw the graph.')
        graph = Digraph()

        subgraphs = [Digraph(graph_attr={'rank': 'same'}), Digraph(graph_attr={'rank': 'same'})]
        nodes = [{}, {}]  # type: List[Dict[Union[TLeft, TRight], str]]
        edges = []  # type: List [Tuple[str, str]]
        node_id = 0
        for (tail_part, tail), head_set in self.items():
            if tail not in nodes[tail_part]:
                name = 'node{:d}'.format(node_id)
                nodes[tail_part][tail] = name
                subgraphs[tail_part].node(name, label=str(tail))
                node_id += 1
            for head_part, head in head_set:
                if head not in nodes[head_part]:
                    name = 'node{:d}'.format(node_id)
                    nodes[head_part][head] = name
                    subgraphs[head_part].node(name, label=str(head))
                    node_id += 1
                edges.append((nodes[tail_part][tail], nodes[head_part][head]))
        graph.subgraph(subgraphs[0])
        graph.subgraph(subgraphs[1])
        for tail_node, head_node in edges:
            graph.edge(tail_node, head_node)
        return graph
917358
public Graph<T> transpose() {
        Builder<T> builder = new Builder<>();
        builder.addNodes(nodes);
        // reverse edges
        edges.keySet().forEach(u -> {
            edges.get(u).stream()
                .forEach(v -> builder.addEdge(v, u));
        });
        return builder.build();
    }
244038
def build_edges(nodes):
    
    backward_edges = {}
    # pre-populate the forward edge dict for simplicity
    forward_edges = {node.unique_id: [] for node in nodes}
    for node in nodes:
        backward_edges[node.unique_id] = node.depends_on_nodes[:]
        for unique_id in node.depends_on_nodes:
            forward_edges[unique_id].append(node.unique_id)
    return _sort_values(forward_edges), _sort_values(backward_edges)