Reputation: 331
.. I have to use a piece of code in java but I don't understand some parts of it.
The code uses methods (.isEmpty() etc. ) from a simple Queue i made in another document.
It is suppposed to investigate an array (which has linked lists in each address) and do some sort of processing with its values.
The problem is that i dont know what marked[s] = true; ,marked[t.v] = true; and parent[t.v] = k; are and how do they work as variables (?)
void BFS(int s)
{
Queue<Integer> Q = new Queue<Integer>();
marked[s] = true;
Q.put(s);
while (!Q.isEmpty())
{
k = Q.get();
for (Node t = adj[k]; t != null; t = t.next)
if (!marked[t.v]) {
marked[t.v] = true;
parent[t.v] = k;
Q.put(t.v);
}
}
}
}
edit: I wrote matrix instead of array, sorry.
Upvotes: 2
Views: 93
Reputation: 1439
marked[]
parent
and adj
are all arrays.
t
as you can see from the code, is a Node
object. That node object will have a member variable called v
. t.v
therefore fetches the value of the variable v
in the Node
object t
.
marked[t.v]
finds the element in the array with index equal to t.v
. e.g. if t.v
is equal to 0, then you are fetching marked[0]
which is the first element in the marked
array.
Upvotes: 1