Example #1
0
class ComputePath:

    def __init__(self, mylist, start, end):
        #pass into a list of grocery location (with each element in tuple of x and y coordinates)
        #start: entrance; end: check-out, both in tuple
        self.distance=Graph()
        self.curMin=1000
        self.trackOrder=[]
        self.trackPath=[start]
        self.minPath([start], 0,start,mylist,end, self.trackPath)
        print("test3")
        print("length of path is "+str(self.curMin))
        self.iterateOrder()
        self.distance.paintPath(self.trackPath)
        print("my path is ")
        print(self.trackPath)

        self.start=start
        self.end=end
        self.mylist=mylist

    def minPath(self, trackOrder, curDis, lastPop, mylist, end, trackPath):
        #compute the min BFS path of passing all location in mylist; O(n!)
        if len(mylist)==0:
            curDis+=self.distance.bfs(lastPop,end)
            trackOrder.append(end)
            trackPath.append((self.distance.getItrPath()))
            if (curDis<=self.curMin):
                self.curMin=curDis
                self.trackOrder=trackOrder
                self.trackPath=trackPath
            return
        else:
            for index in range(len(mylist)):
                self.minPath(trackOrder+[mylist[index]], curDis+self.distance.bfs(lastPop, mylist[index]), self.distance.get_lastPop(), mylist[:index]+mylist[index+1:], end, trackPath+self.distance.getItrPath())

    def iterateOrder(self):
        mypath=""
        for ele in self.trackOrder:
            mypath+=str(ele)+" "
        print("order of shopping is: "+mypath)

    def redraw(self,new_row, new_col):
        self.distance.set_graph(new_row,new_col)
        self.curMin=1000
        self.trackOrder=[]
        self.trackPath=[self.start]

        self.minPath([self.start], 0,self.start,self.mylist,self.end, self.trackPath)
        print("length of path is "+str(self.curMin))
        self.iterateOrder()
        self.distance.paintPath(self.trackPath)
        print("my path is ")
        print(self.trackPath)

    def get_para(self):
        return self.distance.get_para()

    def get_trackPath(self):
        return self.trackPath
def gen_shimmer(n: int):
    nodes = [Node(x) for x in range(n)]  # start with 1

    nodes_dict = {}

    for node in nodes:
        nodes_dict[node.id] = node

    finished_nodes = []

    for x in range(n - 10):
        x = x
        node = nodes_dict[x]
        for i in range(4 - node.num_in()):
            added = 0
            nid = 0
            while added != 1:
                added, nid = node.add_in(random.choice(list(
                    nodes_dict.keys())))
            nodes_dict[nid].add_out(node.id)
        for i in range(4 - node.num_out()):
            added = 0
            nid = 0
            while added != 1:
                added, nid = node.add_out(
                    random.choice(list(nodes_dict.keys())))
            nodes_dict[nid].add_in(node.id)

        finished_nodes.append(node)
        del nodes_dict[node.id]

    edges = []
    nodes_ = []
    for node in finished_nodes:
        for in_ in node.inn:
            if not (node.id, in_) in edges:
                edges.append((node.id, in_))
        for out in node.out:
            if not (out, node.id) in edges:
                edges.append((out, node.id))
        nodes_.append(node.id)

    g = Graph()
    for edge in edges:
        g.addEdge(edge[0], edge[1])

    bfs_ordered = []
    g.BFS(0, bfs_ordered)

    result = Result([x.id for x in nodes], edges)

    return result
Example #3
0
 def test_two_components(self):
     adj_matrix = [
         '5\n', '0 1 1 0 0', '1 0 1 0 0', '1 1 0 0 0', '0 0 0 0 1',
         '0 0 0 1 0'
     ]
     graph = Graph.get_from_adjective_matrix(adj_matrix)
     components = graph.find_component()
     self.assertEqual({1: {0, 1, 2}, 2: {3, 4}}, components)
Example #4
0
def challenge_2():
    from_vert = sys.argv[2]
    to_vert = sys.argv[3]
    vertex_list = []
    edges = []
    counter = 0
    with open(sys.argv[1], 'r') as f:
        #graph_data = f.readlines()
        for line in f:
            x = line.strip('()\n').split(',')
            # counter at 1 indicates the input list or the vert list
            if counter == 1:
                vertex_list = x
            counter += 1
            # counter > 2 indicates the edges
            if counter > 2:
                edges.append(x)

    g = Graph()

    for vertex in vertex_list:
        g.add_vertex(vertex)

    for edge in edges:
        # print(edge)
        g.add_edge(edge[0], edge[1])
    # print('{} {}'.format(from_vert, to_vert))
    print(g.bfs(from_vert, to_vert))
Example #5
0
    def __init__(self, mylist, start, end):
        #pass into a list of grocery location (with each element in tuple of x and y coordinates)
        #start: entrance; end: check-out, both in tuple
        self.distance=Graph()
        self.curMin=1000
        self.trackOrder=[]
        self.trackPath=[start]
        self.minPath([start], 0,start,mylist,end, self.trackPath)
        print("test3")
        print("length of path is "+str(self.curMin))
        self.iterateOrder()
        self.distance.paintPath(self.trackPath)
        print("my path is ")
        print(self.trackPath)

        self.start=start
        self.end=end
        self.mylist=mylist
Example #6
0
 def test_bfs_ssp(self):
     graph = Graph()
     a = Vertex(1)
     b = Vertex(2)
     c = Vertex(3)
     d = Vertex(4)
     e = Vertex(5)
     graph.add_vertex(a)
     graph.add_vertex(b)
     graph.add_vertex(c)
     graph.add_vertex(d)
     graph.add_vertex(e)
     graph.add_edge(a, b)
     graph.add_edge(a, d)
     graph.add_edge(b, c)
     graph.add_edge(b, e)
     graph.add_edge(c, e)
     assert graph.bfs(a, e) == [1, 2, 5]
Example #7
0
 def test_one_component(self):
     adj_matrix = ['4\n', '0 1 1 0', '1 0 1 0', '1 1 0 1', '0 0 1 0']
     graph = Graph.get_from_adjective_matrix(adj_matrix)
     components = graph.find_component()
     self.assertEqual({1: {0, 1, 2, 3}}, components)
Example #8
0
 def test_loop(self):
     adj_matrix = ['3\n', '1 0 0', '0 1 0', '0 0 1']
     graph = Graph.get_from_adjective_matrix(adj_matrix)
     components = graph.find_component()
     self.assertEqual({1: {0}, 2: {1}, 3: {2}}, components)
Example #9
0
 def test_two_alone_vertexes(self):
     adj_matrix = ['4\n', '0 0 0 0', '0 0 1 0', '0 1 0 0', '0 0 0 0']
     graph = Graph.get_from_adjective_matrix(adj_matrix)
     components = graph.find_component()
     self.assertEqual({1: {0}, 2: {1, 2}, 3: {3}}, components)
Example #10
0
 def test_simple(self):
     g = Graph(3)
     self.assertEqual(g.find_all_distances(0), [-1, -1])
     g.connect(0, 1)
     self.assertEqual(g.find_all_distances(1), [6, -1])
Example #11
0
 def test_failing(self):
     g = Graph(4)
     g.connect(3, 1)
     g.connect(0, 1)
     g.connect(0, 2)
     self.assertEqual(g.find_all_distances(0), [6, 6, 12])