shukdevdatta123 commited on
Commit
aa3d7ac
·
verified ·
1 Parent(s): 2e88b92

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -1
app.py CHANGED
@@ -23,7 +23,108 @@ sidebar_option = st.sidebar.radio("Select an option",
23
  "Drawing: Simple Path", "Drawing: Spectral Embedding", "Drawing: Traveling Salesman Problem",
24
  "Drawing: Weighted Graph", "3D Drawing: Animations of 3D Rotation", "3D Drawing: Basic Matplotlib",
25
  "Graph: DAG - Topological Layout", "Graph: Erdos Renyi", "Graph: Karate Club", "Graph: Minimum Spanning Tree",
26
- "Graph: Triads", "Algorithms: Cycle Detection", "Algorithms: Greedy Coloring"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  def plot_greedy_coloring(graph):
29
  # Apply greedy coloring
 
23
  "Drawing: Simple Path", "Drawing: Spectral Embedding", "Drawing: Traveling Salesman Problem",
24
  "Drawing: Weighted Graph", "3D Drawing: Animations of 3D Rotation", "3D Drawing: Basic Matplotlib",
25
  "Graph: DAG - Topological Layout", "Graph: Erdos Renyi", "Graph: Karate Club", "Graph: Minimum Spanning Tree",
26
+ "Graph: Triads", "Algorithms: Cycle Detection", "Algorithms: Greedy Coloring", "Algorithms: Find Shortest Path"])
27
+
28
+ def plot_shortest_path(graph, start_node, end_node):
29
+ # Find the shortest path from start_node to end_node
30
+ path = nx.shortest_path(graph, start_node, end_node, weight="weight")
31
+ st.write("Shortest Path:", path)
32
+
33
+ # Create a list of edges in the shortest path
34
+ path_edges = list(zip(path, path[1:]))
35
+
36
+ # Create a list of all edges, and assign colors based on whether they are in the shortest path or not
37
+ edge_colors = [
38
+ "red" if edge in path_edges or tuple(reversed(edge)) in path_edges else "black"
39
+ for edge in graph.edges()
40
+ ]
41
+
42
+ # Visualize the graph
43
+ pos = nx.spring_layout(graph)
44
+ nx.draw_networkx_nodes(graph, pos)
45
+ nx.draw_networkx_edges(graph, pos, edge_color=edge_colors)
46
+ nx.draw_networkx_labels(graph, pos)
47
+ nx.draw_networkx_edge_labels(
48
+ graph, pos, edge_labels={(u, v): d["weight"] for u, v, d in graph.edges(data=True)}
49
+ )
50
+
51
+ plt.title(f"Shortest Path from {start_node} to {end_node}")
52
+ st.pyplot(plt)
53
+
54
+ def algorithms_shortest_path():
55
+ st.title("Algorithms: Find Shortest Path")
56
+
57
+ # Option to choose between creating your own or using the default example
58
+ graph_mode = st.radio(
59
+ "Choose a Mode:",
60
+ ("Default Example", "Create Your Own"),
61
+ help="The default example shows a predefined graph, or you can create your own."
62
+ )
63
+
64
+ if graph_mode == "Default Example":
65
+ # Create a predefined graph with nodes and weighted edges
66
+ G = nx.Graph()
67
+ G.add_nodes_from(["A", "B", "C", "D", "E", "F", "G", "H"])
68
+ G.add_edge("A", "B", weight=4)
69
+ G.add_edge("A", "H", weight=8)
70
+ G.add_edge("B", "C", weight=8)
71
+ G.add_edge("B", "H", weight=11)
72
+ G.add_edge("C", "D", weight=7)
73
+ G.add_edge("C", "F", weight=4)
74
+ G.add_edge("C", "I", weight=2)
75
+ G.add_edge("D", "E", weight=9)
76
+ G.add_edge("D", "F", weight=14)
77
+ G.add_edge("E", "F", weight=10)
78
+ G.add_edge("F", "G", weight=2)
79
+ G.add_edge("G", "H", weight=1)
80
+ G.add_edge("G", "I", weight=6)
81
+ G.add_edge("H", "I", weight=7)
82
+
83
+ # Set default start and end nodes
84
+ start_node = "A"
85
+ end_node = "E"
86
+ st.write(f"Finding the shortest path from {start_node} to {end_node}")
87
+ plot_shortest_path(G, start_node, end_node)
88
+
89
+ elif graph_mode == "Create Your Own":
90
+ st.write("### Create Your Own Graph")
91
+
92
+ # Input for nodes
93
+ nodes_input = st.text_area("Enter nodes (e.g., A, B, C, D, E, F, G, H):")
94
+ edges_input = st.text_area("Enter edges with weights (e.g., (A, B, 4), (B, C, 8)):").strip()
95
+
96
+ # Input for start and end nodes
97
+ start_node = st.text_input("Enter start node (e.g., A):")
98
+ end_node = st.text_input("Enter end node (e.g., E):")
99
+
100
+ if st.button("Generate Graph"):
101
+ if nodes_input and edges_input and start_node and end_node:
102
+ try:
103
+ # Parse the input for nodes and edges
104
+ nodes = nodes_input.split(",")
105
+ edges = [
106
+ tuple(edge.strip()[1:-1].split(",") + [edge.strip().split(",")[-1]])
107
+ for edge in edges_input.split("),")
108
+ ]
109
+ edges = [
110
+ (edge[0], edge[1], int(edge[2])) for edge in edges if len(edge) == 3
111
+ ]
112
+
113
+ # Create the graph and add nodes and edges
114
+ G = nx.Graph()
115
+ G.add_nodes_from(nodes)
116
+ G.add_weighted_edges_from(edges)
117
+
118
+ st.write("Custom Graph:", G.edges())
119
+ plot_shortest_path(G, start_node, end_node)
120
+
121
+ except Exception as e:
122
+ st.error(f"Error creating the graph: {e}")
123
+ else:
124
+ st.error("Please enter valid nodes, edges, and start/end nodes.")
125
+
126
+ if sidebar_option == "Algorithms: Find Shortest Path":
127
+ algorithms_shortest_path()
128
 
129
  def plot_greedy_coloring(graph):
130
  # Apply greedy coloring