Find a path from s to t using as few red nodes as possible Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Find a path passing through the minimum number of odd nodesDijkstra algorithm vs breadth first search for shortest path in graphAlgorithm to find diameter of a tree using BFS/DFS. Why does it work?Finding shortest path from a node to any node of a particular typeParallel algorithm to find if a set of nodes is on an elememtry cycle in a directed/undirected graphShortest path in unweighted graph using an iterator onlyShortest Path using DFS on weighted graphsCan a 3 Color DFS be used to identify cycles (not just detect them)?Find a path that contains specific nodes without back and forward edgesChecking if there is a single path that visits all nodes in a directed graphFind shortest path that goes through at least 5 red edges

Why do people hide their license plates in the EU?

Why did the US and UK choose different solutions to the problem of an undemocratic upper house?

What's the purpose of writing one's academic biography in the third person?

Why aren't air breathing engines used as small first stages

Generate an RGB colour grid

What is the meaning of the new sigil in Game of Thrones Season 8 intro?

Do I really need recursive chmod to restrict access to a folder?

Extract all GPU name, model and GPU ram

tcolorbox: Potential bug with duplicate label for hyperref link

Why are both D and D# fitting into my E minor key?

Sci-Fi book where patients in a coma ward all live in a subconscious world linked together

How do pianists reach extremely loud dynamics?

How can I make names more distinctive without making them longer?

Installing Debian packages from Stretch DVD 2 and 3 after installation using apt?

In predicate logic, does existential quantification (∃) include universal quantification (∀), i.e. can 'some' imply 'all'?

Can I cast Passwall to drop an enemy into a 20-foot pit?

An adverb for when you're not exaggerating

How come Sam didn't become Lord of Horn Hill?

How to tell that you are a giant?

Dating a Former Employee

Can a USB port passively 'listen only'?

Tht Aain’t Right... #2

Why didn't Eitri join the fight?

How do I keep my slimes from escaping their pens?



Find a path from s to t using as few red nodes as possible



Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Find a path passing through the minimum number of odd nodesDijkstra algorithm vs breadth first search for shortest path in graphAlgorithm to find diameter of a tree using BFS/DFS. Why does it work?Finding shortest path from a node to any node of a particular typeParallel algorithm to find if a set of nodes is on an elememtry cycle in a directed/undirected graphShortest path in unweighted graph using an iterator onlyShortest Path using DFS on weighted graphsCan a 3 Color DFS be used to identify cycles (not just detect them)?Find a path that contains specific nodes without back and forward edgesChecking if there is a single path that visits all nodes in a directed graphFind shortest path that goes through at least 5 red edges










5












$begingroup$


Was doing a little interview prep. Given an undirected graph G, such that each node is colored red or blue and |E|≥|V|, find a path in O(|E|) time such that starting and ending at 2 blue nodes, s and t, that you pass through as few red nodes as possible.



Initial Impressions: Since |E|≥|V|, O(|E|) time would include O(|E|+|V|), which means the solution likely uses BFS or DFS. Modifying the graph such that causing the all red nodes must be forced down a directed path of some long length (after making the whole graph directed) in order to use out-of-the-box BFS seems not viable, as it would mean constructing a new graph would be along O(|E||V|) time.



Another method I toyed around with was propagating values to nodes based on the safest path to that node while doing a DFS search, but not all values were guaranteed to update.



I still want to try to solve this myself, but I'm really stuck right now. Was wondering if there were any hints I could get. There are much simpler ways of doing this if it weren't for the O(|E|) time. Djikstras with creating some edge weights would work, but wouldn't be within the time bound.










share|cite|improve this question









$endgroup$







  • 1




    $begingroup$
    Try a variant of BFS in which you first find all red nodes reachable only via blue nodes, and so on.
    $endgroup$
    – Yuval Filmus
    Apr 1 at 20:22















5












$begingroup$


Was doing a little interview prep. Given an undirected graph G, such that each node is colored red or blue and |E|≥|V|, find a path in O(|E|) time such that starting and ending at 2 blue nodes, s and t, that you pass through as few red nodes as possible.



Initial Impressions: Since |E|≥|V|, O(|E|) time would include O(|E|+|V|), which means the solution likely uses BFS or DFS. Modifying the graph such that causing the all red nodes must be forced down a directed path of some long length (after making the whole graph directed) in order to use out-of-the-box BFS seems not viable, as it would mean constructing a new graph would be along O(|E||V|) time.



Another method I toyed around with was propagating values to nodes based on the safest path to that node while doing a DFS search, but not all values were guaranteed to update.



I still want to try to solve this myself, but I'm really stuck right now. Was wondering if there were any hints I could get. There are much simpler ways of doing this if it weren't for the O(|E|) time. Djikstras with creating some edge weights would work, but wouldn't be within the time bound.










share|cite|improve this question









$endgroup$







  • 1




    $begingroup$
    Try a variant of BFS in which you first find all red nodes reachable only via blue nodes, and so on.
    $endgroup$
    – Yuval Filmus
    Apr 1 at 20:22













5












5








5


1



$begingroup$


Was doing a little interview prep. Given an undirected graph G, such that each node is colored red or blue and |E|≥|V|, find a path in O(|E|) time such that starting and ending at 2 blue nodes, s and t, that you pass through as few red nodes as possible.



Initial Impressions: Since |E|≥|V|, O(|E|) time would include O(|E|+|V|), which means the solution likely uses BFS or DFS. Modifying the graph such that causing the all red nodes must be forced down a directed path of some long length (after making the whole graph directed) in order to use out-of-the-box BFS seems not viable, as it would mean constructing a new graph would be along O(|E||V|) time.



Another method I toyed around with was propagating values to nodes based on the safest path to that node while doing a DFS search, but not all values were guaranteed to update.



I still want to try to solve this myself, but I'm really stuck right now. Was wondering if there were any hints I could get. There are much simpler ways of doing this if it weren't for the O(|E|) time. Djikstras with creating some edge weights would work, but wouldn't be within the time bound.










share|cite|improve this question









$endgroup$




Was doing a little interview prep. Given an undirected graph G, such that each node is colored red or blue and |E|≥|V|, find a path in O(|E|) time such that starting and ending at 2 blue nodes, s and t, that you pass through as few red nodes as possible.



Initial Impressions: Since |E|≥|V|, O(|E|) time would include O(|E|+|V|), which means the solution likely uses BFS or DFS. Modifying the graph such that causing the all red nodes must be forced down a directed path of some long length (after making the whole graph directed) in order to use out-of-the-box BFS seems not viable, as it would mean constructing a new graph would be along O(|E||V|) time.



Another method I toyed around with was propagating values to nodes based on the safest path to that node while doing a DFS search, but not all values were guaranteed to update.



I still want to try to solve this myself, but I'm really stuck right now. Was wondering if there were any hints I could get. There are much simpler ways of doing this if it weren't for the O(|E|) time. Djikstras with creating some edge weights would work, but wouldn't be within the time bound.







graphs






share|cite|improve this question













share|cite|improve this question











share|cite|improve this question




share|cite|improve this question










asked Apr 1 at 17:42









user-2147481704user-2147481704

454




454







  • 1




    $begingroup$
    Try a variant of BFS in which you first find all red nodes reachable only via blue nodes, and so on.
    $endgroup$
    – Yuval Filmus
    Apr 1 at 20:22












  • 1




    $begingroup$
    Try a variant of BFS in which you first find all red nodes reachable only via blue nodes, and so on.
    $endgroup$
    – Yuval Filmus
    Apr 1 at 20:22







1




1




$begingroup$
Try a variant of BFS in which you first find all red nodes reachable only via blue nodes, and so on.
$endgroup$
– Yuval Filmus
Apr 1 at 20:22




$begingroup$
Try a variant of BFS in which you first find all red nodes reachable only via blue nodes, and so on.
$endgroup$
– Yuval Filmus
Apr 1 at 20:22










2 Answers
2






active

oldest

votes


















6












$begingroup$

To solve this, you need to use $BFS$. But first, manipulate $G$ so that the path will always favor blue vertices.



The solution has 2 parts:



  1. Use $DFS$ on blue vertices only, to find all all-blue connected components. Let's denote each such component by $v'$. Now, each blue $v in v'$ will be "compressed" to a single vertex $u$, and an edge $(u,x)$ will be added for every $x in N(v')$.
    Note any such $x$ is necessarily red.
    This step costs $O(V+E) = O(E)$, since $DFS$ is $O(V+E)$, and you have at most $V$ blue vertices, which make no more than $E$ new edges to add.

Step 1 means all paths that are blue-only will be free. On the new graph, the $BFS$ will only consider the edges which pass through a red vertex.



  1. Use $BFS$ from $s$. That length of the path to $t$ will essentially be the shortest path under the constraint of least red vertices in the path.





share|cite|improve this answer











$endgroup$








  • 2




    $begingroup$
    the graph is undirected, so step 1 should be to find all all-blue connected components, not SCCs, right?
    $endgroup$
    – Kevin Wang
    Apr 1 at 23:27










  • $begingroup$
    @KevinWang yes, I will fix it
    $endgroup$
    – lox
    Apr 2 at 8:18


















1












$begingroup$

Convert $G$ to a directed graph $G'$ where we have two edges $(u,v)$ and $(v,u)$ in $G'$ for every edge $u,v$ in $G$. Let the length of $(u,v)$ be 1 if $v$ is a red node and 0 otherwise. Now run Dijkstra's algorithm on $G'$ from the starting node $s$ to the ending node $t$.





It is clear that the shortest path thus found passes as few red nodes as possible.



While we are running Dijkstra's algorithm, we are in one of two kinds of stages alternatively. One kind of stage is when we are exploring towards red nodes. The other kind of stage is when we are exploring towards blue nodes. Since each edge is checked/visited as most twice, the running time is $O(|E|)$.






share|cite|improve this answer











$endgroup$












  • $begingroup$
    Yeah that definitely works, but the runtime of Dijkstra's is O(|E| + |V|log|V|) which is more than O(|E|).
    $endgroup$
    – user-2147481704
    Apr 1 at 22:26










  • $begingroup$
    As explained, the running time is $O(|E|)$.
    $endgroup$
    – Apass.Jack
    Apr 3 at 0:36












Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "419"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcs.stackexchange.com%2fquestions%2f106337%2ffind-a-path-from-s-to-t-using-as-few-red-nodes-as-possible%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









6












$begingroup$

To solve this, you need to use $BFS$. But first, manipulate $G$ so that the path will always favor blue vertices.



The solution has 2 parts:



  1. Use $DFS$ on blue vertices only, to find all all-blue connected components. Let's denote each such component by $v'$. Now, each blue $v in v'$ will be "compressed" to a single vertex $u$, and an edge $(u,x)$ will be added for every $x in N(v')$.
    Note any such $x$ is necessarily red.
    This step costs $O(V+E) = O(E)$, since $DFS$ is $O(V+E)$, and you have at most $V$ blue vertices, which make no more than $E$ new edges to add.

Step 1 means all paths that are blue-only will be free. On the new graph, the $BFS$ will only consider the edges which pass through a red vertex.



  1. Use $BFS$ from $s$. That length of the path to $t$ will essentially be the shortest path under the constraint of least red vertices in the path.





share|cite|improve this answer











$endgroup$








  • 2




    $begingroup$
    the graph is undirected, so step 1 should be to find all all-blue connected components, not SCCs, right?
    $endgroup$
    – Kevin Wang
    Apr 1 at 23:27










  • $begingroup$
    @KevinWang yes, I will fix it
    $endgroup$
    – lox
    Apr 2 at 8:18















6












$begingroup$

To solve this, you need to use $BFS$. But first, manipulate $G$ so that the path will always favor blue vertices.



The solution has 2 parts:



  1. Use $DFS$ on blue vertices only, to find all all-blue connected components. Let's denote each such component by $v'$. Now, each blue $v in v'$ will be "compressed" to a single vertex $u$, and an edge $(u,x)$ will be added for every $x in N(v')$.
    Note any such $x$ is necessarily red.
    This step costs $O(V+E) = O(E)$, since $DFS$ is $O(V+E)$, and you have at most $V$ blue vertices, which make no more than $E$ new edges to add.

Step 1 means all paths that are blue-only will be free. On the new graph, the $BFS$ will only consider the edges which pass through a red vertex.



  1. Use $BFS$ from $s$. That length of the path to $t$ will essentially be the shortest path under the constraint of least red vertices in the path.





share|cite|improve this answer











$endgroup$








  • 2




    $begingroup$
    the graph is undirected, so step 1 should be to find all all-blue connected components, not SCCs, right?
    $endgroup$
    – Kevin Wang
    Apr 1 at 23:27










  • $begingroup$
    @KevinWang yes, I will fix it
    $endgroup$
    – lox
    Apr 2 at 8:18













6












6








6





$begingroup$

To solve this, you need to use $BFS$. But first, manipulate $G$ so that the path will always favor blue vertices.



The solution has 2 parts:



  1. Use $DFS$ on blue vertices only, to find all all-blue connected components. Let's denote each such component by $v'$. Now, each blue $v in v'$ will be "compressed" to a single vertex $u$, and an edge $(u,x)$ will be added for every $x in N(v')$.
    Note any such $x$ is necessarily red.
    This step costs $O(V+E) = O(E)$, since $DFS$ is $O(V+E)$, and you have at most $V$ blue vertices, which make no more than $E$ new edges to add.

Step 1 means all paths that are blue-only will be free. On the new graph, the $BFS$ will only consider the edges which pass through a red vertex.



  1. Use $BFS$ from $s$. That length of the path to $t$ will essentially be the shortest path under the constraint of least red vertices in the path.





share|cite|improve this answer











$endgroup$



To solve this, you need to use $BFS$. But first, manipulate $G$ so that the path will always favor blue vertices.



The solution has 2 parts:



  1. Use $DFS$ on blue vertices only, to find all all-blue connected components. Let's denote each such component by $v'$. Now, each blue $v in v'$ will be "compressed" to a single vertex $u$, and an edge $(u,x)$ will be added for every $x in N(v')$.
    Note any such $x$ is necessarily red.
    This step costs $O(V+E) = O(E)$, since $DFS$ is $O(V+E)$, and you have at most $V$ blue vertices, which make no more than $E$ new edges to add.

Step 1 means all paths that are blue-only will be free. On the new graph, the $BFS$ will only consider the edges which pass through a red vertex.



  1. Use $BFS$ from $s$. That length of the path to $t$ will essentially be the shortest path under the constraint of least red vertices in the path.






share|cite|improve this answer














share|cite|improve this answer



share|cite|improve this answer








edited Apr 2 at 8:19

























answered Apr 1 at 20:53









loxlox

4456




4456







  • 2




    $begingroup$
    the graph is undirected, so step 1 should be to find all all-blue connected components, not SCCs, right?
    $endgroup$
    – Kevin Wang
    Apr 1 at 23:27










  • $begingroup$
    @KevinWang yes, I will fix it
    $endgroup$
    – lox
    Apr 2 at 8:18












  • 2




    $begingroup$
    the graph is undirected, so step 1 should be to find all all-blue connected components, not SCCs, right?
    $endgroup$
    – Kevin Wang
    Apr 1 at 23:27










  • $begingroup$
    @KevinWang yes, I will fix it
    $endgroup$
    – lox
    Apr 2 at 8:18







2




2




$begingroup$
the graph is undirected, so step 1 should be to find all all-blue connected components, not SCCs, right?
$endgroup$
– Kevin Wang
Apr 1 at 23:27




$begingroup$
the graph is undirected, so step 1 should be to find all all-blue connected components, not SCCs, right?
$endgroup$
– Kevin Wang
Apr 1 at 23:27












$begingroup$
@KevinWang yes, I will fix it
$endgroup$
– lox
Apr 2 at 8:18




$begingroup$
@KevinWang yes, I will fix it
$endgroup$
– lox
Apr 2 at 8:18











1












$begingroup$

Convert $G$ to a directed graph $G'$ where we have two edges $(u,v)$ and $(v,u)$ in $G'$ for every edge $u,v$ in $G$. Let the length of $(u,v)$ be 1 if $v$ is a red node and 0 otherwise. Now run Dijkstra's algorithm on $G'$ from the starting node $s$ to the ending node $t$.





It is clear that the shortest path thus found passes as few red nodes as possible.



While we are running Dijkstra's algorithm, we are in one of two kinds of stages alternatively. One kind of stage is when we are exploring towards red nodes. The other kind of stage is when we are exploring towards blue nodes. Since each edge is checked/visited as most twice, the running time is $O(|E|)$.






share|cite|improve this answer











$endgroup$












  • $begingroup$
    Yeah that definitely works, but the runtime of Dijkstra's is O(|E| + |V|log|V|) which is more than O(|E|).
    $endgroup$
    – user-2147481704
    Apr 1 at 22:26










  • $begingroup$
    As explained, the running time is $O(|E|)$.
    $endgroup$
    – Apass.Jack
    Apr 3 at 0:36
















1












$begingroup$

Convert $G$ to a directed graph $G'$ where we have two edges $(u,v)$ and $(v,u)$ in $G'$ for every edge $u,v$ in $G$. Let the length of $(u,v)$ be 1 if $v$ is a red node and 0 otherwise. Now run Dijkstra's algorithm on $G'$ from the starting node $s$ to the ending node $t$.





It is clear that the shortest path thus found passes as few red nodes as possible.



While we are running Dijkstra's algorithm, we are in one of two kinds of stages alternatively. One kind of stage is when we are exploring towards red nodes. The other kind of stage is when we are exploring towards blue nodes. Since each edge is checked/visited as most twice, the running time is $O(|E|)$.






share|cite|improve this answer











$endgroup$












  • $begingroup$
    Yeah that definitely works, but the runtime of Dijkstra's is O(|E| + |V|log|V|) which is more than O(|E|).
    $endgroup$
    – user-2147481704
    Apr 1 at 22:26










  • $begingroup$
    As explained, the running time is $O(|E|)$.
    $endgroup$
    – Apass.Jack
    Apr 3 at 0:36














1












1








1





$begingroup$

Convert $G$ to a directed graph $G'$ where we have two edges $(u,v)$ and $(v,u)$ in $G'$ for every edge $u,v$ in $G$. Let the length of $(u,v)$ be 1 if $v$ is a red node and 0 otherwise. Now run Dijkstra's algorithm on $G'$ from the starting node $s$ to the ending node $t$.





It is clear that the shortest path thus found passes as few red nodes as possible.



While we are running Dijkstra's algorithm, we are in one of two kinds of stages alternatively. One kind of stage is when we are exploring towards red nodes. The other kind of stage is when we are exploring towards blue nodes. Since each edge is checked/visited as most twice, the running time is $O(|E|)$.






share|cite|improve this answer











$endgroup$



Convert $G$ to a directed graph $G'$ where we have two edges $(u,v)$ and $(v,u)$ in $G'$ for every edge $u,v$ in $G$. Let the length of $(u,v)$ be 1 if $v$ is a red node and 0 otherwise. Now run Dijkstra's algorithm on $G'$ from the starting node $s$ to the ending node $t$.





It is clear that the shortest path thus found passes as few red nodes as possible.



While we are running Dijkstra's algorithm, we are in one of two kinds of stages alternatively. One kind of stage is when we are exploring towards red nodes. The other kind of stage is when we are exploring towards blue nodes. Since each edge is checked/visited as most twice, the running time is $O(|E|)$.







share|cite|improve this answer














share|cite|improve this answer



share|cite|improve this answer








edited Apr 1 at 23:32

























answered Apr 1 at 22:13









Apass.JackApass.Jack

14.3k1940




14.3k1940











  • $begingroup$
    Yeah that definitely works, but the runtime of Dijkstra's is O(|E| + |V|log|V|) which is more than O(|E|).
    $endgroup$
    – user-2147481704
    Apr 1 at 22:26










  • $begingroup$
    As explained, the running time is $O(|E|)$.
    $endgroup$
    – Apass.Jack
    Apr 3 at 0:36

















  • $begingroup$
    Yeah that definitely works, but the runtime of Dijkstra's is O(|E| + |V|log|V|) which is more than O(|E|).
    $endgroup$
    – user-2147481704
    Apr 1 at 22:26










  • $begingroup$
    As explained, the running time is $O(|E|)$.
    $endgroup$
    – Apass.Jack
    Apr 3 at 0:36
















$begingroup$
Yeah that definitely works, but the runtime of Dijkstra's is O(|E| + |V|log|V|) which is more than O(|E|).
$endgroup$
– user-2147481704
Apr 1 at 22:26




$begingroup$
Yeah that definitely works, but the runtime of Dijkstra's is O(|E| + |V|log|V|) which is more than O(|E|).
$endgroup$
– user-2147481704
Apr 1 at 22:26












$begingroup$
As explained, the running time is $O(|E|)$.
$endgroup$
– Apass.Jack
Apr 3 at 0:36





$begingroup$
As explained, the running time is $O(|E|)$.
$endgroup$
– Apass.Jack
Apr 3 at 0:36


















draft saved

draft discarded
















































Thanks for contributing an answer to Computer Science Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcs.stackexchange.com%2fquestions%2f106337%2ffind-a-path-from-s-to-t-using-as-few-red-nodes-as-possible%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

Tähtien Talli Jäsenet | Lähteet | NavigointivalikkoSuomen Hippos – Tähtien Talli

Do these cracks on my tires look bad? The Next CEO of Stack OverflowDry rot tire should I replace?Having to replace tiresFishtailed so easily? Bad tires? ABS?Filling the tires with something other than air, to avoid puncture hassles?Used Michelin tires safe to install?Do these tyre cracks necessitate replacement?Rumbling noise: tires or mechanicalIs it possible to fix noisy feathered tires?Are bad winter tires still better than summer tires in winter?Torque converter failure - Related to replacing only 2 tires?Why use snow tires on all 4 wheels on 2-wheel-drive cars?