Problem Set 5
Problem Set 5
Yeleswarapu
Problem 1
This is because the heuristic function can sometimes make the function less strict, giving us a slightly less efficient
solution. This error happens when the heuristic is greater than the actual value.
Problem 2
A)
We know that
is the same as
We also know that from the triangle inequality, that the potential function is less than the actual distance. Therefore,
= dist(u, l) − dist(v, l)
Therefore
B)
def create_landmark_heuristic_generator(G):
landmarks = numpy.random.choice(G(1))
for u in G:
a[0][u] = math.inf
a[1][u] = math.inf
a[2][u] = null
for l in landmarks:
for u in G:
if a[0][u] > djikstra[u][l] and a[1][u] > djikstra[l][t]:
a[0][u] = djikstra[u][l]
a[1][u] = djikstra[l][t]
a[2][u] = l
Akhilesh Yeleswarapu
def construct_heuristic_for(t):
def heuristic(u):
return a[0][u]+a[1][u]
return heuristic
return construct_heuristic_for
pylab.subplot(2,2,4)
pylab.title("ALT heuristic")
heuristic_generator = create_landmark_heuristic_generator(G)
heuristic_for_t = heuristic_generator(t)
visualize_path(G, s, t, *dijkstra(G, s, t, heuristic_for_t))
Problem 3
All of them are obviously increasing with n and d, however, I noticed that the Euclidian heuristic was initially
making it less efficient. After a point though, the ranking was consistently Manhattan, Euclidean, and
Dijkstra last.
Problem 4
import PQdict
def prim(G,s):
cur = s
visited = set()
pq = PQdict()
mst = []
while len(mst) < G.number_of_nodes() – 1
for n in G.neighbors(cur):
if n not in visited and cur not in visited:
if (cur, n) not in pq and (n, cur) not in pq:
w = G.edge[cur][n]['weight']
pq.add((cur, n), w)
visited.add(cur)
t, w = pq.popitem()
while(t[1] in visited):
t, w = pq.popitem()
mst.append(t)
current = t[1]
return mst