The problem is following we have to find path from A to C that goes thru node B or following example graph A-G-F-B-L-C.
Now to implement from A to C is easy using BFS, but I don't know how to make sure that this path passes thru B ?
By 'path' you probably mean 'simple path' - a path with no repeating vertices.
First, ensure that A, B, and C are connected.
An A-...-B-..-C path exists iff:
There is no cut-vertex that splits A, B, and C into 3 different components
A is not a cut-vertex that splits B and C into different components
C is not a cut-vertex that splits B and A into different components
First run a bfs to reach the intermediate node, then run a bfs from that intermediate node to your desired goal node.
Related
I have an undirected graph and i want to list all possible paths from a starting node.
Each connection between 2 nodes is unique in a listed path is unique, for example give this graph representation:
{A: [B, C, D],
B: [A, C, D],
C: [A, B, D],
D: [A, B, C]}
some listed path starting from A
A, B, C, D, A, C in this path we have a connection between
A and B but we can't have a connection between B and A
I can't accomplish it using the existing algorithm that i know like DFS .
Any help will be very appreciated .
The simplest way would be to recursively try each neighbor and combine all the results.
This assumes there are no loops - if you allow loops (as in your example) there will be infinitely-many paths. In this case, you can make a path-generator by limiting the path-length to check for, then looping over all possible path-lengths.
A probably most intuitive way would be to, as previously suggested, iterate over all neighbours of each potential starting point until all paths are exhausted.
However, the risk with this method is that it tends to loop forever if the graph has cycles. This can be mitigated by using a list of visited vertices (or, probably preferable in your case, visited edges)
In pseudocode, that might give something like this:
paths = []
for node in graph
visited = []
path = [node]
add node and path to stack-or-queue
while node on stack-or-queue
pop node and path
for edges of node
if edge is not visited
add edge to visited
add neighbour to path
add neighbour and path to stack-or-queue
add path to paths
It produces an algorithm of relatively high complexity, so be sure to test it well to avoid crap.
Writing it recursively might be easier, although it removes the possibility of easily changing between DFS and BFS.
This is the problem I got stumped on the exam:
Given a directed graph G = (V,E), and let A and B be partition of V(union of A and B is V and they are disjoint). I want to find a set of paths such that:
1) Every path contains exactly one node in A
2) Each node in A is the first node of corresponding path
3) For each path, the last node is in B
4) All paths are node-disjoint
Given A,B, and G=(B,E), I have to design a poly-time algorithm to decide whether such a set of paths exist.
The observation I made is that if I have a cut (A,B) and then look at the number of arcs going from A to B, then the number of arcs must be at least |A| for such a set of paths to exist. Also, every node in A must be adjacent to at least one of nodes in B. Also, once a path crosses the cut from A to B, you can't cross from B to A because of the constraint (1), but what's difficult is how to find node-disjoint paths among the vertices in B... How should I approach this?
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.
I go the un-directed graph G an my goal is to find all the possible chains longest than N of nodes in a one to one relation.
For example:
In the the next Graph the "chains" of length more than 2 of nodes in one to one relation are:
- d -> e -> f -> g
- c -> k -> l -> m
So what is the best approach or algorithm to solve this problem ?
If you want to find all paths so that each vertex in it has a degree <=2, then the simple approach may be as follows.
Remove all vertices with degree >2 from your graph. You are left with a graph with each vertex having a degree <=2. It is easy to prove that every connected component of such a graph is either a simple way, either a simple loop, and it is easy to distinguish them (for example, running a DFS from one node and seeing whether you ever return to it).
So, every component that is a path is a path you look for. Every component that is a loop is also a path you look for, or can be easily converted to such a path by removing an edge or a vertex, depending on whether you allow a loop as the needed path.
What would be the best way to visualize DAG's nodes and edges in a text form/file? The node can be referred by its name.
A good way that always worked fine for me is this:
In the first line put all node names (maybe also the total number of nodes).
After that, at each line put first the node name and then all nodes the current node has a directed edge.
Example:
A B C D
A B C
B A D
C D
D A
That works of course if all edge weights are equal (or none exist).
If you also want to use weights, one possible solution would be like this:
Example:
A B C D
A B(3) C(4)
etc