Im taking a course in algorithms and data structers, and my instructor wants me to implement several data structers (such as BST, stack etc.), and algorithms (such as quick search, DFS, etc.).
I want to belive that I understand the basics, but everytime Im starting to plan the code I have the same difficulty:
here's my current assigment: my instructor wants me to implement a DFS (depth first search) for a directed graph (using c++).
my question is- how do I suppose to implement the graph? should I use adjacency matrix? or should I use adjacency list? neither this nor that??
so I asked my instructor, and his answare was this: "think of the graph as a black box"...
more confused than before, I rashed to stackoverflow, and here i am posting this question...
I dont look for someone to tell me how to implement DFS (or any other algorithm- I can google too!)- I just need someone to explain what should I get as input, and what should I provide as output?
I'll appreciate any comment! thanks!
What he means by a black box is just that you cannot see the nodes and how they connect before you do your DFS. You will probably just get the root node and your algorithm with have to explore from there. As for what you should output- that depends on the assignment. Are you looking for specific data? if not, perhaps a detail of which nodes were visited in which order.
Related
the title is a question I was asked in class, supposedly there is an answer, I guess you could use the map to sort the data before putting it into the tree but that's all I can think of.
putting this out there in-case someone has any other solutions and can point me in the right direction, I'm stumped atm and my tutor isn't the best when it comes to answering questions
I try to understand several weeks the principles of operation with clang
AST, but meanwhile I did not answer the main issue: how to walk on this
tree?
I read all guides which I found, studied doxygen documentation and even
watched couple of lectures on YouTube, however the understanding did not
come.
That I understood:
1) AST of a tree has no general type of nodes
2) For movement on a tree it is offered to use either RecursiveASTVisitor,
or matcher. The first recursively realizes bypass in depth, and the second
allows to look for those nodes which are interesting.
The problem is in what part of a tree I am in what in one of two options I
cannot learn. I do not know how to define at what moment my visitor passed
to other branch and at what moment continues to move to depth.
Ideally it would be desirable to know depth of the node visited by me. It is
possible?
I very much like a dump() function output because in it communications
between nodes are accurately shown. However how to receive it in pure form
(but not as the text) I do not know.
Generally, the question is as follows: whether I can construct the tree on
the basis of AST, but with uniform type of nodes and how to make it?
Recently I asked a question on Stack Overflow asking for help to solve a problem. It is a travelling salesman problem where I have up to 40,000 cities but I only need to visit 15 of them.
I was pointed to use Dijkstra with a priority queue to make a connectivity matrix for the 15 cities I need to visit and then do TSP on that matrix with DP. I had previously only used Dijkstra with O(n^2). After trying to figure out how to implement Dijkstra, I finally did it (enough to optimize from 240 seconds to 0.6 for 40,000 cities). But now I am stuck at the TSP part.
Here are the materials I used for learning TSP :
Quora
GeeksForGeeks
I sort of understand the algorithm (but not completely), but I am having troubles implementing it. Before this I have done dynamic programming with arrays that would be dp[int] or dp[int][int]. But now when my dp matrix has to be dp[subset][int] I don't have any idea how should I do this.
My questions are :
How do I handle the subsets with dynamic programming? (an example in C++ would be appreciated)
Do the algorithms I linked to allow visiting cities more than once, and if they don't what should I change?
Should I perhaps use another TSP algorithm instead? (I noticed there are several ways to do it). Keep in mind that I must get the exact value, not approximate.
Edit:
After some more research I stumbled across some competitive programming contest lectures from Stanford and managed to find TSP here (slides 26-30). The key is to represent the subset as a bitmask. This still leaves my other questions unanswered though.
Can any changes be made to that algorithm to allow visiting a city more than once. If it can be done, what are those changes? Otherwise, what should I try?
I think you can use the dynamic solution and add to each pair of node a second edge with the shortest path. See also this question:Variation of TSP which visits multiple cities.
Here is a TSP implementation, you will find the link of the implemented problem in the post.
The algorithms you linked don't allow visiting cities more than once.
For your third question, I think Phpdna answer was good.
Can cities be visited more than once? Yes and no. In your first step, you reduce the problem to the 15 relevant cities. This results in a complete graph, i.e. one where every node is connected to every other node. The connection between two such nodes might involve multiple cities on the original map, including some of the relevant ones, but that shouldn't be relevant to your algorithm in the second step.
Whether to use a different algorithm, I would perhaps do a depth-first search through the graph. Using a minimum spanning tree, you can give an upper and lower bound to the remaining cities, and use that to pick promising solutions and to discard hopeless ones (aka pruning). There was also a bunch of research done on this topic, just search the web. For example, in cases where the map is actually carthesian (i.e. the travelling costs are the distance between two points on a plane), you can exploit this info to improve the algorithms a bit.
Lastly, if you really intend to increase the number of visited cities, you will find that the time for computing it increases vastly, so you will have to abandon your requirement for an exact solution.
Before you start throwing links to wikipedia and blogs in my face, please hear me out.
I'm trying to find the optimal algorithm/function to do a dependency sort on... stuff. Each item has a list of its dependencies.
I would like to have something iterator-based, but that's not very important.
What is important is that the algorithm points out exactly which items are part of the dependency cycle. I'd like to give detailed error information in this case.
Practically, I'm thinking of subclassing my items from a "dependency node" class, which has the necessary booleans/functions to get the job done. Cool (but descriptive) names are welcome :)
It's normally called a topological sort. Most books/papers/whatever that cover topological sorting will also cover cycle detection as a matter of course.
I don't exactly get why is it so hard to find the dependecy cycle if there is any! you just have to check if there is any node you already passed over while appling bfs algorithm to find out all the dependecies. if there is one you just roll back the way you came down to revisit a node alll the way up and mark all the nodes until you reach the first visit at the specified node. all the ones in your pass will be marked as a cycle. (just leave a comment and i'll give a code to do that if you need)
I want to write a shortest path program. I know how the algorithm works, but I don't know where to start
Initially, I thought of using an adjacency matrix but then decided against it because of space. Now I think adjacency list would be better.
Can anyone suggest me a websites or tutorials how to start writing adjacency list to give the input to the program?
You might start with Boost::Graph, which will provide you both mechanisms for storing graph data and a structure for writing an algorithm that consumes that data.