Pathfinding algorithm constrained to one-way roads - algorithm

Say I have 5 "towns": A, B, C, D and E.
Now I want to figure out a path between these towns, but I am constrained to roads, for example A only has roads to B, C and E and B only has a road to C etc.
I can't visit a town twice and the roads are only one-way. I want to "visit" all the towns. How would I programmatically solve this?
I've already tried starting at A, then seeing what roads there are and checking each option, although all my tries so far at implementing this have failed.

Related

How to understand the random contraction algorithm?

I'm studying random contraction algorithm and could not understand how it works.
In the course it says that a graph G=(V,E) with n vertices, m edges. a minumum cut(A,B), are there are many vertices either on A's or B's sides. k=number of edges crossing (A,B).(call these edges F)
then it make the conclusion that an edge of F is contracted at some point, algorithm will not output(A,B).
Why is this? suppos there is a vertice C in A's side and a vertice D in B's side, so the edge(C,D) would belong to F, but even if C and D are to contract, we still have A and B, the algorithm can still return (A,B) right?
If G is a graph and (A, B) is a cut in the graph, then A and B are sets of nodes, not individual nodes in G. That is, there is no node in G called A or B. Rather, the nodes in G can be split apart into two groups, one of which we’ll call A and one of which we’ll call B. As a result, you can’t have the situation you’ve described.
Here’s another way to think about this. Suppose (A, B) is a minimum cut. You can then imagine that every node in the graph is painted red if it’s in the A side and blue if it’s in the B side. The randomized contraction algorithm will then find the cut (A, B) assuming that no red node is merged with a blue node. If nodes of opposite colors are merged, then the resulting cut won’t consist of all the red nodes on one side and all the blue nodes on the other.

Find minimum cost among nodes

Suppose I have three destinations A, B, and C and associate travel cost between A to B, B to A, A to C, C to A, B to C and C to B and all are different. I have to search the particular location among A, B, and C where the other two will come and the cost will minimize. e.g., A and B can come to C location. And How it can be optimized for N location?
I would claim that this problem is in O(|V|^{2}) and you would therefore have to check the travel cost for each node. Pick a node, check the travel cost of the other n-1 nodes to this node and do this for every node while maintaining the one with the least travel cost.

Minimum Spanning Tree with Additional Vertex

Does minimum spanning tree works for situation like this: If I want to go from A to B and I do not have to go to E, but the direct distance between A and B is larger than distance_AE + distanceEB, so I can go E first and then go to B. I'm not sure if the normal implementation of mst also works for this kind of graph. So if I want to find the mst of ABCD, but E is not included in this graph, how can I solve this?
I believe that you're confused about the basic problem: what you've posted is a contradiction. If E is not in the graph, then by definition dist(A, E) is undefined; for algorithmic purposes, it's Inf (infinity).
Yes, the MST algorithms work fine for this: the entire universe consists of (A, B, C, D).

How can I cast this into a graph algorithm?

Given a set of nodes A and a set of nodes B, where each node in A are connected to only one node in B. Now I want to select a minimum set of nodes in A that is connected to every nodes in B, while every selected node cannot be connected to each other.
I believe there is a certain way of solving this problem maybe with matching, independent set or whatever.
Although I haven't been able to prove that my algorithm is correct, I'm still putting it here.
If the nodes in A didn't have edges between them, then this would be a bipartite graph matching problem. Right now this has become a bipartite graph matching problem with the constraint the if some node in A is selected, it may disqualify some other nodes in A.
For ease of visualisation, imagine the set of A on one sides and B on the other side, similar to the visuals drawn for Bipartite matching. Now, add another set of nodes C between A and B. The condition for adding a node in C are as follows:
If there is a node in A (say a) with no edges in A, create a new node in C (say c), join a to c, and join all the corresponding nodes of a in B to c.
If there are two nodes in A (a1 and a2) which share an edge, create a new node in C (c) and create edges from a1 to c, a2 to c, and from c to all corresponding nodes in B.
Now we can use the Max-Flow algorithm to get the desired output. We should set the node capacities of each node in C to be 1 (the way to achieve this is by creating another intermediate layer D which is a copy of C, with edge capacities equal to 1 for edges between C and D)
Any help in making the proof rigorous or any counter examples would be helpful.

Algorithm for laying out a directed acyclic graph in memory to maxmise data locality

Say I have the edges
A -> C
A -> D
A -> E
B -> D
B -> E
To maximise data locality I'd arrange for the DAG to be stored in this order in memory (as an array) to minimise the distance between the node and it's dependencies.
C, A, D, E, B
so that A has a distance 1 to C, 1 to D, 2 to E.
And that B has a distance of 1 to E, and 2 to D.
Is there name for an algorithm that does this? If not, how would one implement this?
Looks like you want to linearize the DAG. I don't know whether you are using it for dependancy resolution. Topological_sorting looks familiar to your question. also the program tsort does very similer thing.
However it is dependancy linearization.
neel#gentoo:~$ tsort
C A
D A
E A
D B
E B
C
D
E
B
A
Which prints the order in which that tasks have to be performed. and it will possible not work if there is a cycle. its relevant as you mentioned its acyclic.
I dont know if there is any such algorithm for data locality ordering string or anything similar however It looks like your data locality string have some problem.
What if C is close(1) to A and is also close(1) to B and B is too far(4) from A how will you represent it with your data locality string ?
I don't now what exactly you want to do. If you want to liniarize dependency to perform tasks in proper order then do a topological sort.
Here is slightly different approach to improve locality:
http://ceur-ws.org/Vol-733/paper_pacher.pdf
The described algorithm seems to be closer to force-directed graph drawing algorithm than to topological sorting.
You should also read papers on in-memory graph databases such as imGraph

Resources