line.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. """Functions for generating line graphs."""
  2. from collections import defaultdict
  3. from functools import partial
  4. from itertools import combinations
  5. import networkx as nx
  6. from networkx.utils import arbitrary_element
  7. from networkx.utils.decorators import not_implemented_for
  8. __all__ = ["line_graph", "inverse_line_graph"]
  9. def line_graph(G, create_using=None):
  10. r"""Returns the line graph of the graph or digraph `G`.
  11. The line graph of a graph `G` has a node for each edge in `G` and an
  12. edge joining those nodes if the two edges in `G` share a common node. For
  13. directed graphs, nodes are adjacent exactly when the edges they represent
  14. form a directed path of length two.
  15. The nodes of the line graph are 2-tuples of nodes in the original graph (or
  16. 3-tuples for multigraphs, with the key of the edge as the third element).
  17. For information about self-loops and more discussion, see the **Notes**
  18. section below.
  19. Parameters
  20. ----------
  21. G : graph
  22. A NetworkX Graph, DiGraph, MultiGraph, or MultiDigraph.
  23. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  24. Graph type to create. If graph instance, then cleared before populated.
  25. Returns
  26. -------
  27. L : graph
  28. The line graph of G.
  29. Examples
  30. --------
  31. >>> G = nx.star_graph(3)
  32. >>> L = nx.line_graph(G)
  33. >>> print(sorted(map(sorted, L.edges()))) # makes a 3-clique, K3
  34. [[(0, 1), (0, 2)], [(0, 1), (0, 3)], [(0, 2), (0, 3)]]
  35. Edge attributes from `G` are not copied over as node attributes in `L`, but
  36. attributes can be copied manually:
  37. >>> G = nx.path_graph(4)
  38. >>> G.add_edges_from((u, v, {"tot": u+v}) for u, v in G.edges)
  39. >>> G.edges(data=True)
  40. EdgeDataView([(0, 1, {'tot': 1}), (1, 2, {'tot': 3}), (2, 3, {'tot': 5})])
  41. >>> H = nx.line_graph(G)
  42. >>> H.add_nodes_from((node, G.edges[node]) for node in H)
  43. >>> H.nodes(data=True)
  44. NodeDataView({(0, 1): {'tot': 1}, (2, 3): {'tot': 5}, (1, 2): {'tot': 3}})
  45. Notes
  46. -----
  47. Graph, node, and edge data are not propagated to the new graph. For
  48. undirected graphs, the nodes in G must be sortable, otherwise the
  49. constructed line graph may not be correct.
  50. *Self-loops in undirected graphs*
  51. For an undirected graph `G` without multiple edges, each edge can be
  52. written as a set `\{u, v\}`. Its line graph `L` has the edges of `G` as
  53. its nodes. If `x` and `y` are two nodes in `L`, then `\{x, y\}` is an edge
  54. in `L` if and only if the intersection of `x` and `y` is nonempty. Thus,
  55. the set of all edges is determined by the set of all pairwise intersections
  56. of edges in `G`.
  57. Trivially, every edge in G would have a nonzero intersection with itself,
  58. and so every node in `L` should have a self-loop. This is not so
  59. interesting, and the original context of line graphs was with simple
  60. graphs, which had no self-loops or multiple edges. The line graph was also
  61. meant to be a simple graph and thus, self-loops in `L` are not part of the
  62. standard definition of a line graph. In a pairwise intersection matrix,
  63. this is analogous to excluding the diagonal entries from the line graph
  64. definition.
  65. Self-loops and multiple edges in `G` add nodes to `L` in a natural way, and
  66. do not require any fundamental changes to the definition. It might be
  67. argued that the self-loops we excluded before should now be included.
  68. However, the self-loops are still "trivial" in some sense and thus, are
  69. usually excluded.
  70. *Self-loops in directed graphs*
  71. For a directed graph `G` without multiple edges, each edge can be written
  72. as a tuple `(u, v)`. Its line graph `L` has the edges of `G` as its
  73. nodes. If `x` and `y` are two nodes in `L`, then `(x, y)` is an edge in `L`
  74. if and only if the tail of `x` matches the head of `y`, for example, if `x
  75. = (a, b)` and `y = (b, c)` for some vertices `a`, `b`, and `c` in `G`.
  76. Due to the directed nature of the edges, it is no longer the case that
  77. every edge in `G` should have a self-loop in `L`. Now, the only time
  78. self-loops arise is if a node in `G` itself has a self-loop. So such
  79. self-loops are no longer "trivial" but instead, represent essential
  80. features of the topology of `G`. For this reason, the historical
  81. development of line digraphs is such that self-loops are included. When the
  82. graph `G` has multiple edges, once again only superficial changes are
  83. required to the definition.
  84. References
  85. ----------
  86. * Harary, Frank, and Norman, Robert Z., "Some properties of line digraphs",
  87. Rend. Circ. Mat. Palermo, II. Ser. 9 (1960), 161--168.
  88. * Hemminger, R. L.; Beineke, L. W. (1978), "Line graphs and line digraphs",
  89. in Beineke, L. W.; Wilson, R. J., Selected Topics in Graph Theory,
  90. Academic Press Inc., pp. 271--305.
  91. """
  92. if G.is_directed():
  93. L = _lg_directed(G, create_using=create_using)
  94. else:
  95. L = _lg_undirected(G, selfloops=False, create_using=create_using)
  96. return L
  97. def _lg_directed(G, create_using=None):
  98. """Returns the line graph L of the (multi)digraph G.
  99. Edges in G appear as nodes in L, represented as tuples of the form (u,v)
  100. or (u,v,key) if G is a multidigraph. A node in L corresponding to the edge
  101. (u,v) is connected to every node corresponding to an edge (v,w).
  102. Parameters
  103. ----------
  104. G : digraph
  105. A directed graph or directed multigraph.
  106. create_using : NetworkX graph constructor, optional
  107. Graph type to create. If graph instance, then cleared before populated.
  108. Default is to use the same graph class as `G`.
  109. """
  110. L = nx.empty_graph(0, create_using, default=G.__class__)
  111. # Create a graph specific edge function.
  112. get_edges = partial(G.edges, keys=True) if G.is_multigraph() else G.edges
  113. for from_node in get_edges():
  114. # from_node is: (u,v) or (u,v,key)
  115. L.add_node(from_node)
  116. for to_node in get_edges(from_node[1]):
  117. L.add_edge(from_node, to_node)
  118. return L
  119. def _lg_undirected(G, selfloops=False, create_using=None):
  120. """Returns the line graph L of the (multi)graph G.
  121. Edges in G appear as nodes in L, represented as sorted tuples of the form
  122. (u,v), or (u,v,key) if G is a multigraph. A node in L corresponding to
  123. the edge {u,v} is connected to every node corresponding to an edge that
  124. involves u or v.
  125. Parameters
  126. ----------
  127. G : graph
  128. An undirected graph or multigraph.
  129. selfloops : bool
  130. If `True`, then self-loops are included in the line graph. If `False`,
  131. they are excluded.
  132. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  133. Graph type to create. If graph instance, then cleared before populated.
  134. Notes
  135. -----
  136. The standard algorithm for line graphs of undirected graphs does not
  137. produce self-loops.
  138. """
  139. L = nx.empty_graph(0, create_using, default=G.__class__)
  140. # Graph specific functions for edges.
  141. get_edges = partial(G.edges, keys=True) if G.is_multigraph() else G.edges
  142. # Determine if we include self-loops or not.
  143. shift = 0 if selfloops else 1
  144. # Introduce numbering of nodes
  145. node_index = {n: i for i, n in enumerate(G)}
  146. # Lift canonical representation of nodes to edges in line graph
  147. edge_key_function = lambda edge: (node_index[edge[0]], node_index[edge[1]])
  148. edges = set()
  149. for u in G:
  150. # Label nodes as a sorted tuple of nodes in original graph.
  151. # Decide on representation of {u, v} as (u, v) or (v, u) depending on node_index.
  152. # -> This ensures a canonical representation and avoids comparing values of different types.
  153. nodes = [tuple(sorted(x[:2], key=node_index.get)) + x[2:] for x in get_edges(u)]
  154. if len(nodes) == 1:
  155. # Then the edge will be an isolated node in L.
  156. L.add_node(nodes[0])
  157. # Add a clique of `nodes` to graph. To prevent double adding edges,
  158. # especially important for multigraphs, we store the edges in
  159. # canonical form in a set.
  160. for i, a in enumerate(nodes):
  161. edges.update(
  162. [
  163. tuple(sorted((a, b), key=edge_key_function))
  164. for b in nodes[i + shift :]
  165. ]
  166. )
  167. L.add_edges_from(edges)
  168. return L
  169. @not_implemented_for("directed")
  170. @not_implemented_for("multigraph")
  171. def inverse_line_graph(G):
  172. """Returns the inverse line graph of graph G.
  173. If H is a graph, and G is the line graph of H, such that G = L(H).
  174. Then H is the inverse line graph of G.
  175. Not all graphs are line graphs and these do not have an inverse line graph.
  176. In these cases this function raises a NetworkXError.
  177. Parameters
  178. ----------
  179. G : graph
  180. A NetworkX Graph
  181. Returns
  182. -------
  183. H : graph
  184. The inverse line graph of G.
  185. Raises
  186. ------
  187. NetworkXNotImplemented
  188. If G is directed or a multigraph
  189. NetworkXError
  190. If G is not a line graph
  191. Notes
  192. -----
  193. This is an implementation of the Roussopoulos algorithm[1]_.
  194. If G consists of multiple components, then the algorithm doesn't work.
  195. You should invert every component separately:
  196. >>> K5 = nx.complete_graph(5)
  197. >>> P4 = nx.Graph([("a", "b"), ("b", "c"), ("c", "d")])
  198. >>> G = nx.union(K5, P4)
  199. >>> root_graphs = []
  200. >>> for comp in nx.connected_components(G):
  201. ... root_graphs.append(nx.inverse_line_graph(G.subgraph(comp)))
  202. >>> len(root_graphs)
  203. 2
  204. References
  205. ----------
  206. .. [1] Roussopoulos, N.D. , "A max {m, n} algorithm for determining the graph H from
  207. its line graph G", Information Processing Letters 2, (1973), 108--112, ISSN 0020-0190,
  208. `DOI link <https://doi.org/10.1016/0020-0190(73)90029-X>`_
  209. """
  210. if G.number_of_nodes() == 0:
  211. return nx.empty_graph(1)
  212. elif G.number_of_nodes() == 1:
  213. v = arbitrary_element(G)
  214. a = (v, 0)
  215. b = (v, 1)
  216. H = nx.Graph([(a, b)])
  217. return H
  218. elif G.number_of_nodes() > 1 and G.number_of_edges() == 0:
  219. msg = (
  220. "inverse_line_graph() doesn't work on an edgeless graph. "
  221. "Please use this function on each component separately."
  222. )
  223. raise nx.NetworkXError(msg)
  224. if nx.number_of_selfloops(G) != 0:
  225. msg = (
  226. "A line graph as generated by NetworkX has no selfloops, so G has no "
  227. "inverse line graph. Please remove the selfloops from G and try again."
  228. )
  229. raise nx.NetworkXError(msg)
  230. starting_cell = _select_starting_cell(G)
  231. P = _find_partition(G, starting_cell)
  232. # count how many times each vertex appears in the partition set
  233. P_count = {u: 0 for u in G.nodes}
  234. for p in P:
  235. for u in p:
  236. P_count[u] += 1
  237. if max(P_count.values()) > 2:
  238. msg = "G is not a line graph (vertex found in more than two partition cells)"
  239. raise nx.NetworkXError(msg)
  240. W = tuple((u,) for u in P_count if P_count[u] == 1)
  241. H = nx.Graph()
  242. H.add_nodes_from(P)
  243. H.add_nodes_from(W)
  244. for a, b in combinations(H.nodes, 2):
  245. if any(a_bit in b for a_bit in a):
  246. H.add_edge(a, b)
  247. return H
  248. def _triangles(G, e):
  249. """Return list of all triangles containing edge e"""
  250. u, v = e
  251. if u not in G:
  252. raise nx.NetworkXError(f"Vertex {u} not in graph")
  253. if v not in G[u]:
  254. raise nx.NetworkXError(f"Edge ({u}, {v}) not in graph")
  255. triangle_list = []
  256. for x in G[u]:
  257. if x in G[v]:
  258. triangle_list.append((u, v, x))
  259. return triangle_list
  260. def _odd_triangle(G, T):
  261. """Test whether T is an odd triangle in G
  262. Parameters
  263. ----------
  264. G : NetworkX Graph
  265. T : 3-tuple of vertices forming triangle in G
  266. Returns
  267. -------
  268. True is T is an odd triangle
  269. False otherwise
  270. Raises
  271. ------
  272. NetworkXError
  273. T is not a triangle in G
  274. Notes
  275. -----
  276. An odd triangle is one in which there exists another vertex in G which is
  277. adjacent to either exactly one or exactly all three of the vertices in the
  278. triangle.
  279. """
  280. for u in T:
  281. if u not in G.nodes():
  282. raise nx.NetworkXError(f"Vertex {u} not in graph")
  283. for e in list(combinations(T, 2)):
  284. if e[0] not in G[e[1]]:
  285. raise nx.NetworkXError(f"Edge ({e[0]}, {e[1]}) not in graph")
  286. T_neighbors = defaultdict(int)
  287. for t in T:
  288. for v in G[t]:
  289. if v not in T:
  290. T_neighbors[v] += 1
  291. return any(T_neighbors[v] in [1, 3] for v in T_neighbors)
  292. def _find_partition(G, starting_cell):
  293. """Find a partition of the vertices of G into cells of complete graphs
  294. Parameters
  295. ----------
  296. G : NetworkX Graph
  297. starting_cell : tuple of vertices in G which form a cell
  298. Returns
  299. -------
  300. List of tuples of vertices of G
  301. Raises
  302. ------
  303. NetworkXError
  304. If a cell is not a complete subgraph then G is not a line graph
  305. """
  306. G_partition = G.copy()
  307. P = [starting_cell] # partition set
  308. G_partition.remove_edges_from(list(combinations(starting_cell, 2)))
  309. # keep list of partitioned nodes which might have an edge in G_partition
  310. partitioned_vertices = list(starting_cell)
  311. while G_partition.number_of_edges() > 0:
  312. # there are still edges left and so more cells to be made
  313. u = partitioned_vertices.pop()
  314. deg_u = len(G_partition[u])
  315. if deg_u != 0:
  316. # if u still has edges then we need to find its other cell
  317. # this other cell must be a complete subgraph or else G is
  318. # not a line graph
  319. new_cell = [u] + list(G_partition[u])
  320. for u in new_cell:
  321. for v in new_cell:
  322. if (u != v) and (v not in G_partition[u]):
  323. msg = (
  324. "G is not a line graph"
  325. "(partition cell not a complete subgraph)"
  326. )
  327. raise nx.NetworkXError(msg)
  328. P.append(tuple(new_cell))
  329. G_partition.remove_edges_from(list(combinations(new_cell, 2)))
  330. partitioned_vertices += new_cell
  331. return P
  332. def _select_starting_cell(G, starting_edge=None):
  333. """Select a cell to initiate _find_partition
  334. Parameters
  335. ----------
  336. G : NetworkX Graph
  337. starting_edge: an edge to build the starting cell from
  338. Returns
  339. -------
  340. Tuple of vertices in G
  341. Raises
  342. ------
  343. NetworkXError
  344. If it is determined that G is not a line graph
  345. Notes
  346. -----
  347. If starting edge not specified then pick an arbitrary edge - doesn't
  348. matter which. However, this function may call itself requiring a
  349. specific starting edge. Note that the r, s notation for counting
  350. triangles is the same as in the Roussopoulos paper cited above.
  351. """
  352. if starting_edge is None:
  353. e = arbitrary_element(G.edges())
  354. else:
  355. e = starting_edge
  356. if e[0] not in G.nodes():
  357. raise nx.NetworkXError(f"Vertex {e[0]} not in graph")
  358. if e[1] not in G[e[0]]:
  359. msg = f"starting_edge ({e[0]}, {e[1]}) is not in the Graph"
  360. raise nx.NetworkXError(msg)
  361. e_triangles = _triangles(G, e)
  362. r = len(e_triangles)
  363. if r == 0:
  364. # there are no triangles containing e, so the starting cell is just e
  365. starting_cell = e
  366. elif r == 1:
  367. # there is exactly one triangle, T, containing e. If other 2 edges
  368. # of T belong only to this triangle then T is starting cell
  369. T = e_triangles[0]
  370. a, b, c = T
  371. # ab was original edge so check the other 2 edges
  372. ac_edges = len(_triangles(G, (a, c)))
  373. bc_edges = len(_triangles(G, (b, c)))
  374. if ac_edges == 1:
  375. if bc_edges == 1:
  376. starting_cell = T
  377. else:
  378. return _select_starting_cell(G, starting_edge=(b, c))
  379. else:
  380. return _select_starting_cell(G, starting_edge=(a, c))
  381. else:
  382. # r >= 2 so we need to count the number of odd triangles, s
  383. s = 0
  384. odd_triangles = []
  385. for T in e_triangles:
  386. if _odd_triangle(G, T):
  387. s += 1
  388. odd_triangles.append(T)
  389. if r == 2 and s == 0:
  390. # in this case either triangle works, so just use T
  391. starting_cell = T
  392. elif r - 1 <= s <= r:
  393. # check if odd triangles containing e form complete subgraph
  394. triangle_nodes = set()
  395. for T in odd_triangles:
  396. for x in T:
  397. triangle_nodes.add(x)
  398. for u in triangle_nodes:
  399. for v in triangle_nodes:
  400. if u != v and (v not in G[u]):
  401. msg = (
  402. "G is not a line graph (odd triangles "
  403. "do not form complete subgraph)"
  404. )
  405. raise nx.NetworkXError(msg)
  406. # otherwise then we can use this as the starting cell
  407. starting_cell = tuple(triangle_nodes)
  408. else:
  409. msg = (
  410. "G is not a line graph (incorrect number of "
  411. "odd triangles around starting edge)"
  412. )
  413. raise nx.NetworkXError(msg)
  414. return starting_cell