Questions tagged [longest-path]

104 questions
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,168
  • 1
  • 9
  • 17
14
votes
6 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
  • 569
  • 2
  • 8
  • 16
11
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
  • 113
  • 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
  • 15,937
  • 49
  • 156
  • 269
8
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
  • 409
  • 1
  • 6
  • 21
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
  • 35,407
  • 69
  • 202
  • 359
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
  • 519
  • 2
  • 6
  • 18
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
  • 44,010
  • 23
  • 124
  • 163
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
  • 775
  • 4
  • 14
  • 28
3
votes
2 answers

Split a tree into equal parts by deleting an edge

I am looking for an algorithm to split a tree with N nodes (where the maximum degree of each node is 3) by removing one edge from it, so that the two trees that come as the result have as close as possible to N/2. How do I find the edge that is "the…
Michal Ferko
  • 95
  • 1
  • 9
3
votes
2 answers

Finding the hamiltonian path in a directed cyclic graph

i want to know if there is an algorithm to find the longest cyclic path in a directed weighted graph (i think this i a problem of finding the largest Hamiltonian sub-graph). I need to start from one vertex and return to the same vertex, whit the…
Joksim
  • 59
  • 5
1
2 3 4 5 6 7