Better performance for the shortest paths in undirected unweighted graph









up vote
1
down vote

favorite
1












I got the undirected unweighted graph. Where every node represents one city. Every city produces one type of grocery (does not have to be unique).
Then you will have on the input a number of type groceries which has to be delivered to every city. You can transport every grocery separately and it has to be the shortest way. The distance between every node is 1.



Actually, I'm using BFS for every node. But there should be a better solution.



Example: 
INPUT:

5(number of nodes) 5(number of edges)
4(number of groceries) 3(required number of groceries in every city)
0 1 3 2 1 ( city(node number) has grocery 0->0 1->1 2->3 3->2 4->1)
0 1 (edges)
2 1 (edges)
2 3 (edges)
3 0 (edges)
4 3 (edges)

OUTPUT:
11 (total distance)
2(total distance for town) 0 1 2 (groceries in the city)
2 1 0 3
2 3 1 2
2 2 0 1
3 1 2 0









share|improve this question



















  • 1




    You can do one BFS per grocery type, if there are not many.
    – zch
    Nov 9 at 22:49














up vote
1
down vote

favorite
1












I got the undirected unweighted graph. Where every node represents one city. Every city produces one type of grocery (does not have to be unique).
Then you will have on the input a number of type groceries which has to be delivered to every city. You can transport every grocery separately and it has to be the shortest way. The distance between every node is 1.



Actually, I'm using BFS for every node. But there should be a better solution.



Example: 
INPUT:

5(number of nodes) 5(number of edges)
4(number of groceries) 3(required number of groceries in every city)
0 1 3 2 1 ( city(node number) has grocery 0->0 1->1 2->3 3->2 4->1)
0 1 (edges)
2 1 (edges)
2 3 (edges)
3 0 (edges)
4 3 (edges)

OUTPUT:
11 (total distance)
2(total distance for town) 0 1 2 (groceries in the city)
2 1 0 3
2 3 1 2
2 2 0 1
3 1 2 0









share|improve this question



















  • 1




    You can do one BFS per grocery type, if there are not many.
    – zch
    Nov 9 at 22:49












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I got the undirected unweighted graph. Where every node represents one city. Every city produces one type of grocery (does not have to be unique).
Then you will have on the input a number of type groceries which has to be delivered to every city. You can transport every grocery separately and it has to be the shortest way. The distance between every node is 1.



Actually, I'm using BFS for every node. But there should be a better solution.



Example: 
INPUT:

5(number of nodes) 5(number of edges)
4(number of groceries) 3(required number of groceries in every city)
0 1 3 2 1 ( city(node number) has grocery 0->0 1->1 2->3 3->2 4->1)
0 1 (edges)
2 1 (edges)
2 3 (edges)
3 0 (edges)
4 3 (edges)

OUTPUT:
11 (total distance)
2(total distance for town) 0 1 2 (groceries in the city)
2 1 0 3
2 3 1 2
2 2 0 1
3 1 2 0









share|improve this question















I got the undirected unweighted graph. Where every node represents one city. Every city produces one type of grocery (does not have to be unique).
Then you will have on the input a number of type groceries which has to be delivered to every city. You can transport every grocery separately and it has to be the shortest way. The distance between every node is 1.



Actually, I'm using BFS for every node. But there should be a better solution.



Example: 
INPUT:

5(number of nodes) 5(number of edges)
4(number of groceries) 3(required number of groceries in every city)
0 1 3 2 1 ( city(node number) has grocery 0->0 1->1 2->3 3->2 4->1)
0 1 (edges)
2 1 (edges)
2 3 (edges)
3 0 (edges)
4 3 (edges)

OUTPUT:
11 (total distance)
2(total distance for town) 0 1 2 (groceries in the city)
2 1 0 3
2 3 1 2
2 2 0 1
3 1 2 0






graph-theory shortest-path breadth-first-search






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 22:19









Dominique Fortin

1,622816




1,622816










asked Oct 17 at 19:37









Lukáš Frajt

10619




10619







  • 1




    You can do one BFS per grocery type, if there are not many.
    – zch
    Nov 9 at 22:49












  • 1




    You can do one BFS per grocery type, if there are not many.
    – zch
    Nov 9 at 22:49







1




1




You can do one BFS per grocery type, if there are not many.
– zch
Nov 9 at 22:49




You can do one BFS per grocery type, if there are not many.
– zch
Nov 9 at 22:49

















active

oldest

votes











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%2f52862402%2fbetter-performance-for-the-shortest-paths-in-undirected-unweighted-graph%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52862402%2fbetter-performance-for-the-shortest-paths-in-undirected-unweighted-graph%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

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20