Is there a more efficient way to calculate displacement for all possible paths in a grid?How to get all possible combinations of a list’s elements?Fastest way to list all primes below NEfficient way to rotate a list in pythonwhat is the neat way to divide huge nested loops to 8(or more) processes using Python?Most efficient way to reverse a numpy arrayWhat is the easiest way to remove all packages installed by pip?hamiltonian Path of an Obstructed Grid and Python Recursion LimtsGraph Theory: Finding all possible paths of 'n' length (with some constraints)Appending to lists in PythonMaximum OR value of 2D Array of bits
What does Argus Filch specifically do?
Search and replace a substring only if another substring is not present
Is there a general term for the items in a directory?
Unlocked Package Dependencies
What is it exactly about flying a Flyboard across the English channel that made Zapata's thighs burn?
Astable 555 circuit not oscillating
(7 of 11: Fillomino) What is Pyramid Cult's Favorite Shape?
What is a summary of basic Jewish metaphysics or theology?
Why isn't the new LEGO CV joint available on Bricklink or Brickowl?
Confused over role of 「自分が」in this particular passage
A wiild aanimal, a cardinal direction, or a place by the water
What is the reason behind water not falling from a bucket at the top of loop?
Do moonless nights cause dim light to become darkness, and bright light (e.g. from torches) to become dim light?
How to design an effective polearm-bow hybrid?
how to change ^L code in many files in ubuntu?
Being told my "network" isn't PCI compliant. I don't even have a server! Do I have to comply?
How to transform a function from f[#1] to f[x]
Is it moral to remove/hide certain parts of a photo, as a photographer?
Why is the Vasa Museum in Stockholm so Popular?
Current in only inductive AC circuit
Any information about the photo with Army Uniforms
Accurately recalling the key - can everyone do it?
What is the most 'environmentally friendly' way to learn to fly?
How long should I wait to plug in my refrigerator after unplugging it?
Is there a more efficient way to calculate displacement for all possible paths in a grid?
How to get all possible combinations of a list’s elements?Fastest way to list all primes below NEfficient way to rotate a list in pythonwhat is the neat way to divide huge nested loops to 8(or more) processes using Python?Most efficient way to reverse a numpy arrayWhat is the easiest way to remove all packages installed by pip?hamiltonian Path of an Obstructed Grid and Python Recursion LimtsGraph Theory: Finding all possible paths of 'n' length (with some constraints)Appending to lists in PythonMaximum OR value of 2D Array of bits
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to write an algorithm in Python which can calculate and store the maximum displacement for all possible paths in an n by m grid. There is no obstacle. We start at the origin and can travel either upwards or to the right by one unit length of the grid each time. For each path I calculate the maximum displacement (using a predefined formula), and that displacement is then appended to a list so it can be accessed after the program completes.
I can calculate the maximum displacement for each path without a problem, and I have implemented my solution such that only the result of the calculation is saved, as without this the RAM was being overloaded. It appears to work just fine with a grid size of 10 by 10. However, when I consider a larger grid, such as 30 by 30, the calculation will take an intractable time as there are an enormous number of paths to create. The code I am using is below.
# This function creates the initial path list and calls the function
# to create all possible paths
def findPaths(startRow,startCol,endRow,endCol):
path = [0 for d in range(endRow+endCol-startRow-startCol-1)]
findPathsUtil(endRow,endCol,startRow,startCol,path,0)
# This function is called iteratively until either of the if conditions are met
def findPathsUtil(endRow,endCol,currRow,currCol,path,indx):
global count_global
# If we reach the bottom of maze, we can only move right
if currRow==(endRow-1):
# Completes the remainder of the path until it hits the end point
for k in range(currCol,endCol):
path[indx+k-currCol] = (currRow,k)
# Calculates the maximum displacement for the current path
D_curr = max([abs( elem[1]/endCol - elem[0]/endRow ) for elem in path])
# Append this new displacement to a list of all other found displacements
D_list.append(D_curr)
return
# If we reach to the right most corner, we can only move down
if currCol == (endCol-1):
# Completes the remainder of the path until it hits the end point
for k in range(currRow,endRow):
path[indx+k-currRow] = (k,currCol)
# Calculates the maximum displacement for the current path
D_curr = max([abs( elem[1]/endCol - elem[0]/endRow ) for elem in path])
# Append this new displacement to a list of all other found displacements
D_list.append(D_curr)
return
# This is activated any time we have not yet hit one of the walls
else:
# add Current coordinate to the path list
path[indx]=(currRow,currCol)
findPathsUtil(endRow, endCol, currRow+1, currCol, path, indx+1)
findPathsUtil(endRow, endCol, currRow, currCol+1, path, indx+1)
if __name__ == '__main__':
# Initialize cell array to consider
startRow = 0
startCol = 0
endRow = 3
endCol = 3
global D_list
D_list = []
# First find all displacements for all possible paths are store in global variable D_list
findPaths(startRow,startCol,endRow+1,endCol+1)
I understand that a 30 by 30 grid has an incredibly large number of possible paths associated with it, but all things considered there are programs which consider grids much much larger. Is there any way to drastically reduce the time complexity of this calculation? Any thoughts or advice would be highly appreciated.
python dynamic-programming
add a comment |
I am trying to write an algorithm in Python which can calculate and store the maximum displacement for all possible paths in an n by m grid. There is no obstacle. We start at the origin and can travel either upwards or to the right by one unit length of the grid each time. For each path I calculate the maximum displacement (using a predefined formula), and that displacement is then appended to a list so it can be accessed after the program completes.
I can calculate the maximum displacement for each path without a problem, and I have implemented my solution such that only the result of the calculation is saved, as without this the RAM was being overloaded. It appears to work just fine with a grid size of 10 by 10. However, when I consider a larger grid, such as 30 by 30, the calculation will take an intractable time as there are an enormous number of paths to create. The code I am using is below.
# This function creates the initial path list and calls the function
# to create all possible paths
def findPaths(startRow,startCol,endRow,endCol):
path = [0 for d in range(endRow+endCol-startRow-startCol-1)]
findPathsUtil(endRow,endCol,startRow,startCol,path,0)
# This function is called iteratively until either of the if conditions are met
def findPathsUtil(endRow,endCol,currRow,currCol,path,indx):
global count_global
# If we reach the bottom of maze, we can only move right
if currRow==(endRow-1):
# Completes the remainder of the path until it hits the end point
for k in range(currCol,endCol):
path[indx+k-currCol] = (currRow,k)
# Calculates the maximum displacement for the current path
D_curr = max([abs( elem[1]/endCol - elem[0]/endRow ) for elem in path])
# Append this new displacement to a list of all other found displacements
D_list.append(D_curr)
return
# If we reach to the right most corner, we can only move down
if currCol == (endCol-1):
# Completes the remainder of the path until it hits the end point
for k in range(currRow,endRow):
path[indx+k-currRow] = (k,currCol)
# Calculates the maximum displacement for the current path
D_curr = max([abs( elem[1]/endCol - elem[0]/endRow ) for elem in path])
# Append this new displacement to a list of all other found displacements
D_list.append(D_curr)
return
# This is activated any time we have not yet hit one of the walls
else:
# add Current coordinate to the path list
path[indx]=(currRow,currCol)
findPathsUtil(endRow, endCol, currRow+1, currCol, path, indx+1)
findPathsUtil(endRow, endCol, currRow, currCol+1, path, indx+1)
if __name__ == '__main__':
# Initialize cell array to consider
startRow = 0
startCol = 0
endRow = 3
endCol = 3
global D_list
D_list = []
# First find all displacements for all possible paths are store in global variable D_list
findPaths(startRow,startCol,endRow+1,endCol+1)
I understand that a 30 by 30 grid has an incredibly large number of possible paths associated with it, but all things considered there are programs which consider grids much much larger. Is there any way to drastically reduce the time complexity of this calculation? Any thoughts or advice would be highly appreciated.
python dynamic-programming
This code does the iteration in 0.04s on tio.com, I don't know what else you want to do with it but I hope it helps. My code simply counts the 118264581564861424 possible paths.
– Artemis Fowl
Mar 27 at 1:29
add a comment |
I am trying to write an algorithm in Python which can calculate and store the maximum displacement for all possible paths in an n by m grid. There is no obstacle. We start at the origin and can travel either upwards or to the right by one unit length of the grid each time. For each path I calculate the maximum displacement (using a predefined formula), and that displacement is then appended to a list so it can be accessed after the program completes.
I can calculate the maximum displacement for each path without a problem, and I have implemented my solution such that only the result of the calculation is saved, as without this the RAM was being overloaded. It appears to work just fine with a grid size of 10 by 10. However, when I consider a larger grid, such as 30 by 30, the calculation will take an intractable time as there are an enormous number of paths to create. The code I am using is below.
# This function creates the initial path list and calls the function
# to create all possible paths
def findPaths(startRow,startCol,endRow,endCol):
path = [0 for d in range(endRow+endCol-startRow-startCol-1)]
findPathsUtil(endRow,endCol,startRow,startCol,path,0)
# This function is called iteratively until either of the if conditions are met
def findPathsUtil(endRow,endCol,currRow,currCol,path,indx):
global count_global
# If we reach the bottom of maze, we can only move right
if currRow==(endRow-1):
# Completes the remainder of the path until it hits the end point
for k in range(currCol,endCol):
path[indx+k-currCol] = (currRow,k)
# Calculates the maximum displacement for the current path
D_curr = max([abs( elem[1]/endCol - elem[0]/endRow ) for elem in path])
# Append this new displacement to a list of all other found displacements
D_list.append(D_curr)
return
# If we reach to the right most corner, we can only move down
if currCol == (endCol-1):
# Completes the remainder of the path until it hits the end point
for k in range(currRow,endRow):
path[indx+k-currRow] = (k,currCol)
# Calculates the maximum displacement for the current path
D_curr = max([abs( elem[1]/endCol - elem[0]/endRow ) for elem in path])
# Append this new displacement to a list of all other found displacements
D_list.append(D_curr)
return
# This is activated any time we have not yet hit one of the walls
else:
# add Current coordinate to the path list
path[indx]=(currRow,currCol)
findPathsUtil(endRow, endCol, currRow+1, currCol, path, indx+1)
findPathsUtil(endRow, endCol, currRow, currCol+1, path, indx+1)
if __name__ == '__main__':
# Initialize cell array to consider
startRow = 0
startCol = 0
endRow = 3
endCol = 3
global D_list
D_list = []
# First find all displacements for all possible paths are store in global variable D_list
findPaths(startRow,startCol,endRow+1,endCol+1)
I understand that a 30 by 30 grid has an incredibly large number of possible paths associated with it, but all things considered there are programs which consider grids much much larger. Is there any way to drastically reduce the time complexity of this calculation? Any thoughts or advice would be highly appreciated.
python dynamic-programming
I am trying to write an algorithm in Python which can calculate and store the maximum displacement for all possible paths in an n by m grid. There is no obstacle. We start at the origin and can travel either upwards or to the right by one unit length of the grid each time. For each path I calculate the maximum displacement (using a predefined formula), and that displacement is then appended to a list so it can be accessed after the program completes.
I can calculate the maximum displacement for each path without a problem, and I have implemented my solution such that only the result of the calculation is saved, as without this the RAM was being overloaded. It appears to work just fine with a grid size of 10 by 10. However, when I consider a larger grid, such as 30 by 30, the calculation will take an intractable time as there are an enormous number of paths to create. The code I am using is below.
# This function creates the initial path list and calls the function
# to create all possible paths
def findPaths(startRow,startCol,endRow,endCol):
path = [0 for d in range(endRow+endCol-startRow-startCol-1)]
findPathsUtil(endRow,endCol,startRow,startCol,path,0)
# This function is called iteratively until either of the if conditions are met
def findPathsUtil(endRow,endCol,currRow,currCol,path,indx):
global count_global
# If we reach the bottom of maze, we can only move right
if currRow==(endRow-1):
# Completes the remainder of the path until it hits the end point
for k in range(currCol,endCol):
path[indx+k-currCol] = (currRow,k)
# Calculates the maximum displacement for the current path
D_curr = max([abs( elem[1]/endCol - elem[0]/endRow ) for elem in path])
# Append this new displacement to a list of all other found displacements
D_list.append(D_curr)
return
# If we reach to the right most corner, we can only move down
if currCol == (endCol-1):
# Completes the remainder of the path until it hits the end point
for k in range(currRow,endRow):
path[indx+k-currRow] = (k,currCol)
# Calculates the maximum displacement for the current path
D_curr = max([abs( elem[1]/endCol - elem[0]/endRow ) for elem in path])
# Append this new displacement to a list of all other found displacements
D_list.append(D_curr)
return
# This is activated any time we have not yet hit one of the walls
else:
# add Current coordinate to the path list
path[indx]=(currRow,currCol)
findPathsUtil(endRow, endCol, currRow+1, currCol, path, indx+1)
findPathsUtil(endRow, endCol, currRow, currCol+1, path, indx+1)
if __name__ == '__main__':
# Initialize cell array to consider
startRow = 0
startCol = 0
endRow = 3
endCol = 3
global D_list
D_list = []
# First find all displacements for all possible paths are store in global variable D_list
findPaths(startRow,startCol,endRow+1,endCol+1)
I understand that a 30 by 30 grid has an incredibly large number of possible paths associated with it, but all things considered there are programs which consider grids much much larger. Is there any way to drastically reduce the time complexity of this calculation? Any thoughts or advice would be highly appreciated.
python dynamic-programming
python dynamic-programming
asked Mar 27 at 1:18
ChironChiron
83 bronze badges
83 bronze badges
This code does the iteration in 0.04s on tio.com, I don't know what else you want to do with it but I hope it helps. My code simply counts the 118264581564861424 possible paths.
– Artemis Fowl
Mar 27 at 1:29
add a comment |
This code does the iteration in 0.04s on tio.com, I don't know what else you want to do with it but I hope it helps. My code simply counts the 118264581564861424 possible paths.
– Artemis Fowl
Mar 27 at 1:29
This code does the iteration in 0.04s on tio.com, I don't know what else you want to do with it but I hope it helps. My code simply counts the 118264581564861424 possible paths.
– Artemis Fowl
Mar 27 at 1:29
This code does the iteration in 0.04s on tio.com, I don't know what else you want to do with it but I hope it helps. My code simply counts the 118264581564861424 possible paths.
– Artemis Fowl
Mar 27 at 1:29
add a comment |
0
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',
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
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55368419%2fis-there-a-more-efficient-way-to-calculate-displacement-for-all-possible-paths-i%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- 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.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55368419%2fis-there-a-more-efficient-way-to-calculate-displacement-for-all-possible-paths-i%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
This code does the iteration in 0.04s on tio.com, I don't know what else you want to do with it but I hope it helps. My code simply counts the 118264581564861424 possible paths.
– Artemis Fowl
Mar 27 at 1:29