def hdag(graph): def clone(adj): copy = {} for k,v in adj.items(): copy[k]=v return copy _,_,adj = create_adjacency(graph,back=False,self=False) ordered = topological_order(clone(adj)) # use clone because topological_order destroys its parameter for a,b in zip(ordered[:-1],ordered[1:]): if not b in adj[a]: return (-1,[]) return (1,ordered)
def sdag(m, adjacency, weights): t = topological_order(adjacency.copy()) D = [None] * (m + 1) D[1] = 0 for i in t: if D[i] == None: continue for j in adjacency[i]: if D[j] == None: D[j] = D[i] + weights[(i, j)] else: trial = D[i] + weights[(i, j)] if trial < D[j]: D[j] = trial return D[1:]
# # 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/> # # TS Topological sort from align import topological_order from helpers import parse_graph, create_adjacency, format_list if __name__ == '__main__': with open(r'C:\Users\Simon\Downloads\rosalind_ts.txt') as f: g = parse_graph(f) _, _, adj = create_adjacency(g, back=False) for k, v in adj.items(): if k in v: v.remove(k) t = topological_order(adj) print(format_list(t))