How to check the features in Digraph object in networkx?
时间: 2024-01-20 22:02:58 浏览: 81
You can check the features of a directed graph (Digraph) object in NetworkX by using the various built-in methods and attributes. Here are some examples:
1. To check the number of nodes and edges in the graph, use the `number_of_nodes()` and `number_of_edges()` methods respectively:
```python
num_nodes = G.number_of_nodes()
num_edges = G.number_of_edges()
```
2. To get a list of all nodes in the graph, use the `nodes()` method:
```python
nodes = list(G.nodes())
```
3. To get a list of all edges in the graph, use the `edges()` method:
```python
edges = list(G.edges())
```
4. To check if a node or edge is present in the graph, use the `has_node()` and `has_edge()` methods respectively:
```python
has_node = G.has_node('A')
has_edge = G.has_edge('A', 'B')
```
5. To get the in-degree and out-degree of a node, use the `in_degree()` and `out_degree()` methods respectively:
```python
in_deg = G.in_degree('A')
out_deg = G.out_degree('A')
```
6. To get the successors and predecessors of a node, use the `successors()` and `predecessors()` methods respectively:
```python
successors = list(G.successors('A'))
predecessors = list(G.predecessors('A'))
```
These are just a few examples of the many methods and attributes available in the NetworkX library for working with directed graphs.
阅读全文