[Math] Finding all nodes that are passed by all paths from A to B in an undirected graph

graph theory

I searched all over internet but couldn't find any particular info on this one, so i thought to give it a shot here.

Problem: We have an undirected unweighted graph $G$, and in this graph we have two nodes $A$ and $B$. Now lets take the set of all paths between $A$ and $B$ and call that set $\mathcal{P}$, further note these paths can pass each node either once or dont pass a node at all. Of course one path $P \in \mathcal{P}$ exists out of both nodes and edges, so let $N(P)$ be the set of nodes associated with the path $P$. Now I would like to know in a fast algorithmical way which nodes are left if we take the the intersection between all vertices of all paths between $A$ and $B$, so more mathematically i want to know the set $\bigcap_{P \in \mathcal{P}} N(P)$.

Example:

enter image description here

The green nodes are traveling through all paths between $A$ and $B$, so they are the nodes i look for. the intersection between all pathnodes therefor is the set of all green nodes and $A$ and $B$ might be included in that set as well, depending on the algorithm.

Best Answer

The vertices that you are looking for are all cut-vertices (or articulation points) of the graph. A cut vertex is a vertex that disconnects the graph/increases the number of connected components. However, not all cut-vertices are a solution. Any cut-vertex, whose removal causes $A$ and $B$ to become disconnected, will lie on every path from $A$ to $B$.

Edit: Another observation is that the desired vertices will lie on the shortest path from $A$ to $B$ (if they lie on any path from $A$ to $B$, then they also lie on the shortest path). You can find the shortest path in linear time using a BFS. Now, you can shortlist those vertices which are cut-vertices and lie on the shortest path from $A$ to $B$.

Related Question