Networkx Plannar Embedding confusing

58 views Asked by At

I am running the following code to get a plannar graph with a specific embedding:

import numpy as np
import matplotlib.pyplot as plt
import networkx as nx

#Defining the graph with pannar embedding
H=nx.PlanarEmbedding()
H.add_half_edge_cw(1,2,None)
H.add_half_edge_cw(2,1,None)
H.add_half_edge_ccw(2,3,1)
H.add_half_edge_ccw(3,2,None)
H.add_half_edge_ccw(3,1,2)
H.add_half_edge_cw(1,3,2)
H.add_half_edge_ccw(2,4,1)
H.add_half_edge_ccw(4,2,None)
H.add_half_edge_cw(4,3,2)
H.add_half_edge_cw(3,4,1)

#Try to plot the graph with the above embedding,
#if the embedding is not correct it prints 'Fail'
try:
  H.check_structure()
  pos=nx.combinatorial_embedding_to_pos(H)
  nx.draw(H,pos,with_labels=True)
except:
  print('Fail')

As an output I get:

Outupt

But I was expecting to get the following embedding:

enter image description here

I don't undestand why my way of defining H is wrong.

Maybe I didn't understand exactly how this .add_half_edge_cw works, if someone could better explain it to me, I would be really thankful.

For example I added H.add_half_edge_ccw(2,4,1), which for me means that the counter-clockwise neighbor of 2-4 should be 2-1, but in the output it is clearly 2-3.

1

There are 1 answers

0
MT0 On

From the documentation:

PlanarEmbedding.add_half_edge_ccw

PlanarEmbedding.add_half_edge_ccw(start_node, end_node, reference_neighbor)[source]

Adds a half-edge from start_node to end_node.

The half-edge is added counter clockwise next to the existing half-edge (start_node, reference_neighbor).

Your assumption is:

I added H.add_half_edge_ccw(2,4,1), which for me means that the counter-clockwise neighbor of 2-4 should be 2-1, but in the output it is clearly 2-3.

The implementation appears to be, start with the existing edge (2,1) and, relative to node 2, go counter-clockwise and add the edge (2,4).

You are assuming that the angular direction is measured from new-to-existing edges rather than the opposite existing-to-new edges.