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 recursion only to finish the work?

Solutions

Expert Solution

Pseudo Algorithm for Depth First Traversal:

DFS(v,G):
   visited[v] = True

   repeat{
           for all vertices w adj. to v do{
               if(visited[w]==False)
                   DFS(w,G)
           }
   }

Python Code for solving the problem:

# global variable for storing the order in which the nodes were visited
node_visiting_order_list = []

# helper function for recursively performing depth first traversal
def DFS_actual(node,graph,visited):
   visited[node] = True
   node_visiting_order_list.append(node)

   for vertex in graph[node]:
       if not visited[vertex]:
           DFS_actual(vertex,graph,visited)
   return

# function for depth first traversal of the tree
def DFS(node,graph):
   visited = {}
   for vertex in graph:
       visited[vertex] = False

   node_visiting_order_list = []
   DFS_actual(node,graph,visited)

# main function
def main():
   global node_visiting_order_list
  
   # defining the graph(as dictionary)
   graph = { "a" : ["c"],
       "b" : ["c", "e"],
           "c" : ["a", "b", "d", "e"],
           "d" : ["c"],
           "e" : ["c", "b"],
           "f" : []
   }

   # list of all the nodes in the graph
   nodes = []
   for vertex in graph:
       nodes.append(vertex)

   # iterating over all the vertices in the graph  
   for node in nodes:
       DFS(node,graph)
       print("Following is Depth First Traversal (starting from vertex "+node+" ):")
       print(node_visiting_order_list)  
       node_visiting_order_list = [] # re-initializing the list

if __name__ == '__main__':
main()   

Snapshot of the console output:



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 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?
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