Hi, i need help with a graph problem.
I want to find all paths up to a length of X, but the paths can be forked.
(so we can take 3 nodes on the left, and 4 on the right if we want a length 7 path)
Can someone tell me what this graph problem is called, do we have a problem like this on Leetcode?
For each Node i have all adjacent nodes, (usually 2-4, some have 10 adj nodes)
i can precalc all adj nodes and all combinations of adj nodes for each Node.
Total Nodes are about 5000.
a simple DFS doesnt seem to work, because we can do partial forks / paths.
A Path needs to be connected.
A Path can go "backwards" so we can go length 6 on the left, and length 1 on the right
DataNode has the following props:
public List<List<DataNode>> CombosAdj
public List<DataNode> NodesAdj
id, value etcthis is the code i have so far, but its missing the partial forks:
(i'll optimize it with arrays later, but for testing a hashset is fine)
private int PathLen = 3;
public void DFS(DataNode current, HashSet<DataNode> currentPath)
{
if (currentPath.Count >= PathLen)
{
// Add currentPath to resultset
return;
}
foreach (var item in current.NodesAdj)
{
var curId = item.id;
if (currentPath.Contains(item)) continue;
currentPath.Add(item);
DFS(item, currentPath);
currentPath.Remove(item);
}
}
My manual calculations come up with
5 resuls for length 3 (including start 0, path must be connected with 0)
12? results for length 4
The final goal is to "collect" all nodes with the highest score, but first i just want to find all paths.
Example: