def valid_schedules(tasks, deps): """ Returns a list of valid orderings based on dependencies and time constraints Can assume that for every task, deadline - available >= duration """ # just to print out some stats valids = 0 failed = 0 # each schedule is a list of task_ids schedules = topsort(tasks, deps) result = [] for schedule in schedules: time_now = 0 # TODO : empty list? for task_id in schedule: available, duration, deadline = tasks[task_id] # unable to get to this task with this schedule so this sched is invalid if deadline < time_now + duration: failed += 1 break time_now = available + duration else: valids += 1 print "valid: ", schedule result.append(schedule) print "time constraints => failed: " + str(failed), "success: " + str(valids) return result
def assert_topsort(dag): visited = set() for t in topsort(dag): for u, v in dag.get_edges(): if t == v: assert u in visited visited.add(t)
def make(thetarget, MAX_PARALLEL_JOBS, dir): """ Make Loop """ if thetarget not in depend: depend[thetarget] = [None] lockdir = dir + "/LOCKS" os.chdir(dir) # Produce "parent" list suitable for topsort: depend_flat = [(k, lst) for k, v in depend.iteritems() for lst in v] # assoc. array --> list transformation. parent_pairs = [(dep[1], dep[0]) for dep in depend_flat] kids = {} for pair in parent_pairs: parent, child = pair if parent not in kids: kids[parent] = [] kids[parent].append(child) sorted_nodes = topsort.topsort(parent_pairs) sorted_related = test_rules(rules, sorted_nodes, depend, thetarget, echo = 0) targets=strip_input_files(sorted_related, depend) print "Final target list: ", targets changed_files = set() tobeupdated, preserved = need_update(targets, changed_files, kids, echo = 1) print bcolors.OKBLUE + "Targets that will be preserved: " + str(preserved) + bcolors.ENDC print bcolors.HEADER + "Targets that will be updated: " + str(tobeupdated) + bcolors.ENDC print bcolors.HEADER + "Rules will be executed in the following order:" + bcolors.ENDC for target in tobeupdated: if target in rules: if rules[target] in queues: queue = queues[rules[target]] else: queue = default_que print rules[target].__name__ + "(" + target + ", " + str(depend[target]) + ")" + "[" + queue + "]" else: # This is not likely to happen - a similar check is done earlier. print bcolors.FAIL + "ERROR: Rule for --->" + str(target) + "<--- not found. Exit!" + bcolors.ENDC sys.exit() answer = raw_input("Proceed? Say \"y\" or \"Y\" if yes >>> ") if answer != "y" and answer != "Y": print "Aborted by user" sys.exit() # Create lockdir, exit if a regular file with the same name exists. try: os.mkdir(lockdir) except OSError as (errnum, errmsg): if not (errnum == errno.EEXIST and os.path.isdir(lockdir)): print "Remove file", lockdir, "from the way." raise
def ns3_module_scan(top_builddir, pygen_file_name, everything_h): ns3_modules = eval(sys.stdin.read()) ## do a topological sort on the modules graph from topsort import topsort graph = [] for ns3_module_name, (ns3_module_deps, dummy) in ns3_modules.iteritems(): for dep in ns3_module_deps: graph.append((dep, ns3_module_name)) sorted_ns3_modules = topsort(graph) #print >> sys.stderr, "******* topological sort: ", sorted_ns3_modules sections = [PygenSection('__main__', FileCodeSink(open(pygen_file_name, "wt")))] headers_map = {} # header_name -> section_name for ns3_module in sorted_ns3_modules: section_name = "ns3_module_%s" % ns3_module.replace('-', '_') file_name = os.path.join(os.path.dirname(pygen_file_name), "%s.py" % section_name) sections.append(PygenSection(section_name, FileCodeSink(open(file_name, "wt")), section_name + "__local")) for header in ns3_modules[ns3_module][1]: headers_map[header] = section_name module_parser = ModuleParser('ns3', 'ns3') module_parser.add_pre_scan_hook(pre_scan_hook) #module_parser.add_post_scan_hook(post_scan_hook) gccxml_options = dict( include_paths=[top_builddir], define_symbols={ #'NS3_ASSERT_ENABLE': None, #'NS3_LOG_ENABLE': None, } ) module_parser.parse_init([everything_h], None, whitelist_paths=[top_builddir, os.path.dirname(everything_h)], #includes=['"ns3/everything.h"'], pygen_sink=sections, pygen_classifier=MyPygenClassifier(headers_map), gccxml_options=gccxml_options) module_parser.scan_types() callback_classes_file = open(os.path.join(os.path.dirname(pygen_file_name), "callbacks_list.py"), "wt") scan_callback_classes(module_parser, callback_classes_file) callback_classes_file.close() module_parser.scan_methods() module_parser.scan_functions() module_parser.parse_finalize() for section in sections: section.code_sink.file.close()
def sssp_dag(G: Graph, s: str) -> Dict[str, List[str]]: f = topsort(G) SD = {v: (0 if v == s else inf) for v in f} # Shortest Distances paths = {v: [] for v in f} for v in f: curpath = [] for u in G.get_in_edges(v): tmp = SD[u] + G.get_weight(u, v) if tmp < SD[v]: SD[v] = tmp curpath = paths[u] paths[v].extend(curpath) paths[v].append(v) return paths
def kosaraju(G: Graph) -> List[List[str]]: Grev = reverse_graph(G) f = topsort(Grev) visited = set() scc = [] def DFS(u): visited.add(u) scc[-1].append(u) for v in G[u]: if v not in visited: DFS(v) for v in f: if v not in visited: scc.append([]) DFS(v) return scc
def derive_alien_language(sorted_words: List[str]) -> List[str]: """ Given a list of words, derive the order of the alphabet that would classify the list of words as "lexicographically" sorted. """ vertices = set() for word in sorted_words: vertices |= set(list(word)) edges = [] for w1, w2 in zip(sorted_words, sorted_words[1:]): for l1, l2 in zip(w1, w2): if l1 != l2: edges.append((l1, l2)) break G = create_graph(list(vertices), list(edges)) return topsort(G)
def ns3_module_scan(top_builddir, pygen_file_name, everything_h, cflags): ns3_modules = eval(sys.stdin.read()) ## do a topological sort on the modules graph from topsort import topsort graph = [] module_names = ns3_modules.keys() module_names.sort() for ns3_module_name in module_names: ns3_module_deps = list(ns3_modules[ns3_module_name][0]) ns3_module_deps.sort() for dep in ns3_module_deps: graph.append((dep, ns3_module_name)) sorted_ns3_modules = topsort(graph) #print >> sys.stderr, "******* topological sort: ", sorted_ns3_modules sections = [ PygenSection('__main__', FileCodeSink(open(pygen_file_name, "wt"))) ] headers_map = {} # header_name -> section_name section_precendences = {} # section_name -> precedence for prec, ns3_module in enumerate(sorted_ns3_modules): section_name = "ns3_module_%s" % ns3_module.replace('-', '_') file_name = os.path.join(os.path.dirname(pygen_file_name), "%s.py" % section_name) sections.append( PygenSection(section_name, FileCodeSink(open(file_name, "wt")), section_name + "__local")) for header in ns3_modules[ns3_module][1]: headers_map[header] = section_name section_precendences[section_name] = prec module_parser = ModuleParser('ns3', 'ns3') module_parser.add_pre_scan_hook(pre_scan_hook) #module_parser.add_post_scan_hook(post_scan_hook) gccxml_options = dict( include_paths=[top_builddir], define_symbols={ #'NS3_ASSERT_ENABLE': None, #'NS3_LOG_ENABLE': None, }, cflags=('--gccxml-cxxflags %r' % (cflags, ))) module_parser.parse_init( [everything_h], None, whitelist_paths=[top_builddir, os.path.dirname(everything_h)], #includes=['"ns3/everything.h"'], pygen_sink=sections, pygen_classifier=MyPygenClassifier(headers_map, section_precendences), gccxml_options=gccxml_options) module_parser.scan_types() callback_classes_file = open( os.path.join(os.path.dirname(pygen_file_name), "callbacks_list.py"), "wt") scan_callback_classes(module_parser, callback_classes_file) callback_classes_file.close() module_parser.scan_methods() module_parser.scan_functions() module_parser.parse_finalize() for section in sections: section.code_sink.file.close()
def ns3_module_scan(top_builddir, pygen_file_name, everything_h, cflags): ns3_modules = eval(sys.stdin.readline()) from topsort import topsort graph = [] module_names = ns3_modules.keys() module_names.sort() for ns3_module_name in module_names: ns3_module_deps = list(ns3_modules[ns3_module_name][0]) ns3_module_deps.sort() for dep in ns3_module_deps: graph.append((dep, ns3_module_name)) sorted_ns3_modules = topsort(graph) sections = [PygenSection('__main__', FileCodeSink(open(pygen_file_name, "wt")))] headers_map = {} section_precendences = {} for prec, ns3_module in enumerate(sorted_ns3_modules): section_name = "ns3_module_%s" % ns3_module.replace('-', '_') file_name = os.path.join(os.path.dirname(pygen_file_name), "%s.py" % section_name) sections.append(PygenSection(section_name, FileCodeSink(open(file_name, "wt")), section_name + "__local")) for header in ns3_modules[ns3_module][1]: headers_map[header] = section_name section_precendences[section_name] = prec module_parser = ModuleParser('ns3', 'ns3') module_parser.add_pre_scan_hook(pre_scan_hook) gccxml_options = dict( include_paths=[top_builddir], define_symbols={ }, cflags=('--gccxml-cxxflags "%s -DPYTHON_SCAN"' % cflags) ) module_parser.parse_init([everything_h], None, whitelist_paths=[top_builddir, os.path.dirname(everything_h)], pygen_sink=sections, pygen_classifier=MyPygenClassifier(headers_map, section_precendences), gccxml_options=gccxml_options) module_parser.scan_types() callback_classes_file = open(os.path.join(os.path.dirname(pygen_file_name), "callbacks_list.py"), "wt") scan_callback_classes(module_parser, callback_classes_file) callback_classes_file.close() module_parser.scan_methods() module_parser.scan_functions() module_parser.parse_finalize() for section in sections: section.code_sink.file.close()
def ns3_module_scan(top_builddir, pygen_file_name, everything_h, cflags): ns3_modules = eval(sys.stdin.readline()) ## do a topological sort on the modules graph from topsort import topsort graph = [] module_names = ns3_modules.keys() module_names.sort() for ns3_module_name in module_names: ns3_module_deps = list(ns3_modules[ns3_module_name][0]) ns3_module_deps.sort() for dep in ns3_module_deps: graph.append((dep, ns3_module_name)) sorted_ns3_modules = topsort(graph) # print >> sys.stderr, "******* topological sort: ", sorted_ns3_modules sections = [PygenSection("__main__", FileCodeSink(open(pygen_file_name, "wt")))] headers_map = {} # header_name -> section_name section_precendences = {} # section_name -> precedence for prec, ns3_module in enumerate(sorted_ns3_modules): section_name = "ns3_module_%s" % ns3_module.replace("-", "_") file_name = os.path.join(os.path.dirname(pygen_file_name), "%s.py" % section_name) sections.append(PygenSection(section_name, FileCodeSink(open(file_name, "wt")), section_name + "__local")) for header in ns3_modules[ns3_module][1]: headers_map[header] = section_name section_precendences[section_name] = prec module_parser = ModuleParser("ns3", "ns3") module_parser.add_pre_scan_hook(pre_scan_hook) # module_parser.add_post_scan_hook(post_scan_hook) gccxml_options = dict( include_paths=[top_builddir], define_symbols={ #'NS3_ASSERT_ENABLE': None, #'NS3_LOG_ENABLE': None, }, cflags=('--gccxml-cxxflags "%s -DPYTHON_SCAN"' % cflags), ) module_parser.parse_init( [everything_h], None, whitelist_paths=[top_builddir, os.path.dirname(everything_h)], # includes=['"ns3/everything.h"'], pygen_sink=sections, pygen_classifier=MyPygenClassifier(headers_map, section_precendences), gccxml_options=gccxml_options, ) module_parser.scan_types() callback_classes_file = open(os.path.join(os.path.dirname(pygen_file_name), "callbacks_list.py"), "wt") scan_callback_classes(module_parser, callback_classes_file) callback_classes_file.close() module_parser.scan_methods() module_parser.scan_functions() module_parser.parse_finalize() for section in sections: section.code_sink.file.close()
def sorted(self): pairlist = [] for migration in self.migrations.values(): for req in migration.requirements: pairlist.append((self.migrations[req], migration)) return topsort.topsort(pairlist)