Skip to main content
`Ctrl`+`K`

# double_edge_swap

double_edge_swap(G, nswap=1, max_tries=100, seed=None)

  Swap two edges in the graph while keeping the node degrees fixed.

  A double-edge swap removes two randomly chosen edges u-v and x-y and creates the new edges u-x and v-y:

      u--v            u  v
             becomes  |  |
      x--y            x  y

  If either the edge u-x or v-y already exist no swap is performed and another attempt is made to find a suitable edge pair.

  Parameters:
    G graph

      An undirected graph

    nswap integer (optional, default=1)

      Number of double-edge swaps to perform

    max_tries integer (optional)

      Maximum number of attempts to swap edges

    seed integer, random_state, or None (default)

      Indicator of random number generation state. See Randomness.

  Returns:
    G graph

      The graph after double edge swaps.

  Raises:
    NetworkXError

      If `G` is directed, or If `nswap` > `max_tries`, or If there are fewer than 4 nodes or 2 edges in `G`.

    NetworkXAlgorithmError

      If the number of swap attempts exceeds `max_tries` before `nswap` swaps are made

  Notes

  Does not enforce any connectivity constraints.

  The graph G is modified in place.