-
-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Longest path length per vertex? #39
Comments
Hello, Ho indeed this is no more possible due to the move to private. I didn't thought about such use case when cleaning the library :-s BTW this seems legit usage. I think we can move back to public callable method. Note that it may cause issues if it is called without having run the algorithm, I mean on a direct call to it. |
Hi again @Bouke Sorry for not having provided you an alternative earlier. I think what you're searching is something described there. Here is a code sample using QuikGraph that should do the job, is it ok for you? var edges = new(string Source, string Target)[] {
("A", "D"),
("A", "B"),
("B", "C"),
("C", "E"),
("D", "E"),
};
var graph = new BidirectionalGraph<string, Edge<string>>();
graph.AddVerticesAndEdgeRange(edges.Select(edge => new Edge<string>(edge.Source, edge.Target)));
var pathLengths = new Dictionary<string, int>(graph.VertexCount);
foreach(var vertex in graph.Vertices)
pathLengths[vertex] = 0;
var dfs = new DepthFirstSearchAlgorithm<string, Edge<string>>(graph);
dfs.TreeEdge += OnEdgeAction;
dfs.BackEdge += OnEdgeAction;
dfs.ForwardOrCrossEdge += OnEdgeAction;
dfs.Compute("A");
void OnEdgeAction(Edge<string> edge)
{
pathLengths[edge.Target] = Math.Max(pathLengths[edge.Target], 1 + pathLengths[edge.Source]);
} |
Longest path per vertex will be any hamiltonian path that starts from this particular vertex, or if a hamiltonian cycle exists within graph, then longest path will be the same for all verticies and equal to a length of a this cycle. But because there is a cases when nor hamiltonian path, nor cycle exists I propose different solution. In order to build a longest path we can use Ant colony simulation. It will strike ants to do dfs with some prefering of choosing path depending on smell left on edge. After ant reached all nodes or stuck it will compute coefficient of it's path = (nodes count in path)/(path length) and if it is found path better than previous ants did it will update smell so this new path will be more preferable for other ants. This method allows to find a good approximation of longest path for any vertex. |
Hum, interesting, do you suggest having this kind of algorithm implemented directly in |
Just sharing my experience. |
I'd like to record the longest path length per vertex in my graph. Using the original package I could call
EdgeDepthFirstSearchAlgorithm.Visit
, to re-visit edges when I came across a longer path, but it has been made private. How would I now do this?Below an example of what used to work:
The text was updated successfully, but these errors were encountered: