Question

In: Computer Science

Python 3 question Suppose that a directed and weighted graph represented as vertex-list, how could it...

Python 3 question

Suppose that a directed and weighted graph represented as vertex-list, how could it perform uniform cost search by using priority queue?

Solutions

Expert Solution

CODE

class Node:

def __init__(self, label):

self.out_edges = []

self.label = label

self.is_goal = False


def add_edge(self, node, weight = 0):

self.out_edges.append(Edge(node, weight))

class Edge:

def __init__(self, node, weight = 0):

self.node = node

self.weight = weight

def to(self):

return self.node

class Graph:

def __init__(self):

self.nodes = []

def ucs(G, v):

visited = set() # set of visited nodes

q = queue.PriorityQueue() # we store vertices in the (priority) queue as tuples

# (f, n, path), with

# f: the cumulative cost,

# n: the current node,

# path: the path that led to the expansion of the current node

q.put((0, v, [v])) # add the starting node, this has zero *cumulative* cost

# and it's path contains only itself.

while not q.empty(): # while the queue is nonempty

f, current_node, path = q.get()

visited.add(current_node) # mark node visited on expansion,

# only now we know we are on the cheapest path to

# the current node.

if current_node.is_goal: # if the current node is a goal

return path # return its path

else:

for edge in in current_node.out_edges:

child = edge.to()

if child not in visited:

q.put((current_node_priority + edge.weight, child, path + [child]))

​​​​​​​


Related Solutions

Python3 question. Suppose I have a undirect and unweighted graph with vertex list type, here is...
Python3 question. Suppose I have a undirect and unweighted graph with vertex list type, here is the code of the graph. graph = { "a" : ["c"], "b" : ["c", "e"], "c" : ["a", "b", "d", "e"], "d" : ["c"], "e" : ["c", "b"], "f" : [] } I want to use this to create a DFS method. How can I use recursion only to finish the work?
Python3 question. Suppose I have a undirect and unweighted graph with vertex list type, here is...
Python3 question. Suppose I have a undirect and unweighted graph with vertex list type, here is the code of the graph. graph = { "a" : ["c"], "b" : ["c", "e"], "c" : ["a", "b", "d", "e"], "d" : ["c"], "e" : ["c", "b"], "f" : [] } I want to use this to create a DFS method. How can I use stack only to finish the work?
Python3 question. Suppose I have a undirect and unweighted graph with vertex list type, here is...
Python3 question. Suppose I have a undirect and unweighted graph with vertex list type, here is the code of the graph. graph = { "a" : ["c"], "b" : ["c", "e"], "c" : ["a", "b", "d", "e"], "d" : ["c"], "e" : ["c", "b"], "f" : [] } I want to use this to create a BFS method. How can I use queue only to finish the work?
I wanted c++ program for the question Problem 6 - Weighted Graph In the vertex and...
I wanted c++ program for the question Problem 6 - Weighted Graph In the vertex and edge structure defined below Vertex        Edge   Vertex SFO        2702   BOS SFO        1846   ORD ORD         867   BOS ORD         742   JFK JFK         189   BOS SFO        1464   DFW DFW         802   ORD DFW        1123   MIA MIA        1092   JFK MIA        1258   BOS SFO         339   LAX LAX        1235   DFW LAX        2342   MIA a) Find the shortest...
Prove that \strongly connected" is an equivalence relation on the vertex set of a directed graph
Prove that \strongly connected" is an equivalence relation on the vertex set of a directed graph
Suppose you have an all positive edge-weighted graph G and a start vertex a and asks...
Suppose you have an all positive edge-weighted graph G and a start vertex a and asks you to find the lowest-weight path from a to every vertex in G. Now considering the weight of a path to be the maximum weight of its edges, how can you alter Dijkstra’s algorithm to solve this?
Consider a relation from daily life that can be represented in a directed acyclic graph (DAG)....
Consider a relation from daily life that can be represented in a directed acyclic graph (DAG). Describe the relation in words and draw the directed acyclic graph. Give a topological sort of the directed acyclic graph.
Python3 question. Suppose that a graph represent as adjacency matrix, how could it perform DFS by...
Python3 question. Suppose that a graph represent as adjacency matrix, how could it perform DFS by using stack. Graph is a undirect and unweighted graph.
. Provide a weighted directed graph G = (V, E, c) that includes three vertices a,...
. Provide a weighted directed graph G = (V, E, c) that includes three vertices a, b, and c, and for which the maximum-cost simple path P from a to b includes vertex c, but the subpath from a to c is not the maximum-cost path from a to c
Suppose that a graph G is such that each vertex of G has degree at least...
Suppose that a graph G is such that each vertex of G has degree at least 100. Show that G contains a cycle of length at least 101, i.e., a cycle passing through at least 101 vertices.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT