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.

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 two nodes 1 step away from 2 (i.e. 4 and 5).

countNodesWithDist(3, 2) should return 2 since there are two nodes 2 steps away from 3 (i.e. 2 and 4).

Write a main function that creates a graph and demonstrates the method above. Make sure your graph covers as many edge case scenarios as you can think of and call the method with multiple combinations of starting vertex and distance to show that it works in all cases, and print out the results.

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

Code:

/*We can make use of standard Dijikstra's algorithm here , although the graph is not weighted but
we could assign weight of 1 to every edge and store the distance of each vertex from the source
and the compare the distances which are equal to distance.

We are using the adjacency listr representation of the graph here

Time Complexity - O(ElogV) where E is number of edges and V are the number of vertices.
*/

#include <queue>
#include <vector>
#include <iostream>
using namespace std;


vector<pair<int,int> > adj[101];
int dis[101]; // dis array to store the distances from source
bool vis[101]; // vis array to keep track of visited vertices
int vertices,edges;

struct comp
{
bool operator()(pair<int,int> &a, pair<int,int> &b)
{
return a.second > b.second;
}
};

priority_queue<pair<int,int>, vector<pair<int,int> > , comp> pq;

int countNodeWithDistance(int source,int distance){
int i, u, v, w, sz;
for (i = 1; i <= vertices; i++)
dis[i] = 1<<20;
dis[source] = 0;
pq.push({source, 0});

// standard dijikstra algorithm using priority queue
while (!pq.empty())
{
u = pq.top().first;
pq.pop();
if (vis[u])
continue;
sz = adj[u].size();
for (i = 0; i < sz; i++)
{
v = adj[u][i].first; //vertex
w = adj[u][i].second; // cost
  
// comparing the distance for current node from source if its not visited yet
//If its lesser than already computed distance than we will update it with new value

//You could replace w with 1 since the weight is 1 only in each case
if (!vis[v] && dis[u] + w < dis[v])
{
dis[v] = dis[u] + w;
pq.push({v, dis[v]});
}
}

vis[u] = 1;
}

int count=0;
for (int j = 1; j <= vertices; j++)
{
if(dis[j]==distance){
count++;
cout<<"Node "<<j<<" at distance "<<dis[j]<<"\n";
}
}

return count;
}

int main()
{
// take vertices, edges and source as input
int i, u, v, w, sz, source,distance;


  

cin >> vertices >> edges;
  
for (i = 0; i < edges; i++)
{
cin >> u >> v;
adj[u].push_back({v, 1});
adj[v].push_back({u, 1});
}
  
cin >> source>>distance;

cout<<"Nodes at distance "<<distance<<" from source "<<source<<" are : "
<<countNodeWithDistance(source,distance);

return 0;
}

/*We can make use of standard Dijikstras algorithm here , although the graph is not weighted but we could assign weight of 1priority_queue<pair int, int>, vector<pair<int, int> >, comp> pq; int countNodeWithDistance(int source, int distance) { int ivis[u] = 1; int count=0; for (int j = 1; j <= vertices; j++) if(dis[j]==distance) { count++; cout<<Node <<j«< at distancefor (i = 0; i < edges; i++) cin >> U >> V; adj[u].push_back({v, 1}); adj[v].push_back({u, 1}); cin >> source>>distance; cout<

Input:

Awwa 11

Output:

Node 3 at distance 1 Node 4 at distance 1 Nodes at distance 1 from source 1 are : 2

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
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