Question

Write the pseudocode of the Depth First Search Algorithm DFS(G) using adjacencymatrix representation of the graph...

Write the pseudocode of the Depth First Search Algorithm DFS(G) using adjacencymatrix
representation of the graph G. What is the running time of your pseudocode?

Specification: start from the psedocode discussed in class and do only the modifications
needed for adjacency-matrix graph representation.

Below is the pseudocode discussed in class:


0 0
Add a comment Improve this question Transcribed image text
Answer #1

Pseudocode for Depth first search algorithm using adjacency matrix representation of graph G:

DFS(G, i)

  1. Enter the number of vertices ‘n’ in the graph

  2. Initialize an array G[i][j] and complete the adjacency matrix.

    For i=1 to n

    For j=1 to n

    Enter G[i][j]

  3. Initialize visited[i] to zero.

  4. Call procedure DFS(G,i)

  5. Repeat steps 6 to 8 for ‘n’ times

  6. Mark visited[i]=1

  7. For j=1 to n

  8. If (visited[j]==0 && G[i][j]==1) then call DFS(G,j)

    Running time of this pseudocode is O(n2 ) where n is the number of vertices. As we can see in the pseudocode, function DFS(G,i) is called for every vertex and it executes ‘for loop’ for n times. So for n number of vertices, for loop will be executed n times. Thus it takes O(n*n)= O(n2) time.

Add a comment
Know the answer?
Add Answer to:
Write the pseudocode of the Depth First Search Algorithm DFS(G) using adjacencymatrix representation of the graph...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT