
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
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.](https://content.bartleby.com/qna-images/question/4a46be85-81d4-49e6-8ea4-5055a0d2528a/825431ce-db44-4db3-8abb-8bf3bae4fad9/lvv44w_thumbnail.jpeg)
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.

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

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by stepSolved in 3 steps with 1 images

Knowledge Booster
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
- Note: javaarrow_forwardImplement a Single linked list to store a set of Integer numbers (no duplicate) • Instance variable• Constructor• Accessor and Update methods 2.) Define SLinkedList Classa. Instance Variables: # Node head # Node tail # int sizeb. Constructorc. Methods # int getSize() //Return the number of nodes of the list. # boolean isEmpty() //Return true if the list is empty, and false otherwise. # int getFirst() //Return the value of the first node of the list. # int getLast()/ /Return the value of the Last node of the list. # Node getHead()/ /Return the head # setHead(Node h)//Set the head # Node getTail()/ /Return the tail # setTail(Node t)//Set the tail # addFirst(E e) //add a new element to the front of the list # addLast(E e) // add new element to the end of the list # E removeFirst()//Return the value of the first node of the list # display()/ /print out values of all the nodes of the list # Node search(E key)//check if a given…arrow_forwardvector<VertexT> Vertices; map<VertexT, vector<list>> adjList; What is the worst case time complexity for this version of _LookupVertex? bool _LookupVertex(VertexT v) const { if (adjList.count(v) != 0) { return true; } return false; } V is the number of total vertices in the graph, E is the average number of edges per vertex. O(v) O(VlogE) O(logV) O(V^2) O(E+V)arrow_forward
- 5. Given an efficient circular bent array-based queue q capable of holding 7 objects. Show the final contents of the array after the following code is executed: for (int k = 1; k <= 6; k++) q.enqueue(k); for (int k = 1; k <= 3; k++) { q.dequeue(); q.enqueue(q.dequeue()); } 0 1 2 3 4 5 6arrow_forwardDFS is run on the following graph. F is the starting vertex. F A B E Which vertices are in the stack before the first iteration of the while loop? Given vertices B, C are in the stack after the first iteration of the while loop: Which vertices are in the stack after the second iteration of the while loop? Which vertices are in visitedSet after the second iteration of the while loop? Ex: A, B, C, D, E, F (commas between values)arrow_forwardPython code please thank you!!arrow_forward
- Implement an algorithm to delete a node in the middle (i.e., any node butthe first and last node, not necessarily the exact middle) of a singly linked list, given only access tothat node.EXAMPLElnput:the node c from the linked list a->b->c->d->e->fResult: nothing is returned, but the new linked list looks like a ->b->d->e->farrow_forwardA set is a container that stores a collection of sorted unique elements. When we use a sorted linked structure to implement Set ADT, the time complexity of find(x), insert(x) and remove(x) are all O(n). Group of answer choices True Falsearrow_forwardIN JAVA LANGUAGE ALL INFORMATION IN PICTURES FIND MIN OF A BST CALL ON A RECURSIVE FINDMIN_R THAT WILL RETURN THE SMALLEST ELEMENT IN A BST THANK YOU!!!!!!!!!!!arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

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)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education