line.py 17 KB

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