Java - Graph critical links - java

I have a graph in which i have two nodes (0 and 6) and i have to cut the least edges possible so that they aren´t connected. For example in this graph
Being the nodes 0 and 6 the least edges that i have to cut are 2-7 and 3-7.
My idea was finding the shortest path between the both using bfs, i find one (0-2-7-6) but then i don´t know how to find the other (0-3-7-6). Even then i have no idea how choose the edges to cut.
It would be nice if someone could give me some pointers on this matter.

This problem seems very similar to that of finding articulation nodes within a graph. The technical definition of an articulation point, or a biconnected component is a node whose removal would cause a graph to be split in two.
The discovery of articulated nodes from a graph is a largely solved problem and you can find more details about it here: http://en.wikipedia.org/wiki/Biconnected_component
It seems to me that the way you would like to approach a problem like this in general would something along these lines:
1. Find all articulation points
2. Do a bfs from each node and determine articulation points along the path
3. Split graph at the articulation point, choosing the side with minimal edges
4. Continue until the two nodes are not connected
In the above example, 7 is the only articulation point and so you would snip the edges between 7, 2 and 3 since there are only two edges between 7 and the 0-4 graph and 3 edges between 7 and the 5,6,8 graph.
There is a more established algorithm for doing this (read: one that I didn't come up with) called Karger's algorithm that can solve your problem, albeit in n^2 time.
That algorithm works by effectively joining adjacent nodes to each other until there are only two nodes and then by counting the number of edges left between the two nodes. The number of edges is then the minimum number of cuts required to split the graph.
The way you would implement Karger's algorithm in your problem would just need the caveat that you should always be joining nodes to the two nodes you want to split. Additionally, in order to be able to go backward to the original edges you need to cut you should keep a reference to which nodes the edges originally belonged to.
There's a great visualization of Karger's algorithm here: http://en.wikipedia.org/wiki/Karger%27s_algorithm

What you want is a min s-t cut. The usual way to find one in a general graph is to run an algorithm like push relabel with source 0 and sink 6, which computes a min s-t cut as a byproduct of computing a maximum flow.
Karger's algorithm finds a min cut, i.e., it minimizes over s and t as well as cuts. Since s and t are fixed for you, Karger's is not appropriate. An articulation vertex is a vertex whose removal disconnects the graph. You are interested in removing edges, not vertices.

Related

Graph algorithm advise: find all affected paths between two nodes

I'm looking if there is a known solution for the task I have in mind
Given: Undirected graph with V > 100k nodes. Most of the vertices have a degree of 1-2, but some of them might have up to 10-20k edges.
I'm interested in all simple paths between two vertices or all paths ending on the start verticle limited by maxLength. Path can't contain the visited nodes but might end on the start node.
Goal: Occasionally some graph's edges are updated in batch. I'd like to find out what routes between vertices of interest were updated. Ideally in the form of Iterator but List is fine too.
Effectively I'm looking for a reactive way to find out about the affected routes.
The implementation could be in any language but Java/Kotlin specific is more preferred :)
Any references/relevant information would be very appreciated.
I wasn't sure if it's a question for CS StackExchange but it looks more relevant to SO.
To find paths between two nodes that do not share any nodes except the start and end.
Run Dijskta to find shortest simple path between start, end
Add large cost ( = largest integer for example ) to the in-edges of all nodes on shortest path, except start,end
Repeat until no new path found with cost less than largest integer
To find out what routes between vertices of interest were updated. This is trivial
- Multimap of edges, keyed by route index
- Vector of updated edges
- Loop over updated edges
- IF edge in multimap
- print index of affected route

Travelling Salesman and shortest path

I am currently working on a project involving a mixture of travelling salesman and shortest path. It goes as follows:
I am given a set of 9 vertices,all with positive coordinates in 2 space, (x,y), where x and y are positive real numbers. I am then asked to compute the shortest path a travelling salesman can take, and traverse through all the points. In essence, the graph is simply a collection of nodes with coordinates, and I have to arrange the edges, such that there is a connection from node 1 to node 9, that is the shortest possible one. It is assumed that I have already visited at the beginning node 1 (x1,y1), and need to visit each node once and only once. I will end on node 9, specified by (x9,y9).
I am writing the program in Java, although the professor said that the program can be written in any language.
I started by writing a node class, which represents each node, and has fields x,y which represent the x and y coordinates respectively. However, I am completely stumped as to how to perform the optimization.
I would greatly appreciate any assistance with respect to this problem.
Thank you very much, and I am excited to become a member of this community!
Euclidean TSP remains NP-hard, so just go for whatever algorithm for general TSP you want; for 9 vertices, the brute force solution compares 7! = 5040 permutations of the 7 intermediate vertices (since you start at 1 and end in 9 as per the problem description), so all you would need to do for that is generate all permutations of {2,...,8} and then just check the length of those paths.
If you want something slightly fancy, go for the dynamic programming approach by Held, Karp and separately Bellman which you can read up on in lots of places online, so I won't be posting how that works.

Performant Graph algorithm to find path from node to root in a directed graph

I have a directed graph with a special root node from which all other nodes are reachable.
It is quite easy to find an arbitrary algorithm to find all pathes from a giving node to the root, for example this solution (DFS) using LinkedHashSets.
Well, this algorithm works well for small graphs but with larger graphs it doesn't come to an end in a reasonable amount of time.
My example graph has 129 nodes and 200 edges. In my eyes not an extremly HUGE graph...
Can somebody give me a hint how to solve this problem efficiently?
Maybe we can make use of other properties of my graphs. They all are:
connected
directed with loops
have a designated starting node
have a designated end node
Well, not really - you cannot make it significantly more efficient, because the output size itself is huge. The number of paths is exponential in the number of nodes, have a look at this simple example:
V = {a, b, c}, E = {(a,b), (a,c), (b,c)}
This looks pretty simple, there are "only" 2 none trivial paths in the graph leading to c. a->c, a->b->c.
Now, consider adding a node d with edges (d,a), (d,b) - you will have 3 paths leading to c from the new root, d->a->b->c, d->a->c, d->b->c, still not too bad, but notice what happens when adding e with (e,d),(e,a), you will get one "family" of paths starting with e->d (3 above paths), and in addition, "family" of paths starting with "e->a" (2 paths). totaling in 5 paths. By repeating the process, you will get another two families, one with 5 paths, and the other with 3 paths. You probably start to understand that if you repeat the process k times, you will get #fibo(k) different paths, but the fibonacci number is very, very big for inputs such as 100 (~354*10^20, and keeps growing fast).
Thus, whatever you do - finding all paths in a graph is going to be inefficient, except for some "easy" graphs, such as trees.
tl;dr:
There are exponential number of paths leading to your target node, and finding all of them takes exponential time. DFS is a solid solution for this problem.

How to order graph's nodes such that the distance between two adjacent nodes is minimal

I was wondering if this problem is known in graph theory:
I have an undirected graph with no weights G=(V,A) and I want to place the nodes of this graph in a string so that directed nodes are placed as much close as possible. So for example:
Given this graph:
a,b;a,d;b,e;c,f;c,h;f,h;e,g;e,h.
where arcs are separated by ';'
I need to get to this solution:
a,b,d,e,g,h,c,f=2
where 2 is the maximum distance in the string a,b,d,e,g,h,c,f between two directed nodes.
Formally:
let d(v,u) be the distance between two nodes according to the graph.
Find an order v1,,v2,,vn,, such that max{d(vi,,vi+1,) } is minimal
Well, it seems you are facing a variation of the Hamiltonian Path Problem.
In this problem, given a graph - you are looking for a path that go through all vertices without repeating any node twice.
Note that a hamiltonian path is a perfect solution to your problem, and thus if your problem can be solved efficiently - so does the hamiltonoan path problem.
Unfortunately, there is no known polynomial solution for the hamiltonian path problem, and the problem is NP-Complete (so the general belief is such a (efficient) solution does not exist).
A brute force solution will be O(n!) - check all possible permutations, and choose the optimal one. This can be optimized using branch and bound techniques.

How to get the minimum cost of disconnecting some node from each other in a graph

In a given graph, i want to calculate the minimum cost of disconnecting some node from each other in a graph. Example: In this graph, let say i want to disconnect node A, node C and node F with each other by removing some edges among these nodes. i.e. by removing edge A-B and edge F-E, the nodes A, C and F will be disconnected. Here cost means the length of the edge which is being removed. in this example total minimum cost for disconnecting Node A, Node C and Node F from each other is 2+1=3.
Can somebody provide some hint. I am unable to categorize this problem, whether this is a kind of shortest path problem or minimum spanning tree problem?
This is called the multiterminal cut problem. Unfortunately there seems not to be a Wikipedia entry. The problem is, given a weighted graph and a subset of vertices called the terminals, compute the minimum-weight set of edges whose removal disconnects every pair of terminals. The bad news is that this problem is NP-hard, so if you really need an optimal solution on an instance that can't be brute forced, you'll probably have to turn to integer programming. The good news is that there's a simple 2-approximation algorithm (not the best factor known, but you may have to brush up on your linear programming and read the research literature to use the "better" ones), which can be achieved by taking the union of s-t cuts separating each terminal from the others.

Categories

Resources