/usr/share/pyshared/networkx/algorithms/product.py is in python-networkx 1.6-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | """
Graph products.
"""
# Copyright (C) 2011 by
# Aric Hagberg <hagberg@lanl.gov>
# Dan Schult <dschult@colgate.edu>
# Pieter Swart <swart@lanl.gov>
# All rights reserved.
# BSD license.
import networkx as nx
from itertools import product
__author__ = """\n""".join(['Aric Hagberg (hagberg@lanl.gov)',
'Pieter Swart (swart@lanl.gov)',
'Dan Schult(dschult@colgate.edu)'
'Ben Edwards(bedwards@cs.unm.edu)'])
__all__ = ['tensor_product','cartesian_product',
'lexicographic_product', 'strong_product']
def _dict_product(d1,d2):
return dict((k,(d1.get(k),d2.get(k))) for k in set(d1)|set(d2))
# Generators for producting graph products
def _node_product(G,H):
for u,v in product(G, H):
yield ((u,v), _dict_product(G.node[u], H.node[v]))
def _directed_edges_cross_edges(G,H):
if not G.is_multigraph() and not H.is_multigraph():
for u,v,c in G.edges_iter(data=True):
for x,y,d in H.edges_iter(data=True):
yield (u,x),(v,y),_dict_product(c,d)
if not G.is_multigraph() and H.is_multigraph():
for u,v,c in G.edges_iter(data=True):
for x,y,k,d in H.edges_iter(data=True,keys=True):
yield (u,x),(v,y),k,_dict_product(c,d)
if G.is_multigraph() and not H.is_multigraph():
for u,v,k,c in G.edges_iter(data=True,keys=True):
for x,y,d in H.edges_iter(data=True):
yield (u,x),(v,y),k,_dict_product(c,d)
if G.is_multigraph() and H.is_multigraph():
for u,v,j,c in G.edges_iter(data=True,keys=True):
for x,y,k,d in H.edges_iter(data=True,keys=True):
yield (u,x),(v,y),(j,k),_dict_product(c,d)
def _undirected_edges_cross_edges(G,H):
if not G.is_multigraph() and not H.is_multigraph():
for u,v,c in G.edges_iter(data=True):
for x,y,d in H.edges_iter(data=True):
yield (v,x),(u,y),_dict_product(c,d)
if not G.is_multigraph() and H.is_multigraph():
for u,v,c in G.edges_iter(data=True):
for x,y,k,d in H.edges_iter(data=True,keys=True):
yield (v,x),(u,y),k,_dict_product(c,d)
if G.is_multigraph() and not H.is_multigraph():
for u,v,k,c in G.edges_iter(data=True,keys=True):
for x,y,d in H.edges_iter(data=True):
yield (v,x),(u,y),k,_dict_product(c,d)
if G.is_multigraph() and H.is_multigraph():
for u,v,j,c in G.edges_iter(data=True,keys=True):
for x,y,k,d in H.edges_iter(data=True,keys=True):
yield (v,x),(u,y),(j,k),_dict_product(c,d)
def _edges_cross_nodes(G,H):
if G.is_multigraph():
for u,v,k,d in G.edges_iter(data=True,keys=True):
for x in H:
yield (u,x),(v,x),k,d
else:
for u,v,d in G.edges_iter(data=True):
for x in H:
if H.is_multigraph():
yield (u,x),(v,x),None,d
else:
yield (u,x),(v,x),d
def _nodes_cross_edges(G,H):
if H.is_multigraph():
for x in G:
for u,v,k,d in H.edges_iter(data=True,keys=True):
yield (x,u),(x,v),k,d
else:
for x in G:
for u,v,d in H.edges_iter(data=True):
if G.is_multigraph():
yield (x,u),(x,v),None,d
else:
yield (x,u),(x,v),d
def _edges_cross_nodes_and_nodes(G,H):
if G.is_multigraph():
for u,v,k,d in G.edges_iter(data=True,keys=True):
for x in H:
for y in H:
yield (u,x),(v,y),k,d
else:
for u,v,d in G.edges_iter(data=True):
for x in H:
for y in H:
if H.is_multigraph():
yield (u,x),(v,y),None,d
else:
yield (u,x),(v,y),d
def _init_product_graph(G,H):
if not G.is_directed() == H.is_directed():
raise nx.NetworkXError("G and H must be both directed or",
"both undirected")
if G.is_multigraph() or H.is_multigraph():
GH = nx.MultiGraph()
else:
GH = nx.Graph()
if G.is_directed():
GH = GH.to_directed()
return GH
def tensor_product(G,H):
r"""Return the tensor product of G and H.
The tensor product P of the graphs G and H has a node set that
is the Cartesian product of the node sets, $V(P)=V(G) \times V(H)$.
P has an edge ((u,v),(x,y)) if and only if (u,v) is an edge in G
and (x,y) is an edge in H.
Sometimes referred to as the categorical product.
Parameters
----------
G, H: graphs
Networkx graphs.
Returns
-------
P: NetworkX graph
The tensor product of G and H. P will be a multi-graph if either G
or H is a multi-graph. Will be a directed if G and H are directed,
and undirected if G and H are undirected.
Raises
------
NetworkXError
If G and H are not both directed or both undirected.
Notes
-----
Node attributes in P are two-tuple of the G and H node attributes.
Missing attributes are assigned None.
For example
>>> G = nx.Graph()
>>> H = nx.Graph()
>>> G.add_node(0,a1=True)
>>> H.add_node('a',a2='Spam')
>>> P = nx.tensor_product(G,H)
>>> P.nodes(data=True)
[((0, 'a'), {'a1': (True, None), 'a2': (None, 'Spam')})]
Edge attributes and edge keys (for multigraphs) are also copied to the
new product graph
"""
GH = _init_product_graph(G,H)
GH.add_nodes_from(_node_product(G,H))
GH.add_edges_from(_directed_edges_cross_edges(G,H))
if not GH.is_directed():
GH.add_edges_from(_undirected_edges_cross_edges(G,H))
GH.name = "Tensor product("+G.name+","+H.name+")"
return GH
def cartesian_product(G,H):
"""Return the Cartesian product of G and H.
The tensor product P of the graphs G and H has a node set that
is the Cartesian product of the node sets, $V(P)=V(G) \times V(H)$.
P has an edge ((u,v),(x,y)) if and only if (u,v) is an edge in G
and x==y or and (x,y) is an edge in H and u==v.
and (x,y) is an edge in H.
Parameters
----------
G, H: graphs
Networkx graphs.
Returns
-------
P: NetworkX graph
The Cartesian product of G and H. P will be a multi-graph if either G
or H is a multi-graph. Will be a directed if G and H are directed,
and undirected if G and H are undirected.
Raises
------
NetworkXError
If G and H are not both directed or both undirected.
Notes
-----
Node attributes in P are two-tuple of the G and H node attributes.
Missing attributes are assigned None.
For example
>>> G = nx.Graph()
>>> H = nx.Graph()
>>> G.add_node(0,a1=True)
>>> H.add_node('a',a2='Spam')
>>> P = nx.tensor_product(G,H)
>>> P.nodes(data=True)
[((0, 'a'), {'a1': (True, None), 'a2': (None, 'Spam')})]
Edge attributes and edge keys (for multigraphs) are also copied to the
new product graph
"""
if not G.is_directed() == H.is_directed():
raise nx.NetworkXError("G and H must be both directed or",
"both undirected")
GH = _init_product_graph(G,H)
GH.add_nodes_from(_node_product(G,H))
GH.add_edges_from(_edges_cross_nodes(G,H))
GH.add_edges_from(_nodes_cross_edges(G,H))
GH.name = "Cartesian product("+G.name+","+H.name+")"
return GH
def lexicographic_product(G,H):
"""Return the lexicographic product of G and H.
The lexicographical product P of the graphs G and H has a node set that
is the Cartesian product of the node sets, $V(P)=V(G) \times V(H)$.
P has an edge ((u,v),(x,y)) if and only if (u,v) is an edge in G
or u==v and (x,y) is an edge in H.
Parameters
----------
G, H: graphs
Networkx graphs.
Returns
-------
P: NetworkX graph
The Cartesian product of G and H. P will be a multi-graph if either G
or H is a multi-graph. Will be a directed if G and H are directed,
and undirected if G and H are undirected.
Raises
------
NetworkXError
If G and H are not both directed or both undirected.
Notes
-----
Node attributes in P are two-tuple of the G and H node attributes.
Missing attributes are assigned None.
For example
>>> G = nx.Graph()
>>> H = nx.Graph()
>>> G.add_node(0,a1=True)
>>> H.add_node('a',a2='Spam')
>>> P = nx.tensor_product(G,H)
>>> P.nodes(data=True)
[((0, 'a'), {'a1': (True, None), 'a2': (None, 'Spam')})]
Edge attributes and edge keys (for multigraphs) are also copied to the
new product graph
"""
GH = _init_product_graph(G,H)
GH.add_nodes_from(_node_product(G,H))
# Edges in G regardless of H designation
GH.add_edges_from(_edges_cross_nodes_and_nodes(G,H))
# For each x in G, only if there is an edge in H
GH.add_edges_from(_nodes_cross_edges(G,H))
GH.name = "Lexographic product("+G.name+","+H.name+")"
return GH
def strong_product(G,H):
"""Return the strong product of G and H.
The strong product P of the graphs G and H has a node set that
is the Cartesian product of the node sets, $V(P)=V(G) \times V(H)$.
P has an edge ((u,v),(x,y)) if and only if
u==v and (x,y) is an edge in H, or
x==y and (u,v) is an edge in G, or
(u,v) is an edge in G and (x,y) is an edge in H.
Parameters
----------
G, H: graphs
Networkx graphs.
Returns
-------
P: NetworkX graph
The Cartesian product of G and H. P will be a multi-graph if either G
or H is a multi-graph. Will be a directed if G and H are directed,
and undirected if G and H are undirected.
Raises
------
NetworkXError
If G and H are not both directed or both undirected.
Notes
-----
Node attributes in P are two-tuple of the G and H node attributes.
Missing attributes are assigned None.
For example
>>> G = nx.Graph()
>>> H = nx.Graph()
>>> G.add_node(0,a1=True)
>>> H.add_node('a',a2='Spam')
>>> P = nx.tensor_product(G,H)
>>> P.nodes(data=True)
[((0, 'a'), {'a1': (True, None), 'a2': (None, 'Spam')})]
Edge attributes and edge keys (for multigraphs) are also copied to the
new product graph
"""
GH = _init_product_graph(G,H)
GH.add_nodes_from(_node_product(G,H))
GH.add_edges_from(_nodes_cross_edges(G,H))
GH.add_edges_from(_edges_cross_nodes(G,H))
GH.add_edges_from(_directed_edges_cross_edges(G,H))
if not GH.is_directed():
GH.add_edges_from(_undirected_edges_cross_edges(G,H))
GH.name = "Strong product("+G.name+","+H.name+")"
return GH
|