shukdevdatta123 commited on
Commit
652c01a
·
verified ·
1 Parent(s): b67c5a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -1
app.py CHANGED
@@ -23,7 +23,80 @@ 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"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  # Helper function to draw and display graph
29
  def draw_graph(G, pos=None, title="Graph Visualization"):
 
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
30
+ graph_coloring = nx.greedy_color(graph)
31
+ unique_colors = set(graph_coloring.values())
32
+
33
+ # Assign colors to nodes based on the greedy coloring
34
+ graph_color_to_mpl_color = dict(zip(unique_colors, mpl.TABLEAU_COLORS))
35
+ node_colors = [graph_color_to_mpl_color[graph_coloring[n]] for n in graph.nodes()]
36
+
37
+ # Layout of the graph
38
+ pos = nx.spring_layout(graph, seed=14)
39
+
40
+ # Draw the graph
41
+ nx.draw(
42
+ graph,
43
+ pos,
44
+ with_labels=True,
45
+ node_size=500,
46
+ node_color=node_colors,
47
+ edge_color="grey",
48
+ font_size=12,
49
+ font_color="#333333",
50
+ width=2,
51
+ )
52
+
53
+ plt.title("Greedy Coloring of Graph")
54
+ st.pyplot(plt)
55
+
56
+ def algorithms_greedy_coloring():
57
+ st.title("Algorithms: Greedy Coloring")
58
+
59
+ # Option to choose between creating your own or using the default example
60
+ graph_mode = st.radio(
61
+ "Choose a Mode:",
62
+ ("Default Example", "Create Your Own"),
63
+ help="The default example shows a predefined graph, or you can create your own."
64
+ )
65
+
66
+ if graph_mode == "Default Example":
67
+ # Create a predefined graph (Dodecahedral graph) for the greedy coloring example
68
+ G = nx.dodecahedral_graph()
69
+ st.write("Default Graph: Dodecahedral Graph with Greedy Coloring.")
70
+ plot_greedy_coloring(G)
71
+
72
+ elif graph_mode == "Create Your Own":
73
+ st.write("### Create Your Own Graph")
74
+
75
+ # Input for creating a custom graph
76
+ nodes_input = st.text_area("Enter nodes (e.g., 1, 2, 3, 4):")
77
+ edges_input = st.text_area("Enter edges (e.g., (1, 2), (2, 3), (3, 4)):").strip()
78
+
79
+ if st.button("Generate Graph"):
80
+ if nodes_input and edges_input:
81
+ try:
82
+ # Parse the input for nodes and edges
83
+ nodes = list(map(int, nodes_input.split(",")))
84
+ edges = [tuple(map(int, edge.strip()[1:-1].split(","))) for edge in edges_input.split("),")]
85
+
86
+ G = nx.Graph()
87
+ G.add_nodes_from(nodes)
88
+ G.add_edges_from(edges)
89
+
90
+ st.write("Custom Graph:", G.edges())
91
+ plot_greedy_coloring(G)
92
+
93
+ except Exception as e:
94
+ st.error(f"Error creating the graph: {e}")
95
+ else:
96
+ st.error("Please enter valid nodes and edges.")
97
+
98
+ if sidebar_option == "Algorithms: Greedy Coloring":
99
+ algorithms_greedy_coloring()
100
 
101
  # Helper function to draw and display graph
102
  def draw_graph(G, pos=None, title="Graph Visualization"):