REQUIREMENTS: 1. Using Python, you will write a program called dfs-stack.py that implements Algorithm 2.3 (p. 49): Graph depth-first search (DFS) with a stack. 2. 3. 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: 1. Based upon the REQUIREMENTS above, along with the IMPLEMENTATION DETAILS (i.e., this section), you MUST first develop an algorithmic solution using pseudocode. This includes both your logic (in pseudocode) and the logic presented in the pseudocode indicated in Algorithm 2.3. 2. Be sure to include your name, along with the Certificate of Authenticity, as comments at the very beginning of your Python code. Also, if you collaborated with others, be sure to state their names as well. 3. Your program should begin by prompting the user for the number of vertices, V, in the graph, G. 4. 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 VxVtwo-dimensional array -- in Python, a list of lists. (I recommend that your program initialize each element of the matrix equal to zero.) 5. 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. 6. 7. 8. 9. The result of steps 3, 4, and 5 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 9 above should be a display of the stack evolution for your implementation of the DFS algorithm on graph G.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 1TF
icon
Related questions
Question
Python please thank you
REQUIREMENTS:
1. Using Python, you will write a program called dfs-stack.py that implements Algorithm 2.3 (p. 49): Graph depth-first search (DFS) with a stack.
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. Based upon the REQUIREMENTS above, along with the IMPLEMENTATION DETAILS (i.e., this section), you MUST first develop an algorithmic solution using
pseudocode. This includes both your logic (in pseudocode) and the logic presented in the pseudocode indicated in Algorithm 2.3.
2.
Be sure to include your name, along with the Certificate of Authenticity, as comments at the very beginning of your Python code. Also, if you collaborated with others, be
sure to state their names as well.
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 sa 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 Vx V two-dimensional array -- in Python, a list of lists. (I recommend that your program initialize each element of the matrix equal to
zero.)
5. 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.
3.
4.
6.
7.
8
9.
The result of steps 3, 4, and 5 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 9 above should be a display of the stack evolution for your implementation of the DFS algorithm on graph G.
Transcribed Image Text:REQUIREMENTS: 1. Using Python, you will write a program called dfs-stack.py that implements Algorithm 2.3 (p. 49): Graph depth-first search (DFS) with a stack. 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. Based upon the REQUIREMENTS above, along with the IMPLEMENTATION DETAILS (i.e., this section), you MUST first develop an algorithmic solution using pseudocode. This includes both your logic (in pseudocode) and the logic presented in the pseudocode indicated in Algorithm 2.3. 2. Be sure to include your name, along with the Certificate of Authenticity, as comments at the very beginning of your Python code. Also, if you collaborated with others, be sure to state their names as well. 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 sa 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 Vx V two-dimensional array -- in Python, a list of lists. (I recommend that your program initialize each element of the matrix equal to zero.) 5. 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. 3. 4. 6. 7. 8 9. The result of steps 3, 4, and 5 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 9 above should be a display of the stack evolution for your implementation of the DFS algorithm on graph G.
Algorithm 2.3: Graph depth-first search with a stack.
StackDFS (G, node) → visited
Input: G = (V, E), a graph
1
2
3
4
5
6
7
8
9
10
11
12
node, the starting vertex in G
Output: visited, an array of size |V| such that visited[i] is TRUE if we
have visited node i, FALSE otherwise
S CreateStack()
visited CreateArray (IV)
for i0 to V| do
visited[i] ← FALSE
Push (S, node)
while not IsStackEmpty (S) do
c← Pop (s)
visited[c] ← TRUE
foreach v in AdjacencyList (G, c) do
if not visited[v] then
Push (S, v)
return visited
Transcribed Image Text:Algorithm 2.3: Graph depth-first search with a stack. StackDFS (G, node) → visited Input: G = (V, E), a graph 1 2 3 4 5 6 7 8 9 10 11 12 node, the starting vertex in G Output: visited, an array of size |V| such that visited[i] is TRUE if we have visited node i, FALSE otherwise S CreateStack() visited CreateArray (IV) for i0 to V| do visited[i] ← FALSE Push (S, node) while not IsStackEmpty (S) do c← Pop (s) visited[c] ← TRUE foreach v in AdjacencyList (G, c) do if not visited[v] then Push (S, v) return visited
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning