Using BFS/DFS to solve programming task - c++

I am currently trying out Task #2 in the 2016/2017 COCI. Although I have attempted to solve this problem, I couldn't do it.
So, I looked at the solution and it said,
In order to solve this task, we need to find any path Barry can take
from the initial position to any position in the last column. We can
do this by using BFS or DFS algorithm, after which we need to
construct the path. Finally, all that’s left is to format the path
according to the task.
So I went ahead and studied the BFS and DFS algorithm. However, I am not sure how I can implement this algorithm into my program.
Although I can find certain elements in a tree with the algorithm, I don't know how to use it to find a pathway.
So can someone tell my, briefly, how to use the BFS/DFS algorithm to solve the programming problem?
Thanks in advance.
This is the contest page:
http://hsin.hr/coci/archive/2016_2017/contest1_tasks.pdf

What you could do is convert the whole map into a tree.
Here is a diagram I made to demonstrate what I exactly mean:Click here to view the image
Hope that made sense.

Related

How to implement "recalculate outside" in Blender

When importing CAD file like STL format file, sometimes, some of the facets are reversed so their normal vectors are directing inside the volume while others going outside. the "recalculate outside" comes in to fix the situation.
But, I am wondering how I can implement the function with CGAL. It would be appreciated if some guy show me the way about hint or code snippet.
Thanks in advance.
The Polygon Mesh Processing package introduced in CGAL 4.7 might help here.
In particular I think the function orient_polygon_soup() is what you need. Here is an example of how to use it.
You might also need the function polygon_soup_to_polygon_mesh()

DFS shortest path of a maze in C++

I'm having trouble figuring out how exactly to get this to work... I'm attempting to get the shortest path to the goal using a DFS. I know that BFS is better but I was asked to use DFS for this. As you can see, I attempt to make a comparison between all stacks that lead to the end to find the goal, but it does not work, only the first stack that leads to the goal is ever printed... I know somewhere I need to unvisit nodes, but I cannot figure out exactly how. Right now I do get a path to the goal, but not the shortest one. Any help with this would be greatly appreciated.
Writing a non-recursive DFS is possible by using your own stack, but I find the recursive solution to be more elegant. Here is a sketch of one:
DFS(vertex)
path.push_back(vertex)
visited[vertex] = true
if we found the exit
output path
else
for each neighbor v of vertex
if not visited[v]
DFS(v)
visited[vertex] = false
path.pop_back()

Creating a graph using Boost Graph library

I am new to graph theory and graph concept.
I am workign on something, that requires me to create a mesh(Undirected graphs) with n number of nodes.
Once the structure is created, I would be running various algorithms on the structure, to find a shortest path from a node to other.
No for this I have decided to use Boost graph librabry.
I read through the online documentation. The online documentation is good but at the same time not sufficient.
I went through various examples online and everywhere, they import the graph from Graphviz.
If i am not wrong, we have to manually draw or write a dot program to get a graph in Graphviz and import in .dot format(Please correct me if i am wrong)
But is there a way in Boost where I could create a graph, instead of importing it from GraphViz?
And I would let user to decide the number of vertices in it, instead of pre-defining it.
Any help would be very much appreciated.
Thanks a ton in advance.
It's maybe not very correct, but I am giving a response a gave before.
https://stackoverflow.com/a/3100220/202083
You can see how to programmaticaly add nodes and edges.
I hope this is enough for you to get started.

Directed acyclic graph

I have problems understanding the directed acyclic graph on page 9
http://mitpress.mit.edu/books/chapters/0262033844chap27.pdf
Someone who can explain it?
If it's a general understanding you require, you could think of it this way. It's "directed" because it has a direction. "acyclic" because it goes one way. Then, think of the graph as a way to navigate one way, in a direction.
If you consider this as applied to the storage of a dictionary as an example, it can be very useful. Rather than store every single word in the dictionary as a flat text file, you could instead store them as a DAG. The benefit of this is that it takes up much less space, and can be very fast to do look ups.
So, you would store a word like "hello" as a graph made up of different letters. Each letter would be a "node". From "h" you would say ok, where do I go from here? The graph directs you to "e", and from "e" to "l" and so on.
A "graph" therefore is a method of navigation, and "directed" and "acyclic" refer to how that navigation is done.
Hope this helps. My experience of DAGs is very word specific as I implemented it for a dictionary. I hope this contributes to your understanding. If others have better understanding or if I have misrepresented anything please do comment.

Graph-Drawing / TSP-Route-Drawing in C++ with "known" coordinates: How? Which Library/Tool?

i'm developing some kind of heuristics for a variation of the vehicle-routing-problem in C++.
After generating a solution, i want to plot this solution. The solution is a composite of various tours, all starting and ending at a common depot.
Therefore i have a vertex-set with all the coordinates and edges defined by two vertex-id's each. Furthermore i have all the distances between vertex-pairs of course.
It would be helpful to plot this in an extra-window opening in my program, but writing a plot to a graphics-file should be okay too.
What is an easy way to plot this? How would you tackle this?
First i tried to look for common graph-visualization packages (graphviz, tulip, networkx (python)), but i realized that all of them are specialized at graph-layouting (when there are no coordinates). Correct me when i'm wrong.
I don't know if it is possible to tell these packages that i already have the coordinates, helping the layouting-algorithms.
Next thing i tried is the CGAL library with geomview output -> no luck until now -> ubuntu crashes geomview.
One more question: Is it a better idea to use some non-layouting 2d-plot-libraries risking a plot, which isn't really good to view at (is there more to do than scaling?) or to use some layout-algorithm-based-libraries (e.g. graphviz, tulip, networkx), feed them with the distances between the vertices and hope the layouting-algorithms are keeping the distances while plotting in a good-to-view-at way?
If non-layouting-plotting is the way to do it: which library do you recommend?
If layout-based-plotting is the way to do it: how can i make use of the distances/coordinates in these libraries? And which library do you recommend?
Thanks for all your input!
Sascha
EDIT: I completed a prototype implementation using the PLplot library (http://plplot.sourceforge.net/). The results are nice and should be enough for the moment. I discovered and chosed this library because a related project (VRPH Software Package / Groer) used this plot and the source code was distributed. So the implementation was done in a short amount of time. The API is in my opinion bit awkward and low-level. Maybe there are some more modern (maybe not a c-based library) libraries out there? MathGL? Dislin? Maybe i will try them too.
The nice thing about drawing multiple tours in a vehicle routing problem is that "not so bad" algorithms tend to discover nice non-overlapping and divergent tours which is really good for the eye ;-)
It is not quite clear what you are trying to archive, but if I understand your question correctly, then you could do it using OpenGL. Having vertex coordinates, it should be fairly easy.
You can use Gnuplot with a input text file that contains your solution.
It is convenient to draw the points (vertex) then lines (agents paths) than link them.
To make the plot script easy, you can have a separate file for each vehicle, if the number
of vehicles is known.
check out:
http://www.cleveralgorithms.com/nature-inspired/advanced/visualizing_algorithms.html