Question

EXAMPLES OF FUNCTIONS/ALGORITHMS please in c++ b. Write a function that return true if an undirected graph is complete, false otherwise

EXAMPLES OF FUNCTIONS/ALGORITHMS

please in c++

b. Write a function that return true if an undirected graph is complete, false otherwise

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

The question is quite ambiguous. You should have specified how the edges are stored (like adjacency matrix or adjacency list, etc,.). Any way I will write two functions because the task can be done in two ways. The parameters to the functions will be different.

1. If a graph with 'V' vertices is complete, then total number of edges should be V*(V-1)/2; The time compexity for this approach would be O(1) the function for this would look like.

bool checkGraph(int v, int e)
{
int must_edges = v*(v-1)/2;
if(must_edges==e) return true;
else return false;
}

2. If we have an Adjacency matrix then all the vertices must be connected and this has to be checked in matrix. If we found any connection is missing we return false. The time compexity for this approach would be O(V^2), The function for it would look like

bool checkGraph(vector<vector<int> > v)
{
for(int i=0;i<v.size()-1;i++)
{
for(int j=i+1;j<v.size();j++)
{
if(v[i][j]==0) return false;
}
}
return true;
}

Add a comment
Know the answer?
Add Answer to:
EXAMPLES OF FUNCTIONS/ALGORITHMS please in c++ b. Write a function that return true if an undirected graph is complete, false otherwise
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