richds commited on
Commit
f038fa2
·
verified ·
1 Parent(s): ac0ca6c
Files changed (1) hide show
  1. app.py +127 -0
app.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import networkx as nx
3
+ import plotly.graph_objects as go
4
+ import string
5
+ import random
6
+ import heapq
7
+
8
+
9
+ # generate the graph
10
+ def generate_graph():
11
+ alphabet = list(string.ascii_uppercase)
12
+ node_labels = alphabet + ['A' + letter for letter in alphabet[:22]]
13
+
14
+ G = nx.Graph()
15
+ G.add_nodes_from(node_labels)
16
+
17
+ for _ in range(94):
18
+ node1, node2 = random.sample(node_labels, 2)
19
+ weight = random.randint(1, 10)
20
+ G.add_edge(node1, node2, weight=weight)
21
+
22
+ pos = {node: (random.uniform(-10, 10), random.uniform(-10, 10), random.uniform(-10, 10)) for node in node_labels}
23
+
24
+ return G, pos
25
+
26
+
27
+ # visualise the 3D graph
28
+ def visualize_3d_graph_plotly(G, pos, path=None):
29
+ edge_trace = []
30
+ path_edge_trace = []
31
+ node_x, node_y, node_z = [], [], []
32
+ node_text = []
33
+
34
+ for node in G.nodes():
35
+ x, y, z = pos[node]
36
+ node_x.append(x)
37
+ node_y.append(y)
38
+ node_z.append(z)
39
+ node_text.append(node)
40
+
41
+ for edge in G.edges():
42
+ x0, y0, z0 = pos[edge[0]]
43
+ x1, y1, z1 = pos[edge[1]]
44
+ edge_trace.append(go.Scatter3d(x=[x0, x1], y=[y0, y1], z=[z0, z1],
45
+ mode='lines', line=dict(color='gray', width=2)))
46
+
47
+ if path:
48
+ path_edges = list(zip(path, path[1:]))
49
+ for edge in path_edges:
50
+ x0, y0, z0 = pos[edge[0]]
51
+ x1, y1, z1 = pos[edge[1]]
52
+ path_edge_trace.append(go.Scatter3d(x=[x0, x1], y=[y0, y1], z=[z0, z1],
53
+ mode='lines', line=dict(color='blue', width=4)))
54
+
55
+ node_trace = go.Scatter3d(x=node_x, y=node_y, z=node_z,
56
+ mode='markers+text',
57
+ text=node_text,
58
+ textposition='top center',
59
+ marker=dict(size=8, color='skyblue'),
60
+ hoverinfo='text')
61
+
62
+ fig = go.Figure(data=edge_trace + path_edge_trace + [node_trace],
63
+ layout=go.Layout(title='3D Graph Visualization',
64
+ showlegend=False,
65
+ width=1000,
66
+ height=800,
67
+ scene=dict(xaxis=dict(showbackground=False),
68
+ yaxis=dict(showbackground=False),
69
+ zaxis=dict(showbackground=False))))
70
+ return fig
71
+
72
+
73
+ # Dijkstra's Algorithm implementation
74
+ def dijkstra_3d(graph, start, goal):
75
+ queue = [(0, start)]
76
+ distances = {node: float('inf') for node in graph.nodes}
77
+ distances[start] = 0
78
+ previous_nodes = {node: None for node in graph.nodes}
79
+
80
+ while queue:
81
+ current_distance, current_node = heapq.heappop(queue)
82
+
83
+ if current_node == goal:
84
+ break
85
+
86
+ for neighbor, attributes in graph[current_node].items():
87
+ weight = attributes['weight']
88
+ distance = current_distance + weight
89
+
90
+ if distance < distances[neighbor]:
91
+ distances[neighbor] = distance
92
+ previous_nodes[neighbor] = current_node
93
+ heapq.heappush(queue, (distance, neighbor))
94
+
95
+ path = []
96
+ current_node = goal
97
+ while current_node is not None:
98
+ path.insert(0, current_node)
99
+ current_node = previous_nodes[current_node]
100
+
101
+ return path, distances[goal]
102
+
103
+
104
+ # Streamlit app
105
+ def main():
106
+ st.title("3D Graph Dijkstra's Algorithm Visualization")
107
+
108
+ st.sidebar.header("Graph Options")
109
+ G, pos = generate_graph()
110
+
111
+ nodes = list(G.nodes)
112
+ start_node = st.sidebar.selectbox("Select Start Point:", nodes)
113
+ goal_node = st.sidebar.selectbox("Select Goal Point:", nodes)
114
+
115
+ if st.sidebar.button("Run Algorithm"):
116
+ shortest_path, shortest_distance = dijkstra_3d(G, start_node, goal_node)
117
+ st.write(f"**Shortest path from {start_node} to {goal_node}:** {shortest_path}")
118
+ st.write(f"**Shortest distance:** {shortest_distance}")
119
+ fig = visualize_3d_graph_plotly(G, pos, path=shortest_path)
120
+ else:
121
+ fig = visualize_3d_graph_plotly(G, pos)
122
+
123
+ st.plotly_chart(fig)
124
+
125
+
126
+ if __name__ == "__main__":
127
+ main()