Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

REQUIREMENTS: Using Python, you will write a program that implements Algorithm 2.3 (p. 49): Graph depth-first search (DFS) with a stack. You will not use

REQUIREMENTS:

  1. Using Python, you will write a program that implements Algorithm 2.3 (p. 49): Graph depth-first search (DFS) with a stack. image text in transcribed
  2. You will not use an adjacency list, as indicated in Algorithm 2.3. Instead, you will use an adjacency matrix (i.e., a two-dimensional array, or, in Python, a list of lists).
  3. You may use ANY list method you wish (e.g., append, pop, etc.).

IMPLEMENTATION DETAILS:

  1. Your program should begin by prompting the user for the number of vertices, V, in the graph, G.
  2. Your program will represent the graph G using an adjacency matrix, which is a square matrix with a row and a column for each vertex. Thus, your program will need to create a matrix M that consists of a V x V two-dimensional array -- in Python, a list of lists. (I recommend that your program initialize each element of the matrix equal to zero.)
  3. Next, your program should prompt the user to indicate which elements in the matrix should be assigned the value of 1 (i.e., information about vertices). Recall that each element in the matrix is the intersection of a row and a column.
  4. The result of step 4 should be an adjacency matrix representation of a graph, G.
  5. At this point, your program should print the newly-created adjacency matrix on the screen.
  6. Next, your program should prompt the user to specify node -- i.e., the starting vertex in G.
  7. From here, you then proceed with the implementation of Algorithm 2.3, with the following enhancements:
    • Be sure to use the newly-created adjacency matrix, instead of an adjacency list.
    • Immediately following line 5 (but before line 6) of Algorithm 2.3, your program should print the values currently on the stack.
    • At the end of the while block, your program should print the values currently on the stack.

In effect, the result of step 8 above should be a display of the stack evolution for your implementation of the DFS algorithm on graph G.

2.3 Depth-first Graph Traversal 49 Algorithm 2.3: Graph depth-first search with a stack. StackDFS(G, node) visited Input: G (V, E), a graplh node, the starting vertex in G Output: visited, an array of size |VI such that visited[i] is TRUE if we have visited node i, FALSE otherwise 1 S CreateStack() 2 visited CreateArray(IVI) 3 for i. 0 tolvido 4 visitedli] FALSE 5 Push (S, node) 6 while not IsStackEmpty (S) do 7 c Pop(s) visited[c] TRUE 9 foreach v in AdjacencyList(G, c) do 10 if not visited[v] then Push(S, v) 12 return visited added only if it is not already in the stack. To do this we use an additional array. An element of that array will be true if that element is currently in the stack and false otherwise. Algorithm 2.4 is the result. The algorithm is prett much the same as algorithm 2.4 but uses the additional array instack, where we record those nodes that are in the stack. You can see what is happening in the stack in figure 2.25c. You may wonder why, given that we already had algorithm 2.1, we went on to develop algorithm 2.4. Apart from it being instructive, to show how

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

C++ Database Development

Authors: Al Stevens

1st Edition

1558283579, 978-1558283572

More Books

Students also viewed these Databases questions