Implement Breadth First Search using Java: I don't understand some of the code - search

class CheckBFS {
public static String bfs(Graph g){
String result = "";
//Checking if the graph has no vertices
if (g.vertices < 1){
return result;
}
//Boolean Array to hold the history of visited nodes (by default-false)
boolean[] visited = new boolean[g.vertices];
for(int i=0;i<g.vertices;i++)
{
//Checking whether the node is visited or not
if(!visited[i])
{
result = result + bfsVisit(g, i, visited);
}
}
return result;
}
public static String bfsVisit(Graph g, int source, boolean[] visited) {
String result = "";
//Create Queue for Breadth First Traversal and enqueue source in it
Queue<Integer> queue = new Queue<>(g.vertices);
queue.enqueue(source);
visited[source] = true;
//Traverse while queue is not empty
while (!queue.isEmpty()) {
//Dequeue a vertex/node from queue and add it to result
int current_node = queue.dequeue();
result += String.valueOf(current_node);
//Get adjacent vertices to the current_node from the array,
//and if they are not already visited then enqueue them in the Queue
DoublyLinkedList<Integer>.Node temp = null;
if(g.adjacencyList[current_node] != null)
temp = g.adjacencyList[current_node].headNode;
while (temp != null) {
if (!visited[temp.data]) {
queue.enqueue(temp.data);
visited[temp.data] = true; //Visit the current Node
}
temp = temp.nextNode;
}
}//end of while
return result;
}
public static void main(String args[]) {
Graph g = new Graph(5);
g.addEdge(0,1);
g.addEdge(0,2);
g.addEdge(1,3);
g.addEdge(1,4);
System.out.println("Graph1:");
g.printGraph();
System.out.println("BFS traversal of Graph1 : " + bfs(g));
System.out.println();
Graph g2 = new Graph(5);
g2.addEdge(0,1);
g2.addEdge(0,4);
g2.addEdge(1,2);
g2.addEdge(3,4);
System.out.println("Graph2:");
g2.printGraph();
System.out.println("BFS traversal of Graph2 : " + bfs(g2));
}
}
hello,guys! I know this may sound stupid but I am very new to coding and I have no one in real life to seek help for.
My question is: could anybody explain bfsVisit method to me?
especially the code below, so we created a queue to store all the nodes/vertices that we have visited and the part below is how we "extract" nodes/ vertices from the doublyLinkedList one by one and put them into the queue?
DoublyLinkedList.Node temp = null;
if(g.adjacencyList[current_node] != null)
temp = g.adjacencyList[current_node].headNode;
while (temp != null) {
if (!visited[temp.data]) {
queue.enqueue(temp.data);
visited[temp.data] = true; //Visit the current Node
}
temp = temp.nextNode;
}
I am not sure if I get the logic of all the two methods in class CheckBFS
why should we separate the methods into bfs and bfsVisit?
So bfs is the primary method that count/store/record the nodes we visited and bfsVisit is actually the method that help us traverse the array of linkedlist?
Thanks very much!!!!

could anybody explain bfsVisit method to me?
bfsVisit intends to visit all nodes that can be reached from the given source node. Here are some aspects of that part of the algorithm:
visited is important, as it makes sure that the algorithm will not fall into an infinite loop when it encounters a cycle in the graph. Imagine for instance three nodes A, B, and C, where A connects to B, and B connects to C, and C connects to A. We don't want the algorithm to just keep running in circles, revisiting the same nodes over and over again. And so when A is visited the first time, it is marked as visited in the visited boolean array. When the algorithm would meet A again, it would skip it.
queue is something that is typical for breadth-first traversals: it ensures that the nodes that are closest (in terms of edges) to the source node are visited first. So first the nodes that are only one edge apart from the source node are visited, then those that are two edges apart, ...etc. To achieve this order we need a first-in-first-out data structure, which is what queue is.
especially the code below, so we created a queue to store all the nodes/vertices that we have visited and the part below is how we "extract" nodes/ vertices from the doublyLinkedList one by one and put them into the queue?
Yes. The aspect of the doubly linked list is not that important: it happens to be the data structure that the Graph class uses to give access to a node's neighboring nodes. But that is an implementation detail of Graph and is not essential for the breadth-first algorithm. What matters is that somehow you can get hold of the neighboring nodes. This could have been a vector or an array, or any collection data type that could provide you with the neighbors. It is determined by the Graph class.
temp represents that list of neighbors. I find the name of that variable not helpful. Its name could have been more descriptive, like neighbor.
That inner loop intends to loop over all direct neighbors of a given node current_node. For each of the neighbors it is first assured that it was not visited before. In that case it is marked as visited and put at the end of the queue. The queue will hold it until it becomes the first in the queue, and then it will play the role of current_node so that it expends to its own neighbors, ...etc.
temp = temp->nextNode just picks the next neighbor from the list of neighbors. As I stated before, this is an implementation detail and would have looked different if the collection of neighbors would have been provided in a different data structure. What matters is that this loop goes through the list of neighbors and deals with those that were not visited before.
why should we separate the methods into bfs and bfsVisit?
Some of the reasons:
bfsVisit can only visit nodes that are connected to the source node. If the graph happens to be a graph with disconnected components. In that case it is impossible for bfsVisit to find all nodes of the graph. That's why we have the loop in bfs: it makes sure to run bfsVisit on each component of the graph, as otherwise we would only have visited one component.
bfs actually does not perform a BFS traversal itself. I should stress here that the name bfs for the main function is thus a bit misleading: BFS is a term that only makes sense for the collection of nodes in one component. The loop we find in bfs would actually look exactly the same if the purpose was to perform a dfs traversal! That loop's purpose is only to ensure that all nodes of a graph are visited even when the graph has multiple components. Notice how bfs does not use a queue for its own purposes. It just iterates the nodes in their numerical order without any regard of edges. This is entirely different from bfsVisit which looks for connected nodes, following edges.
bfsVisit is also a kind of helper function: it receives arguments that the main function bfs did not get itself as arguments, but manages itself:
visited is the most important of those. bfs starts out by the assumption that no nodes have been visited at all. bfsVisit however needs to tell bfs which nodes it had visited once it completes.
source is the starting point for bfsVisit: it is always one of the nodes that belong to a graph component that was not visited before. This is different from bst which just chooses itself which nodes to visit without any specific node given by the caller.
I hope this clarifies it.

Related

Running time for a shortestP alg in unweighted undirected graph in python3

For this kind of problem I thought it would have been better make some BFS-like implementation. I don't know why, after some running time testing, it came out a plot that resembles an exponential function.
So I began to think of my code's correctness: is it really efficient? Can you help me to make a running time analysis for a good algorithm?
I've plotted the log in base 1.5 for the x-axis (since in the code I use a list of the first 30 powers of 1.5 as number of vertices input in a random graph generetor). Still looks exponential...
def bfs_short(graph, source, target):
visited = set()
queue = collections.deque([source])
d={}
d[source]=0
while queue:
u = queue.pop()
if u==target:
return d[target]
if u not in visited:
visited.add(u)
for w in graph[u]:
if w not in visited:
queue.appendleft(w)
d[w]=d[u]+1
The thing is... I didn't posted also the benching input trials which also may cause problems but first of all I want to be sure that the code works fine... solving the problems related to testing is my final purpose.
A problem in your code is that your queue does not take in account the distance to the origin in order to prioritize the next vertex it will pop.
Also, collections.deque is not a priority queue, and thus will not give you the closest unvisited vertex seen so far when you pop an element from it.
This should do it, using heapq, a built-in heap implementation:
import heapq
def bfs_short(graph, source, target):
visited = set()
queue = [(0, source)]
heapq.heapify(queue)
while not queue:
dist, vertex = heapq.heappop(queue)
if vertex == target:
return dist
if vertex not in visited:
visited.add(vertex)
for neighbor in graph[vertex]:
if neighbor not in visited:
heapq.heappush(queue, (dist + 1, neighbor))
In this version, pairs are pushed in the queue. To evaluate and compare tuples, Python tries to compare their first element, then the second in case of equality, and on and on.
So by pushing dist(vertex, origin) as first member of the tuple, the smallest couple (dist, vertex) in the heap will also be the closest to the origin vertex.

Bounding Box intersection

To find elements that are intersecting a geometry I am using the example post by Jeremy in his blog http://thebuildingcoder.typepad.com/blog/2010/12/find-intersecting-elements.html. But the bounding box is always paralell to the axis X, Y and Z and this may cause a problem, like return elements that are not really clashing, because sometimes the bounding box it's not always coincident with the geometry because the family instance is rotated. Besides that, there is the problem that the bounding box will consider the geometry of the symbol and not the instance, and will consider the flipped geometry too, it means that the bounding box is bigger than I am looking for. Is there a way to get the real geometry that are in the currently view ? How can I solve this problem ?
There are many way to address this. Generally, when performing clash detection, you will always run a super fast pre-processing step first to determine candidate elements, and then narrow down the search step by step more precisely in following steps. In this case, you can consider the bounding box intersection the first step, and then perform post-processing afterwards to narrow down the result to your exact goal.
One important question is: does the bounding box really give you all the elements you need, plus more? Are you sure there are none missing?
Once that is settled, all you need to do is add post-processing steps applying the detailed considerations that you care about.
A simple one might be: are all the target element geometry vertices contained in the target volume?
A more complex one might involve retrieving the full solid of the target element and the target volume and performing a Boolean intersection between them to determine completely and exactly whether they intersect, are disjunct, or contained in each other.
Many others are conceivable.
I am using another strategy that is acess the geometry of the instance to verify if the face of the family instace are clashing with a closer conduit.
class FindIntersection
{
public Conduit ConduitRun { get; set; }
public FamilyInstance Jbox { get; set; }
public List<Conduit> GetListOfConduits = new List<Conduit>();
public FindIntersection(FamilyInstance jbox, UIDocument uiDoc)
{
XYZ jboxPoint = (jbox.Location as LocationPoint).Point;
FilteredElementCollector filteredCloserConduits = new FilteredElementCollector(uiDoc.Document);
List<Element> listOfCloserConduit = filteredCloserConduits.OfClass(typeof(Conduit)).ToList().Where(x =>
((x as Conduit).Location as LocationCurve).Curve.GetEndPoint(0).DistanceTo(jboxPoint) < 30 ||
((x as Conduit).Location as LocationCurve).Curve.GetEndPoint(1).DistanceTo(jboxPoint) < 30).ToList();
//getting the location of the box and all conduit around.
Options opt = new Options();
opt.View = uiDoc.ActiveView;
GeometryElement geoEle = jbox.get_Geometry(opt);
//getting the geometry of the element to acess the geometry of the instance.
foreach (GeometryObject geomObje1 in geoEle)
{
GeometryElement geoInstance = (geomObje1 as GeometryInstance).GetInstanceGeometry();
//the geometry of the family instance can be acess by this method that returns a GeometryElement type.
//so we must get the GeometryObject again to acess the Face of the family instance.
if (geoInstance != null)
{
foreach (GeometryObject geomObje2 in geoInstance)
{
Solid geoSolid = geomObje2 as Solid;
if (geoSolid != null)
{
foreach (Face face in geoSolid.Faces)
{
foreach (Element cond in listOfCloserConduit)
{
Conduit con = cond as Conduit;
Curve conCurve = (con.Location as LocationCurve).Curve;
SetComparisonResult set = face.Intersect(conCurve);
if (set.ToString() == "Overlap")
{
//getting the conduit the intersect the box.
GetListOfConduits.Add(con);
}
}
}
}
}
}
}
}
}
Can you please provide a complete minimal reproducible case so we can understand the exact context and analyse what can be done? Maybe you could include one axis-aligned junction box and one that is not, so we can see how ell versus how badly your existing algorithm performs. Thank you!
I summarised this discussion and the results to date in a blog post on filtering for intersecting elements and conduits intersecting a junction box.

Searching through a multi-branch graph and returning a path [C#]

In my situation I have territory objects. Each territory knows what other territories they are connected to through an array. Here is an visualization of said territories as they would appear on a map:
If you were to map out the connections on a graph, they would look like this:
So say I have a unit stationed in territory [b] and I want to move it to territory [e], I'm looking for a method of searching through this graph and returning a final array that represents the path my unit in territory [b] must take. In this scenario, I would be looking for it to return
[b, e].
If I wanted to go from territory [a] to territory [f] then it would return:
[a, b, e, f].
I would love examples, but even just posts pointing me in the right direction are appreciated. Thanks in advance! :)
Have you heard of Breadth-First Search (BFS) before?
Basically, you simply put your initial territory, "a" in your example, into an otherwise empty queue Q
The second data structure you need is an array of booleans with as many elements as you have territories, in this case 9. It helps with remembering which territories we have already checked. We call it V (for "visited"). It needs to be initialized as follows: All elements equal false except the one corresponding to the initial square. That is for all territories t, we have V[t] = false, but V[a] = true because "a" is already in the queue.
The third and final data structure you need is an array to store the parent nodes (i.e. which node we are coming from). It also has as many elements as you have territories. We call it P (for "parent") and every element points to itself initially, that is for all t in P, set P[t] = t.
Then, it is quite simple:
while Q is not empty:
t = front element in the queue (remove it also from Q)
if t = f we can break from while loop //because we have found the goal
for all neighbors s of t for which V[s] = false:
add s into the back of Q //we have never explored the territory s yet as V[s] = false
set V[s] = true //we do not have to visit s again in the future
//we found s because there is a connection from t to s
//therefore, we need to remember that in s we are coming from the node t
//to do this we simply set the parent of s to t:
P[s] = t
How do you read the solution now?
Simply check the parent of f, then the parent of that and then the parent of that and so on until you find the beginning. You will know what the beginning is once you have found an element which has itself as the parent (remember that we let them point to itself initially) or you can also just compare it to a.
Basically, you just need a empty list L, add f into it and then
while f != a:
f = P[f]
add f into L
Note that this obviously fails if there exists no path because f will never equal a.
Therefore, this:
while f != P[f]:
f = P[f]
add f into L
is a bit nicer. It exploits the fact that initially all territories point to themselves in P.
If you try this on paper with you example above, then you will end up with
L = [f, e, b, a]
If you simply reverse this list, then you have what you wanted.
I don't know C#, so I didn't bother to use C# syntax. I assume that you know it is easiest to index your territories with integers and then use an array to access them.
You will realize quite quickly why this works. It's called breadth-first search because you consider only neighbors of the territory "a" first, with trivially shortest path to them (only 1 edge) and only once you processed all these, then territories that are further away will appear in the queue (only 2 edges from the start now) and so on. This is why we use a queue for this task and not something like a stack.
Also, this is linear in the number of territories and edges because you only need to look at every territory and edge (at most) once (though edges from both directions).
The algorithm I have given to you is basically the same as https://en.wikipedia.org/wiki/Breadth-first_search with only the P data structure added to keep track where you are coming from to be able to figure out the path taken.

Polymorphism in node.js

I have a 2D matrix with 0s and 1s - respectively representing Water and Land. It is used to generate an animated gif with Perlin noise (moving water and waves clashing on shores...).
However I wish to refactor my code and use polymorphism in the following manner:
For each element in my matrix, based on the value, I wish to create a new WaterTile or LandTile (which will represent a 30x30 set of pixels that are either a mixture of blue for water, and some combinations of green/yellow/blue for land).
WaterTile and LandTile will be inherited from BaseTile (this is an abstract class), having just 2 vars (x, y for coordinates) and a draw() method (this method for the BaseTile class does nothing, its just there if it has to be defined).
Child classes will be overriding the draw() method.
Map will be a 2D array that whose elements will be WaterTiles and LandTiles.
After that, in my main method, I will have this (pseudocode for simplicity, i is index of row, j is index of element in that row):
foreach (row in matrix) {
foreach (element in row) {
if (matrix[i][j] == 0) map[i][j] == new WaterTile();
else map[i][j] == new LandTile();
}
}
After this I will need to invoke more methods for the LandTiles, and then have a new foreach loop, simply having
map[i][j].draw();//invoking draw method based on type
I know this can be done in other ways, but i wish to avoid having if statements that check for the type and call the draw method based on the type, and also practice clean code and learn something new.
If someone can provide me a simple example, ive looked at some already but havent found what I want.
Put the different actions inside the if statement you already have.
Or, just call the draw method, and let each instance do what it is does with draw.
Or encapsulate the if inside a function and use that.

Finding median in AVL tree

I have an AVL tree in which I want to return the median element in O(1).
I know I can save a pointer to it every time I insert new element without changing the runtime of the insertion (by saving the size of the subtrees and traversing until I find the n/2'th size subtree).
But I want to know if I can do this using the fact that in every insertion the median shifts "to the right", and in every deletion the median shifts "to the left".
In a more general manner: How can I keep track of the i'th element in an AVL tree using predecessor and successor?
Given an AVL (self balanced binary search tree), find the median. Remember you can't just take the root element even if the tree is balanced because even with the tree balanced you don't know if the median is exactly the root element on a left or right son. Iterative algorithm used to find the median of an AVL. This algorithm is based on a property of every AVL tree, you can get a sorted collection containing the elements of this tree using an in order traversal. Using this property we can get a sorted collection of nodes and then find the median. The complexity order of this algorithm is O(N) in time and space terms where N is the number of nodes in the tree.
public class AvlTreeMedian {
BinaryTreeInOrder binaryTreeInOrder;
public AvlTreeMedian() {
this.binaryTreeInOrder = new BinaryTreeInOrder();
}
public double find(BinaryNode<Integer> root) {
if (root == null) {
throw new IllegalArgumentException("You can't pass a null binary tree to this method.");
}
List<BinaryNode<Integer>> sortedElements = binaryTreeInOrder.getIterative(root);
double median = 0;
if (sortedElements.size() % 2 == 0) {
median = (sortedElements.get(sortedElements.size() / 2).getData() + sortedElements.get(
sortedElements.size() / 2 - 1).getData()) / 2;
} else {
median = sortedElements.get(sortedElements.size() / 2).getData();
}
return median;
}
}

Resources