Skip to content

Commit e984506

Browse files
committed
update, delete
1 parent 904dd3d commit e984506

File tree

4 files changed

+44
-53
lines changed

4 files changed

+44
-53
lines changed

test10.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
import random
12
import networkx as nx
23
import matplotlib.pyplot as plt
34

4-
# Create a Barabasi-Albert graph with 10,000 nodes and m=3
5-
G = nx.barabasi_albert_graph(10000, 3)
5+
# Create a graph
6+
G = nx.Graph()
7+
G.add_nodes_from(range(10))
8+
G.add_edges_from([(0,1), (1,2), (2,3), (3,4), (4,5), (5,6), (6,7), (7,8), (8,9), (9,0)])
69

7-
# Draw a subgraph of the first 100 nodes using NetworkX and Matplotlib
8-
nx.draw(G.subgraph(range(100)), with_labels=False)
10+
# Set random colors for nodes and edges
11+
node_colors = [random.choice(['red', 'blue', 'green']) for _ in range(len(G.nodes()))]
12+
edge_colors = [random.choice(['red', 'blue', 'green']) for _ in range(len(G.edges()))]
13+
14+
# Draw the graph with node and edge colors
15+
nx.draw(G, with_labels=True, node_color=node_colors, edge_color=edge_colors)
916
plt.show()

test11.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
1-
import random
21
import networkx as nx
32
import matplotlib.pyplot as plt
3+
import numpy as np
44

5-
# Create a graph
6-
G = nx.Graph()
7-
G.add_nodes_from(range(10))
8-
G.add_edges_from([(0,1), (1,2), (2,3), (3,4), (4,5), (5,6), (6,7), (7,8), (8,9), (9,0)])
5+
# Create a random graph with 10,000 nodes and 50,000 edges
6+
G = nx.gnm_random_graph(1000, 5000)
97

10-
# Set random colors for nodes and edges
11-
node_colors = [random.choice(['red', 'blue', 'green']) for _ in range(len(G.nodes()))]
12-
edge_colors = [random.choice(['red', 'blue', 'green']) for _ in range(len(G.edges()))]
8+
# Set random weights for the edges
9+
weights = np.random.rand(len(G.edges))
10+
nx.set_edge_attributes(G, dict(zip(G.edges, weights)), 'weight')
1311

14-
# Draw the graph with node and edge colors
15-
nx.draw(G, with_labels=True, node_color=node_colors, edge_color=edge_colors)
12+
# Generate a layout using the spring layout algorithm
13+
pos = nx.spring_layout(G)
14+
15+
# Assign colors to the nodes based on their degree
16+
node_colors = np.array(list(dict(G.degree()).values()))
17+
node_colors = (node_colors - node_colors.min()) / (node_colors.max() - node_colors.min())
18+
19+
# Assign colors to the edges based on their weight
20+
edge_weights = nx.get_edge_attributes(G, 'weight')
21+
edge_weights_norm = (np.array(list(edge_weights.values())) - min(edge_weights.values())) / (max(edge_weights.values()) - min(edge_weights.values()))
22+
cmap = plt.cm.get_cmap('Blues')
23+
edge_colors = cmap(edge_weights_norm)
24+
25+
# Draw the graph using the layout and color scheme
26+
nx.draw(G, pos=pos, node_color=node_colors, edge_color=edge_colors, with_labels=False, node_size=5, width=0.5)
27+
28+
# Add a title and subtitle to the plot
29+
plt.title("Random Graph with 10,000 Nodes and 50,000 Edges")
30+
plt.suptitle("Generated using the gnm_random_graph() function from NetworkX")
31+
32+
# Display the plot
1633
plt.show()

test12.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,17 @@
11
import networkx as nx
22
import matplotlib.pyplot as plt
3-
import numpy as np
43

5-
# Create a random graph with 10,000 nodes and 50,000 edges
6-
G = nx.gnm_random_graph(1000, 5000)
7-
8-
# Set random weights for the edges
9-
weights = np.random.rand(len(G.edges))
10-
nx.set_edge_attributes(G, dict(zip(G.edges, weights)), 'weight')
4+
# Create a random graph with 50 nodes
5+
G = nx.fast_gnp_random_graph(50, 0.2)
116

127
# Generate a layout using the spring layout algorithm
138
pos = nx.spring_layout(G)
149

15-
# Assign colors to the nodes based on their degree
16-
node_colors = np.array(list(dict(G.degree()).values()))
17-
node_colors = (node_colors - node_colors.min()) / (node_colors.max() - node_colors.min())
18-
19-
# Assign colors to the edges based on their weight
20-
edge_weights = nx.get_edge_attributes(G, 'weight')
21-
edge_weights_norm = (np.array(list(edge_weights.values())) - min(edge_weights.values())) / (max(edge_weights.values()) - min(edge_weights.values()))
22-
cmap = plt.cm.get_cmap('Blues')
23-
edge_colors = cmap(edge_weights_norm)
24-
25-
# Draw the graph using the layout and color scheme
26-
nx.draw(G, pos=pos, node_color=node_colors, edge_color=edge_colors, with_labels=False, node_size=5, width=0.5)
10+
# Draw the graph using the layout and default settings
11+
nx.draw(G, pos)
2712

28-
# Add a title and subtitle to the plot
29-
plt.title("Random Graph with 10,000 Nodes and 50,000 Edges")
30-
plt.suptitle("Generated using the gnm_random_graph() function from NetworkX")
13+
# Add a title to the plot
14+
plt.suptitle("Generated using the fast_gnp_random_graph() function from NetworkX", fontsize=12)
3115

3216
# Display the plot
3317
plt.show()

test13.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)