Question

Given a graph and a starting vertex, count the number of nodes a specified distance away....

Given a graph and a starting vertex, count the number of nodes a specified distance away. C++ Programming

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

Qo.

ANS:

C++ Code:


int countNodeDistance(vector<int> graph[], bool assignMark[], int node, vector<int>& distance)
{
   int lastvisited;
   queue<int> q;
   q.push(node);
   distance[node] = 0;
   while (!q.empty())
   {
       node = q.front();   q.pop();
       if (assignMark[node])
           lastvisited = node;
       for (int i = 0; i < graph[node].size(); i++)
       {
           int v = graph[node][i];
           if (distance[v] == -1)
           {
               distance[v] = distance[node] + 1;
               q.pnodesh(v);
           }
       }
   }
   return lastvisited;
}
int nodeDistancevisited(int edgeGraph[][2], int v, int assignvisited[], int N, int K)
{
   v = v + 1;
   vector<int> graph[v];

   int node, v;     
   for (int i = 0; i < (v - 1); i++)
   {
       node = edgeGraph[i][0];
       v = edgeGraph[i][1];

       graph[node].push_back(v);
       graph[v].push_back(node);
   }
   bool assignMark[v] = {false};
   for (int i = 0; i < N; i++)
   {
       assignMark[assignvisited[i]] = true;
   }
   vector<int> tmp(v, -1), dl(v, -1), dr(v, -1);
   node = countNodeDistance(graph, assignMark, 0, tmp);
   node = countNodeDistance(graph, assignMark, node, dl);
   countNodeDistance(graph, assignMark, node, dr);
   int rest = 0;
   for (int i = 0; i < v; i++)
   {
       if (dl[i] <= K && dr[i] <= K)
           rest++;
   }
   return rest;
}

// main or driver program
int main()
{
   int edgeGraph[][2] =
   {
       {1, 0}, {0, 3}, {0, 8}, {2, 3},
       {3, 5}, {3, 6}, {3, 7}, {4, 5},
       {5, 9}
   };
   int v = sizeof(edgeGraph) / sizeof(edgeGraph[0]);
  
   int assignvisited[] = {1, 2, 4};
   int N = sizeof(assignvisited) / sizeof(assignvisited[0]);

   int K = 3;
   cout << nodeDistancevisited(edgeGraph, v, assignvisited, N, K);
   return 0;
}

OUTPUT:

6

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

THANKS!!

Add a comment
Know the answer?
Add Answer to:
Given a graph and a starting vertex, count the number of nodes a specified distance away....
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
  • Given a graph and a starting vertex, count the number of nodes a specified distance away....

    Given a graph and a starting vertex, count the number of nodes a specified distance away. Requirements: Create a graph of int's. Given a starting node with value key and a distance dist , return the number of nodes that have a path from id with exactly dist edges. If there are multiple paths, only consider the shortest one. int countNodesWithDist(int id, int dist); // example declaration Examples: For the graph below, countNodesWithDist(2, 1) should return 2 since there are...

  • Using the following graph and Dijkstra's algorithm, calculate the shortest distance to each other vertex starting...

    Using the following graph and Dijkstra's algorithm, calculate the shortest distance to each other vertex starting from vertex A. Label all vertices with the total distance (from A). Indicate the order nodes are added to cloud. Draw a Minimum Spanning Tree for the graph. You should label all nodes in the tree, but you do not need to indicate edge weights or total distance. 2 D C L 7 6 2 7 2 A K B 4 7 4 1...

  • BFS Given an undirected graph below (a) Show the shortest distance to each vertex from source ver...

    Solve (a) and (b) using BFS and DFS diagram BFS Given an undirected graph below (a) Show the shortest distance to each vertex from source vertex H and predecessor tree on the graph that result from running breadth-finst search (BFS).Choose adjacen vertices in al phabetical order b) Show the start and finsh time for each vertex, starting from source vertex H, that result from running depth-first search (DFS)Choose aljacent vertices in alphabet- ical order DFS BFS Given an undirected graph...

  • BFS Given an undirected graph below (a) Show the shortest distance to each vertex from source ver...

    BFS Given an undirected graph below (a) Show the shortest distance to each vertex from source vertex H and predecessor tree on the graph that result from running breadth-finst search (BFS).Choose adjacen vertices in al phabetical order b) Show the start and finsh time for each vertex, starting from source vertex H, that result from running depth-first search (DFS)Choose aljacent vertices in alphabet- ical order DFS BFS Given an undirected graph below (a) Show the shortest distance to each vertex...

  • Given a directed graph with positive edge lengths and a specified vertex v in the graph,...

    Given a directed graph with positive edge lengths and a specified vertex v in the graph, the "all-pairs" v-constrained shortest path problem" is the problem of computing for each pair of vertices i and j the shortest path from i to j that goes through the vertex v. If no such path exists, the answer is . Describe an algorithm that takes a graph G= (V; E) and vertex v as input parameters and computes values L(i; j) that represent...

  • for this graph starting at vertex H. Ties should be resolved by whichever vertex is alphabetically...

    for this graph starting at vertex H. Ties should be resolved by whichever vertex is alphabetically earlier. list the order the vertices are removed during Dijkstra's algorithm for this graph starting at vertex H. Also list any update to the known distance values for neighboring vertices (including the initial update from infinity to a known value). Also include a list of edges (pairs of vertices) in the tree that is formed as part of the traversal in Dijkstra's algorithm. Ties...

  • Consider the graph given above. Use the nearest neighbor algorithm to find the Hamiltonian circuit starting at vertex C.

    Consider the graph given above. Use the nearest neighbor algorithm to find the Hamiltonian circuit starting at vertex C. a. List the vertices in the Hamiltonian circuit in the order they are visited. Do not forget to include the starting vertex at both ends. b. What is the total weight along the Hamiltonian circuit?

  • PYTHON ONLY Implement the Dijkstra’s Shortest path algorithm in Python. A graph with 10 nodes (Node...

    PYTHON ONLY Implement the Dijkstra’s Shortest path algorithm in Python. A graph with 10 nodes (Node 0 to node 9) must be implemented. You are supposed to denote the distance of the edges via an adjacency matrix (You can assume the edge weights are either 0 or a positive value). The adjacency matrix is supposed to be a 2-D array and it is to be inputted to the graph. Remember that the adjacency list denotes the edge values for the...

  • Find the list of vertices following the breadth-first traversal of the graph below starting from vertex A.

    Find the list of vertices following the breadth-first traversal of the graph below starting from vertex A. (Note: when two or more nodes are equally as likely to be selected, select the one that comes first alphabetically). Enter your answer as a list of nodes with no space (or any other separator) between them. 

  • Consider the graph below. Use Dijkstra's algorithm to find the shortest path from vertex A to vertex C

    Consider the graph below. Use Dijkstra's algorithm to find the shortest path from vertex A to vertex C. Write your answer as a sequence of nodes with no blank spaces or any separators in between, starting with the source node: What's the weight of the shortest path? 

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