shukdevdatta123 commited on
Commit
b79f9b1
·
verified ·
1 Parent(s): 459eb5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -1
app.py CHANGED
@@ -17,7 +17,8 @@ sidebar_option = st.sidebar.radio("Select an option",
17
  "Drawing: House With Colors", "Drawing: Labels And Colors",
18
  "Drawing: Multipartite Layout", "Drawing: Node Colormap",
19
  "Drawing: Rainbow Coloring", "Drawing: Random Geometric Graph","Drawing: Self-loops",
20
- "Drawing: Simple Path", "Drawing: Spectral Embedding", "Drawing: Traveling Salesman Problem"])
 
21
 
22
  # Helper function to draw and display graph
23
  def draw_graph(G, pos=None, title="Graph Visualization"):
@@ -25,6 +26,88 @@ def draw_graph(G, pos=None, title="Graph Visualization"):
25
  nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
26
  st.pyplot(plt)
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  from networkx.algorithms.approximation import christofides
29
 
30
  # Function to display Traveling Salesman Problem
 
17
  "Drawing: House With Colors", "Drawing: Labels And Colors",
18
  "Drawing: Multipartite Layout", "Drawing: Node Colormap",
19
  "Drawing: Rainbow Coloring", "Drawing: Random Geometric Graph","Drawing: Self-loops",
20
+ "Drawing: Simple Path", "Drawing: Spectral Embedding", "Drawing: Traveling Salesman Problem",
21
+ "Drawing: Weighted Graph"])
22
 
23
  # Helper function to draw and display graph
24
  def draw_graph(G, pos=None, title="Graph Visualization"):
 
26
  nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
27
  st.pyplot(plt)
28
 
29
+ # Function to display Weighted Graph
30
+ def display_weighted_graph():
31
+ st.title("Drawing: Weighted Graph")
32
+
33
+ option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
34
+
35
+ if option == "Default Example":
36
+ # Default weighted graph example
37
+ G = nx.Graph()
38
+
39
+ G.add_edge("a", "b", weight=0.6)
40
+ G.add_edge("a", "c", weight=0.2)
41
+ G.add_edge("c", "d", weight=0.1)
42
+ G.add_edge("c", "e", weight=0.7)
43
+ G.add_edge("c", "f", weight=0.9)
44
+ G.add_edge("a", "d", weight=0.3)
45
+
46
+ elarge = [(u, v) for (u, v, d) in G.edges(data=True) if d["weight"] > 0.5]
47
+ esmall = [(u, v) for (u, v, d) in G.edges(data=True) if d["weight"] <= 0.5]
48
+
49
+ pos = nx.spring_layout(G, seed=7) # positions for all nodes - seed for reproducibility
50
+
51
+ # nodes
52
+ nx.draw_networkx_nodes(G, pos, node_size=700)
53
+
54
+ # edges
55
+ nx.draw_networkx_edges(G, pos, edgelist=elarge, width=6)
56
+ nx.draw_networkx_edges(
57
+ G, pos, edgelist=esmall, width=6, alpha=0.5, edge_color="b", style="dashed"
58
+ )
59
+
60
+ # node labels
61
+ nx.draw_networkx_labels(G, pos, font_size=20, font_family="sans-serif")
62
+ # edge weight labels
63
+ edge_labels = nx.get_edge_attributes(G, "weight")
64
+ nx.draw_networkx_edge_labels(G, pos, edge_labels)
65
+
66
+ ax = plt.gca()
67
+ ax.margins(0.08)
68
+ plt.axis("off")
69
+ plt.tight_layout()
70
+ st.pyplot(plt)
71
+
72
+ elif option == "Create your own":
73
+ # User can create their own graph
74
+ nodes = st.text_input("Enter nodes (comma-separated):", "a,b,c,d,e,f").split(',')
75
+ nodes = [node.strip() for node in nodes]
76
+
77
+ G_custom = nx.Graph()
78
+
79
+ for i in range(len(nodes)):
80
+ for j in range(i + 1, len(nodes)):
81
+ weight = st.number_input(f"Weight between {nodes[i]} and {nodes[j]}:", min_value=0.0, value=1.0)
82
+ G_custom.add_edge(nodes[i], nodes[j], weight=weight)
83
+
84
+ # Create layout for visualization
85
+ pos = nx.spring_layout(G_custom, seed=7)
86
+
87
+ # Determine edges based on weight
88
+ elarge = [(u, v) for (u, v, d) in G_custom.edges(data=True) if d["weight"] > 0.5]
89
+ esmall = [(u, v) for (u, v, d) in G_custom.edges(data=True) if d["weight"] <= 0.5]
90
+
91
+ # Draw the graph
92
+ nx.draw_networkx_nodes(G_custom, pos, node_size=700)
93
+ nx.draw_networkx_edges(G_custom, pos, edgelist=elarge, width=6)
94
+ nx.draw_networkx_edges(
95
+ G_custom, pos, edgelist=esmall, width=6, alpha=0.5, edge_color="b", style="dashed"
96
+ )
97
+ nx.draw_networkx_labels(G_custom, pos, font_size=20, font_family="sans-serif")
98
+ edge_labels = nx.get_edge_attributes(G_custom, "weight")
99
+ nx.draw_networkx_edge_labels(G_custom, pos, edge_labels)
100
+
101
+ ax = plt.gca()
102
+ ax.margins(0.08)
103
+ plt.axis("off")
104
+ plt.tight_layout()
105
+ st.pyplot(plt)
106
+
107
+ # Display Drawing: Weighted Graph if selected
108
+ if sidebar_option == "Drawing: Weighted Graph":
109
+ display_weighted_graph()
110
+
111
  from networkx.algorithms.approximation import christofides
112
 
113
  # Function to display Traveling Salesman Problem