-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy path01.3D_Graph_Visualizations.py
122 lines (102 loc) · 3.58 KB
/
01.3D_Graph_Visualizations.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import networkx as nx
import plotly.graph_objects as go
def dijkstra_shortest_path(graph, source):
distances = {node: float('inf') for node in graph.nodes()}
distances[source] = 0
visited = set()
while len(visited) < len(graph.nodes()):
# Find the nearest vertex
min_distance = float('inf')
nearest_vertex = None
for node in graph.nodes():
if distances[node] < min_distance and node not in visited:
min_distance = distances[node]
nearest_vertex = node
visited.add(nearest_vertex)
# Update distances through the nearest vertex
for neighbor in graph[nearest_vertex]:
new_distance = distances[nearest_vertex] + graph[nearest_vertex][neighbor]['weight']
if new_distance < distances[neighbor]:
distances[neighbor] = new_distance
return distances
# Create the graph
G = nx.Graph()
edges = [('K', 'a', 4), ('K', 'b', 2), ('K', 'd', 15), ('a', 'c', 3), ('b', 'c', 7), ('c', 'd', 8), ('c', 'L', 25)]
G.add_weighted_edges_from(edges)
# Calculate the shortest paths from vertex K
shortest_paths_from_K = dijkstra_shortest_path(G, 'K')
# Output the results
print("Shortest Paths from K:")
print(shortest_paths_from_K)
# Update the graph by adding vertex 'a' to S and calculate the new shortest paths
shortest_paths_from_K_and_a = dijkstra_shortest_path(G, 'a')
# Output the updated results
print("\nShortest Paths from K and a:")
print(shortest_paths_from_K_and_a)
# Update the graph by adding vertex 'b' to S and calculate the new shortest paths
shortest_paths_from_K_and_b = dijkstra_shortest_path(G, 'b')
# Output the updated results
print("\nShortest Paths from K and b:")
print(shortest_paths_from_K_and_b)
# Update the graph by adding vertex 'd' to S and calculate the new shortest paths
shortest_paths_from_K_and_d = dijkstra_shortest_path(G, 'd')
# Output the updated results
print("\nShortest Paths from K and d:")
print(shortest_paths_from_K_and_d)
# Create a visualization of the graph
pos = nx.spring_layout(G, seed=42)
edge_trace = go.Scatter(
x=[],
y=[],
line=dict(width=0.5, color='grey'),
hoverinfo='none',
mode='lines'
)
for edge in G.edges():
x0, y0 = pos[edge[0]]
x1, y1 = pos[edge[1]]
edge_trace['x'] += (x0, x1, None)
edge_trace['y'] += (y0, y1, None)
node_trace = go.Scatter(
x=[],
y=[],
text=[],
mode='markers+text',
textposition='bottom center',
marker=dict(
showscale=True,
colorscale='Viridis',
size=10,
colorbar=dict(
thickness=15,
title='Node Connections',
xanchor='left',
titleside='right'
)
)
)
for node in G.nodes():
x, y = pos[node]
node_trace['x'] += (x,)
node_trace['y'] += (y,)
node_trace['text'] += (node,)
fig = go.Figure(data=[edge_trace, node_trace],
layout=go.Layout(
title='Graph Visualization',
showlegend=False,
hovermode='closest',
margin=dict(b=0, l=0, r=0, t=0),
annotations=[dict(
ax=x0,
ay=y0,
axref='x',
ayref='y',
x=x1,
y=y1,
xref='x',
yref='y'
) for x0, y0, x1, y1 in zip(edge_trace['x'][:-1], edge_trace['y'][:-1],
edge_trace['x'][1:], edge_trace['y'][1:])]
)
)
fig.show()