Question

In: Computer Science

Write a C++ or Java program name that conducts the BFS traversal of a graph and...

Write a C++ or Java program name that conducts the BFS traversal of a graph and displays city names in the range of hop(s) from a starting city

Input format: This is a sample input from a user.

4

Monterey

LA

SF

SD

6

Monterey LA

LA SD

SD Monterey

SF Monterey

SF LA

SF SD

Monterey

2

The first line (= 4 in the example) indicates that there are four vertices in the graph. The following four lines describe the names of four cities. The next line (= 6 in the example) indicates the number of edges in the graph. The following six lines are the edge information with the “source city” and “destination city”. The following city (= Monterey in the example) is the starting city, and the last line (= 2 in the example) indicates the hops from the starting city. Thus, the problem is to list the city names that can be reached from the source in two hops. This is the graph with the input information.


Sample Run 0: Assume that the user typed the following lines

4

Monterey

LA

SF

SD

6

Monterey LA

LA SD

SD Monterey

SF Monterey

SF LA

SF SD

Monterey

2

This is the correct output. Your program should present the city names with the hop in the ascending order like below.  Note that LA can be reached from Monterey in one hop, Monterey in zero hop, and SD in two hops. For the problem, you can assume that the city name is always one word. Also, your program should consider only the minimum number of hops from the starting city.

LA:1

Monterey:0

SD:2

Sample Run 1: Assume that the user typed the following lines

4

Monterey

LA

SF

SD

6

Monterey LA

LA SD

SD Monterey

SF Monterey

SF LA

SF SD

LA

1

This is the correct output.

LA:0

SD:1

Sample Run 2: Assume that the user typed the following lines

5

Fresno

LA

SD

SF

NYC

6

SD LA

SD NYC

LA NYC

SF Fresno

SF SD

Fresno SD

SF

2

This is the correct output.

Fresno:1

LA:2

NYC:2

SD:1

SF:0

Solutions

Expert Solution

C++ code

#include <bits/stdc++.h>
using namespace std;

unordered_map<string, int> m1; //map to store integer corresponding to each city for ease of calculation
unordered_map<int, string> m2; // reverse mapping for m1(city corresponding to integer)
void bfs(int start, int n, vector<int> *adj, int hops)
{
queue<int> q; //bfs queue
bool visited[n] = {0};
q.push(start);
q.push(-1); // -1 is used as the delimiter to track the starting of new level
visited[start] = 1;
vector<pair<string, int>> ans; // ans vector for storing the cities reached
int level = 0;
while(!q.empty())
{
int top = q.front();
q.pop();
if(top == -1)
{
level++;
if(!q.empty() && level <= hops)
q.push(-1);
else
break;
}
else
{
ans.push_back(make_pair(m2[top], level));
for(int x: adj[top])
{
if(!visited[x])
{
q.push(x);
visited[x] = 1;
}
}
}
}

sort(ans.begin(), ans.end()); //sorting the ans vector according to city names
for(auto x: ans)
{
cout<<x.first<<" "<<x.second<<endl;
}
}

int main()
{
m1.clear();
m2.clear();
int vertices, edges;
cin>>vertices;
for(int i=0;i<vertices;i++)
{
string city;
cin>>city;
m1[city] = i; //mapping city to int
m2[i] = city; //reverse mapping
}

cin>>edges;
vector<int> *adj = new vector<int>[vertices]; //adjacency list for storing graph
for(int i=0;i<edges;i++)
{
string city1, city2;
cin>>city1;
cin>>city2;
int u = m1[city1];
int v = m1[city2];
adj[u].push_back(v);
}

string startcity;
cin>>startcity;
int start, hops;
start = m1[startcity];
cin>>hops;

bfs(start, vertices, adj, hops);
}


Related Solutions

Traversal tree program in c/c++
Traversal tree program in c/c++
Write a C program that demonstrate tree traversal. A. first is to ask the user to...
Write a C program that demonstrate tree traversal. A. first is to ask the user to enter data B. ask how many nodes THIS IS JUST A SAMPLE. PLEASE ANSWER IT IN COMPLETE CODE. See for the sample output. Sample Output: How many node do you have in your tree : 5 Enter root: 20 if you want to add to the left press 0 otherwise press 1 answer: 0 Enter next node: 10 if you want to add to...
Write a C++ or Java program that reads an input graph data from a user. Then,...
Write a C++ or Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number ofvertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2...
Write a program in java that detects if there is a cycle in an undirected graph...
Write a program in java that detects if there is a cycle in an undirected graph using DFS Two lines of input: 1. Two integers V for the number of vertices and E for the number of edges respectively. 2. List of integers that shows how the graph is connected. Ex input: 4 4 01020323 Output: Graph contains cycle Ex input: 5 4 01020314 Output: Graph doesn't contains cycle
Write a Java program that reads a name and displays on the screen.
Write a Java program that reads a name and displays on the screen.
Write a program in java that asks the name of the buyer and the number of...
Write a program in java that asks the name of the buyer and the number of shares bought. Your program must then calculate and display the following. sold stock/shares to the general public at the rate of $24.89 per share. Theres a 2 percent (2%) commission for the transaction. Outputs need Amount paid for buying the stock ($) Amount paid for the commission ($) Total amount ($)
Modifying the tree traversal C program shown below to do reversed tree traversal as defined below....
Modifying the tree traversal C program shown below to do reversed tree traversal as defined below. Reversed PreOrder: Root, Right, Left. Reversed InOrder:     Right, Root, Left. Reversed PostOrder: Right, Left, Root. The tree is: Root = 1, Left(1) = -2, Right(1) = -3; Left(-2) = 4, Right(-2) = 5; Left(-3) = 6, Right(-3)= 7; Left(5) = -8, Right(5)= -9; Left(7) = 10, Right(7) = 11; Left(11) = -12, Right(11) = -13; Left(-13) = 14. Your program should output the printed...
Write a C++ program to display toy name
Write a C++ program to display toy name
Traversal tree program
Traversal tree program
Write a complete Java program to print out the name bob
Write a complete Java program to print out the name bob
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT