def to_graphics(A): G = MultiDiGraph() G.add_nodes_from(A.vertices()) for u, v in A.edges(): for a, b, d in A.weights(u, v): G.add_edge(u, v, label=a + "->" + b + "," + d) return G
def to_directed(self): """Return a directed representation of the graph. Returns ------- G : MultiDiGraph A directed graph with the same name, same nodes, and with each edge (u,v,data) replaced by two directed edges (u,v,data) and (v,u,data). Notes ----- This returns a "deepcopy" of the edge, node, and graph attributes which attempts to completely copy all of the data and references. This is in contrast to the similar D=DiGraph(G) which returns a shallow copy of the data. See the Python copy module for more information on shallow and deep copies, http://docs.python.org/library/copy.html. Warning ------- If you have subclassed MultiGraph to use dict-like objects in the data structure, those changes do not transfer to the MultiDiGraph created by this method. Examples -------- >>> G = nx.Graph() # or MultiGraph, etc >>> G.add_path([0,1]) >>> H = G.to_directed() >>> H.edges() [(0, 1), (1, 0)] If already directed, return a (deep) copy >>> G = nx.DiGraph() # or MultiDiGraph, etc >>> G.add_path([0,1]) >>> H = G.to_directed() >>> H.edges() [(0, 1)] """ from networkx.classes.multidigraph import MultiDiGraph G=MultiDiGraph() G.add_nodes_from(self) G.add_edges_from( (u,v,key,deepcopy(datadict)) for u,nbrs in self.adjacency_iter() for v,keydict in nbrs.items() for key,datadict in keydict.items() ) G.graph=deepcopy(self.graph) G.node=deepcopy(self.node) return G
def add_snapshot(self, ebunch=None, graph=None, start=None, end=None, time=None, multi=False): """Add a snapshot with a bunch of edge values. Parameters ---------- ebunch : container of edges, optional (default= None) Each edge in the ebunch list will be included to all added graphs. graph : networkx graph object, optional (default= None) networkx graph to be inserted into snapshot graph. start: start timestamp, inclusive end: end timestamp, exclusive time: timestamp for impulses, cannot be used together with (start, end) multi : boolean, optional (default= False) Determines if type of graphs in snapshot are DiGraphs for MultiDiGraphs Returns ------- None Examples -------- >>> G = dnx.SnapshotGraph() >>> G.add_snapshot([(1, 4), (1, 3)]) """ if not graph: if multi is True: g = MultiDiGraph() else: g = DiGraph() g.add_edges_from(ebunch) else: g = graph if time is not None and (start or end): raise ValueError( 'Time and (start or end) cannot both be specified.') elif time is not None: self.insert(g, time=time) elif (start is None and end is not None) or (start is not None and end is None): raise ValueError( 'Start and end must both be specified for intervals.') else: self.insert(g, start=start, end=end)
def read_graph(f, cfgOnly=False): try: if (f[-6:] == "pickle"): return pickle.load(open(f, "rb")) else: G = MultiDiGraph(read_graphml(f)) if (cfgOnly or G.graph.get("nature", None) == "CFG"): print("(removing PDG data from CFG)...") G = _removePDGStructures(G) else: print("(fixing graph)...") G = fix_graph(G) #end if return G #end if except Exception as e: print >> stderr, ("ERROR: could not read graphML file " + f + "!") print >> stderr, (str(e)) exit(1)
def to_subgraph(self, begin, end, multigraph=False, edge_data=False, edge_interval_data=False, node_data=False): """Return a networkx DiGraph or MultiDiGraph which includes all the nodes and edges which have overlapping intervals with the given interval. Parameters ---------- begin: int or float Inclusive beginning time of the edge appearing in the interval graph. end: int or float Non-inclusive ending time of the edge appearing in the interval graph. Must be bigger than or equal to begin. multigraph: bool, optional (default= False) If True, a networkx MultiGraph will be returned. If False, networkx Graph. edge_data: bool, optional (default= False) If True, edges will keep their attributes. edge_interval_data: bool, optional (default= False) If True, each edge's attribute will also include its begin and end interval data. If `edge_data= True` and there already exist edge attributes with names begin and end, they will be overwritten. node_data : bool, optional (default= False) If True, each node's attributes will be included. See Also -------- to_snapshots : divide the interval graph to snapshots Notes ----- If multigraph= False, and edge_data=True or edge_interval_data=True, in case there are multiple edges, only one will show with one of the edge's attributes. Note: nodes with no edges will not appear in any subgraph. Examples -------- >>> G = dnx.IntervalGraph() >>> G.add_edges_from([(1, 2, 3, 10), (2, 4, 1, 11), (6, 4, 12, 19), (2, 4, 8, 15)]) >>> H = G.to_subgraph(4, 12) >>> type(H) <class 'networkx.classes.graph.DiGraph'> >>> list(H.edges(data=True)) [(1, 2, {}), (2, 4, {})] >>> H = G.to_subgraph(4, 12, edge_interval_data=True) >>> type(H) <class 'networkx.classes.graph.DiGraph'> >>> list(H.edges(data=True)) [(1, 2, {'end': 10, 'begin': 3}), (2, 4, {'end': 15, 'begin': 8})] >>> M = G.to_subgraph(4, 12, multigraph=True, edge_interval_data=True) >>> type(M) <class 'networkx.classes.multigraph.MultiDiGraph'> >>> list(M.edges(data=True)) [(1, 2, {'end': 10, 'begin': 3}), (2, 4, {'end': 11, 'begin': 1}), (2, 4, {'end': 15, 'begin': 8})] """ if begin and end and begin > end: raise NetworkXError( "IntervalGraph: interval end must be bigger than or equal to begin: " "begin: {}, end: {}.".format(begin, end)) iedges = self.tree[begin:end] if multigraph: G = MultiDiGraph() else: G = DiGraph() if edge_data and edge_interval_data: G.add_edges_from((iedge.data[0], iedge.data[1], dict(self._adj[iedge.data[0]][iedge], begin=iedge.begin, end=iedge.end)) for iedge in iedges) elif edge_data: G.add_edges_from((iedge.data[0], iedge.data[1], self._adj[iedge.data[0]][iedge].copy()) for iedge in iedges) elif edge_interval_data: G.add_edges_from((iedge.data[0], iedge.data[1], { 'begin': iedge.begin, 'end': iedge.end }) for iedge in iedges) else: G.add_edges_from( (iedge.data[0], iedge.data[1]) for iedge in iedges) # include node attributes if node_data: G.add_nodes_from((n, self._node[n].copy()) for n in G.nodes) return G
def __init__(self, G): self.__graph = MultiDiGraph(); nodesToKeep = {n for n in G.nodes_iter(False) if is_cfg_node(G, n)}; self.__graph.add_nodes_from(nodesToKeep); # mark the entry node (self.__entryNode, isInterprocedural) = findGraphEntry(G); assert(self.__entryNode in self.__graph); # then, copy all CFG edges for n in self.__graph.nodes_iter(False): if(isInterprocedural and G.node[n].get("kind", "") == "call-site"): foundOne = False; for (source, target, attr) in G.out_edges_iter([n], data=True): if(attr.get("type", "flow") == "control" and \ attr.get("scope", "") == "interprocedural"): self.__graph.add_edge(n, target); foundOne = True; #end if #end for # add appropriate intraprocedural edges: only if # (a) the called function is not in the graphml, or # (b) the target is a crash point (which is essentially ambiguity # nonsensemeaning that we crashed trying to make the call itself) for (source, target, attr) in G.out_edges_iter([n], data=True): if(attr.get("type", "flow") == "flow" and \ attr.get("scope", "") != "interprocedural" and \ (not foundOne or G.node[target].get("kind", "") == "crash")): self.__graph.add_edge(n, target); #end if #end for elif(isInterprocedural and G.node[n].get("kind", "") == "exit"): entryForExit = findEntryForNode(G, n); for (source, target, attr) in G.in_edges_iter([entryForExit], data=True): if(attr.get("type", "flow") == "control" and \ attr.get("scope", "") == "interprocedural"): # edge from exit -> all successors of the call to this function for (call, callTarget, attr) in G.out_edges_iter([source], data=True): if(attr.get("type", "flow") == "flow" and \ attr.get("scope", "") != "interprocedural" and \ G.node[callTarget].get("kind", "") != "crash"): self.__graph.add_edge(n, callTarget); #end if #end for #end if #end for else: for (source, target, attr) in G.out_edges_iter([n], data=True): if(attr.get("type", "flow") == "flow" and \ attr.get("scope", "") != "interprocedural"): self.__graph.add_edge(n, target); #end if #end for #end if #end for # setup for yes, no, maybe, and crash constraints self.__crashNode = None; self.__yesVectors = set([]); self.__allYes = set([]); self.__allNo = set([]);
def to_subgraph(self, begin, end, inclusive=(True, False), multigraph=False, edge_data=False, edge_timestamp_data=False, node_data=False): """Return a networkx Graph or MultiGraph which includes all the nodes and edges which have timestamps within the given interval. Parameters ---------- begin: int or float end: int or float Must be bigger than or equal to begin. inclusive: 2-tuple boolean that determines inclusivity of begin and end multigraph: bool, optional (default= False) If True, a networkx MultiGraph will be returned. If False, networkx Graph. edge_data: bool, optional (default= False) If True, edges will keep their attributes. edge_timestamp_data: bool, optional (default= False) If True, each edge's attribute will also include its timestamp data. If `edge_data= True` and there already exist edge attributes named timestamp it will be overwritten. node_data : bool, optional (default= False) if True, each node's attributes will be included. See Also -------- to_snapshots : divide the impulse graph to snapshots Notes ----- If multigraph= False, and edge_data=True or edge_interval_data=True, in case there are multiple edges, only one will show with one of the edge's attributes. Note: nodes with no edges will not appear in any subgraph. Examples -------- >>> G = dnx.ImpulseGraph() >>> G.add_edges_from([(1, 2, 10), (2, 4, 11), (6, 4, 19), (2, 4, 15)]) >>> H = G.to_subgraph(4, 12) >>> type(H) <class 'networkx.classes.graph.DiGraph'> >>> list(H.edges(data=True)) [(1, 2, {}), (2, 4, {})] >>> H = G.to_subgraph(10, 12, edge_timestamp_data=True) >>> type(H) <class 'networkx.classes.graph.DiGraph'> >>> list(H.edges(data=True)) [(1, 2, {'timestamp': 10}), (2, 4, {'timestamp': 11})] >>> M = G.to_subgraph(4, 12, multigraph=True, edge_timestamp_data=True) >>> type(M) <class 'networkx.classes.multigraph.MultiDiGraph'> >>> list(M.edges(data=True)) [(1, 2, {'timestamp': 10}), (2, 4, {'timestamp': 11})] """ iedges = self.__search_tree(begin, end, inclusive=inclusive) if multigraph: G = MultiDiGraph() else: G = DiGraph() if edge_data and edge_timestamp_data: G.add_edges_from((iedge[0], iedge[1], dict(self._pred[iedge[0]][iedge[1]][iedge], timestamp=iedge[3])) for iedge in iedges) elif edge_data: G.add_edges_from( (iedge[0], iedge[1], self._pred[iedge[0]][iedge[1]][iedge]) for iedge in iedges) elif edge_timestamp_data: G.add_edges_from((iedge[0], iedge[1], { 'timestamp': iedge[3] }) for iedge in iedges) else: G.add_edges_from((iedge[0], iedge[1]) for iedge in iedges) if node_data: G.add_nodes_from((n, self._node[n].copy()) for n in G.nodes) return G