def problem81(matrix,graph,n_rows, n_cols): for r in xrange(n_rows): for c in xrange(n_cols): cur = matrix[r][c] # right if(c < n_cols - 1): right = matrix[r][c+1] graph.add_edge((cur, right), wt=right.value) # bottom, don't allow leftmost column to go down if(r < n_rows - 1 and c > 0): bottom = matrix[r+1][c] graph.add_edge((cur, bottom), wt=bottom.value) _, node_to_cost = pygraph.algorithms.minmax.shortest_path(graph, matrix[0][0]) goal = matrix[n_rows - 1][n_cols - 1] print goal, node_to_cost[goal]
def problem81(matrix, graph, n_rows, n_cols): for r in xrange(n_rows): for c in xrange(n_cols): cur = matrix[r][c] # right if (c < n_cols - 1): right = matrix[r][c + 1] graph.add_edge((cur, right), wt=right.value) # bottom, don't allow leftmost column to go down if (r < n_rows - 1 and c > 0): bottom = matrix[r + 1][c] graph.add_edge((cur, bottom), wt=bottom.value) _, node_to_cost = pygraph.algorithms.minmax.shortest_path( graph, matrix[0][0]) goal = matrix[n_rows - 1][n_cols - 1] print goal, node_to_cost[goal]
def build_knn_graph(raw_data): """ :param raw_data: (pts_num, 3) :return: """ nbrs = spatial.cKDTree(raw_data) dists, idxs = nbrs.query(raw_data, k=5) ## build graph graph = pygraph.classes.graph.graph() graph.add_nodes(xrange(len(raw_data))) sid = 0 for idx, dist in zip(idxs, dists): for eid, d in zip(idx, dist): if not graph.has_edge((sid, eid)) and eid < len(raw_data): graph.add_edge((sid, eid), d) sid = sid + 1 return graph
def problem82(matrix, graph,n_rows, n_cols): for r in xrange(n_rows): for c in xrange(n_cols): cur = matrix[r][c] # right if(c < n_cols - 1): right = matrix[r][c+1] graph.add_edge((cur, right), wt=right.value) # bottom, don't allow leftmost column to go down if(r < n_rows - 1 and c > 0): bottom = matrix[r+1][c] graph.add_edge((cur, bottom), wt=bottom.value) # top, don't allow leftmost column to go down if(r > 0 and c > 0): top = matrix[r-1][c] graph.add_edge((cur, top), wt=top.value) min_cost = 10e100 for r in xrange(n_rows): print "row", r path, distances = pygraph.algorithms.minmax.shortest_path(graph, matrix[r][0]) for r2 in xrange(n_rows): cur = matrix[r2][n_cols - 1] if cur in distances and distances[cur] < min_cost: min_cost = distances[cur] min_start = matrix[r][1] min_end = cur min_path = path print min_cost, min_start, min_end
def problem82(matrix, graph, n_rows, n_cols): for r in xrange(n_rows): for c in xrange(n_cols): cur = matrix[r][c] # right if (c < n_cols - 1): right = matrix[r][c + 1] graph.add_edge((cur, right), wt=right.value) # bottom, don't allow leftmost column to go down if (r < n_rows - 1 and c > 0): bottom = matrix[r + 1][c] graph.add_edge((cur, bottom), wt=bottom.value) # top, don't allow leftmost column to go down if (r > 0 and c > 0): top = matrix[r - 1][c] graph.add_edge((cur, top), wt=top.value) min_cost = 10e100 for r in xrange(n_rows): print "row", r path, distances = pygraph.algorithms.minmax.shortest_path( graph, matrix[r][0]) for r2 in xrange(n_rows): cur = matrix[r2][n_cols - 1] if cur in distances and distances[cur] < min_cost: min_cost = distances[cur] min_start = matrix[r][1] min_end = cur min_path = path print min_cost, min_start, min_end