Questions tagged [shortest]
119 questions
16
votes
3 answers
How to find shortest path in this type of maze
Red Dot - Represents the initial location
Black Dot - Already occupied
Green - Free to occupy
Destination - Boundry of the matrix [which means either x = 0 or y = 0 or x = 8 or y = 8]
Example:
The red dot can place itself only one move at a time…

jpm
- 1,042
- 2
- 12
- 36
14
votes
5 answers
Java - Find shortest path between 2 points in a distance weighted map
I need an algorithm to find shortest path between two points in a map
where road distance is indicated by a number.
what is given:
Start City A
Destination City Z
List of Distances between Cities:
A - B : 10
F - K : 23
R - M : 8
K - O : 40
Z - P :…

sanjan
- 147
- 1
- 2
- 6
12
votes
1 answer
Finding all paths/walks of given length in a networkx graph
I'm using networkx and trying to find all the walks with length 3 in the graph, specifically the paths with three edges. I tried to find some information about the algorithms in the networkx documentation but I could only find the algorithms for the…

Josip Kolarić
- 456
- 1
- 7
- 21
10
votes
1 answer
how to enable the shortest match rule in flex (lexer)?
By default flex uses the longest match rule.
Is there any way to override this behavior to make it match the shortest sequence?
Thank you

VilleDePommes
- 119
- 1
- 6
8
votes
2 answers
A* algorithm not working properly
I need some help with my A* algorithm implementation.
When I run the algorithm it does find the goal, but the path is definately not the shortest :-P
Here is my code, please help me spot the bugs!
I think it might be the reconstruct path that is my…

Johan Berg
- 191
- 1
- 11
6
votes
4 answers
Asking for algorithm to find N Knights global shortest path
I've stampled upon a curious problem.
I've got an unbounded chessboard, N knights starting locations and N target locations.
The task is to find minimal number of moves for all knights to reach all target locations.
I know that shortest path problem…

mrjames
- 87
- 1
- 7
5
votes
2 answers
Detecting negative cycles using SPFA algorithm
Using the SPFA algorithm below in a directed graph with negative and positive weights, how can we detect negative cycles?
procedure Shortest-Path-Faster-Algorithm(G, s)
1 for each vertex v ≠ s in V(G)
2 d(v) := ∞
3 d(s) := 0
4 …

mehmetin
- 69
- 4
4
votes
4 answers
For every vertex in a graph, find all vertices within a distance d
In my particular case, the graph is represented as an adjacency list and is undirected and sparse, n can be in the millions, and d is 3. Calculating A^d (where A is the adjacency matrix) and picking out the non-zero entries works, but I'd like…

Robert D
- 137
- 1
- 8
4
votes
1 answer
One Line Task: Check Range - CodeWars (using JavaScript's methods)
Task
You're given an array of integers a and two integers x and y. Count the number of elements in the array such that `x ≤ a[i] ≤ y,
where i is the 0-based index of the element.
Code Limit
Less than 48 characters.
Example
For a = [2, 5, 6, 7,…

Abubakar101
- 121
- 2
- 6
4
votes
1 answer
Shortest distance of a location from a route (Python)
I have a location with its lat and long information
loc = (28.469723, 77.065292)
and a route (polyline) given by five lat long pairs
route = [(28.478324, 77.093916), (28.471647, 77.092457), (28.465498, 77.086105), (28.461273, 77.077651)]
Is there…

Saquib
- 273
- 3
- 9
4
votes
3 answers
Shortest path in a map
I have designed a weighted graph using a normalized adjacency list in mysql. Now I need to find the shortest path between two given nodes.
I have tried to use Dijkstra in php but I couldnt manage to implement it (too difficult for me). Another…

5lackp1x3l0x17
- 349
- 2
- 7
- 14
3
votes
1 answer
Find the shortest word, in a string of words
I am doing a bit of Code-Wars Challenges again and I have a question about this particular one:
Task: " Given a string of words, return the length of the shortest word(s).
String will never be empty and you do not need to account for different data…

Mateusz Woś
- 333
- 2
- 10
3
votes
2 answers
How can i extract last shortest string using regex in java
how can i extract bold range string in below
string :
hello world blah -d blah vlaah -n blah vlahh
hello world blah -n blah vlahh -d blah vlaah
hello world blah -d blaaah
I tried. -[dn] .*$ but it found longest match string like below
hello…

kimwz.kr
- 298
- 1
- 9
3
votes
1 answer
Connecting two points in multidimensional matrix
I'm trying to create a 3d matrix of 1's and 0's. I want to connect 2 points together (shortest distance) by forming a line of 1's in between them.
It would look something like this, but in 3d
path_pixels = [0,0,1,0,0,0,0,0,0,0,0,0,0,0,0;
…

Taimoor Khan
- 585
- 2
- 6
- 15
3
votes
1 answer
algorithm to traverse points horizontally and vertically
There are n points in the 2D plane. A robot wants to visit all of them but can only move horizontally or vertically. How should it visit all of them so that the total distance it covers is minimal?

SD.
- 31
- 1