Create a threshold graph from the creation sequence or compact creation_sequence. The input sequence can be a creation sequence (e.g. ['d','i','d','d','d','i']) labeled creation sequence (e.g. [(0,'d'),(2,'d'),(1,'i')]) compact creation sequence (e.g. [2,1,1,2,0]) Use
(creation_sequence, create_using=None)
| 304 | # Manipulating NetworkX.Graphs in context of threshold graphs |
| 305 | @nx._dispatchable(graphs=None, returns_graph=True) |
| 306 | def threshold_graph(creation_sequence, create_using=None): |
| 307 | """ |
| 308 | Create a threshold graph from the creation sequence or compact |
| 309 | creation_sequence. |
| 310 | |
| 311 | The input sequence can be a |
| 312 | |
| 313 | creation sequence (e.g. ['d','i','d','d','d','i']) |
| 314 | labeled creation sequence (e.g. [(0,'d'),(2,'d'),(1,'i')]) |
| 315 | compact creation sequence (e.g. [2,1,1,2,0]) |
| 316 | |
| 317 | Use cs=creation_sequence(degree_sequence,labeled=True) |
| 318 | to convert a degree sequence to a creation sequence. |
| 319 | |
| 320 | Returns None if the sequence is not valid |
| 321 | """ |
| 322 | # Turn input sequence into a labeled creation sequence |
| 323 | first = creation_sequence[0] |
| 324 | if isinstance(first, str): # creation sequence |
| 325 | ci = list(enumerate(creation_sequence)) |
| 326 | elif isinstance(first, tuple): # labeled creation sequence |
| 327 | ci = creation_sequence[:] |
| 328 | elif isinstance(first, int): # compact creation sequence |
| 329 | cs = uncompact(creation_sequence) |
| 330 | ci = list(enumerate(cs)) |
| 331 | else: |
| 332 | raise ValueError("not a valid creation sequence") |
| 333 | |
| 334 | G = nx.empty_graph(0, create_using) |
| 335 | if G.is_directed(): |
| 336 | raise nx.NetworkXError("Directed Graph not supported") |
| 337 | |
| 338 | G.name = "Threshold Graph" |
| 339 | |
| 340 | # add nodes and edges |
| 341 | # if type is 'i' just add nodea |
| 342 | # if type is a d connect to everything previous |
| 343 | while ci: |
| 344 | (v, node_type) = ci.pop(0) |
| 345 | if node_type == "d": # dominating type, connect to all existing nodes |
| 346 | # We use `for u in list(G):` instead of |
| 347 | # `for u in G:` because we edit the graph `G` in |
| 348 | # the loop. Hence using an iterator will result in |
| 349 | # `RuntimeError: dictionary changed size during iteration` |
| 350 | for u in list(G): |
| 351 | G.add_edge(v, u) |
| 352 | G.add_node(v) |
| 353 | return G |
| 354 | |
| 355 | |
| 356 | @nx._dispatchable |
no test coverage detected
searching dependent graphs…