-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirected_grpah.py
More file actions
58 lines (46 loc) · 1.67 KB
/
directed_grpah.py
File metadata and controls
58 lines (46 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# This code was NOT WRITTEN BY ME. It was written by ChatAI.
# this code creates a weighted directed graph with 10 nodes and random weights between 1 and 10
import random
import matplotlib.pyplot as plt
import networkx as nx
class Graph:
def __init__(self, num_nodes):
self.num_nodes = num_nodes
self.adj_matrix = [[0 for _ in range(num_nodes)] for _ in range(num_nodes)]
def add_edge(self, src, dest, weight):
self.adj_matrix[src][dest] = weight
# Get the number of nodes from the user
num_nodes = int(input("Enter the number of nodes: "))
# Create the graph with the specified number of nodes
g = Graph(num_nodes)
# Add edges to the graph with random weights between 1 and 10
for i in range(num_nodes):
num_edges = random.randint(1, 10)
for j in random.sample(range(num_nodes), num_edges):
if i != j:
weight = random.randint(1, 10)
g.add_edge(i, j, weight)
# Print the adjacency matrix
print("Adjacency matrix:")
for i in range(num_nodes):
for j in range(num_nodes):
print(f"{g.adj_matrix[i][j]:>3}", end=" ")
print()
# Create a NetworkX graph
nx_graph = nx.DiGraph()
# Add nodes to the graph
for i in range(num_nodes):
nx_graph.add_node(i)
# Add edges to the graph
for i in range(num_nodes):
for j in range(num_nodes):
if g.adj_matrix[i][j] > 0:
nx_graph.add_edge(i, j, weight=g.adj_matrix[i][j])
# Draw the graph
pos = nx.circular_layout(nx_graph)
nx.draw(nx_graph, pos, with_labels=True, font_weight='bold')
# Draw edge labels
edge_labels = nx.get_edge_attributes(nx_graph, 'weight')
nx.draw_networkx_edge_labels(nx_graph, pos, edge_labels=edge_labels)
# Show the graph
plt.show()