Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

// function to determine level of each node starting
// from x using BFS
void printLevels(vector<int> graph[], int V, int x)
{
// array to store level of each node
int level[V];
bool marked[V];

// create a queue
queue<int> que;

// enqueue element x
que.push(x);

// initialize level of source node to 0
level[x] = 0;

// marked it as visited
marked[x] = true;

// do until queue is empty
while (!que.empty()) {

// get the first element of queue
x = que.front();

// dequeue element
que.pop();

// traverse neighbors of node x
for (int i = 0; i < graph[x].size(); i++) {
// b is neighbor of node x
int b = graph[x][i];

// if b is not marked already
if (!marked[b]) {

// enqueue b in queue
que.push(b);

// level of b is level of x + 1
level[b] = level[x] + 1;

// mark b
marked[b] = true;
}
}
}

// display all nodes and their levels
cout << "Nodes"
<< " "
<< "Level" << endl;
for (int i = 0; i < V; i++)
cout << " " << i << " --> " << level[i] << endl;
}

// Dirver Code
int main()
{
// adjacency graph for tree
int V = 8;
vector<int> graph[V];

graph[0].push_back(1);
graph[0].push_back(2);
graph[1].push_back(3);
graph[1].push_back(4);
graph[1].push_back(5);
graph[2].push_back(5);
graph[2].push_back(6);
graph[6].push_back(7);

// call levels function with source as 0
printLevels(graph, V, 0);

return 0;
}

What I have tried:

package bfsjava;


public class BFSJAVA {


public static void printLevels(vector <int>[] graph, int V, int x)
{
// array to store level of each node
int[] level = new int[V];
boolean[] marked = new boolean[V];

// create a queue
queue<int> que = new queue<int>();

// enqueue element x
que.push(x);

// initialize level of source node to 0
level[x] = 0;

// marked it as visited
marked[x] = true;

// do until queue is empty
while (!que.empty())
{

// get the first element of queue
x = que.front();

// dequeue element
que.pop();

// traverse neighbors of node x
for (int i = 0; i < graph[x].size(); i++)
{
// b is neighbor of node x
int b = graph[x][i];

// if b is not marked already
if (!marked[b])
{

// enqueue b in queue
que.push(b);

// level of b is level of x + 1
level[b] = level[x] + 1;

// mark b
marked[b] = true;
}
}
}

// display all nodes and their levels
System.out.print("Nodes");
System.out.print(" ");
System.out.print("Level");
System.out.print("\n");
for (int i = 0; i < V; i++)
{
System.out.print(" ");
System.out.print(i);
System.out.print(" --> ");
System.out.print(level[i]);
System.out.print("\n");
}
}

// Dirver Code
public static int Main()
{
// adjacency graph for tree
int V = 8;
vector<int> graph[V];

graph[0].push_back(1);
graph[0].push_back(2);
graph[1].push_back(3);
graph[1].push_back(4);
graph[1].push_back(5);
graph[2].push_back(5);
graph[2].push_back(6);
graph[6].push_back(7);

// call levels function with source as 0
printLevels(graph, V, 0);

return 0;
}

}
Posted
Updated 13-May-18 1:46am

You don't. Converting code between such dissimilar languages never results in good code in the target language: it results in code that works - sometimes - but is difficult to maintain or even understand. And you have to have a very good understanding of both the source and target language.

Instead, use the way the code works as a guide and write a new application using the target language structures and methods.
Instead of finding code that might fit your homework question and trying to get it to work so you can hand it in, solve the problem yourself: you will get better code, which means a better mark - and you will learn something in the process!
 
Share this answer
 
Comments
CPallini 13-May-18 5:39am    
5.
nonabs 13-May-18 5:49am    
Thanks for your help...but may i know what is the equivalent of vector <int>[] graph, in java? can i use ArrayList instead?
A little research goes a long way: Vector (Java Platform SE 8 )[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900