Is there a counter-example for this algorithm finding the Euler Path in a Eulerian Graph? - graph-algorithm

The following is the given algorithm for finding a Euler Path in a Eulerian Graph. However, it is said that there is an counter example with less than 10 vertices. The given Eulerian Graph is undirected and every vertex has even degree and it will start and end at the same vertex.
1. Perform a DFS traversal of G and number the vertices in DFS-preorder.
2. Re-initialize all vertices and edges of G as unused.
3. Produce a cycle as follows:
Start from the vertex with preorder number 1 (computed in step 1), and
repeatedly go to the vertex with highest preorder number possible along
an unused edge.
Stop when all edges incident to the current vertex are used.
I have been trying vertices from 6 to 9 for the last 3 days and I really couldn't come up with one example. Any help is appreciated! Thank you.

It's a little pedantic based on the definition that a Euler graph is a graph where each vertex has even degrees, so what if we considered that the graph was unconnected?
A---C E---G
| | | |
B---D F---H
To run DFS on different components - there would need to be a step prior to #1 which discovers the complete graph.
The following graph would also not work as the algo would take the path: {0,3,4,7,1,3,2,1,0} missing 5,6.

I am posting this as an answer because I didnt know how to show the graph correctly in comment as you could have seen.
Well correct me if i m wrong but wont the algo be struck for following graph-
A---B
\ /
C
/ \
D---E
With DFS- C A B D E
Now as C is node number 1 we will start from it and will have to visit it again to go to other cycle.
Similar graphs with 2 or more cycles with common node will give error if what i understood of your code is correct.
PS- Can anyone tell how start newline in comment

Related

Return path - Bug Algorithms

I am trying to develop a simple algorithm in a MBSE tool to obtain a path between a start and an end points.
Here a summary:
Example
I have all informations regarding the start and end point, as well as I know everything for each polygon points (x and y positions, distance to the next point, indexes). What I do at the moment is to check the intersection between the m-line (Start-End line) and each separate polygon line,** one at the time** and to return the intersection points sorted by distance to the start point and the index where this intersection is found. (see Picture)
I think this is similar to the Bug2 algorithm, however I know in advance all the coordinates.
I would like to obtain the green path shown in the picture: [End, E, A, 29, B, C, 53, 52, 51, D, Start]. Ideally this path is the shortest to reach the end point. The problem that I am having at the moment is that I cannot extract the path and to check the shortest distance.
Any good ideas on how I could do that? I am missing a good logic to do that. Any suggestion/articles would be really appreciated. Thanks in advance.
(I am assuming: -all point sorted in counterclockwise order -I have to followthe polygon edges, I cannot cross the polygons)
I am able to found the intersection points, and the index where the intersections are happening. This information is local for each polygons, polygons are not ordered. I have tried to slice arrays and try to merge together the coordinates, however I don't like this approach and was looking for something more clean to merge together the local informations available for each polygons and to make them a path. I have explored Dijkstra algorithm to solve the issue, but maybe there is also something else.

Efficiently Finding all paths between 2 nodes in a directed graph - RGL Gem

I am struggling to find 1 efficient algorithm which will give me all possible paths between 2 nodes in a directed graph.
I found RGL gem, fastest so far in terms of calculations. I am able to find the shortest path using the Dijkstras Shortest Path Algorithm from the gem.
I googled, inspite of getting many solutions (ruby/non-ruby), either couldn't convert the code or the code is taking forever to calculate (inefficient).
I am here primarily if someone can suggest to find all paths using/tweaking various algorithms from RGL gem itself (if possible) or some other efficient way.
Input of directed graph can be an array of arrays..
[[1,2], [2,3], ..]
P.S. : Just to avoid negative votes/comments, unfortunately I don't have inefficient code snippet to show as I discarded it days ago and didn't save it anywhere for the record or reproduce here.
The main problem is that the number of paths between two nodes grows exponentially in the number of overall nodes. Thus any algorithm finding all paths between two nodes, will be very slow on larger graphs.
Example:
As an example imagine a grid of n x n nodes each connected to their 4 neighbors. Now you want to find all paths from the bottom left node to the top right node. Even when you only allow for moves to the right (r) and moves up (u) your resulting paths can be described by any string of length 2n with equal number of (r)'s and (u)'s. This will give you "2n choose n" number of possible paths (ignoring other moves and cycles)

ELKI OPTICS pre-computed distance matrix

I can't seem to get this algorithm to work on my dataset, so I took a very small subset of my data and tried to get it to work, but that didn't work either.
I want to input a precomputed distance matrix into ELKI, and then have it find the reachability distance list of my points, but I get reachability distances of 0 for all my points.
ID=1 reachdist=Infinity predecessor=1
ID=2 reachdist=0.0 predecessor=1
ID=4 reachdist=0.0 predecessor=1
ID=3 reachdist=0.0 predecessor=1
My ELKI arguments were as follows:
Running: -dbc DBIDRangeDatabaseConnection -idgen.start 1 -idgen.count 4 -algorithm clustering.optics.OPTICSList -algorithm.distancefunction external.FileBasedDoubleDistanceFunction -distance.matrix /Users/jperrie/Documents/testfile.txt -optics.epsilon 1.0 -optics.minpts 2 -resulthandler ResultWriter -out /Applications/elki-0.7.0/elkioutputtest
I use the DBIDRangeDatabaseConnection instead of an input file to create indices 1 through 4 and pass in a distance matrix with the following format, where there are 2 indices and a distance on each line.
1 2 0.0895585119724274
1 3 0.19458931684494
2 3 0.196315720677376
1 4 0.137940123677254
2 4 0.135852232575417
3 4 0.141511023044586
Any pointers to where I'm going wrong would be appreciated.
When I change your distance matrix to start counting at 0, then it appears to work:
ID=0 reachdist=Infinity predecessor=-2147483648
ID=1 reachdist=0.0895585119724274 predecessor=-2147483648
ID=3 reachdist=0.135852232575417 predecessor=1
ID=2 reachdist=0.141511023044586 predecessor=3
Maybe you should file a bug report - to me, this appears to be a bug. Also, predecessor=-2147483648 should probably be predecessor=None or something like that.
This is due to a recent change, that may not yet be correctly presented in the documentation.
When you do multiple invocations in the MiniGUI, ELKI will assign fresh object DBIDs. So if you have a data set with 100 objects, the first run would use 0-99, the second 100-199 the third 200-299 etc. - this can be desired (if you think of longer running processes, you want object IDs to be unique), but it can also be surprising behavior.
However, this makes precomputed distance matrixes really hard to use; in particular with real data. Therefore, these classes were changed to use offsets. So the format of the distance matrix now is
DBIDoffset1 DBIDoffset2 distance
where offset 0 = start + 0 is the first object.
When I'm back in the office (and do not forget), I will 1. update the documentation to reflect this, provide 2. an offset parameter so that you can continue counting starting at 1, 3. make the default distance "NaN" or "infinity", and 4. add a sanity check that warns if you have 100 objects, but distances are given for objects 1-100 instead of 0-99.

A* circular path finding algorithm with restrictions

I have a road map represented as a directed graph of junctions and links leading from one junction to another, each link is weighted with it's own traversal time (the time it takes to cross the link) and im asked to find an algorithm to get from junction A to junction B and back from junction B to junction A so that the total path cost (in time) takes no longer than 10% more time than the optimal path cost (that is the path cost returned by A* algorithm) while keeping the time overlaps of the path to B and the path from B to a minimum, that is if t(x,y) represents the time to cross link (x,y) i need to bring to minimum the sum of t(x,y) + t(y,x) for the links that overlap.
the algorithm should be optimal for the problem at hand and complete (it should also be efficient) and probably use some variants of A* like A*epsilon and the likes...
does anyone have a clue how to go about this problem?
i was thinking of representing the states of this problem as (junction,flag) where flag indicates whether the current node is a part of a path that already passed junction B and the goal state is (A,True) and then using A*epsilon on this... but i don't know how to take into account the time overlap issue.. i guess what im suggesting is not the way im intended to solve this.
any help would be greatly appreciated :)

Does a Given Network has a Unique Min-Cut?

Let G = (V, E) be a network with s and t being the source and the sink. Let f be a maximum flow in G. Find an algorithm that determines whether there exists a unique min-cut in G.
I have managed to find a similar question on this site:
Determining the uniqueness of a min-cut
A summary of the answer given there:
Find all the vertices reachable from s in the residual graph and we've found a min-cut (S,T) in G.
Look at the same residual graph, starting at t. Look at the group of vertices reachable from t in the reverse direction of the arrows (meaning all the vertices which can reach t).
This group is also a min-cut.
If that cut is identical to your original cut, then there is only one. Otherwise, you just found 2 cuts, so the original one can't possibly be unique.
I don't understand why if the cut is identical to the original cut then the cut is unique.
Who can promise us that there is no other min-cut ?
Thanks in advance
Actually, I don't quite understand that solution. But in the original question, the second answer provided by davin is absolutely correct.
I just copy and paste it here
Given a minimum S-T cut, (U,V) with cut-edges E', we make one simple observation:
If this minimum cut is not unique, then there exists some other minimum cut with
a set of cut-edges E'', such that E'' != E'.
If so, we can iterate over each edge in E', add to its capacity, recalculate the
max flow, and check if it increased.
As a result of the observation above, there exists an edge in E' that when
increased, the max flow doesn't increase iff the original cut is not unique.
some explanation of my own:
What you need to prove actually is
there exists an edge in E' that when increased, the max flow doesn't increase
<=>
the original cut is not unique
=>:
You increase the capacity of edge e by 1, calculate the new max flow and it remains the same, which means that e is not in the new min-cut. (if e is in, according to the property of min-cut, f(e)=capacity of e, which leads to an increase). Since e is not in the new min-cut, it is also a min-cut of the original graph which has the same volume with the cut we know.Thus, the original cut is not unique.
<=:
The original cut is not unique(Let's call them E and E'), which means you can find an edge e that is in E but not in E'. Then you just increase the capacity of e by 1. When calculating the min-cut of the new graph, E' is already there. Since E' doesn't contain edge e, max flow remains the same with no doubt.
Hope you understand :)
another option to prove by contradiction the first way:
->:
let's say there's a single minimal (S,T) cut with cut edges E'.
After increasing the capacity of edge e which belongs to E' by 1 and finding that the max flow remains the same, leads that e is not in the new min-cut. (if e is in E', according to the property of min-cut the max flow would be increased).
However at the beginning we said the e is in E' - contradiction
The algorithm that you talked about is more efficient than the suggested ones.
The algorithm:
For graph G=(V,E)
Find maximum flow in the graph, and let R be the last residual graph.
Run BFS from s (find nodes reachable from s), lets call them X
Run BFS from t with reversed edges (find nodes that there is a path to t), lets call them Y.
if X + Y = V ('+' as in union) return TRUE, else FALSE
A short explanation:
in step 2 we find the nodes that determine the minimum cut (X, V/X).X is the set of all nodes reachable from s in our last residual graph. In step 3 we find the set of nodes from which t is reachable in the last residual graph. This set defines the cut (V-Y,Y) which is the minimum-cut closest to t. In step 4, is go the same cut from both ends (X + Y = V), then the graph has a unique minimum cut.
The complexity is mainly finding the maximum flow. With Edmonds Karp O(|E|^2|V|), and BFS O(|E| + |V|).
The complexity of the suggested answer will be O(|V||E|^3).
So far, all discussion of the algorithm presented in the original post (both here an d in the post from which it is copied) seem to stop short of actually proving that if the two minimum cuts are the same, then it is the unique minimum cut. But this isn't hard!
OK, so what are these two minimum cuts? We run a maximum flow algorithm and look at the residual graph. The first cut is (S,T=V-S) where S is the set of all nodes that can be reached from the source using only edges with residual capacity. The second cut is (V-T,T) where T is the set of all nodes that can reach the sink using only edges with residual capacity.
If these two cuts are different, then clearly there is more than one minimal cut. But if they are the same, then we can use the technique described by laike9m to show that this is the only minimum cut. Why? Well, by the definitions of S and T in the previous paragraph, each edge e=(v0->v1) in the cut comes with a path s->v0 and a path v1->t that have residual capacity. Thus, if we increase the capacity of e, we know that we will increase the maximum flow. Since this is true for every edge e in the cut, this means that this minimum cut is unique.

Resources