Answered step by step
Verified Expert Solution
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:
- 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 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).
- You may use ANY list method you wish (e.g., append, pop, etc.).
IMPLEMENTATION DETAILS:
- Your program should begin by prompting the user for the number of vertices, V, in the graph, G.
- 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.)
- 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.
- The result of step 4 should be an adjacency matrix representation of a graph, G.
- At this point, your program should print the newly-created adjacency matrix on the screen.
- Next, your program should prompt the user to specify node -- i.e., the starting vertex in G.
- 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 howStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started