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++ 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 C++ program to display toy name
Write a C++ program to display toy name
Write a complete Java program to print out the name bob
Write a complete Java program to print out the name bob
Tail of a File, C++ Program. write a program that asks the user for the name...
Tail of a File, C++ Program. write a program that asks the user for the name of a text file. The program should display the last 10 lines, or all lines if less than 10. The program should do this using seekg Here is what I have so far. #include<iostream> #include<fstream> #include<string> using namespace std; class File { private:    fstream file;    string name; public:    int countlines();    void printlines(); }; int File::countlines() {    int total =...
Write a program (in C, or Java, or C++, or C#) that creates three new threads...
Write a program (in C, or Java, or C++, or C#) that creates three new threads (besides the already existing main thread) and synchronizes them in such a way that each thread displays it's thread id in turn for 5 iterations. The output of the program should look like this: Thread 1 - iteration no. 1 Thread 2 - iteration no. 1 Thread 3 - iteration no. 1 Thread 1 - iteration no. 2 Thread 2 - iteration no. 2...
Using jGRASP, write a Java program named LastnameFirstname10.java, using your last name and your first name,...
Using jGRASP, write a Java program named LastnameFirstname10.java, using your last name and your first name, that does the following: Create two arrays that will hold related information. You can choose any information to store, but here are some examples: an array that holds a person's name and an array that hold's their phone number an array that holds a pet's name and an array that holds what type of animal that pet is an array that holds a student's...
Using jGRASP, write a Java program named LastnameFirstname09.java, using your last name and your first name,...
Using jGRASP, write a Java program named LastnameFirstname09.java, using your last name and your first name, that does the following: Declare an array reference variable called myFavoriteSnacks for an array of String type. Create the array so that it is able to hold 10 elements - no more, no less. Fill the array by having each array element contain a string stating one of your favorite foods/snacks. Note: Only write the name of the snack, NO numbers (i.e. Do not...
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT