How to find a list of unique shortest paths in a list of paths?











up vote
2
down vote

favorite












Imagine I have a tree graph as shown below, where a node can have multiple children, and so on.. (a node can have only 1 parent). If I have a list of paths along that graph, how can I find a subset of those paths that are unique and shortest?



enter image description here



Example Input (List of paths):



[1, 2, 3]
[1, 2]
[1, 7]
[1, 8, 9, 10]


Expected Output:



[1, 2]
[1, 7]
[1, 8, 9, 10]


The [1, 2, 3] path is ignored because it is longer than [1, 2], while the [1, 8, 9, 10] path is kept because it is unique.










share|improve this question
























  • What is the expected output for [1, 2, 3] [1, 2, 4] ?
    – Cid
    Nov 7 at 10:26










  • @Cid if [1] or [1,2] don't exist, both should appear in the output since they are unique.
    – RandomName
    Nov 7 at 10:43










  • So basically if a prefix exists, then you remove that element?
    – Willem Van Onsem
    Nov 7 at 11:18






  • 1




    @WillemVanOnsem it looks like. The problem become really simpler, the terms graph and tree can be ignored since the whole "roads" are send as input. There is no need to parse the tree, this is just array processing. shortest path is here, in my opinion, badly used
    – Cid
    Nov 7 at 12:18










  • @Cid: well we can make a prefix-tree here we update for each item,but indeed, it is simpler in terms of graph and tree.
    – Willem Van Onsem
    Nov 7 at 12:20















up vote
2
down vote

favorite












Imagine I have a tree graph as shown below, where a node can have multiple children, and so on.. (a node can have only 1 parent). If I have a list of paths along that graph, how can I find a subset of those paths that are unique and shortest?



enter image description here



Example Input (List of paths):



[1, 2, 3]
[1, 2]
[1, 7]
[1, 8, 9, 10]


Expected Output:



[1, 2]
[1, 7]
[1, 8, 9, 10]


The [1, 2, 3] path is ignored because it is longer than [1, 2], while the [1, 8, 9, 10] path is kept because it is unique.










share|improve this question
























  • What is the expected output for [1, 2, 3] [1, 2, 4] ?
    – Cid
    Nov 7 at 10:26










  • @Cid if [1] or [1,2] don't exist, both should appear in the output since they are unique.
    – RandomName
    Nov 7 at 10:43










  • So basically if a prefix exists, then you remove that element?
    – Willem Van Onsem
    Nov 7 at 11:18






  • 1




    @WillemVanOnsem it looks like. The problem become really simpler, the terms graph and tree can be ignored since the whole "roads" are send as input. There is no need to parse the tree, this is just array processing. shortest path is here, in my opinion, badly used
    – Cid
    Nov 7 at 12:18










  • @Cid: well we can make a prefix-tree here we update for each item,but indeed, it is simpler in terms of graph and tree.
    – Willem Van Onsem
    Nov 7 at 12:20













up vote
2
down vote

favorite









up vote
2
down vote

favorite











Imagine I have a tree graph as shown below, where a node can have multiple children, and so on.. (a node can have only 1 parent). If I have a list of paths along that graph, how can I find a subset of those paths that are unique and shortest?



enter image description here



Example Input (List of paths):



[1, 2, 3]
[1, 2]
[1, 7]
[1, 8, 9, 10]


Expected Output:



[1, 2]
[1, 7]
[1, 8, 9, 10]


The [1, 2, 3] path is ignored because it is longer than [1, 2], while the [1, 8, 9, 10] path is kept because it is unique.










share|improve this question















Imagine I have a tree graph as shown below, where a node can have multiple children, and so on.. (a node can have only 1 parent). If I have a list of paths along that graph, how can I find a subset of those paths that are unique and shortest?



enter image description here



Example Input (List of paths):



[1, 2, 3]
[1, 2]
[1, 7]
[1, 8, 9, 10]


Expected Output:



[1, 2]
[1, 7]
[1, 8, 9, 10]


The [1, 2, 3] path is ignored because it is longer than [1, 2], while the [1, 8, 9, 10] path is kept because it is unique.







algorithm tree graph-theory shortest-path






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 16:05









Dominique Fortin

1,606816




1,606816










asked Nov 7 at 10:15









RandomName

284




284












  • What is the expected output for [1, 2, 3] [1, 2, 4] ?
    – Cid
    Nov 7 at 10:26










  • @Cid if [1] or [1,2] don't exist, both should appear in the output since they are unique.
    – RandomName
    Nov 7 at 10:43










  • So basically if a prefix exists, then you remove that element?
    – Willem Van Onsem
    Nov 7 at 11:18






  • 1




    @WillemVanOnsem it looks like. The problem become really simpler, the terms graph and tree can be ignored since the whole "roads" are send as input. There is no need to parse the tree, this is just array processing. shortest path is here, in my opinion, badly used
    – Cid
    Nov 7 at 12:18










  • @Cid: well we can make a prefix-tree here we update for each item,but indeed, it is simpler in terms of graph and tree.
    – Willem Van Onsem
    Nov 7 at 12:20


















  • What is the expected output for [1, 2, 3] [1, 2, 4] ?
    – Cid
    Nov 7 at 10:26










  • @Cid if [1] or [1,2] don't exist, both should appear in the output since they are unique.
    – RandomName
    Nov 7 at 10:43










  • So basically if a prefix exists, then you remove that element?
    – Willem Van Onsem
    Nov 7 at 11:18






  • 1




    @WillemVanOnsem it looks like. The problem become really simpler, the terms graph and tree can be ignored since the whole "roads" are send as input. There is no need to parse the tree, this is just array processing. shortest path is here, in my opinion, badly used
    – Cid
    Nov 7 at 12:18










  • @Cid: well we can make a prefix-tree here we update for each item,but indeed, it is simpler in terms of graph and tree.
    – Willem Van Onsem
    Nov 7 at 12:20
















What is the expected output for [1, 2, 3] [1, 2, 4] ?
– Cid
Nov 7 at 10:26




What is the expected output for [1, 2, 3] [1, 2, 4] ?
– Cid
Nov 7 at 10:26












@Cid if [1] or [1,2] don't exist, both should appear in the output since they are unique.
– RandomName
Nov 7 at 10:43




@Cid if [1] or [1,2] don't exist, both should appear in the output since they are unique.
– RandomName
Nov 7 at 10:43












So basically if a prefix exists, then you remove that element?
– Willem Van Onsem
Nov 7 at 11:18




So basically if a prefix exists, then you remove that element?
– Willem Van Onsem
Nov 7 at 11:18




1




1




@WillemVanOnsem it looks like. The problem become really simpler, the terms graph and tree can be ignored since the whole "roads" are send as input. There is no need to parse the tree, this is just array processing. shortest path is here, in my opinion, badly used
– Cid
Nov 7 at 12:18




@WillemVanOnsem it looks like. The problem become really simpler, the terms graph and tree can be ignored since the whole "roads" are send as input. There is no need to parse the tree, this is just array processing. shortest path is here, in my opinion, badly used
– Cid
Nov 7 at 12:18












@Cid: well we can make a prefix-tree here we update for each item,but indeed, it is simpler in terms of graph and tree.
– Willem Van Onsem
Nov 7 at 12:20




@Cid: well we can make a prefix-tree here we update for each item,but indeed, it is simpler in terms of graph and tree.
– Willem Van Onsem
Nov 7 at 12:20












2 Answers
2






active

oldest

votes

















up vote
2
down vote













First, sort the input paths by length. Maintain a set of leaf nodes. This will contain the last node of each valid path. After adding a leaf node, we'll forbid any path which includes that leaf node. When you add a path, check each of its members against the set of leaf nodes. If you get a match then the path is invalid, otherwise it's valid and you should add it's final element to the set of leaf nodes.



This is O(n log n) in the number of lists and linear in the number of elements across all lists.






share|improve this answer





















  • Assuming the longest path's length s and the number of paths n, the complexity would be actually O(n*log n*s).
    – Emadpres
    Nov 7 at 13:12












  • @Emadpres Say n is the number of paths and m is the number of elements. Then this is O(n log n) + O(m). I don't see why you think n*s comes into it.
    – Dave
    Nov 7 at 14:41










  • @Dave i think @Emadpres is right. Try your algorithm for these paths: [1,2,3],[1,4,5],[1,6,7],[1,8,9]
    – nightfury1204
    Nov 7 at 15:04










  • @nightfury1204 Well, in the case where all the paths are the same length then m = s*n so I agree for this case, but it isn't generally true. Maybe it's unclear what I meant by m? I mean the sum of the lengths of the paths, not the number of unique elements.
    – Dave
    Nov 7 at 16:54


















up vote
1
down vote













Try to build a tree using these path. For each path, try to traverse from first node of the path to the last node of the path by setting edge to consecutive node.Mark last node of the path as leaf node after traversing each path. If you find any node marked as a leaf when traversing a path, you will stop traversing. Also remove the child of the node that marked as a leaf node. Every path from root node to leaf node in the final tree will be you answer. See figure below for more clarification:
enter image description here



Complexity will be sum of all path's length.






share|improve this answer





















  • What do you mean by "Also remove the child of the node that marked as a leaf node"?
    – Emadpres
    Nov 8 at 8:26






  • 1




    @Emadpres see in the figure. In After path 3 node 2 has child 3 and 4. After path 4 [1,2] remove child of node 2.
    – nightfury1204
    Nov 8 at 8:38











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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',
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
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53187409%2fhow-to-find-a-list-of-unique-shortest-paths-in-a-list-of-paths%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








up vote
2
down vote













First, sort the input paths by length. Maintain a set of leaf nodes. This will contain the last node of each valid path. After adding a leaf node, we'll forbid any path which includes that leaf node. When you add a path, check each of its members against the set of leaf nodes. If you get a match then the path is invalid, otherwise it's valid and you should add it's final element to the set of leaf nodes.



This is O(n log n) in the number of lists and linear in the number of elements across all lists.






share|improve this answer





















  • Assuming the longest path's length s and the number of paths n, the complexity would be actually O(n*log n*s).
    – Emadpres
    Nov 7 at 13:12












  • @Emadpres Say n is the number of paths and m is the number of elements. Then this is O(n log n) + O(m). I don't see why you think n*s comes into it.
    – Dave
    Nov 7 at 14:41










  • @Dave i think @Emadpres is right. Try your algorithm for these paths: [1,2,3],[1,4,5],[1,6,7],[1,8,9]
    – nightfury1204
    Nov 7 at 15:04










  • @nightfury1204 Well, in the case where all the paths are the same length then m = s*n so I agree for this case, but it isn't generally true. Maybe it's unclear what I meant by m? I mean the sum of the lengths of the paths, not the number of unique elements.
    – Dave
    Nov 7 at 16:54















up vote
2
down vote













First, sort the input paths by length. Maintain a set of leaf nodes. This will contain the last node of each valid path. After adding a leaf node, we'll forbid any path which includes that leaf node. When you add a path, check each of its members against the set of leaf nodes. If you get a match then the path is invalid, otherwise it's valid and you should add it's final element to the set of leaf nodes.



This is O(n log n) in the number of lists and linear in the number of elements across all lists.






share|improve this answer





















  • Assuming the longest path's length s and the number of paths n, the complexity would be actually O(n*log n*s).
    – Emadpres
    Nov 7 at 13:12












  • @Emadpres Say n is the number of paths and m is the number of elements. Then this is O(n log n) + O(m). I don't see why you think n*s comes into it.
    – Dave
    Nov 7 at 14:41










  • @Dave i think @Emadpres is right. Try your algorithm for these paths: [1,2,3],[1,4,5],[1,6,7],[1,8,9]
    – nightfury1204
    Nov 7 at 15:04










  • @nightfury1204 Well, in the case where all the paths are the same length then m = s*n so I agree for this case, but it isn't generally true. Maybe it's unclear what I meant by m? I mean the sum of the lengths of the paths, not the number of unique elements.
    – Dave
    Nov 7 at 16:54













up vote
2
down vote










up vote
2
down vote









First, sort the input paths by length. Maintain a set of leaf nodes. This will contain the last node of each valid path. After adding a leaf node, we'll forbid any path which includes that leaf node. When you add a path, check each of its members against the set of leaf nodes. If you get a match then the path is invalid, otherwise it's valid and you should add it's final element to the set of leaf nodes.



This is O(n log n) in the number of lists and linear in the number of elements across all lists.






share|improve this answer












First, sort the input paths by length. Maintain a set of leaf nodes. This will contain the last node of each valid path. After adding a leaf node, we'll forbid any path which includes that leaf node. When you add a path, check each of its members against the set of leaf nodes. If you get a match then the path is invalid, otherwise it's valid and you should add it's final element to the set of leaf nodes.



This is O(n log n) in the number of lists and linear in the number of elements across all lists.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 7 at 13:06









Dave

2,4691225




2,4691225












  • Assuming the longest path's length s and the number of paths n, the complexity would be actually O(n*log n*s).
    – Emadpres
    Nov 7 at 13:12












  • @Emadpres Say n is the number of paths and m is the number of elements. Then this is O(n log n) + O(m). I don't see why you think n*s comes into it.
    – Dave
    Nov 7 at 14:41










  • @Dave i think @Emadpres is right. Try your algorithm for these paths: [1,2,3],[1,4,5],[1,6,7],[1,8,9]
    – nightfury1204
    Nov 7 at 15:04










  • @nightfury1204 Well, in the case where all the paths are the same length then m = s*n so I agree for this case, but it isn't generally true. Maybe it's unclear what I meant by m? I mean the sum of the lengths of the paths, not the number of unique elements.
    – Dave
    Nov 7 at 16:54


















  • Assuming the longest path's length s and the number of paths n, the complexity would be actually O(n*log n*s).
    – Emadpres
    Nov 7 at 13:12












  • @Emadpres Say n is the number of paths and m is the number of elements. Then this is O(n log n) + O(m). I don't see why you think n*s comes into it.
    – Dave
    Nov 7 at 14:41










  • @Dave i think @Emadpres is right. Try your algorithm for these paths: [1,2,3],[1,4,5],[1,6,7],[1,8,9]
    – nightfury1204
    Nov 7 at 15:04










  • @nightfury1204 Well, in the case where all the paths are the same length then m = s*n so I agree for this case, but it isn't generally true. Maybe it's unclear what I meant by m? I mean the sum of the lengths of the paths, not the number of unique elements.
    – Dave
    Nov 7 at 16:54
















Assuming the longest path's length s and the number of paths n, the complexity would be actually O(n*log n*s).
– Emadpres
Nov 7 at 13:12






Assuming the longest path's length s and the number of paths n, the complexity would be actually O(n*log n*s).
– Emadpres
Nov 7 at 13:12














@Emadpres Say n is the number of paths and m is the number of elements. Then this is O(n log n) + O(m). I don't see why you think n*s comes into it.
– Dave
Nov 7 at 14:41




@Emadpres Say n is the number of paths and m is the number of elements. Then this is O(n log n) + O(m). I don't see why you think n*s comes into it.
– Dave
Nov 7 at 14:41












@Dave i think @Emadpres is right. Try your algorithm for these paths: [1,2,3],[1,4,5],[1,6,7],[1,8,9]
– nightfury1204
Nov 7 at 15:04




@Dave i think @Emadpres is right. Try your algorithm for these paths: [1,2,3],[1,4,5],[1,6,7],[1,8,9]
– nightfury1204
Nov 7 at 15:04












@nightfury1204 Well, in the case where all the paths are the same length then m = s*n so I agree for this case, but it isn't generally true. Maybe it's unclear what I meant by m? I mean the sum of the lengths of the paths, not the number of unique elements.
– Dave
Nov 7 at 16:54




@nightfury1204 Well, in the case where all the paths are the same length then m = s*n so I agree for this case, but it isn't generally true. Maybe it's unclear what I meant by m? I mean the sum of the lengths of the paths, not the number of unique elements.
– Dave
Nov 7 at 16:54












up vote
1
down vote













Try to build a tree using these path. For each path, try to traverse from first node of the path to the last node of the path by setting edge to consecutive node.Mark last node of the path as leaf node after traversing each path. If you find any node marked as a leaf when traversing a path, you will stop traversing. Also remove the child of the node that marked as a leaf node. Every path from root node to leaf node in the final tree will be you answer. See figure below for more clarification:
enter image description here



Complexity will be sum of all path's length.






share|improve this answer





















  • What do you mean by "Also remove the child of the node that marked as a leaf node"?
    – Emadpres
    Nov 8 at 8:26






  • 1




    @Emadpres see in the figure. In After path 3 node 2 has child 3 and 4. After path 4 [1,2] remove child of node 2.
    – nightfury1204
    Nov 8 at 8:38















up vote
1
down vote













Try to build a tree using these path. For each path, try to traverse from first node of the path to the last node of the path by setting edge to consecutive node.Mark last node of the path as leaf node after traversing each path. If you find any node marked as a leaf when traversing a path, you will stop traversing. Also remove the child of the node that marked as a leaf node. Every path from root node to leaf node in the final tree will be you answer. See figure below for more clarification:
enter image description here



Complexity will be sum of all path's length.






share|improve this answer





















  • What do you mean by "Also remove the child of the node that marked as a leaf node"?
    – Emadpres
    Nov 8 at 8:26






  • 1




    @Emadpres see in the figure. In After path 3 node 2 has child 3 and 4. After path 4 [1,2] remove child of node 2.
    – nightfury1204
    Nov 8 at 8:38













up vote
1
down vote










up vote
1
down vote









Try to build a tree using these path. For each path, try to traverse from first node of the path to the last node of the path by setting edge to consecutive node.Mark last node of the path as leaf node after traversing each path. If you find any node marked as a leaf when traversing a path, you will stop traversing. Also remove the child of the node that marked as a leaf node. Every path from root node to leaf node in the final tree will be you answer. See figure below for more clarification:
enter image description here



Complexity will be sum of all path's length.






share|improve this answer












Try to build a tree using these path. For each path, try to traverse from first node of the path to the last node of the path by setting edge to consecutive node.Mark last node of the path as leaf node after traversing each path. If you find any node marked as a leaf when traversing a path, you will stop traversing. Also remove the child of the node that marked as a leaf node. Every path from root node to leaf node in the final tree will be you answer. See figure below for more clarification:
enter image description here



Complexity will be sum of all path's length.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 7 at 14:59









nightfury1204

80326




80326












  • What do you mean by "Also remove the child of the node that marked as a leaf node"?
    – Emadpres
    Nov 8 at 8:26






  • 1




    @Emadpres see in the figure. In After path 3 node 2 has child 3 and 4. After path 4 [1,2] remove child of node 2.
    – nightfury1204
    Nov 8 at 8:38


















  • What do you mean by "Also remove the child of the node that marked as a leaf node"?
    – Emadpres
    Nov 8 at 8:26






  • 1




    @Emadpres see in the figure. In After path 3 node 2 has child 3 and 4. After path 4 [1,2] remove child of node 2.
    – nightfury1204
    Nov 8 at 8:38
















What do you mean by "Also remove the child of the node that marked as a leaf node"?
– Emadpres
Nov 8 at 8:26




What do you mean by "Also remove the child of the node that marked as a leaf node"?
– Emadpres
Nov 8 at 8:26




1




1




@Emadpres see in the figure. In After path 3 node 2 has child 3 and 4. After path 4 [1,2] remove child of node 2.
– nightfury1204
Nov 8 at 8:38




@Emadpres see in the figure. In After path 3 node 2 has child 3 and 4. After path 4 [1,2] remove child of node 2.
– nightfury1204
Nov 8 at 8:38


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53187409%2fhow-to-find-a-list-of-unique-shortest-paths-in-a-list-of-paths%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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini