(b) Write the code for the function that computes and returns a vector containin
ID: 3708280 • Letter: #
Question
(b) Write the code for the function that computes and returns a vector containing the in-degrees of all vertices of a digraph given the adjacency list for the graph. Assume each adjacency list is a singly-linked, nullpos- terminated list of adjnodes. That is, in the last node the next field has value nullpos. struct adjnode t nt vertex; adjnode * next; vector vertex_indegrees2 (vector Adj) int nAdj.size // number of nodes int i, j adjnode cursor; // The vector to be returned: degree_vecvector vdegrees (n, 0)Explanation / Answer
vector<int>vertex_indgrees2(vector<adjnode*>Adj)
{
int n=Adj.size();
int i,j;
adjnode *cursor;
degree_vec=vector<int>vdgrees(n,0);
for(int i=0;i<n;i++) //looping over all the vertex from 0 to n-1
{
cursor=Adj[i]; //starting from head of the list
while(cursor!=nullpos) //getting till the end of list
{
vdgrees[cursor->vertex]++; //increasing the counter of vertex
cursor=cursor->next; //moving to next node
}
}
return vdgrees; //returning the vector
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.