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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
**Algorithm 2.3: Graph Depth-First Search with a Stack**

This algorithm demonstrates how to perform a depth-first search (DFS) on a graph using a stack data structure.

**Procedure**: `StackDFS(G, node) → visited`

- **Input**:
  - `G = (V, E)`: A graph where `V` is the set of vertices and `E` is the set of edges.
  - `node`: The starting vertex in the graph `G`.

- **Output**:
  - `visited`: An array of size `|V|` such that `visited[i]` is TRUE if the node `i` has been visited, and FALSE otherwise.

**Steps**:

1. `S ← CreateStack()`: Initialize an empty stack `S`.
2. `visited ← CreateArray(|V|)`: Create an array `visited` with a length equal to the number of vertices `|V|`.
3. `for i ← 0 to |V| do`: Loop through all vertices.
4. `visited[i] ← FALSE`: Mark all vertices as not visited.
5. `Push(S, node)`: Push the starting node onto the stack.
6. `while not IsStackEmpty(S) do`: Repeat while the stack `S` is not empty.
7. `c ← Pop(S)`: Pop the top element from the stack and assign it to `c`.
8. `visited[c] ← TRUE`: Mark the vertex `c` as visited.
9. `foreach v in AdjacencyList(G, c) do`: For each vertex `v` adjacent to `c` in the graph.
10. `if not visited[v] then`: If the vertex `v` has not been visited.
11. `Push(S, v)`: Push `v` onto the stack.
12. `return visited`: Return the array indicating the visited status of each vertex.

This algorithm uses a stack to backtrack and explore all reachable vertices from the starting node, marking them as visited in the process.
Transcribed Image Text:**Algorithm 2.3: Graph Depth-First Search with a Stack** This algorithm demonstrates how to perform a depth-first search (DFS) on a graph using a stack data structure. **Procedure**: `StackDFS(G, node) → visited` - **Input**: - `G = (V, E)`: A graph where `V` is the set of vertices and `E` is the set of edges. - `node`: The starting vertex in the graph `G`. - **Output**: - `visited`: An array of size `|V|` such that `visited[i]` is TRUE if the node `i` has been visited, and FALSE otherwise. **Steps**: 1. `S ← CreateStack()`: Initialize an empty stack `S`. 2. `visited ← CreateArray(|V|)`: Create an array `visited` with a length equal to the number of vertices `|V|`. 3. `for i ← 0 to |V| do`: Loop through all vertices. 4. `visited[i] ← FALSE`: Mark all vertices as not visited. 5. `Push(S, node)`: Push the starting node onto the stack. 6. `while not IsStackEmpty(S) do`: Repeat while the stack `S` is not empty. 7. `c ← Pop(S)`: Pop the top element from the stack and assign it to `c`. 8. `visited[c] ← TRUE`: Mark the vertex `c` as visited. 9. `foreach v in AdjacencyList(G, c) do`: For each vertex `v` adjacent to `c` in the graph. 10. `if not visited[v] then`: If the vertex `v` has not been visited. 11. `Push(S, v)`: Push `v` onto the stack. 12. `return visited`: Return the array indicating the visited status of each vertex. This algorithm uses a stack to backtrack and explore all reachable vertices from the starting node, marking them as visited in the process.
**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.

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 \( V \times 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.

6. The result of steps 3, 4, and 5 should be an adjacency matrix representation of a graph, \( G \).

7. At this point, your program should print the newly-created adjacency matrix on the screen.

8. Next, your program should prompt the user to specify node -- i.e., the starting vertex in \( G \).

9. 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.
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. 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 \( V \times 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. 6. The result of steps 3, 4, and 5 should be an adjacency matrix representation of a graph, \( G \). 7. At this point, your program should print the newly-created adjacency matrix on the screen. 8. Next, your program should prompt the user to specify node -- i.e., the starting vertex in \( G \). 9. 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.
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Single source shortest path
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education