Algorithms: Efficiently find a path containing at least $k$ nodes of some given colors. Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)Directed Graph, shortest path algorithm. I don't even understand what this question is asking. Is it a trick question or just Dijkstra's?Finding the longest path in an undirected weighted treeProving Vizing's Theorem using InductionShortest acyclical path between two nodes, negative weights allowedThe graph which has the least edges while satisfying this propertyModified Shortest Path ProblemTest question regarding graph theory - please check my workWeak, Regular, and Strong connectivity in directed graphsQuestion about a proof of “Graph $G$ has no odd cycles $implies$ $G$ is bipartite”Find shortest path in graph that visits 2 nodes from a certain node set

How were pictures turned from film to a big picture in a picture frame before digital scanning?

Did Deadpool rescue all of the X-Force?

Converted a Scalar function to a TVF function for parallel execution-Still running in Serial mode

Maximum summed subsequences with non-adjacent items

How does light 'choose' between wave and particle behaviour?

Is CEO the "profession" with the most psychopaths?

A term for a woman complaining about things/begging in a cute/childish way

Monolithic + MicroServices

How could we fake a moon landing now?

What order were files/directories outputted in dir?

How to get all distinct words within a set of lines?

How to dry out epoxy resin faster than usual?

Is there any word for a place full of confusion?

Would it be easier to apply for a UK visa if there is a host family to sponsor for you in going there?

What does it mean that physics no longer uses mechanical models to describe phenomena?

Why are vacuum tubes still used in amateur radios?

What is the appropriate index architecture when forced to implement IsDeleted (soft deletes)?

How does the math work when buying airline miles?

Can a new player join a group only when a new campaign starts?

Selecting user stories during sprint planning

Take 2! Is this homebrew Lady of Pain warlock patron balanced?

What do you call the main part of a joke?

What is the difference between globalisation and imperialism?

Why is it faster to reheat something than it is to cook it?



Algorithms: Efficiently find a path containing at least $k$ nodes of some given colors.



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)Directed Graph, shortest path algorithm. I don't even understand what this question is asking. Is it a trick question or just Dijkstra's?Finding the longest path in an undirected weighted treeProving Vizing's Theorem using InductionShortest acyclical path between two nodes, negative weights allowedThe graph which has the least edges while satisfying this propertyModified Shortest Path ProblemTest question regarding graph theory - please check my workWeak, Regular, and Strong connectivity in directed graphsQuestion about a proof of “Graph $G$ has no odd cycles $implies$ $G$ is bipartite”Find shortest path in graph that visits 2 nodes from a certain node set










0












$begingroup$


Suppose you have a directed graph with vertices colored A, B, C, and D. One node is $s$, the start node, and another $t$, the terminal node. You want to determine two things: First, find a path from $s$ to $t$ which contain at least one vertex of colors A, B, and C, and minimize the number of vertices of color D. Paths need not be simple.



Second, for any given $k>0$ determine whether a path exists which visits $k$-many vertices of color A, and $k$-many of color B, and $k$-many of color C. Again paths need not be simple.




Solution to the first part:



For the first question, I used the following solution: If the color at $s$ is A then we look for paths that go to a vertex colored B and then C minimizing the number of vertices colored D. Then we look for paths that go C then B minimizing the vertices colored at D. Then take the minimum of these two results. As long as we can find the minimum path for some specific color ordering in linear time, then doing it twice will still take linear time.



So to do that, first give every edge of the graph which enters a D vertex weight 1 and all others weight 0. Next make three copies of the graph, so that you have G, G', and G''. In G reassign every edge coming from an A vertex to a B to map into the corresponding vertex of G'. Then do likewise mapping B to C edges into the corresponding vertex of G''.



On this new graph run Dijkstra's. Since edges are not negative and bounded by a small constant, the algorithm runs in linear time.



To reiterate, since we can do it in some particular order of colors, we can do it then on all permutations of colors and this only multiplies linear time by a constant, so the algorithm continues to run in constant time.




I think I got the first part right but am a little more hung up on the second. I've imagined trying to leverage the solution to the first part. Perhaps make $k$ copies of the graph, on each copy run the algorithm ... but that doesn't make sense, you couldn't just stitch together two such paths and be ensured that such a path exists in the original graph. I thought maybe I should make $3k$ copies of the graph but I'm not sure how I would bind them together.



Hints or advice would be welcome.










share|cite|improve this question











$endgroup$











  • $begingroup$
    What is asymptotic restriction on complexity of algorithm?
    $endgroup$
    – Vladislav
    Mar 27 at 22:51











  • $begingroup$
    @Vladislav $O(|V|+|E|)$
    $endgroup$
    – Addem
    Mar 27 at 22:52










  • $begingroup$
    Ok, maybe I am not following you but I don't get how your solution is linear. Let's suppose a has color A and T has color C. You want to find shortest (in terms of number of D) path containing B. You can find shortest path from a to any B vertex in linear time, but next you have to iterate all B vertexes (it can be O(V) of them) and find the shortest path from every B to t. So it takes O(V *(V +E)), doesn't it?
    $endgroup$
    – Vladislav
    Mar 27 at 23:07










  • $begingroup$
    @Vladislav So my logic is that I have reproduced the graph three times, therefore there are $3|V|$ vertices and $3|E|$ edges. Since this is linear growth, it's not important. I remap some of the edges and add some weights and run Dijkstra's. Since I'm not finding a path from every B vertex to t then I shouldn't have the time complexity you wrote, but rather $O(3|V|+3|E|) = O(|V|+|E|)$. I can try to describe again the nature of the re-mapping and edge weights if that's still not clear.
    $endgroup$
    – Addem
    Mar 27 at 23:46















0












$begingroup$


Suppose you have a directed graph with vertices colored A, B, C, and D. One node is $s$, the start node, and another $t$, the terminal node. You want to determine two things: First, find a path from $s$ to $t$ which contain at least one vertex of colors A, B, and C, and minimize the number of vertices of color D. Paths need not be simple.



Second, for any given $k>0$ determine whether a path exists which visits $k$-many vertices of color A, and $k$-many of color B, and $k$-many of color C. Again paths need not be simple.




Solution to the first part:



For the first question, I used the following solution: If the color at $s$ is A then we look for paths that go to a vertex colored B and then C minimizing the number of vertices colored D. Then we look for paths that go C then B minimizing the vertices colored at D. Then take the minimum of these two results. As long as we can find the minimum path for some specific color ordering in linear time, then doing it twice will still take linear time.



So to do that, first give every edge of the graph which enters a D vertex weight 1 and all others weight 0. Next make three copies of the graph, so that you have G, G', and G''. In G reassign every edge coming from an A vertex to a B to map into the corresponding vertex of G'. Then do likewise mapping B to C edges into the corresponding vertex of G''.



On this new graph run Dijkstra's. Since edges are not negative and bounded by a small constant, the algorithm runs in linear time.



To reiterate, since we can do it in some particular order of colors, we can do it then on all permutations of colors and this only multiplies linear time by a constant, so the algorithm continues to run in constant time.




I think I got the first part right but am a little more hung up on the second. I've imagined trying to leverage the solution to the first part. Perhaps make $k$ copies of the graph, on each copy run the algorithm ... but that doesn't make sense, you couldn't just stitch together two such paths and be ensured that such a path exists in the original graph. I thought maybe I should make $3k$ copies of the graph but I'm not sure how I would bind them together.



Hints or advice would be welcome.










share|cite|improve this question











$endgroup$











  • $begingroup$
    What is asymptotic restriction on complexity of algorithm?
    $endgroup$
    – Vladislav
    Mar 27 at 22:51











  • $begingroup$
    @Vladislav $O(|V|+|E|)$
    $endgroup$
    – Addem
    Mar 27 at 22:52










  • $begingroup$
    Ok, maybe I am not following you but I don't get how your solution is linear. Let's suppose a has color A and T has color C. You want to find shortest (in terms of number of D) path containing B. You can find shortest path from a to any B vertex in linear time, but next you have to iterate all B vertexes (it can be O(V) of them) and find the shortest path from every B to t. So it takes O(V *(V +E)), doesn't it?
    $endgroup$
    – Vladislav
    Mar 27 at 23:07










  • $begingroup$
    @Vladislav So my logic is that I have reproduced the graph three times, therefore there are $3|V|$ vertices and $3|E|$ edges. Since this is linear growth, it's not important. I remap some of the edges and add some weights and run Dijkstra's. Since I'm not finding a path from every B vertex to t then I shouldn't have the time complexity you wrote, but rather $O(3|V|+3|E|) = O(|V|+|E|)$. I can try to describe again the nature of the re-mapping and edge weights if that's still not clear.
    $endgroup$
    – Addem
    Mar 27 at 23:46













0












0








0


0



$begingroup$


Suppose you have a directed graph with vertices colored A, B, C, and D. One node is $s$, the start node, and another $t$, the terminal node. You want to determine two things: First, find a path from $s$ to $t$ which contain at least one vertex of colors A, B, and C, and minimize the number of vertices of color D. Paths need not be simple.



Second, for any given $k>0$ determine whether a path exists which visits $k$-many vertices of color A, and $k$-many of color B, and $k$-many of color C. Again paths need not be simple.




Solution to the first part:



For the first question, I used the following solution: If the color at $s$ is A then we look for paths that go to a vertex colored B and then C minimizing the number of vertices colored D. Then we look for paths that go C then B minimizing the vertices colored at D. Then take the minimum of these two results. As long as we can find the minimum path for some specific color ordering in linear time, then doing it twice will still take linear time.



So to do that, first give every edge of the graph which enters a D vertex weight 1 and all others weight 0. Next make three copies of the graph, so that you have G, G', and G''. In G reassign every edge coming from an A vertex to a B to map into the corresponding vertex of G'. Then do likewise mapping B to C edges into the corresponding vertex of G''.



On this new graph run Dijkstra's. Since edges are not negative and bounded by a small constant, the algorithm runs in linear time.



To reiterate, since we can do it in some particular order of colors, we can do it then on all permutations of colors and this only multiplies linear time by a constant, so the algorithm continues to run in constant time.




I think I got the first part right but am a little more hung up on the second. I've imagined trying to leverage the solution to the first part. Perhaps make $k$ copies of the graph, on each copy run the algorithm ... but that doesn't make sense, you couldn't just stitch together two such paths and be ensured that such a path exists in the original graph. I thought maybe I should make $3k$ copies of the graph but I'm not sure how I would bind them together.



Hints or advice would be welcome.










share|cite|improve this question











$endgroup$




Suppose you have a directed graph with vertices colored A, B, C, and D. One node is $s$, the start node, and another $t$, the terminal node. You want to determine two things: First, find a path from $s$ to $t$ which contain at least one vertex of colors A, B, and C, and minimize the number of vertices of color D. Paths need not be simple.



Second, for any given $k>0$ determine whether a path exists which visits $k$-many vertices of color A, and $k$-many of color B, and $k$-many of color C. Again paths need not be simple.




Solution to the first part:



For the first question, I used the following solution: If the color at $s$ is A then we look for paths that go to a vertex colored B and then C minimizing the number of vertices colored D. Then we look for paths that go C then B minimizing the vertices colored at D. Then take the minimum of these two results. As long as we can find the minimum path for some specific color ordering in linear time, then doing it twice will still take linear time.



So to do that, first give every edge of the graph which enters a D vertex weight 1 and all others weight 0. Next make three copies of the graph, so that you have G, G', and G''. In G reassign every edge coming from an A vertex to a B to map into the corresponding vertex of G'. Then do likewise mapping B to C edges into the corresponding vertex of G''.



On this new graph run Dijkstra's. Since edges are not negative and bounded by a small constant, the algorithm runs in linear time.



To reiterate, since we can do it in some particular order of colors, we can do it then on all permutations of colors and this only multiplies linear time by a constant, so the algorithm continues to run in constant time.




I think I got the first part right but am a little more hung up on the second. I've imagined trying to leverage the solution to the first part. Perhaps make $k$ copies of the graph, on each copy run the algorithm ... but that doesn't make sense, you couldn't just stitch together two such paths and be ensured that such a path exists in the original graph. I thought maybe I should make $3k$ copies of the graph but I'm not sure how I would bind them together.



Hints or advice would be welcome.







graph-theory algorithms






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited Mar 27 at 20:50







Addem

















asked Mar 27 at 19:32









AddemAddem

1,7581429




1,7581429











  • $begingroup$
    What is asymptotic restriction on complexity of algorithm?
    $endgroup$
    – Vladislav
    Mar 27 at 22:51











  • $begingroup$
    @Vladislav $O(|V|+|E|)$
    $endgroup$
    – Addem
    Mar 27 at 22:52










  • $begingroup$
    Ok, maybe I am not following you but I don't get how your solution is linear. Let's suppose a has color A and T has color C. You want to find shortest (in terms of number of D) path containing B. You can find shortest path from a to any B vertex in linear time, but next you have to iterate all B vertexes (it can be O(V) of them) and find the shortest path from every B to t. So it takes O(V *(V +E)), doesn't it?
    $endgroup$
    – Vladislav
    Mar 27 at 23:07










  • $begingroup$
    @Vladislav So my logic is that I have reproduced the graph three times, therefore there are $3|V|$ vertices and $3|E|$ edges. Since this is linear growth, it's not important. I remap some of the edges and add some weights and run Dijkstra's. Since I'm not finding a path from every B vertex to t then I shouldn't have the time complexity you wrote, but rather $O(3|V|+3|E|) = O(|V|+|E|)$. I can try to describe again the nature of the re-mapping and edge weights if that's still not clear.
    $endgroup$
    – Addem
    Mar 27 at 23:46
















  • $begingroup$
    What is asymptotic restriction on complexity of algorithm?
    $endgroup$
    – Vladislav
    Mar 27 at 22:51











  • $begingroup$
    @Vladislav $O(|V|+|E|)$
    $endgroup$
    – Addem
    Mar 27 at 22:52










  • $begingroup$
    Ok, maybe I am not following you but I don't get how your solution is linear. Let's suppose a has color A and T has color C. You want to find shortest (in terms of number of D) path containing B. You can find shortest path from a to any B vertex in linear time, but next you have to iterate all B vertexes (it can be O(V) of them) and find the shortest path from every B to t. So it takes O(V *(V +E)), doesn't it?
    $endgroup$
    – Vladislav
    Mar 27 at 23:07










  • $begingroup$
    @Vladislav So my logic is that I have reproduced the graph three times, therefore there are $3|V|$ vertices and $3|E|$ edges. Since this is linear growth, it's not important. I remap some of the edges and add some weights and run Dijkstra's. Since I'm not finding a path from every B vertex to t then I shouldn't have the time complexity you wrote, but rather $O(3|V|+3|E|) = O(|V|+|E|)$. I can try to describe again the nature of the re-mapping and edge weights if that's still not clear.
    $endgroup$
    – Addem
    Mar 27 at 23:46















$begingroup$
What is asymptotic restriction on complexity of algorithm?
$endgroup$
– Vladislav
Mar 27 at 22:51





$begingroup$
What is asymptotic restriction on complexity of algorithm?
$endgroup$
– Vladislav
Mar 27 at 22:51













$begingroup$
@Vladislav $O(|V|+|E|)$
$endgroup$
– Addem
Mar 27 at 22:52




$begingroup$
@Vladislav $O(|V|+|E|)$
$endgroup$
– Addem
Mar 27 at 22:52












$begingroup$
Ok, maybe I am not following you but I don't get how your solution is linear. Let's suppose a has color A and T has color C. You want to find shortest (in terms of number of D) path containing B. You can find shortest path from a to any B vertex in linear time, but next you have to iterate all B vertexes (it can be O(V) of them) and find the shortest path from every B to t. So it takes O(V *(V +E)), doesn't it?
$endgroup$
– Vladislav
Mar 27 at 23:07




$begingroup$
Ok, maybe I am not following you but I don't get how your solution is linear. Let's suppose a has color A and T has color C. You want to find shortest (in terms of number of D) path containing B. You can find shortest path from a to any B vertex in linear time, but next you have to iterate all B vertexes (it can be O(V) of them) and find the shortest path from every B to t. So it takes O(V *(V +E)), doesn't it?
$endgroup$
– Vladislav
Mar 27 at 23:07












$begingroup$
@Vladislav So my logic is that I have reproduced the graph three times, therefore there are $3|V|$ vertices and $3|E|$ edges. Since this is linear growth, it's not important. I remap some of the edges and add some weights and run Dijkstra's. Since I'm not finding a path from every B vertex to t then I shouldn't have the time complexity you wrote, but rather $O(3|V|+3|E|) = O(|V|+|E|)$. I can try to describe again the nature of the re-mapping and edge weights if that's still not clear.
$endgroup$
– Addem
Mar 27 at 23:46




$begingroup$
@Vladislav So my logic is that I have reproduced the graph three times, therefore there are $3|V|$ vertices and $3|E|$ edges. Since this is linear growth, it's not important. I remap some of the edges and add some weights and run Dijkstra's. Since I'm not finding a path from every B vertex to t then I shouldn't have the time complexity you wrote, but rather $O(3|V|+3|E|) = O(|V|+|E|)$. I can try to describe again the nature of the re-mapping and edge weights if that's still not clear.
$endgroup$
– Addem
Mar 27 at 23:46










1 Answer
1






active

oldest

votes


















0












$begingroup$

I don't think I understood your solution, but I can provide an easy one for the first.



Let's build a new graph $G'=(V', E')$ where $V' = v in V$



In other words, vertexes of $G'$ are pairs of an original vertex and bitmask of visited colors. For instance, $(3, 011)$ means vertex $3$ of $G$ and $A$ has not been visited, $B$ and $C$ has been visited).



$E'$ contains an edge $(v, mask1) rightarrow(u, mask2)$ iff the original graph has the edge $u rightarrow v$ and $mask2$ is $mask1$ with the bit corresponding to color of $u$ set to $1$.



Clearly, all you left to do is to find the shortest path (in terms of number of $D$'s) from $(s, 100)$ (if $s$ has color $A$, if $s$ has color $B$ it will be $010$ and so on) to $(t, 111)$.



Clearly, this new graph has $8|V|$ vertexes and $8|E|$ edges, so it linearly depends on the size of the original one.



On minimization the number of $D$'s: it is a good idea to set edge's weight $0$ or $1$ but it is kind of overkill to run Dijkstra on this graph. In order to find the shortest path on $0,1$ graph you just need to slightly modify breadth-first search. If you explore $1$-weighted edge you add the vertex in the end of the queue (as usual in breadth-first search), but to the beginning of queue if you explore $0$-weighted edge. This algorithm will give us the shortest path.



Unfortunately, this algorithm can not be generalized for an arbitrary $k$ with linear complexity as $G'$ will have $k^3|V|$ vertexes.






share|cite|improve this answer











$endgroup$












  • $begingroup$
    I'm not certain whether I need to treat $k$ as a variable for the growth of the algorithm or not. I've been assuming that I only need to treat it as a fixed parameter. Otherwise, I am pretty skeptical anything could be linear with a growing $k$.
    $endgroup$
    – Addem
    Mar 27 at 23:48










  • $begingroup$
    In this case, you've got a linear solution for an arbitrarily fixed k.
    $endgroup$
    – Vladislav
    Mar 27 at 23:50












Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "69"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
,
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3165007%2falgorithms-efficiently-find-a-path-containing-at-least-k-nodes-of-some-given%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0












$begingroup$

I don't think I understood your solution, but I can provide an easy one for the first.



Let's build a new graph $G'=(V', E')$ where $V' = v in V$



In other words, vertexes of $G'$ are pairs of an original vertex and bitmask of visited colors. For instance, $(3, 011)$ means vertex $3$ of $G$ and $A$ has not been visited, $B$ and $C$ has been visited).



$E'$ contains an edge $(v, mask1) rightarrow(u, mask2)$ iff the original graph has the edge $u rightarrow v$ and $mask2$ is $mask1$ with the bit corresponding to color of $u$ set to $1$.



Clearly, all you left to do is to find the shortest path (in terms of number of $D$'s) from $(s, 100)$ (if $s$ has color $A$, if $s$ has color $B$ it will be $010$ and so on) to $(t, 111)$.



Clearly, this new graph has $8|V|$ vertexes and $8|E|$ edges, so it linearly depends on the size of the original one.



On minimization the number of $D$'s: it is a good idea to set edge's weight $0$ or $1$ but it is kind of overkill to run Dijkstra on this graph. In order to find the shortest path on $0,1$ graph you just need to slightly modify breadth-first search. If you explore $1$-weighted edge you add the vertex in the end of the queue (as usual in breadth-first search), but to the beginning of queue if you explore $0$-weighted edge. This algorithm will give us the shortest path.



Unfortunately, this algorithm can not be generalized for an arbitrary $k$ with linear complexity as $G'$ will have $k^3|V|$ vertexes.






share|cite|improve this answer











$endgroup$












  • $begingroup$
    I'm not certain whether I need to treat $k$ as a variable for the growth of the algorithm or not. I've been assuming that I only need to treat it as a fixed parameter. Otherwise, I am pretty skeptical anything could be linear with a growing $k$.
    $endgroup$
    – Addem
    Mar 27 at 23:48










  • $begingroup$
    In this case, you've got a linear solution for an arbitrarily fixed k.
    $endgroup$
    – Vladislav
    Mar 27 at 23:50
















0












$begingroup$

I don't think I understood your solution, but I can provide an easy one for the first.



Let's build a new graph $G'=(V', E')$ where $V' = v in V$



In other words, vertexes of $G'$ are pairs of an original vertex and bitmask of visited colors. For instance, $(3, 011)$ means vertex $3$ of $G$ and $A$ has not been visited, $B$ and $C$ has been visited).



$E'$ contains an edge $(v, mask1) rightarrow(u, mask2)$ iff the original graph has the edge $u rightarrow v$ and $mask2$ is $mask1$ with the bit corresponding to color of $u$ set to $1$.



Clearly, all you left to do is to find the shortest path (in terms of number of $D$'s) from $(s, 100)$ (if $s$ has color $A$, if $s$ has color $B$ it will be $010$ and so on) to $(t, 111)$.



Clearly, this new graph has $8|V|$ vertexes and $8|E|$ edges, so it linearly depends on the size of the original one.



On minimization the number of $D$'s: it is a good idea to set edge's weight $0$ or $1$ but it is kind of overkill to run Dijkstra on this graph. In order to find the shortest path on $0,1$ graph you just need to slightly modify breadth-first search. If you explore $1$-weighted edge you add the vertex in the end of the queue (as usual in breadth-first search), but to the beginning of queue if you explore $0$-weighted edge. This algorithm will give us the shortest path.



Unfortunately, this algorithm can not be generalized for an arbitrary $k$ with linear complexity as $G'$ will have $k^3|V|$ vertexes.






share|cite|improve this answer











$endgroup$












  • $begingroup$
    I'm not certain whether I need to treat $k$ as a variable for the growth of the algorithm or not. I've been assuming that I only need to treat it as a fixed parameter. Otherwise, I am pretty skeptical anything could be linear with a growing $k$.
    $endgroup$
    – Addem
    Mar 27 at 23:48










  • $begingroup$
    In this case, you've got a linear solution for an arbitrarily fixed k.
    $endgroup$
    – Vladislav
    Mar 27 at 23:50














0












0








0





$begingroup$

I don't think I understood your solution, but I can provide an easy one for the first.



Let's build a new graph $G'=(V', E')$ where $V' = v in V$



In other words, vertexes of $G'$ are pairs of an original vertex and bitmask of visited colors. For instance, $(3, 011)$ means vertex $3$ of $G$ and $A$ has not been visited, $B$ and $C$ has been visited).



$E'$ contains an edge $(v, mask1) rightarrow(u, mask2)$ iff the original graph has the edge $u rightarrow v$ and $mask2$ is $mask1$ with the bit corresponding to color of $u$ set to $1$.



Clearly, all you left to do is to find the shortest path (in terms of number of $D$'s) from $(s, 100)$ (if $s$ has color $A$, if $s$ has color $B$ it will be $010$ and so on) to $(t, 111)$.



Clearly, this new graph has $8|V|$ vertexes and $8|E|$ edges, so it linearly depends on the size of the original one.



On minimization the number of $D$'s: it is a good idea to set edge's weight $0$ or $1$ but it is kind of overkill to run Dijkstra on this graph. In order to find the shortest path on $0,1$ graph you just need to slightly modify breadth-first search. If you explore $1$-weighted edge you add the vertex in the end of the queue (as usual in breadth-first search), but to the beginning of queue if you explore $0$-weighted edge. This algorithm will give us the shortest path.



Unfortunately, this algorithm can not be generalized for an arbitrary $k$ with linear complexity as $G'$ will have $k^3|V|$ vertexes.






share|cite|improve this answer











$endgroup$



I don't think I understood your solution, but I can provide an easy one for the first.



Let's build a new graph $G'=(V', E')$ where $V' = v in V$



In other words, vertexes of $G'$ are pairs of an original vertex and bitmask of visited colors. For instance, $(3, 011)$ means vertex $3$ of $G$ and $A$ has not been visited, $B$ and $C$ has been visited).



$E'$ contains an edge $(v, mask1) rightarrow(u, mask2)$ iff the original graph has the edge $u rightarrow v$ and $mask2$ is $mask1$ with the bit corresponding to color of $u$ set to $1$.



Clearly, all you left to do is to find the shortest path (in terms of number of $D$'s) from $(s, 100)$ (if $s$ has color $A$, if $s$ has color $B$ it will be $010$ and so on) to $(t, 111)$.



Clearly, this new graph has $8|V|$ vertexes and $8|E|$ edges, so it linearly depends on the size of the original one.



On minimization the number of $D$'s: it is a good idea to set edge's weight $0$ or $1$ but it is kind of overkill to run Dijkstra on this graph. In order to find the shortest path on $0,1$ graph you just need to slightly modify breadth-first search. If you explore $1$-weighted edge you add the vertex in the end of the queue (as usual in breadth-first search), but to the beginning of queue if you explore $0$-weighted edge. This algorithm will give us the shortest path.



Unfortunately, this algorithm can not be generalized for an arbitrary $k$ with linear complexity as $G'$ will have $k^3|V|$ vertexes.







share|cite|improve this answer














share|cite|improve this answer



share|cite|improve this answer








edited Mar 28 at 15:11

























answered Mar 27 at 23:43









VladislavVladislav

1165




1165











  • $begingroup$
    I'm not certain whether I need to treat $k$ as a variable for the growth of the algorithm or not. I've been assuming that I only need to treat it as a fixed parameter. Otherwise, I am pretty skeptical anything could be linear with a growing $k$.
    $endgroup$
    – Addem
    Mar 27 at 23:48










  • $begingroup$
    In this case, you've got a linear solution for an arbitrarily fixed k.
    $endgroup$
    – Vladislav
    Mar 27 at 23:50

















  • $begingroup$
    I'm not certain whether I need to treat $k$ as a variable for the growth of the algorithm or not. I've been assuming that I only need to treat it as a fixed parameter. Otherwise, I am pretty skeptical anything could be linear with a growing $k$.
    $endgroup$
    – Addem
    Mar 27 at 23:48










  • $begingroup$
    In this case, you've got a linear solution for an arbitrarily fixed k.
    $endgroup$
    – Vladislav
    Mar 27 at 23:50
















$begingroup$
I'm not certain whether I need to treat $k$ as a variable for the growth of the algorithm or not. I've been assuming that I only need to treat it as a fixed parameter. Otherwise, I am pretty skeptical anything could be linear with a growing $k$.
$endgroup$
– Addem
Mar 27 at 23:48




$begingroup$
I'm not certain whether I need to treat $k$ as a variable for the growth of the algorithm or not. I've been assuming that I only need to treat it as a fixed parameter. Otherwise, I am pretty skeptical anything could be linear with a growing $k$.
$endgroup$
– Addem
Mar 27 at 23:48












$begingroup$
In this case, you've got a linear solution for an arbitrarily fixed k.
$endgroup$
– Vladislav
Mar 27 at 23:50





$begingroup$
In this case, you've got a linear solution for an arbitrarily fixed k.
$endgroup$
– Vladislav
Mar 27 at 23:50


















draft saved

draft discarded
















































Thanks for contributing an answer to Mathematics 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%2fmath.stackexchange.com%2fquestions%2f3165007%2falgorithms-efficiently-find-a-path-containing-at-least-k-nodes-of-some-given%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

Moe incest case Sentencing See also References Navigation menu"'Australian Josef Fritzl' fathered four children by daughter""Small town recoils in horror at 'Australian Fritzl' incest case""Victorian rape allegations echo Fritzl case - Just In (Australian Broadcasting Corporation)""Incest father jailed for 22 years""'Australian Fritzl' sentenced to 22 years in prison for abusing daughter for three decades""RSJ v The Queen"

Daza language Contents Vocabulary Phonology References External links Navigation menudaza1242Daza"Dazaga"eeee178086576

Who is our nearest planetary neighbor, on average?Santa Claus flies to the South PoleSeven Spheres of Unequal Mass, a weighing problem with a twistDescribe a large integerFast Mental Calculation of $7.5^7$Math in Space (without the help of celebrities)Find the value of $bigstar$: Puzzle 8 - InequalityWho drinks beer while running anyway?A Crucial DeliveryRanking And AverageHow long will my money last at roulette?