Questions tagged [longest-path]

106 questions
17
votes
7 answers

Dijkstra for longest path in a DAG

I am trying to find out if it is possible to use Dijkstra's algorithm to find the longest path in a directed acyclic path. I know that it is not possible to find the longest path with Dijkstra in a general graph, because of negative cost cycles. But…
punkyduck
  • 669
  • 2
  • 9
  • 17
15
votes
1 answer

Optimizations for longest path problem in cyclic graph

What optimizations exist for trying to find the longest path in a cyclic graph? Longest path in cyclic graphs is known to be NP-complete. What optimizations or heuristics can make finding the longest path faster than DFSing the entire graph? Are…
Craig Younkins
  • 1,360
  • 2
  • 11
  • 18
12
votes
4 answers

Why is topological sort needed for Longest Path in Directed Acyclic Graph?

Problem: Given a Weighted Directed Acyclic Graph (DAG) and a source vertex s in it, find the longest distances from s to all other vertices in the given graph. Please find the reference graph: link Why do we need topological sorting? Can we not…
user1447073
  • 123
  • 1
  • 1
  • 5
11
votes
1 answer

How do find the longest path in a cyclic Graph between two nodes?

I already solved most the questions posted here, all but the longest path one. I've read the Wikipedia article about longest paths and it seems any easy problem if the graph was acyclic, which mine is not. How do I solve the problem then? Brute…
rfgamaral
  • 16,546
  • 57
  • 163
  • 275
10
votes
2 answers

finding longest path in a graph

I am trying to solve a program, where in I have to find the max number of cities connected for a given list of routes. for eg: if the given route is [['1', '2'], ['2', '4'], ['1', '11'], ['4', '11']] then max cities connected will be…
LearningNinja
  • 437
  • 1
  • 8
  • 23
7
votes
2 answers

How to find the longest path between two nodes in Lisp?

I need to program a Lisp function that finds the longest path between two nodes, without revisiting any nodes. Though, if the start and end node are the same, this node can be revisited. The function needs to be both recursive and…
Jay
  • 183
  • 6
6
votes
1 answer

Computational complexity of a longest path algorithm witn a recursive method

I wrote a code segment to determine the longest path in a graph. Following is the code. But I don't know how to get the computational complexity in it because of the recursive method in the middle. Since finding the longest path is an NP complete…
nirandi
  • 63
  • 1
  • 5
6
votes
6 answers

Print the longest path starting from root in a binary tree

In this tree: a / \ b d / / \ c e f / g The longest path starting from the root would be a-d-f-g Here is my attempt: class Node: def __init__(self, x): self.val = x self.left = None self.right =…
coder_learner
  • 95
  • 1
  • 8
6
votes
2 answers

How to find the longest simple path in a graph?

I know that for non-directed graph this problem is NP-complete hence we should do Brute Force in order to check all possible paths. How we can do that? Please suggest a pseudo code and tell me the complexity of that algorithm. If there are…
Narek
  • 38,779
  • 79
  • 233
  • 389
6
votes
4 answers

Longest chain of elements from list in Python

I have a list of nations, and I want to have the longest path of nations where each country chosen must begin with the same letter that ended the previous element nations = ['albania','andorra','austria','belarus','belgium','bosnia and…
fege
  • 547
  • 3
  • 7
  • 19
4
votes
2 answers

How to find the maximum-weight path between two vertices in a DAG?

In a DAG G, with non negative weighted edges, how do you find the maximum-weight path between two vertices in G? Thank you guys!
Jonathan
  • 41
  • 1
  • 2
4
votes
2 answers

Longest path search on a topologically sorted weighted directed acyclic graph, but with a maximum edge count for valid paths

I know that longest/shortest path can be found in linear time by "processing the vertices in a topological order, and calculating the path length for each vertex to be the minimum or maximum length obtained via any of its incoming edges", or to put…
Acorn
  • 49,061
  • 27
  • 133
  • 172
4
votes
1 answer

Cypher COLLECT makes UNWIND unwind in wrong order

Graph gist: http://gist.neo4j.org/?6182d024325343760cb4 I want to get a (longest) path in order and it works as expected until I add a COLLECT statement, is there something concerning Cypher and COLLECT that I just don't understand or is this a…
Lundberg
  • 111
  • 7
4
votes
1 answer

In a graph, find longest path with a certain property?

I have a directed graph (more specifically, a control flow graph), and each of the vertices has a set of properties. I'd like to find (or write) an algorithm that, given a vertex V with property P, finds the longest path to some vertex E such that…
stepthom
  • 1,432
  • 2
  • 16
  • 27
3
votes
1 answer

solving the Longest-Path-Length. Is my solution correct?

This is the question [From CLRS]: Define the optimization problem LONGEST-PATH-LENGTH as the relation that associates each instance of an undirected graph and two vertices with the number of edges in a longest simple path between the two vertices.…
Rave
  • 835
  • 4
  • 15
  • 28
1
2 3 4 5 6 7 8