<jupyter_start><jupyter_text>NetworkX tutorial<jupyter_code>import numpy as np
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_node(1)
G.add_nodes_from([
    (2, {"color": "red"}),
    (3, {"color": "green"}),
    ("AB", {"color": "blue"}),
])
H = nx.path_graph(10)
G.add_nodes_from(H)
G.add_node(H)<jupyter_output><empty_output><jupyter_text>^^^  Cały graf **H** może występować w roli wierzchołka grafu **G**<jupyter_code>G.add_edge(1, 2)
edg = (2, 3)
G.add_edge(*edg)
G.add_edges_from([
    (1, 2, {'weight': 3.1415}), 
    (1, 3, {'weight': 2.2423}),
])
G.add_edges_from(H.edges)
list(H.edges)
G.clear()
G.add_edges_from([(1, 2), (1, 3)])
G.add_node(1)
G.add_edge(1, 2)
G.add_node("spam")        # adds node "spam"
G.add_nodes_from("spam")  # adds 4 nodes: 's', 'p', 'a', 'm'
G.add_edge(3, 'm')
G.number_of_nodes(), G.number_of_edges()
DG = nx.DiGraph()
DG.add_edge(2, 1)   # adds the nodes in order 2, 1
DG.add_edge(1, 3)
DG.add_edge(2, 4)
DG.add_edge(1, 2)
assert list(DG.successors(2)) == [1, 4]
assert list(DG.edges) == [(2, 1), (2, 4), (1, 3), (1, 2)]
list(G.nodes)
list(G.edges)
list(G.adj)
list(G.degree)
G.degree[1]
list(G.degree([1, 2]))
list(G.edges([2, 'm']))
list(G.neighbors(1))
list(G.neighbors(1)) == list(G.adj[1])
G.remove_node(2)
G.remove_nodes_from("spam")
list(G.nodes)
G.remove_edge(1, 3)    # G.remove_edges_from([...]) works as well<jupyter_output><empty_output><jupyter_text>^^^  Poprzednie komórki **zwrócą błąd**, jeśli nie ma już takiej krawędzi/wierzchołka<jupyter_code>G.add_edge(1, 2)
H = nx.DiGraph(G)   # create a DiGraph using the connections from G
list(H.edges())
edgelist = [(0, 1), (1, 2), (2, 3)]
H = nx.Graph(edgelist)
list(H.edges())<jupyter_output><empty_output><jupyter_text>Ale niestety coś takiego już **nie działa**, zwraca błąd:```pythonnodelist = [0, 1, 2, 3, 4, 5]H = nx.Graph(nodelist)```<jupyter_code>G = nx.Graph([(1, 2, {"color": "yellow"})])
G[1]  # same as G.adj[1]
G[1][2]
G.edges[1, 2]
# Works ONLY if the edge already exists
G.add_edge(1, 3)
G[1][3]['color'] = "blue"
G[1][3]
G.edges[1, 3]['color'] = "red"
G.edges[1, 3]
FG = nx.Graph()

FG.add_weighted_edges_from([
    (1, 2, 0.125), 
    (1, 3, 0.75), 
    (2, 4, 1.2), 
    (3, 4, 0.375),
])

for n, nbrs in FG.adj.items():
    for nbr, eattr in nbrs.items():
        wt = eattr['weight']
        if wt < 0.5: 
            print(f"({n}, {nbr}, {wt:.3})")
for (u, v, wt) in FG.edges.data('weight'):
    if wt < 0.5:
        print(f"({u}, {v}, {wt:.3})")
G = nx.Graph(day="Friday")
G.graph
G.add_node(1, time='5pm')
G.add_nodes_from([3], time='2pm')
G.nodes[1]
G.nodes[1]['room'] = 714
G.nodes.data()

DG = nx.DiGraph()
DG.add_weighted_edges_from([(1, 2, 0.5), (3, 1, 0.75)])
DG.out_degree(1, weight='weight')
DG.degree(1, weight='weight')
list(DG.successors(1))
list(DG.predecessors(1))
list(DG.neighbors(1))
H = nx.Graph(G)  # create an undirected graph H from a directed graph G
G = nx.Graph()
G.add_edges_from([(1, 2), (1, 3)])
G.add_node("spam")       # adds node "spam"
list(nx.connected_components(G))
sorted(d for n, d in G.degree())
nx.clustering(G)
dict(nx.all_pairs_shortest_path(G))
fig, (ax1, ax2) = plt.subplots(1, 2)    # 1 row 2 columns
G = nx.petersen_graph()
nx.draw(G, with_labels=True, 
        font_weight='bold', 
        ax=ax1
       )
nx.draw_shell(
    G, nlist=[range(5, 10), range(5)], 
    with_labels=True, 
    font_weight='bold', 
    ax=ax2
)
options = {
    'node_color': 'black',
    'node_size': 100,
    'width': 3,
}
G = nx.dodecahedral_graph()
shells = [[2, 3, 4, 5, 6], [8, 1, 0, 19, 18, 17, 16, 15, 14, 7], [9, 10, 11, 12, 13]]
nx.draw_shell(G, nlist=shells, **options)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2191891891891892, 'fraction_numerical': 0.052702702702702706, 'mean_word_length': 4.325179856115108, 'pattern_counts': {'":': 4, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '532363', 'license': 'MIT', 'n_tokens_mistral': 1868, 'n_tokens_neox': 1716, 'n_words': 369, 'path': 'Notebooks/NetworkX-tutorial.ipynb', 'repo_name': 'FrightenedFox/Networks-and-Graphs', 'size': '3700'}