Ejemplo n.º 1
0
 def _findCenter(self, G):
     for source in range(G.V()):
         maxdist = 0
         abfs = BFS(G, source)
         for v in range(G.V()):
             if abfs.hasPathTo(v):
                 dist = abfs.distTo(v)
                 if maxdist < dist:
                     maxdist = dist
         self._maxdistances[source] = maxdist
Ejemplo n.º 2
0
	def _findCenter(self, G):
		for source in range(G.V()):
			maxdist = 0
			abfs = BFS(G, source)
			for v in range(G.V()):
				if abfs.hasPathTo(v):
					dist = abfs.distTo(v)
					if maxdist < dist:
						maxdist = dist
			self._maxdistances[source] = maxdist
 def test(self):
     bb = BreadthFirstSearch()
     
     n = 2
     m = 1
     edges = []
     start = 1
     self.assertEquals([-1], bb.calculate(n, m, edges, start))
     
     n = 3
     m = 1
     edges = [[1, 2]]
     start=1-1
     self.assertEquals([6], bb.calculate(n, m, edges, start))
Ejemplo n.º 4
0
 def setSearchAlgorithm(self, searchAlgorithm):
     if searchAlgorithm == "dfs":
         self.searchAlgorithm = DepthFirstSearch()
     elif searchAlgorithm == "bfs":
         self.searchAlgorithm = BreadthFirstSearch()
     elif searchAlgorithm == "astar":
         self.searchAlgorithm = BestFirstSearch()
Ejemplo n.º 5
0
def solution(initial):
	buildDictOfStates(initial)
	buildSymTbls()
	G = buildGraph()
	source = symTbl[tuple(initial)]
	node = symTbl[tuple(final)]
	abfs = BFS(G, source)
	# path is represented as a list of vertices
	path = abfs.pathTo(node)
	if path is None: return "No solution"
	# actions = [ (1, 2), (2, 3) ]
	actions = buildActions(path)
	dist = abfs.distTo(node)
	print dist
	for action in actions:
		print '{0} {1}'.format(action[0], action[1])
	return dist, actions
Ejemplo n.º 6
0
	def _findDiam(self, G, source):
		"find length of longest shortest path"
		abfs = BFS(G, source)
		node = -1
		for v in range(G.V()):
			if abfs.hasPathTo(v):
				dist = abfs.distTo(v)
				if self._max_dist < dist:
					self._max_dist = dist
					node = v # node will be set here at least 1x
		if self._marked[node]:
			# we returned to this vertex from the other end of the longest path
			return self._max_dist
		else:
			# run BFS on node furthest from source
			self._marked[node] = True
			self._findDiam(G, node)
Ejemplo n.º 7
0
def solution(initial):
    buildDictOfStates(initial)
    buildSymTbls()
    G = buildGraph()
    source = symTbl[tuple(initial)]
    node = symTbl[tuple(final)]
    abfs = BFS(G, source)
    # path is represented as a list of vertices
    path = abfs.pathTo(node)
    if path is None: return "No solution"
    # actions = [ (1, 2), (2, 3) ]
    actions = buildActions(path)
    dist = abfs.distTo(node)
    print dist
    for action in actions:
        print '{0} {1}'.format(action[0], action[1])
    return dist, actions
Ejemplo n.º 8
0
			G.addEdge(v, w)
	return G

def buildActions(path):
	actions = []
	for i in range(len(path)-1):
		from_state = path[i]
		to_state = path[i+1]
		# convert state from vertex to form [n1, n2, ...]
		from_state = symTbl2[from_state]
		to_state = symTbl2[to_state]
		for e in d[from_state]:
			if e[0] == to_state:
				actions.append(e[1])
	return actions

buildDictOfStates(initial)
buildSymTbls()
G = buildGraph()
source = symTbl[tuple(initial)]
node = symTbl[tuple(final)]
abfs = BFS(G, source)
# path is represented as a list of vertices
path = abfs.pathTo(node)
if path is None: print "No solution"
# actions = [ (1, 2), (1, 3) ]
actions = buildActions(path)
dist = abfs.distTo(node)
print dist
for action in actions:
	print '{0} {1}'.format(action[0], action[1])
Ejemplo n.º 9
0

def buildActions(path):
    actions = []
    for i in range(len(path) - 1):
        from_state = path[i]
        to_state = path[i + 1]
        # convert state from vertex to form [n1, n2, ...]
        from_state = symTbl2[from_state]
        to_state = symTbl2[to_state]
        for e in d[from_state]:
            if e[0] == to_state:
                actions.append(e[1])
    return actions


buildDictOfStates(initial)
buildSymTbls()
G = buildGraph()
source = symTbl[tuple(initial)]
node = symTbl[tuple(final)]
abfs = BFS(G, source)
# path is represented as a list of vertices
path = abfs.pathTo(node)
if path is None: print "No solution"
# actions = [ (1, 2), (1, 3) ]
actions = buildActions(path)
dist = abfs.distTo(node)
print dist
for action in actions:
    print '{0} {1}'.format(action[0], action[1])