Exemple #1
0
class Graph:

    def __init__(self, graphfile="graph.txt"):
        self.nodelist = []
        self.namelookup = dict()
        self.idlookup = dict()
        self.adjmatrix = None
        self.load_graph(graphfile)
        self.print_stats()



    def load_graph(self, filename):
        with open(filename, 'r') as graphfile: 

            if graphfile == None:
                sys.exit("Graph File Non-Existent, ensure file exists.")

            nodecount = 0


            for line in graphfile:
                if line.strip()[0] == '#':
                    continue
                else:
                    matchobj = re.match(r"(?P<nodeone>\w+) (?P<nodetwo>\w+) (?P<adjval>\d+)", line)
                    if matchobj != None:
                        if matchobj.group('nodeone') not in self.idlookup:
                            self.namelookup[nodecount] = matchobj.group('nodeone')
                            self.idlookup[matchobj.group('nodeone')] = nodecount
                            self.nodelist.append([nodecount, matchobj.group('nodeone')])
                            nodecount += 1

                        if matchobj.group('nodetwo') not in self.idlookup:
                            self.namelookup[nodecount] = matchobj.group('nodetwo')
                            self.idlookup[matchobj.group('nodetwo')] = nodecount
                            self.nodelist.append([nodecount, matchobj.group('nodetwo')])
                            nodecount += 1


            self.adjmatrix = AdjMatrix(nodecount)

            graphfile.seek(0)
            for line in graphfile:
                if line.strip()[0] == '#':
                    continue
                else:
                    matchobj = re.match(r"(?P<nodeone>\w+) (?P<nodetwo>\w+) (?P<adjval>\d+)", line)
                    if matchobj != None:
                        self.adjmatrix.set_adjvalue(self.idlookup[matchobj.group('nodeone')], self.idlookup[matchobj.group('nodetwo')], float(matchobj.group('adjval')))



    def print_stats(self):
        print ""
        print self.nodelist
        print self.namelookup
        print self.idlookup
        self.adjmatrix.print_adjmatrix
        print ""
Exemple #2
0
class TSPGraph:

    def __init__(self):
        self.nodelist = []
        self.idlookup = []
        self.adjmatrix = None
        self.read_graph()
        self.generate_adjmatrix()



    def read_graph(self):
        for line in sys.stdin:
            # May restore index variable. This area kind of hacky.
            nodeid, xcoord, ycoord = list(map(float, line.split()))
            self.nodelist.append([len(self.nodelist), float(xcoord), float(ycoord)])
            self.idlookup.append(int(nodeid))



    def generate_adjmatrix(self):
        self.adjmatrix = AdjMatrix(len(self.nodelist))

        # Try to fix the redundant assignments later.
        for n in self.nodelist:
            for m in self.nodelist[n[0]:len(self.nodelist)]:
                if m == n:
                    self.adjmatrix.set_adjvalue(n[0], m[0], 0)
                else:
                    deltax = n[1] - m[1]
                    deltay = n[2] - m[2]
                    distance = math.sqrt(math.pow(deltax, 2) + math.pow(deltay, 2))
                    self.adjmatrix.set_adjvalue(n[0], m[0], distance)