def main():
    if (len(sys.argv) < 2):
        print("Usage: python top_order_2836796.py <input_n.txt>")
        sys.exit()
    sys.setrecursionlimit(10000)
    myGraph = graph.read_file(sys.argv[1])
    sorted = top_sort(myGraph)

    for node in sorted:
        print(str(node.index) + ' ', end='')
Beispiel #2
0
def graph():
    results = session.get('results', None)
    school = session.get('school_name', None)
    file_name = request.form.get('file')
    if file_name:
        labels, values, wls = read_file(file_name)
        print(wls)
        return render_template('graph.html',labels=labels, values=values,wls=wls)
    else:
        return render_template('graph.html',data=results, school=school)
Beispiel #3
0
def eulerian(filename):
    """ Return wheter or not the graph built from the given file contains a eulerian cycle """

    # read from a file to grab graph properties
    graph, vertices, edges = read_file(filename)
    # builded graph object
    graph = build_graph(graph, vertices, edges)

    eulerian = graph.is_eulerian()

    print("This graph is Eulerian: {}".format(eulerian))
Beispiel #4
0
def main():
    #check input
    if (len(sys.argv) < 2):
        print("Usage: python dfs_Lopez.py <graph_n.txt>")
        sys.exit()

    #run program
    myGraph = graph.read_file(sys.argv[1])
    search = DFS(myGraph)

    #print output
    print(' '.join(map(str, search)))
Beispiel #5
0
def main():
    #check input
    if (len(sys.argv) < 2):
        print("Usage: python bfs_Lopez.py <graph_n.txt>")
        sys.exit()

    #run program
    myGraph = graph.read_file(sys.argv[1])
    search = BFS(myGraph)

    #print output
    for layer in search:
        if layer:
            for item in layer:
                print(str(item) + ' ', end='')
Beispiel #6
0
def shortest_path(filename, from_vertex, to_vertex):

    # parse file and build graph
    graph, vertices, edges = read_file(filename)
    graph = build_graph(graph, vertices, edges)


    # Finds shortest between two verticies and stores path in array and length of path as an int
    path, path_length = graph.find_shortest_path(from_vertex, to_vertex)


    # If path exists, print the verticies
    if path:
        for i, vert in enumerate(path):
            if i < len(path)-1:
                print(vert, end=", ")
            else:
                print(vert)
Beispiel #7
0
def file_to_graph(filename):
    """ Builds and returns a graph based on the content in the filename provided """

    # Read file content and parse graph information
    graph, vertices, edges = read_file(filename)

    # Build graph
    graph = build_graph(graph, vertices, edges)

    # Resulting Graph info
    print('Number of vertices: {}'.format(len(graph.get_vertices())))
    print('Number of edges: {}'.format(len(graph.edge_list)))
    print("The Edge List:")

    for edge in graph.get_vertices():
        print(edge)

    return graph
Beispiel #8
0
    def run(self, file):
        try:
            instance = graph.read_file(file)
        except FileNotFoundError:
            print("Cannot find file {}, skipping.".format(file))
            return ""
        logfile_name = "_".join([os.path.basename(file), str(floor(time()))])
        logger = Logger(logfile_name, log_to_stdout=self.log_to_stdout)

        if self.cpu_count > 1:
            run_tasks = Queue(self.cpu_count)
            finished_queue = Queue()

            def generate_run_tasks():
                for algorithm_config in self.algorithm_configs:
                    runner = Runner(instance, logger, self.iterations,
                                    algorithm_config)
                    for run_nr in range(self.run_count):
                        # using the same runner for each thread is fine,
                        # because run_algorithm does not mutate the runner instance
                        yield (runner, run_nr)
                for _ in range(self.cpu_count):
                    yield 'STOP'

            for _ in range(self.cpu_count):
                Process(target=worker,
                        args=(run_tasks, finished_queue)).start()

            for run_task in generate_run_tasks():
                run_tasks.put(run_task)

            for _ in range(len(self.algorithm_configs * self.run_count)):
                lines = finished_queue.get()
                logger.write(lines)
        else:
            for algorithm_config in self.algorithm_configs:
                runner = Runner(instance, logger, self.iterations,
                                algorithm_config)

                for run_nr in range(self.run_count):
                    runner.run_algorithm(run_nr)
        logger.close()
        return logfile_name
Beispiel #9
0
def recursive_depth_first(filename, from_vertex, to_vertex):

    # Build Graph
    graph, vertices, edges = read_file(filename)
    graph = build_graph(graph, vertices, edges)


    # Evaluate Path using DFS recursively
    path = graph.dfs_paths(from_vertex, to_vertex)

    # Does path exist?
    print("A path between vertex {} and {}: {}".format(from_vertex, to_vertex, bool(path)))

    # Print path if it exists
    if path:
        print("Verticies in the path: ", end="")
        for i, vert in enumerate(path[::-1]):
            if i < len(path)-1:
                print(vert, end=",")
            else:
                print(vert)
 def setUp(self):
     self.graph = read_file('placeinfo_test')
     self.source_place = self.graph.get_place('Kochi')
     self.destination_place = self.graph.get_place('Elanthoor')
     self.disconnected_destination_place = self.graph.get_place('Boston')