forked from weka511/bioinformatics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BA7A.py
52 lines (39 loc) · 1.61 KB
/
BA7A.py
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
#!/usr/bin/env python
# Copyright (C) 2017-2021 Greenweaves Software Limited
# This is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
# BA7A Compute Distances Between Leaves
from helpers import create_weighted_adjacency_list
# ComputeDistancesBetweenLeaves
# Inputs: n an integer n
# T the adjacency list of a weighted tree with n leaves.
#
# Returns: An n by n symmetric matrix of distannces between leaves
def ComputeDistancesBetweenLeaves(n,T):
# D
# Recursively compute the distances between two nodes
def D(i,j,path=[]):
if i==j: return 0
d = float('inf')
for node,weight in T[i]:
if node==j:
return weight
if node in path:
continue
test = weight + D(node,j,path+[node])
if test<d:
d=test
return d
return [[D(i,j)for j in range(n)] for i in range(n) ]
if __name__=='__main__':
n,T = create_weighted_adjacency_list()
for ds in ComputeDistancesBetweenLeaves(n,T):
print(' '.join(str(d) for d in ds))