Spaces:
Sleeping
Sleeping
Anshuman1970
commited on
Commit
•
43d1eac
1
Parent(s):
2788b5c
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import ast
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
def myfunc(file_path):
|
5 |
+
with open(file_path, "r") as file:
|
6 |
+
file_contents = file.read()
|
7 |
+
|
8 |
+
string = file_contents
|
9 |
+
matrix = ast.literal_eval(string)
|
10 |
+
def sort_vertices_by_degree(matrix):
|
11 |
+
num_vertices = len(matrix)
|
12 |
+
degrees = [(i, sum(matrix[i])) for i in range(num_vertices)]
|
13 |
+
sorted_vertices = sorted(degrees, key=lambda x: x[1], reverse=True)
|
14 |
+
return [vertex[0] for vertex in sorted_vertices]
|
15 |
+
sorted_vertices = sort_vertices_by_degree(matrix)
|
16 |
+
|
17 |
+
|
18 |
+
def dfs(u, visited, disc, low, parent, is_cut_vertex):
|
19 |
+
nonlocal time
|
20 |
+
children = 0
|
21 |
+
visited[u] = True
|
22 |
+
disc[u] = time
|
23 |
+
low[u] = time
|
24 |
+
time += 1
|
25 |
+
|
26 |
+
for v in range(len(matrix)):
|
27 |
+
if matrix[u][v]:
|
28 |
+
if not visited[v]:
|
29 |
+
children += 1
|
30 |
+
dfs(v, visited, disc, low, u, is_cut_vertex)
|
31 |
+
low[u] = min(low[u], low[v])
|
32 |
+
if parent == -1 and children > 1:
|
33 |
+
is_cut_vertex[u] = True
|
34 |
+
if parent != -1 and low[v] >= disc[u]:
|
35 |
+
is_cut_vertex[u] = True
|
36 |
+
elif v != parent:
|
37 |
+
low[u] = min(low[u], disc[v])
|
38 |
+
|
39 |
+
num_vertices = len(matrix)
|
40 |
+
visited = [False] * num_vertices
|
41 |
+
disc = [float("inf")] * num_vertices
|
42 |
+
low = [float("inf")] * num_vertices
|
43 |
+
is_cut_vertex = [False] * num_vertices
|
44 |
+
time = 0
|
45 |
+
|
46 |
+
for i in range(num_vertices):
|
47 |
+
if not visited[i]:
|
48 |
+
dfs(i, visited, disc, low, -1, is_cut_vertex)
|
49 |
+
|
50 |
+
cut_vertices = [i for i, is_cut in enumerate(is_cut_vertex) if is_cut]
|
51 |
+
result=[]
|
52 |
+
for x in sorted_vertices:
|
53 |
+
for y in cut_vertices:
|
54 |
+
if x==y:
|
55 |
+
result.append(x)
|
56 |
+
|
57 |
+
return result
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
+
iface = gr.Interface(
|
62 |
+
fn=myfunc,
|
63 |
+
inputs=gr.File(label="Upload Text File Containing the adjacency matrix of the graph"),
|
64 |
+
outputs=gr.Text(label="The Important stations are in the order:")
|
65 |
+
)
|
66 |
+
iface.launch(share=True)
|