Question

In: Computer Science

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?

Solutions

Expert Solution

CODE

def DFS(graph, start):

visited = {}

for v in graph:

visited[v] = False

stack = []

stack.append(start)

while (len(stack)):

start = stack[-1]

stack.pop()

if (not visited[start]):

print(start, end=' ')

visited[start] = True

for node in graph[start]:

if (not visited[node]):

stack.append(node)

graph = { "a" : ["c"],

"b" : ["c", "e"],

"c" : ["a", "b", "d", "e"],

"d" : ["c"],

"e" : ["c", "b"],

"f" : []

}

start = 'a'

DFS(graph, start)


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 BFS method. How can I use queue only to finish the work?
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?
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.
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?
Suppose the unweighted graph graph G = (V, E), represents connections between cities in a country. Salesman wants to get from city A to city P using the unweighted graph G = (V, E) of cities.
Algorithm and Data StructuresSuppose the unweighted graph graph G = (V, E), represents connections between cities in a country. Salesman wants to get from city A to city P using the unweighted graph G = (V, E) of cities.a) Explain how the salesman use BFS algorithm to get from city A to city P passing smallest number of cities. (all steps required)b) Now the salesman likes to visit city R on his way to city P. Describe an efficient algorithm...
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.
I have looked at other answers on here in regards to this question. But I do...
I have looked at other answers on here in regards to this question. But I do not know what "In" stands for and those who answer are using different descriptions than I am used to. Here is the question. Number of Periods. How long will it take for $400 to grow to $1,000 at the interest rate specified? (LO1) a. 4% b. 8% c. 16%. Could someone please break this down a little further for me.
I DO NOT have a specific question, my question here is GENERAL HOW TO DETERMINE IF...
I DO NOT have a specific question, my question here is GENERAL HOW TO DETERMINE IF A COMPOUND IS POLAR OR NONPOLAR? I know how to figure about the number of valence electrons and I KNOW how to draw lewis structure, but then I just mess every thing up. I want to know how to determine if it's polar or not. for example: sicl2f2, co2, xef2, xeo4 ... I KNOW how to draw the lewis structure, but this I CANNOT...
Suppose I have a list of 128 unsorted numbers. If I use the binary search to...
Suppose I have a list of 128 unsorted numbers. If I use the binary search to search it, how many comparisons will I have to do in the worst case, assuming I use a quadratic algorithm to sort the list first.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT