Class PathSimple
java.lang.Object
topics.backtracking.paths.PathSimple
Simple Paths in a Graph
Finds ALL simple paths from a source node to a target node using Backtracking. A simple path is a path that does not repeat any nodes.
*Complexity
Since finding all simple paths between two nodes in a complete graph involves exploring all possible permutations of intermediate nodes, the worst-case time complexity is factorial: O(N!).
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intprotected intprotected static final org.slf4j.Loggerprotected boolean[]protected final intprotected final String[]protected intprotected int[]protected intprotected intprotected int[][] -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidbacktrack(int current) voidintprotected StringgetPathString(int upToLength) voidsetSource(int source) voidsetTarget(int target) voidsetWeightMatrix(int[][] weights)
-
Field Details
-
log
protected static final org.slf4j.Logger log -
n
protected final int n -
nodes
-
weights
protected int[][] weights -
source
protected int source -
target
protected int target -
mark
protected boolean[] mark -
path
protected int[] path -
cost
protected int cost -
length
protected int length -
nsol
protected int nsol
-
-
Constructor Details
-
PathSimple
public PathSimple(int n)
-
-
Method Details
-
setSource
public void setSource(int source) -
setTarget
public void setTarget(int target) -
setWeightMatrix
public void setWeightMatrix(int[][] weights) -
backtracking
public void backtracking() -
backtrack
protected void backtrack(int current) -
getPathString
-
getNumberSolutions
public int getNumberSolutions() -
writeWeights
-