There are three types of nodes in my graph, red, blue, and black. For each red node I want to find a path to one black node (in whatever time). On the way, I cannot pass through another red node. How can I find all red nodes where there is a path to a black node that satisfies the above condition?
2026-04-03 17:02:50.1775235770
Graph Traversal
125 Views Asked by user616300 https://math.techqa.club/user/user616300/detail At
1
There are 1 best solutions below
Related Questions in GRAPH-THEORY
- characterisation of $2$-connected graphs with no even cycles
- Explanation for the static degree sort algorithm of Deo et al.
- A certain partition of 28
- decomposing a graph in connected components
- Is it true that if a graph is bipartite iff it is class 1 (edge-coloring)?
- Fake induction, can't find flaw, every graph with zero edges is connected
- Triangle-free graph where every pair of nonadjacent vertices has exactly two common neighbors
- Inequality on degrees implies perfect matching
- Proving that no two teams in a tournament win same number of games
- Proving that we can divide a graph to two graphs which induced subgraph is connected on vertices of each one
Trending Questions
- Induction on the number of equations
- How to convince a math teacher of this simple and obvious fact?
- Find $E[XY|Y+Z=1 ]$
- Refuting the Anti-Cantor Cranks
- What are imaginary numbers?
- Determine the adjoint of $\tilde Q(x)$ for $\tilde Q(x)u:=(Qu)(x)$ where $Q:U→L^2(Ω,ℝ^d$ is a Hilbert-Schmidt operator and $U$ is a Hilbert space
- Why does this innovative method of subtraction from a third grader always work?
- How do we know that the number $1$ is not equal to the number $-1$?
- What are the Implications of having VΩ as a model for a theory?
- Defining a Galois Field based on primitive element versus polynomial?
- Can't find the relationship between two columns of numbers. Please Help
- Is computer science a branch of mathematics?
- Is there a bijection of $\mathbb{R}^n$ with itself such that the forward map is connected but the inverse is not?
- Identification of a quadrilateral as a trapezoid, rectangle, or square
- Generator of inertia group in function field extension
Popular # Hahtags
second-order-logic
numerical-methods
puzzle
logic
probability
number-theory
winding-number
real-analysis
integration
calculus
complex-analysis
sequences-and-series
proof-writing
set-theory
functions
homotopy-theory
elementary-number-theory
ordinary-differential-equations
circles
derivatives
game-theory
definite-integrals
elementary-set-theory
limits
multivariable-calculus
geometry
algebraic-number-theory
proof-verification
partial-derivative
algebra-precalculus
Popular Questions
- What is the integral of 1/x?
- How many squares actually ARE in this picture? Is this a trick question with no right answer?
- Is a matrix multiplied with its transpose something special?
- What is the difference between independent and mutually exclusive events?
- Visually stunning math concepts which are easy to explain
- taylor series of $\ln(1+x)$?
- How to tell if a set of vectors spans a space?
- Calculus question taking derivative to find horizontal tangent line
- How to determine if a function is one-to-one?
- Determine if vectors are linearly independent
- What does it mean to have a determinant equal to zero?
- Is this Batman equation for real?
- How to find perpendicular vector to another vector?
- How to find mean and median from histogram
- How many sides does a circle have?
Now this depends on how your data structures are represented in some ways, but I would use the following algorithm. (To find all the paths at once)
Each node should be augmented with a variable which will store an edge to follow backwards to get to a black node.
Start with the set of black nodes. Make a queue containing all edges connected to them (store the edges as directed edges with the end being the black node).
Then for each edge in the queue check the start node of the edge. If the start node has been processed already, discard the edge. Otherwise check the color of the start node of the edge. If it is red, store the edge in the red node and add it to the set of processed vertices. If the node is blue store the edge in the node, add the blue node to the set of processed vertices and then add all other edges adjacent to the blue node (except for those connecting to processed vertices) to the queue (with their end node being set to our blue node). The start node of edges in the queue can never be black since we already processed all the black nodes, so if it were we would have discarded the edge at the start of the loop.
If you want to know if every red node has a path to a black node then also maintain a set of unprocessed nodes. If there remain nodes in this set after the queue of edges to check is empty, these nodes cannot reach a black node without traveling through a red node. Iterate through this set to see if there are any red nodes in the set.
At the end, we'll end up with a set of processed vertices, and the nodes in this set each store an edge that points you along a path (that doesn't go through red nodes) that ends at a black node.
If we use hash sets, this should be $O(E)$ I think since each edge is processed at most once. Well $O(V)+O(E)$ for sparse graphs, but for connected graphs $V$ is $O(E)$.
Edit:
Proof of correctness. By which I mean a proof of the following fact. A red vertex is in the set of processed vertices at the end of the algorithm if and only if there exists a path from the red vertex to a black vertex passing through only blue vertices.
Proof: We prove instead the equivalent statement: The set of processed vertices at the end of the algorithm is precisely the set of vertices with a path to a black vertex that doesn't pass through a red vertex.
First we show inductively that the set of processed vertices is a subset of the set of vertices with such a path. The initial set of processed vertices is the set of black vertices. Trivially these all have a path to a black vertex not passing through a red vertex, so the condition is true to start with. Then at each stage of the loop we consider an edge $(s,e)$ with $e$ already in the set of processed vertices (assume $s$ is unprocessed, since otherwise we discard the edge without changing the set of processed vertices). Hence there is a path from $e$ to a black vertex not passing through a red vertex. Moreover, we never add directed edges with a red target vertex to the queue since we only add edges whose targets are black or blue. Thus $e$ is not red. Hence adding the edge $s\to e$ to the path from $e$ to a black vertex that doesn't pass through a red vertex gives a path from $s$ to a black vertex not passing through a red vertex. Thus adding $s$ to the set of processed vertices preserves the property that all vertices in the set of processed vertices have a path to a black vertex not passing through a red vertex.
Conversely if a vertex $v$ has a path to a black vertex not passing through red vertices, define $d(v)$ to be the length of the shortest such path. We'll prove by induction on $d(v)$ that $v$ is processed at some point in the algorithm. If $d(v)=0$ $v$ is black, and it is processed at the beginning of the algorithm. Otherwise $d(v) > 0$, so let $w$ be the next vertex along the minimal length path from $v$ to a black vertex. Clearly $d(w)=d(v)-1$, so by our induction hypothesis $w$ is processed at some point in the algorithm. Since $w$ is not red when it was processed all of its (previously unprocessed) adjacent edges are added to the queue of edges to be processed. Thus the edge $v\to w$ is added to the queue when $w$ is processed, and therefore $v$ will be processed at some point (either when we come to the edge $v\to w$ or perhaps before that, but either way $v$ must be processed). Thus the induction holds. This completes the other half of the proof of our assertion. $\blacksquare$
It is also true that by following the edges stored at a vertex you will get a path to a black vertex not passing through a red vertex, but the proof of correctness is similar to the proof above, so I won't repeat myself.