REQUIREMENTS: Using Python, you will write a program called df s -stack.py that
ID: 3749070 • Letter: R
Question
REQUIREMENTS:
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.
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:
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.
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 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.
DELIVERABLES:
Submit a PRINT copy of your algorithm (pseudocode) at the start of class on the due date.
Using the Moodle upload link provided, submit your dfs-stack.py Python program.
Explanation / Answer
Python program to print DFS traversal from a
# given given graph
from collections import defaultdict
# This class represents a directed graph using
# adjacency list representation
class Graph:
# Constructor
def __init__(self):
# default dictionary to store graph
self.graph = defaultdict(list)
# function to add an edge to graph
def addEdge(self,u,v):
self.graph[u].append(v)
# A function used by DFS
def DFSUtil(self,v,visited):
# Mark the current node as visited and print it
visited[v]= True
print v,
# Recur for all the vertices adjacent to this vertex
for i in self.graph[v]:
if visited[i] == False:
self.DFSUtil(i, visited)
# The function to do DFS traversal. It uses
# recursive DFSUtil()
def DFS(self,v):
# Mark all the vertices as not visited
visited = [False]*(len(self.graph))
# Call the recursive helper function to print
# DFS traversal
self.DFSUtil(v,visited)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.