Question

PROMPT:

Consider a graph G. A connected component is a maximal subset of nodes that induces a connected sub graph. It’s maximal in the sense that you cannot add a node with the resulting induced sub graph remaining connected.The following function numComponents returns the number of connected components in an undirected graph.

int numComponents(int numNodes, struct ListNode *adjList) enum Boolean visited[numNodes]; for (int k-0; k<numNodes; k++){ //

QUESTION:

What is the time complexity for this function? The time complexity should be a function of the number of nodes |V| and the number of edges |E|.

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

Constant time operations are not considered while calculating the time complexity.

you should concentrate on loops for checking the time complexity.

The first loop runs number of node times so it's complexity is O(V) where V is number of nodes.

this will be considered

Next for loop is outer for loop which again runs O(V) times

BFS deals with removing one element from the queue and appending further elements with queue. this has O(V2) complexity if adjacency matrix is used. if adjacency list is used, complexity is O(V+E). means that whichever is higher will dominate.

We use a list in our case so the complexity is O(V+E).

this is inner loop complexity. total complexity is inner loop complexity * outer loop complexity

= O(V*(V+E))

To find the complexity of the function, add O(V) + O(V(V+E))

now complexity = O(V) + O(V2 + VE)

since higher order terms are present lower order terms can be ignored.

complexity is O(V2 + VE)

Add a comment
Know the answer?
Add Answer to:
PROMPT: Consider a graph G. A connected component is a maximal subset of nodes that induces a connected sub graph. It’s maximal in the sense that you cannot add a node with the resulting induced sub g...
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
  • Introduction In this lab, you are supposed to implement a graph class with the data structure...

    Introduction In this lab, you are supposed to implement a graph class with the data structure implemented before like linked list and queue. graph The class graph contains three member variables: linkedList *adjacentVertices; //an array of linked list. For a vertice i, adjacentVertices[i] stores the linked list that contains all other vertices connected to vertice i. int numVertices; //The number of vertices in the graph. int maxNumVertices; //The maximum number of vertices the graph can hold. Following public methods are...

  • IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as...

    IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as well as make sure the Big 3 are defined. IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined...

  • I've posted 3 classes after the instruction that were given at start You will implement and...

    I've posted 3 classes after the instruction that were given at start You will implement and test a PriorityQueue class, where the items of the priority queue are stored on a linked list. The material from Ch1 ~ 8 of the textbook can help you tremendously. You can get a lot of good information about implementing this assignment from chapter 8. There are couple notes about this assignment. 1. Using structure Node with a pointer point to Node structure to...

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