das auf die Komplexität dieser Thematik hinweist.
Ich bin zufrieden, wenn ich den vorliegenden Code halbwegs verstehe.
Code: Alles auswählen
mport networkx as nx
G = nx.Graph()
# Adding one edge at a time
# Node 1 and 2 will be automatically added
G.add_edge(1,2)
G.add_edge(3,2)
print (G)
# Adding multiple edges at a time
G.add_edges_from([(4,2), (3,5), (5,4)])
# Adding duplicates will be ignored.
G.add_node(1)
G.add_edge(1,2)
print(G.nodes())
print(G.edges())
[1, 2, 3, 4, 5]
[(1, 2), (2, 3), (2, 4), (3, 5), (4, 5)]
import networkx as nx
G = nx.Graph()
# Creating graph
G.add_edges_from([(1, 2), (2, 3), (3, 4), (4, 1)])
G.add_edges_from([(5, 6), (5, 7), (5, 8), (7, 8)])
print(G.nodes())
print(G.edges())
# Removing edge 1-2 from graph
G.remove_edge(2, 1)
# Removing edge 3-4 and 1-4 at once
G.remove_edges_from([(3, 4), (1, 4)])
print()
print(G.nodes())
print(G.edges())
# Removing node 5 from graph
G.remove_node(5)
# Removing node 7 and 8
G.remove_nodes_from([7,8])
print()
print(G.nodes())
print(G.edges())
[1, 2, 3, 4, 5, 6, 7, 8]
[(1, 2), (1, 4), (2, 3), (3, 4), (5, 6), (5, 7), (5, 8), (7, 8)]
[1, 2, 3, 4, 5, 6, 7, 8]
[(2, 3), (5, 6), (5, 7), (5, 8), (7, 8)]
[1, 2, 3, 4, 6]
[(2, 3)]
import networkx as nx
G = nx.Graph()
G.add_edges_from([(1,2), (1,3), (3,4), (3,5)])
print("Nodes")
print(G.nodes)
print("Edges")
print(G.edges)
print("Adjacency List")
print(G.adj)
print("Degree")
print(G.degree)
print()
print("Adjacency List for node 3")
print(G.adj[3])
print("Degree for node 3")
print(G.degree[3])
[1, 2, 3, 4, 5]
[(1, 2), (1, 3), (3, 4), (3, 5)]
#Adjacency List
{1: {2: {}, 3: {}}, 2: {1: {}}, 3: {1: {}, 4: {}, 5: {}}, 4: {3: {}}, 5: {3: {}}}
#[(1, 2), (2, 1), (3, 3), (4, 1), (5, 1)]
#Adjacency List for node 3
{1: {}, 4: {}, 5: {}}
#3
#Jeder Graph, Knoten und jede Kante kann Schlüssel/Wert-Attributpaare
#
#Sie können dem Diagramm beim Erstellen Attribute zuweisen nx.Graph().
import networkx as nx
G = nx.Graph(graph_description = "This is an empty graph")
print(G.graph)
import networkx as nx
G = nx.Graph()
G.graph["description"] = "This is empty graph"
G.graph["data"] = 5
print(G.graph)
# Output: {'description': 'This is empty graph', 'data': 5}
#2. Knotenattribute
#
import networkx as nx
G = nx.Graph()
# Using add_node
G.add_node(1, data = "data1")
# Using add_nodes_from
G.add_nodes_from([(2, {"data": "data2"}),
(3, {"data": "data3"})],
node_type = "child node")
# Adding more attributes on node 1 using G.nodes
G.nodes[1]["type"] = "root node"
print(G.nodes.data())
# Output: [(1, {'data': 'data1', 'type': 'root node'}), (2, {'node_type': 'child node', 'data': 'data2'}), (3, {'node_type': 'child node', 'data': 'data3'})]
print(G.nodes[1])
import networkx as nx
G = nx.Graph()
# Using add_edge
G.add_edge(1, 2, weight = 50)
# Using add_edges_from
G.add_edges_from([
(1, 3, {"weight": 70}),
(1, 4, {"weight": 100})
])
# Using subscript notation
G.add_edge(4,5)
G[4][5]["weight"] = 175
# Using G.edges
G.edges[1, 2]["weight"] = 10
print(G.edges.data())
# Output: [(1, 2, {'weight': 10}), (1, 3, {'weight': 70}), (1, 4, {'weight': 100}), (4, 5, {'weight': 175})]
#draw()Mit der gezeigten Methode können wir Diagramme zeichnen und sie im NetworkX-Paket visualisieren .
import networkx as nx
G = nx.Graph()
# Using add_edge
G.add_edge(1, 2, weight = 12.5)
G.add_edge(3, 2, weight = 50.0)
G.add_edge(1, 3, weight = 17)
G.add_edge(4, 2, weight = 100)
G.add_edge(2, 5, weight = 1)
G.add_edge(4, 6, weight = 25.5)
G.add_edge(7, 4, weight = 175)
G.add_edge(5, 8, weight = 90)
nx.draw(G, with_labels= True, font_weight='bold')
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
# Using add_edge
G.add_edge(1, 2, weight = 12.5)
G.add_edge(3, 2, weight = 50.0)
G.add_edge(1, 3, weight = 17)
G.add_edge(4, 2, weight = 100)
G.add_edge(2, 5, weight = 1)
G.add_edge(4, 6, weight = 25.5)
G.add_edge(7, 4, weight = 175)
G.add_edge(5, 8, weight = 90)
pos=nx.circular_layout(G)
nx.draw(G, pos, with_labels=True, font_weight='bold')
edge_weight = nx.get_edge_attributes(G,'weight')
nx.draw_networkx_edge_labels(G, pos, edge_labels = edge_weight)
plt.show()
import networkx as nx
DG = nx.DiGraph()
DG.add_edges_from([(1,2), (2,3), (3,4), (4,5), (5,2), (4, 6)])
print(DG)
# Print edges going out from node 4
print("Out edges of node 4 are:",DG.out_edges(4))
# Print in degree of node 2Predecessors
print("In Degree of node 2 is:",DG.in_degree(2))
# Print successors of node 4
print("Successors of node 4 are:",list(DG.successors(4)))
# Print predecessors of node 2
print("Predecessors of node 2 are:",list(DG.Predecessors(2)))
nx.draw(DG, with_labels= True, font_weight='bold')
#Out edges of node 4 are: [(4, 5), (4, 6)]
#Predecessors of node 2 are: [1, 5]