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

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.
here i have a dictionary with the words and i have to find the words with...
here i have a dictionary with the words and i have to find the words with the largest size, (i mean word count for eg abdominohysterectomy) which have anagrams , like for example ,loop -> pool. Also , i have to sort them in alphabetical way so what could be the algorithm for that public class Anagrams {    private static final int MAX_WORD_LENGTH = 25;    private static IArray<IVector<String>> theDictionaries =        new Array<>(MAX_WORD_LENGTH);    public static void...
Here I have one question. A positive net income figure on the income statement is ultimately...
Here I have one question. A positive net income figure on the income statement is ultimately insignificant unless a company can translate its earnings into cash. Explain support for this statement.
Type or paste question here Fill in the empty cells and the values required in the...
Type or paste question here Fill in the empty cells and the values required in the last row of the table. These values will help you answer subsequent questions and calculate the Pearson’s r and the linear regression equation. (2 decimals) X Y ()( 3 3 -2.00 -4.00 4.00 16.00 8.00 6 9 1.00 2.00 1.00 4.00 2.00 5 8 0.00 1.00 0.00 1.00 0.00 4 3 -1.00 -4.00 1.00 16.00 4.00 7 10 2.00 3.00 4.00 9.00 6.00 5...
THIS IS A SPANISH QUESTION (I have seen Spanish questions answered here before so I'd figure...
THIS IS A SPANISH QUESTION (I have seen Spanish questions answered here before so I'd figure i would try it) Write two paragraphs of at least five sentences describing your favorite restaurant and your least favorite restaurant. Describe the food (breakfast, lunch, dinner, and drinks), prices and service. These restaurants can be of whatever you'd like, just as long as it fits all the requirements when its being described.
Here is the discussion Question. The reply is below the discussion question. I need a reply...
Here is the discussion Question. The reply is below the discussion question. I need a reply to the reply below.(I need an elaborate and comprehensive answer) Do you think markets, in general, are “self-regulating” at least in the US today ? if so why and if not why not? [2] As the prices of good and services begin to increase, shifting to its alternatives is possibly to occur in consumption. However, potential issues may arise from waiting for current prices...
Suppose I have a list of 17 sorted elements (A0 ---- A16) and an integer x.....
Suppose I have a list of 17 sorted elements (A0 ---- A16) and an integer x.. Assuming that we compare x with the first element A0, if x is equal to A0 , we found x and we return 1; otherwise we jump 4 elements (to A4). If x is equal to A4, we return 5 (index + 1), otherwise if it is less we back up 1 space to A3; if x is equal to A3 we return 4,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT