Spaces:
Sleeping
app.py
Browse filesimport subprocess
import sys
# Manually install required libraries (each installed separately)
subprocess.check_call([sys.executable, "-m", "pip", "install", "gradio"])
subprocess.check_call([sys.executable, "-m", "pip", "install", "networkx"])
subprocess.check_call([sys.executable, "-m", "pip", "install", "matplotlib"])
subprocess.check_call([sys.executable, "-m", "pip", "install", "numpy"])
subprocess.check_call([sys.executable, "-m", "pip", "install", "scipy"])
import os
import math
import itertools
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import gradio as gr
from scipy.spatial import distance
# -------------------------------
# Step-by-Step Index Calculation Functions
# -------------------------------
# Formulas for the indices (written in a typical mathematical style)
formulas = {
"Wiener Index (W)": "W = Σ d(u,v) for all pairs (u,v)",
"First Zagreb Index (M₁)": "M₁ = Σ [d(v)]² for all vertices v",
"Second Zagreb Index (M₂)": "M₂ = Σ [d(u) * d(v)] for all adjacent vertices (u,v)",
"Randić Index (R)": "R = Σ [1 / √(d(u) * d(v))] for all edges (u,v)",
"Balaban Index (J)": "J = (m / (q + 1)) Σ [1 / √(d(u) * d(v))]",
"Harary Index (H)": "H = Σ [1 / d(u,v)] for all pairs (u,v)",
"Atom-Bond Connectivity Index (ABC)": "ABC = Σ [(√d(u) + √d(v))/(d(u) + d(v))] for all edges (u,v)",
"Geometric Arithmetic Index (GA)": "GA = Σ [√(d(u) * d(v))] for all edges (u,v)",
"Harmonic Index (Harm)": "Harm = Σ [1 / d(v)] for all vertices v"
}
def solve_index(index, user_input):
try:
if index == "Wiener Index (W)":
distances = list(map(int, user_input.split(',')))
result = sum(distances)
steps = [
"1. **Extract the shortest path distances:**\n - Distances = " + f"{distances}",
"2. **Sum the distances:**\n - W = " + " + ".join(map(str, distances)) + f" = {result}"
]
output = "### **Wiener Index (W)**\n\n"
output += "**Formula:**\n" + formulas["Wiener Index (W)"] + "\n"
output += "Where d(u,v) is the shortest path distance between vertices u and v.\n\n"
output += "**Step-by-step solution:**\n\n" + "\n".join(steps) + "\n\n"
output += f"**Answer:** The Wiener Index W = {result}."
return output
elif index == "First Zagreb Index (M₁)":
degrees = list(map(int, user_input.split(',')))
squared_degrees = [d**2 for d in degrees]
result = sum(squared_degrees)
steps = [
"1. **Retrieve the vertex degrees:**\n - Degrees = " + f"{degrees}",
"2. **Square each degree:**\n - Squared degrees = " + f"{squared_degrees}",
"3. **Sum the squared degrees:**\n - M₁ = " + " + ".join(map(str, squared_degrees)) + f" = {result}"
]
output = "### **First Zagreb Index (M₁)**\n\n"
output += "**Formula:**\n" + formulas["First Zagreb Index (M₁)"] + "\n\n"
output += "**Step-by-step solution:**\n\n" + "\n".join(steps) + "\n\n"
output += f"**Answer:** The First Zagreb Index M₁ = {result}."
return output
elif index == "Second Zagreb Index (M₂)":
adj_degrees = [tuple(map(int, pair.split('-'))) for pair in user_input.split(',')]
products = [d1 * d2 for d1, d2 in adj_degrees]
result = sum(products)
steps = [
"1. **Extract the degree pairs for adjacent vertices:**\n - Pairs = " + f"{adj_degrees}",
"2. **Multiply each pair:**\n - Products = " + f"{products}",
"3. **Sum the products:**\n - M₂ = " + " + ".join(map(str, products)) + f" = {result}"
]
output = "### **Second Zagreb Index (M₂)**\n\n"
output += "**Formula:**\n" + formulas["Second Zagreb Index (M₂)"] + "\n"
output += "Where d(u) and d(v) are the degrees of adjacent vertices u and v.\n\n"
output += "**Step-by-step solution:**\n\n" + "\n".join(steps) + "\n\n"
output += f"**Answer:** The Second Zagreb Index M₂ = {result}."
return output
elif index == "Randić Index (R)":
edge_degrees = [tuple(map(int, pair.split('-'))) for pair in user_input.split(',')]
values = [1 / (d1 * d2)**0.5 for d1, d2 in edge_degrees]
result = sum(values)
steps = [
"1. **Extract the degree pairs for each edge:**\n - Edge pairs = " + f"{edge_degrees}"
]
for (d1, d2), v in zip(edge_degrees, values):
steps.append(f"2. **For edge {d1}-{d2}:**\n - 1/√({d1}×{d2}) = {v:.4f}")
steps.append("3. **Sum the values:**\n - R = " + " + ".join(f"{v:.4f}" for v in values) + f" = {result:.4f}")
output = "### **Randić Index (R)**\n\n"
output += "**Formula:**\n" + formulas["Randić Index (R)"] + "\n"
output += "Where the sum is taken over all edges (u,v) and d(u) and d(v) are their degrees.\n\n"
output += "**Step-by-step solution:**\n\n" + "\n".join(steps) + "\n\n"
output += f"**Answer:** The Randić Index R = {result:.4f}."
return output
elif index == "Balaban Index (J)":
parts = user_input.split(';')
if len(parts) < 3:
return "Invalid input format. Use m;q;edge pairs like 6;4;2-3,3-4."
m = int(parts[0])
q = int(parts[1])
edge_degrees = [tuple(map(int, pair.split('-'))) for pair in parts[2].split(',')]
values = [1 / (d1 * d2)**0.5 for d1, d2 in edge_degrees]
numerator = sum(values)
result = (m / (q + 1)) * numerator
steps = [
"1. **Extract parameters and edge pairs:**",
f" - m = {m}, q = {q}",
f" - Edge pairs = {edge_degrees}"
]
for (d1, d2), v in zip(edge_degrees, values):
steps.append(f"2. **For edge {d1}-{d2}:**\n - 1/√({d1}×{d2}) = {v:.4f}")
steps.append("3. **Sum the computed values:**\n - Sum = " + f"{numerator:.4f}")
steps.append("4. **Multiply by m/(q+1):**\n - J = ({m}/({q}+1)) × {numerator:.4f} = {result:.4f}")
output = "### **Balaban Index (J)**\n\n"
output += "**Formula:**\n" + formulas["Balaban Index (J)"] + "\n"
output += "Where m and q are given parameters and the sum is taken over all edges.\n\n"
output += "**Step-by-step solution:**\n\n" + "\n".join(steps) + "\n\n"
output += f"**Answer:** The Balaban Index J = {result:.4f}."
return output
elif index == "Harary Index (H)":
distances = list(map(int, user_input.split(',')))
values = [1 / d for d in distances if d > 0]
result = sum(values)
steps = [
"1. **Extract the shortest path distances:**\n - Distances = " + f"{distances}"
]
for d, v in zip(distances, values):
steps.append(f"2. **For distance {d}:**\n - 1/{d} = {v:.4f}")
steps.append("3. **Sum the reciprocals:**\n - H = " + " + ".join(f"{v:.4f}" for v in values) + f" = {result:.4f}")
output = "### **Harary Index (H)**\n\n"
output += "**Formula:**\n" + formulas["Harary Index (H)"] + "\n"
output += "Where d(u,v) is the shortest path distance between vertices u and v.\n\n"
output += "**Step-by-step solution:**\n\n" + "\n".join(steps) + "\n\n"
output += f"**Answer:** The Harary Index H = {result:.4f}."
return output
elif index == "Atom-Bond Connectivity Index (ABC)":
edge_degrees = [tuple(map(int, pair.split('-'))) for pair in user_input.split(',')]
values = [((d1**0.5 + d2**0.5) / (d1 + d2)) for d1, d2 in edge_degrees]
result = sum(values)
steps = [
"1. **Extract the edge pairs:**\n - Pairs = " + f"{edge_degrees}"
]
for (d1, d2), v in zip(edge_degrees, values):
steps.append(f"2. **For edge {d1}-{d2}:**\n - (√{d1} + √{d2})/( {d1} + {d2} ) = {v:.4f}")
steps.append("3. **Sum the values:**\n - ABC = " + " + ".join(f"{v:.4f}" for v in values) + f" = {result:.4f}")
output = "### **Atom-Bond Connectivity Index (ABC)**\n\n"
output += "**Formula:**\n" + formulas["Atom-Bond Connectivity Index (ABC)"] + "\n\n"
output += "**Step-by-step solution:**\n\n" + "\n".join(steps) + "\n\n"
output += f"**Answer:** The ABC Index = {result:.4f}."
return output
elif index == "Geometric Arithmetic Index (GA)":
edge_degrees = [tuple(map(int, pair.split('-'))) for pair in user_input.split(',')]
values = [(d1 * d2)**0.5 for d1, d2 in edge_degrees]
result = sum(values)
steps = [
"1. **Extract the edge pairs:**\n - Pairs = " + f"{edge_degrees}"
]
for (d1, d2), v in zip(edge_degrees, values):
steps.append(f"2. **For edge {d1}-{d2}:**\n - √({d1}×{d2}) = {v:.4f}")
steps.append("3. **Sum the values:**\n - GA = " + " + ".join(f"{v:.4f}" for v in values) + f" = {result:.4f}")
output = "### **Geometric Arithmetic Index (GA)**\n\n"
output += "**Formula:**\n" + formulas["Geometric Arithmetic Index (GA)"] + "\n\n"
output += "**Step-by-step solution:**\n\n" + "\n".join(steps) + "\n\n"
output += f"**Answer:** The GA Index = {result:.4f}."
return output
elif index == "Harmonic Index (Harm)":
degrees = list(map(int, user_input.split(',')))
values = [1 / d for d in degrees]
result = sum(values)
steps = [
"1. **Extract the vertex degrees:**\n - Degrees = " + f"{degrees}"
]
for d, v in zip(degrees, values):
steps.append(f"2. **For degree {d}:**\n
|
File without changes
|