Question

Write the code for the function that returns a vector containing the out-degrees of all vertices...

Write the code for the function that returns a vector containing the out-degrees of all vertices given the adjacency list of some directed graph.

struct vnode {

int v; // vertex

                vnode *next;

              vnode(int u, vnode *n) {v = u; next =n;}

};

typedef vnode *vnodeptr;

vector<int> outdegrees(vector<vnodeptr> adj)

{

0 0
Add a comment Improve this question Transcribed image text
Answer #1
struct vnode {
int v; // vertex
   vnode *next;
    vnode(int u, vnode *n) {v = u; next =n;}
};

typedef vnode *vnodeptr;

vector<int> outdegrees(vector<vnodeptr> adj)
{
   vector<int> v;
   int count;
   vnodeptr temp;
   for(int i = 0; i < adj.size(); ++i) {
      count = 0;
      temp = adj[i];
      while(temp != NULL) {
         count++;
         temp = temp->next;
      }
      v.push_back(count);
   }
   return v;
}
Add a comment
Know the answer?
Add Answer to:
Write the code for the function that returns a vector containing the out-degrees of all vertices...
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
  • Graph 5. (15 pts) You're asked to compute the in- and out- degrees of all vertices...

    Graph 5. (15 pts) You're asked to compute the in- and out- degrees of all vertices in a directed multigraph G (V, E). But you're not sure how to represent the graph so that the calculation is most efficient. For each of the following three possible representations, express your answers in asymptotic notation in terms of |VI and |El, and justify your answers (a) An edge list representation. Assume vertices have arbitrary labels. (b) An adjacency list representation. Assume the...

  • 3. The indegree of a vertex u is the number of incoming edges into u, .e, edges of the form (v,u)...

    3. The indegree of a vertex u is the number of incoming edges into u, .e, edges of the form (v,u) for some vertex v Consider the following algorithm that takes the adjacency list Alvi, v2, n] of a directed graph G as input and outputs an array containing all indegrees. An adjacency list Alvi, v.. /n] is an array indexed by the vertices in the graph. Each entry Alv, contains the list of neighbors of v) procedure Indegree(Alvi, v2,......

  • /* Graph read from file, and represnted as adjacency list. To implement DFS and BFS on...

    /* Graph read from file, and represnted as adjacency list. To implement DFS and BFS on the graph */ #include <iostream> #include <sstream> #include <fstream> #include <vector> #include <utility> #include <unordered_map> #include <set> #include <queue> using namespace std; // Each vertex has an integer id. typedef vector<vector<pair<int,int>>> adjlist; // Pair: (head vertex, edge weight) adjlist makeGraph(ifstream& ifs); void printGraph(const adjlist& alist); vector<int> BFS(const adjlist& alist, int source); // Return vertices in BFS order vector<int> DFS(const adjlist& alist, int source); //...

  • 8, (10 pts) Show that given a directed graph G = (V,E) already stored in adjacency matrix form, determining if there is a vertex with in-degree n - 1 and out-degree 0 can be done in O(n) time whe...

    8, (10 pts) Show that given a directed graph G = (V,E) already stored in adjacency matrix form, determining if there is a vertex with in-degree n - 1 and out-degree 0 can be done in O(n) time where n is the number of vertices in V. 8, (10 pts) Show that given a directed graph G = (V,E) already stored in adjacency matrix form, determining if there is a vertex with in-degree n - 1 and out-degree 0 can...

  • How would I traverse through this graph? Provide example code, please! class Edge {    int src,...

    How would I traverse through this graph? Provide example code, please! class Edge {    int src, dest;    Edge(int src, int dest)    {        this.src = src;        this.dest = dest;    } }; // class to represent a graph object class Graph {    // A list of lists to represent adjacency list    List<List<Integer>> adj = new ArrayList<>();    // Constructor to construct graph    public Graph(List<Edge> edges)    {        // allocate memory for adjacency list        for (int i = 0; i < edges.size(); i++) {            adj.add(i,...

  • 3) (10 pts) Write a function that takes in a root node of a trie and...

    3) (10 pts) Write a function that takes in a root node of a trie and returns the length of the longest word stored in that trie. Use the struct given and function prototype given below. typedef struct trienode { int isWord; struct trienode* next [26]; } trienode; int maxWordLength (trienode* root) { // Fill in code }

  • Translate psuedo code for computing chromatic number of a grapgh to java code 1 //graph G,...

    Translate psuedo code for computing chromatic number of a grapgh to java code 1 //graph G, vertices n, vertices are numbered o,1-- //G i //colors are stored in arrayq //qlil has color of vertex i, initially all0 s stored in adjacency list or adjacency matrix p , returns chromatic number //colors G using mininum number of colors int color () for (i 1 to n) //if G can be colored using i colors starting at vertex 0 if (color (0,...

  • (5 marks) a. The pseudo-code for breadth-first search, modified slightly from Drozdek,1 is as follows: void...

    (5 marks) a. The pseudo-code for breadth-first search, modified slightly from Drozdek,1 is as follows: void breadthFirstSearch (vertex w) for all vertices u num (u) 0 null edges i=1; num (w) i++ enqueue (w) while queue is not empty dequeue ( V= for all vertices u adjacent to v if num(u) is 0 num (u) = i++; enqueue (u) attach edge (vu) to edges; output edges; Now consider the following graph. Give the breadth-first traversal of the graph, starting from...

  • C programming Write an iterative and recursive version of a function to print out a linked...

    C programming Write an iterative and recursive version of a function to print out a linked list from head to tail and then do the same for printing a linked list from tail to head. Assume a singly linked list in all cases and access only to a head pointer at the time of the function call. struct node; typedef struct node Node; struct node int data; Node next;

  • For a directed graph the in-degree of a vertex is the number of edges it has...

    For a directed graph the in-degree of a vertex is the number of edges it has coming in to it, and the out- degree is the number of edges it has coming out. (a) Let G[i,j] be the adjacency matrix representation of a directed graph, write pseudocode (in the same detail as the text book) to compute the in-degree and out-degree of every vertex in the Page 1 of 2 CSC 375 Homework 3 Spring 2020 directed graph. Store results...

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