-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithm_D.py
More file actions
224 lines (154 loc) · 5.99 KB
/
Copy pathAlgorithm_D.py
File metadata and controls
224 lines (154 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# -*- coding: utf-8 -*-
"""Algo D FINAL
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1qwkUR1nHU3V0YPLJGtyJ2JTKvWoJytyF
"""
import networkx as nx
import numpy as np
import math
from collections import Counter
global dist
dist = [[0 for x in range(300)] for y in range(300)]
global vis
vis = [300]
global dis
dis = [300]
global distsum
distsum = [300]
global distcount
distcount = [300]
global C
C={}
G = nx.Graph()
def create_TT():
edges=[]
node_count = int(input("Enter the number of vertices in the Transposition tree "))
for i in range(1,(node_count+1)): #add the vertices
G.add_node(i)
print("Enter the edges ")
for i in range(1,node_count): #add the edges
t = tuple(map(int,input().split()))
G.add_edge(*t)
edges.append(list(t))
adjacency_matrix = nx.to_numpy_array(G)
bfs(adjacency_matrix, node_count)
# function to create a Transposition tree
def checkStar(adjacency_matrix, node_count, count):
vertexD1 = 0
vertexDn_1 = 0
for i in range(0, node_count):
degreeI = 0
for j in range(0, node_count):
if (adjacency_matrix[i][j]==1):
degreeI = degreeI + 1
if (degreeI == 1):
vertexD1 = vertexD1 + 1
elif (degreeI == count - 1):
vertexDn_1 = vertexDn_1 + 1
return (vertexD1 == (count - 1) and
vertexDn_1 == 1)
def bfs(adjacency_matrix, node_count):
dist = [[0 for x in range(300)] for y in range(300)]
tno = node_count
for x in range(1,tno+1): #bfs is done from every vertex in order to obtain the shortest path from every vertex to every other vertex in the tree.
dis = [0 for y in range(300)]
vis = [0 for y in range(300)]
queue = []
queue.append(x)
vis[x] = 1
dis[x] = 0
while (not (len(queue)==0)):
tmp = queue[0]
queue.pop(0)
for y in range(1,tno+1):
if ((adjacency_matrix[tmp-1][y-1] or adjacency_matrix[y-1][tmp-1]) and not (vis[y])):
vis[y] = 1
dis[y] = dis[tmp] + 1
queue.append(y)
elif ((adjacency_matrix[tmp-1][y-1] or adjacency_matrix[y-1][tmp-1]) and vis[y] and dis[y] > dis[tmp] + 1):
dis[y] = dis[tmp] + 1
for y in range(1,tno+1):
if (vis[y] and (dist[x][y] == 0 or dist[x][y] > dis[y])):
dist[x][y] = dis[y]
distsum = [0 for x in range(300)]
for x in range(1,tno+1): #finding the distance-sum of each vertex.
for y in range(1, tno+1):
distsum[x] = distsum[x] + dist[x][y]
algoD(adjacency_matrix, node_count,distsum,dist)
global s_nodelistD
def algoS(adjacency_matrix, node_count,distsum,dist,nodecount_temp):
s_nodelistD=[]
max1 = max(distsum) #finding the node having maximum distance sum
sourceNode = distsum.index(max1)
Ecc = 0
for x in range(1,nodecount_temp+1): #finding the maximum eccentricity
Ecc = max(Ecc, dist[sourceNode][x])
#print("Ecc : ",Ecc)
for x in range(1,nodecount_temp+1):
for y in range(1,nodecount_temp+1):
if (dist[x][y] == Ecc) :
s_nodelistD.append(x)
return (list(set(s_nodelistD)))
def algoD(adjacency_matrix, node_count,distsum,dist):
gamma=0
count=0
nodecount_temp=node_count
if (checkStar(adjacency_matrix, nodecount_temp,node_count)): #checks if the given tree is a star graph.
gamma = gamma + 3 * (node_count - 1) // 2
print("\nIs a star graph.")
else:
no=node_count
while (no > 0):
S=algoS(adjacency_matrix, node_count,distsum,dist,nodecount_temp)
max1 = max(distsum)
sourceNode = distsum.index(max1)
diam = 0
for x in range(1,nodecount_temp+1): #finding the diameter
diam = max(diam, dist[sourceNode][x])
if(len(S)%2==0):
gamma=math.floor(gamma+(len(S)*(diam-(1/2))))
tno=nodecount_temp
for y in S :
distsum[y]=0
for x in range(1,tno+1): #making all its entries invalid in the distance matrix, updating the distsum and degree_count
if (not (x == y) and not (distsum[x] == 0)):
if (x < y):
distsum[x] = distsum[x] - dist[x][y]
else:
distsum[x] = distsum[x] - dist[x][y]
for x in range(1,tno+1):
dist[y][x] = 0
dist[x][y] = 0
adjacency_matrix[y-1][x-1] = 0
adjacency_matrix[x-1][y-1] = 0
no = no - len(S)
node_count=node_count-len(S)
C.clear()
S.clear()
else :
gamma=math.floor(gamma+(len(S)*((diam-(1/2))-(1/2))))
tno=nodecount_temp
for y in S :
distsum[y]=0
for x in range(1,tno+1): #making all its entries invalid in the distance matrix, updating the distsum and degree_count
if (not (x == y) and not (distsum[x] == 0)):
if (x < y):
distsum[x] = distsum[x] - dist[x][y]
else:
distsum[x] = distsum[x] - dist[x][y]
for x in range(1,tno+1):
dist[y][x] = 0
dist[x][y] = 0
adjacency_matrix[y-1][x-1] = 0
adjacency_matrix[x-1][y-1] = 0
no = no - len(S)
node_count=node_count-len(S)
C.clear()
S.clear()
if (checkStar(adjacency_matrix, nodecount_temp,node_count)): #checks if the given tree is a star graph.
gamma = gamma + 3 * (node_count - 1) // 2
break
print("\nUPPERBOUND :",gamma)
return
create_TT()