Euler vs. Hamiltonian path or circuit for a bus route - algorithm

Let's say that we have to pick up and drop off children at different stops along a bus route. Would a Euler path and circuit be more practical, or a Hamiltonian path or circuit for a mapping algorithm?

In the optimal case, you want to visit each bus stop exactly once. So, it's a Hamiltonian path by definition.

Related

bi-directional really gives shortest path?

I was told by the cracking interview book that bi-directional algorithm give shortest path between 2 points in graph.
I don't get why it is guaranteed shortest path. Doesn't collision point change depended on vertexs' queuing order during breadth-first search?
thx
Doesn't collision point change depended on vertexs' queuing order during breadth-first search?
Yes, it does. However, whichever node ends up being chosen, it will connect one of the shortest paths between the source and target nodes.
So if there are multiple such choices, it can be through any of them depending on queueing order as you said. But you are guaranteed that the resulting path will be of the same optimal length.

Algorithm like Bellman-Ford, only for multiple start, single destination?

Algorithms like the Bellman-Ford algorithm and Dijkstra's algorithm exist to find the shortest path from a single starting vertex on a graph to every other vertex. However, in the program I'm writing, the starting vertex changes a lot more often than the destination vertex does. What algorithm is there that does the reverse--that is, given a single destination vertex, to find the shortest path from every starting vertex?
Just reverse all the edges, and treated destination as start node. Problem solved.
If this is an undirected graph: I think inverting the problem would give you advantages. View the actual destination as the start and find the shortest path from that to all of the other nodes in the graph. By doing this you can use the traditional pathing algorithms.

Shortest path with constraints on traversing edges

While working on a project I've stumbled upon a graph algorithms problem I
haven't been able to solve. The problem is as follows:
You have a directed, weighted graph and want to find the shortest path between a
start node and an end node while visiting specified nodes (very much like
Find the shortest path in a graph which visits certain nodes).
However, along with nodes and edges, this graph also has the notion of "items",
which reside at nodes and you "pick up" when you enter that node. Now there is an extra constraint that edges can only be
traversed if you have obtained the necessary items, I, for that particular edge.
Think of it as a key for a door; you need to obtain a key before being able to
pass through the door.
I can only think of brute-force methods that blow up exponentially. Can anyone think of anything better or point me to a place where this problem is solved? Or maybe convince me that this is "hard" (computationally speaking)? Thanks for any help!
This problem is NP-HARD to solve optimally. There's a simple reduction from the Hamiltonian path problem:
Put unique items on each vertex of the original graph. Construct an sink vertex connected only to the destination vertex. Let the edge between these two vertices require all of the items.
You can try a modified version of the saving algorithm. It's a heuristic to solve the vehicle routing problem. Maybe you can reverse it and create a pick up function for the wanted keys. It's use for a delivery and shortest path problem.

Shortest path from single source to single destination in a graph

My graph contains no such edges which connect a vertex to itself. There is only one edge between two vertices. From Wikipedia i got to know about some algorithm which are used for calculating shortest path based on the given conditions. One of the most famous algorithm is Dijkstra's algorithm, which finds a shortest paths from source vertex to all other vertices in the graph.
But by using Dijkstra's algorithm, i am unnecessary exploring all the vertices, however my goal is just to find shortest path from single source to single destination. Which strategy should i use here? So that i need not to explore all other vertices.
One of my approach is to use bidirectional bfs. By bidirectional bfs i mean to apply two bfs one from source node, another one from destination node. As soon as for the first time when i find any same child in both tree,i can stop both bfs .Now the path from source to that child union path from child to destination would be my shortest path from source to destination.
But out of all the approaches described by Wikipedia and bidirectional bfs, which one suits best for my graph?
It depends if there's any apparent heuristic function that you could use or if you don't have any further usable information about your graph.
Your options are:
BFS: in general case or if you don't care about computation time that much.
Dijkstra (Dijkstra "is" BFS with priority queue): if your edges have weights/prices (non negative) and you care about CPU time.
A* (A* "is" Dijkstra with heuristic function - e.g. distance on a city map): if you want it to be really fast in average case, but you have to provide good heuristic function.
For some graph problems it may be possible to use Dynamic programming or other algorithmic tools. It depends on a situation. Further information can be found in tutorials from http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=alg_index ...
From Introduction to Algorithms (CLRS) second edition, page 581 :
Find a shortest path from u to v for a given vertices u and v. If we solve the single-source problem with source vertex u, we solve this problem also. Moreover, no algorithms for this problem are known that run asymptotically faster than the best single-source algorithms in the worst case.
So, stick to Dijkstra's algorithm :)
The Wikipedia article spells out the answer for you:
If we are only interested in a shortest path between vertices source and target, we can terminate the search at line 13 if u = target.
You could use Dijkstra's algorithm and optimize it in the way that you stop exploring paths that are already longer than the shortest you found so far. Because those are not going to be shorter (provided that you don't have negative weighs on your edges).

What is the problem name for Traveling salesman problem(TSP) without considering going back to starting point?

I would like to know what is the problem name for TSP w/o considering the way of going back to starting point and what is the algorithm to solve this.
I looked into Shortest path problem but that is not what I am looking for, the problem only find the shortest path from 2 assigned points. But what I am looking for is the problem which we give n points and inputting only 1 starting point. Then, find the shortest path traveling all points exactly once. (end point can be any point.)
I also looked into Hamiltonian path problem but it seems not to solve my defined problem but rather find whether there is Hamiltonian path or not.
I've found the answer to my question in this book. It is the same with Computer wiring problem which occurs repeatedly in the design of computers and other digital systems. The purpose is to minimize the total wire length. So, it is indeed a minimum length Hamiltonian path.
What the book suggests is to create a dummy point whose distances to every other points is 0. Therefore, the problem becomes an (n+1)-city symmetric TSP. After solving, just delete dummy point and then the minimum length Hamiltonian path is solved and we can get the TSP path without returning back the start point.
If I understand correctly, you want to find the shortest path (that starts from some vertex s) and goes through all the nodes in the graph without visiting the same node twice. A simpler problem, is the hamiltonian path problem. It asks, like you said, weather there exists such a path or not. Since that problem is NP-hard, and it's easier than your problem, solving your problem is at least NP-Hard. Well, that isn't true because your problem is not a decision problem. But what it does say is that we can almost be sure that there is no polynomial algorithm for your problem.
You can resort to approximation. There is a pretty cool approximation for the metric TSP here: http://en.wikipedia.org/wiki/Travelling_salesman_problem#Metric_TSP.
Using Asymmetric TSP Solver (if any)
If a start node has to be specified, as mentioned by #Vivek Payasi,
you can build a typical TSP problem with weights otherwise normal except for the arcs that flow into the start node. Set their weights to 0. This will produce an asymmetric travelling salesman problem(ATSP).
Now if you have an exact ATSP solver, this will look for the shortest cycle.
When it's done, remove the arc with weight 0 from the solution. This will look like a path you're looking for.
*All this relies on an efficient ATSP solver.
Using a plain Symmetric TSP Solver Instead
If you want to avoid using ATSP solver, refer to this answer: https://stackoverflow.com/a/59354134/7470152
The bottom line is: fix start node & an end node, add a dummy node whose arcs are connected to both node with weight 0 and then treat it like a normal TSP problem. Repeat this for all (n-1) end nodes, and choose the shortest one.

Resources