Question

In: Computer Science

Python3 question. Suppose that a graph represent as adjacency matrix, how could it perform DFS by...

Python3 question.

Suppose that a graph represent as adjacency matrix, how could it perform DFS by using stack.

Graph is a undirect and unweighted graph.

Solutions

Expert Solution

# do comment if any problem arises

# Code

#this implementation uses list as stacks

# this function returns all vertices connected with given vertex that are not visited till now

def find_next(graph, visited):

    vertices = []

    # for all vertices in graph

    for vertex in range(len(graph)):

        # if vertex is connected to given vertex and not visited till now

        if graph[vertex] == True and vertex not in visited:

                # then add it to vertices

                vertices.append(int(vertex))

    return vertices


def dfs(graph, start, visited=[]):

    #if vertex is already visited then return

    if start in visited:

        return

    #add to visited vertices

    visited.append(start)

    #print current vertex

    print(start, end=' ')

    #this list stores all connected vertices to start which are not in visited

    next_v = find_next(graph[start], visited)

    for next in next_v:

        dfs(graph, next, visited)

    return visited


graph = [[0, 1, 1, 0, 0],

         [1, 0, 0, 1, 0],

         [1, 0, 0, 0, 1],

         [0, 1, 0, 0, 0],

         [0, 0, 1, 0, 0]]

# graphical representation of graph

#           0

#          /  \

#         1    2

#        /      \

#       3        4

# call dfs with starting vertex 0

dfs(graph, 0)

Output:


Related Solutions

Answer True or False 1. For graph representation, adjacency Matrix is more efficiency than adjacency list...
Answer True or False 1. For graph representation, adjacency Matrix is more efficiency than adjacency list in term of searching for edge. 2. Topological sort runs in O(|V| + |E|) where |V| is the number of vertices, and |E| is the number of edges in the input graph. 3. If vertex u can reach vertex v, and vertex v can reach vertex u, then vertices u and v are in the same Strongly-connected component (SCC). 4. The Bellman-Ford algorithm will...
One simple representation for a graph is to use an adjacency matrix: each of the N...
One simple representation for a graph is to use an adjacency matrix: each of the N nodes is given a unique number in the range 0 to N-1 to identify it. A large two dimensional array A with N rows and N columns is created so that A[x][y] stores the cost of travelling directly from node x to node y. if A[x][y] is zero, then there is no direct connection from x to y. A[x][y] does not need to equal...
Python3 question. Suppose I have a undirect and unweighted graph with vertex list type, here is...
Python3 question. Suppose I have a undirect and unweighted graph with vertex list type, here is the code of the graph. graph = { "a" : ["c"], "b" : ["c", "e"], "c" : ["a", "b", "d", "e"], "d" : ["c"], "e" : ["c", "b"], "f" : [] } I want to use this to create a DFS method. How can I use recursion only to finish the work?
Express the degree of a vertex in terms of the adjacency matrix Describe how you can...
Express the degree of a vertex in terms of the adjacency matrix Describe how you can find out whether a graph is connected, based on its adjacency matrix, using only matrix addition and multiplication.
How could you perform a complete sequence of a genome? What experiments would be performed? How...
How could you perform a complete sequence of a genome? What experiments would be performed? How can you create an entire database with the complete genome sequence?
The following situations represent errors and frauds that could occur in financial statements. State how the...
The following situations represent errors and frauds that could occur in financial statements. State how the ratio in question would compare (higher, equal, or lower) to what the ratio should have been had the error or fraud not occurred. The company recorded fictitious sales with credits to sales revenue accounts and debits to accounts receivable. Inventory was reduced, and cost of goods sold was increased for the profitable “sales.” Is the current ratio higher than, equal to, or lower than...
Plot a graph and write a paragraph to show how a Pigovian fee could work. Show...
Plot a graph and write a paragraph to show how a Pigovian fee could work. Show two different MACs and discuss whether equi-marginal principle holds. If yes, why? If no, why not?
Plot a graph and write a paragraph to show how a Pigovian fee could work. Show...
Plot a graph and write a paragraph to show how a Pigovian fee could work. Show two different MACs and discuss whether equi-marginal principle holds. If yes, why? If no, why not?
1. Can you perform quantitative analysis by IR? How? what difficulties will you encounter? how could...
1. Can you perform quantitative analysis by IR? How? what difficulties will you encounter? how could you deal with these? 2. Explain ATR sampling method and are any corrections necessary to be performed on ATR spectra? why?
How does the graph of y = sin x compare with the graph of y = cos x? Explain how you could horizontally translate the graph of y = sin x to obtain y = cos x.
How does the graph of y = sin x compare with the graph of y = cos x? Explain how you could horizontally translate the graph of y = sin x to obtain y = cos x.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT