Javascript 我如何计算此问题的最短路径和最长路径;流动?
我的问答流程如下: 其基本思想是,根据为一个问题选择的答案,下一个问题将被提出 我目前使用以下JavaScript对象表示此问答流程:Javascript 我如何计算此问题的最短路径和最长路径;流动?,javascript,json,recursion,shortest-path,longest-path,Javascript,Json,Recursion,Shortest Path,Longest Path,我的问答流程如下: 其基本思想是,根据为一个问题选择的答案,下一个问题将被提出 我目前使用以下JavaScript对象表示此问答流程: var QAndAObj = { 1: { question: 'Question 1', answers: [ { answerText: 'Answer 1-1', nextQuestion: 2 }, { answerText: 'Answer 1-2
var QAndAObj = {
1: {
question: 'Question 1',
answers: [
{
answerText: 'Answer 1-1',
nextQuestion: 2
},
{
answerText: 'Answer 1-2',
nextQuestion: 3
}
]
},
2: {
question: 'Question 2',
answers: [
{
answerText: 'Answer 2-1',
nextQuestion: 3
},
{
answerText: 'Answer 2-2',
nextQuestion: null
}
]
},
3: {
question: 'Question 3',
answers: [
{
answerText: 'Answer 3-1',
nextQuestion: 4
},
{
answerText: 'Answer 3-2',
nextQuestion: null
},
{
answerText: 'Answer 3-3',
nextQuestion: null
}
]
},
4: {
question: 'Question 4',
answers: [
{
answerText: 'Answer 4-1',
nextQuestion: null
},
{
answerText: 'Answer 4-2',
nextQuestion: null
}
]
}
};
为了向用户显示进度条,我希望能够计算通过问题流的最长和最短路径
我最初的想法是编写一个递归函数,如下所示,沿着流中的每个可能路径:
function recurse(node) {
for (var i = 0; i < node.answers.length; i++) {
if (node.answers[i].nextQuestion) {
recurse(QAndAObj[node.answers[i].nextQuestion]);
}
}
}
函数递归(节点){
对于(var i=0;i
上面的函数确实允许我点击流中的每个节点,但我不确定如何计算通过流的最长和最短路径
任何帮助/建议/代码都将不胜感激。非常感谢。看看这个例子
function shortAndLong(QATree, startNode) {
var paths = [];
function findAllPaths(startNode, currentCost) {
for (var i = 0; i < startNode.answers.length; i++) {
var child = startNode.answers[i];
if (child.nextQuestion == null) {
paths.push(currentCost);
}else {
findAllPaths(QATree[child.nextQuestion], currentCost+1);
}
}
}
findAllPaths(startNode, 1);
return [Math.min.apply(Math, paths), Math.max.apply(Math, paths)]
}
console.debug('ans',shortAndLong(QAndAObj, QAndAObj[1]));//returns [2, 4]
console.debug('ans',shortAndLong(QAndAObj, QAndAObj[2]));//returns [1, 3]
console.debug('ans',shortAndLong(QAndAObj, QAndAObj[3]));//returns [1, 2]
console.debug('ans',shortAndLong(QAndAObj, QAndAObj[4]));//returns [1, 1]
函数shortAndLong(QATree,startNode){
var路径=[];
函数findAllPath(startNode,currentCost){
对于(变量i=0;i
最基本的是
function shortAndLong(QATree, startNode) {
var paths = [];
function findAllPaths(startNode, currentCost) {
for (var i = 0; i < startNode.answers.length; i++) {
var child = startNode.answers[i];
if (child.nextQuestion == null) {
paths.push(currentCost);
}else {
findAllPaths(QATree[child.nextQuestion], currentCost+1);
}
}
}
findAllPaths(startNode, 1);
return [Math.min.apply(Math, paths), Math.max.apply(Math, paths)]
}
console.debug('ans',shortAndLong(QAndAObj, QAndAObj[1]));//returns [2, 4]
console.debug('ans',shortAndLong(QAndAObj, QAndAObj[2]));//returns [1, 3]
console.debug('ans',shortAndLong(QAndAObj, QAndAObj[3]));//returns [1, 2]
console.debug('ans',shortAndLong(QAndAObj, QAndAObj[4]));//returns [1, 1]
函数shortAndLong(QATree,startNode){
var路径=[];
函数findAllPath(startNode,currentCost){
对于(变量i=0;i
最基本的是
function shortAndLong(QATree, startNode) {
var paths = [];
function findAllPaths(startNode, currentCost) {
for (var i = 0; i < startNode.answers.length; i++) {
var child = startNode.answers[i];
if (child.nextQuestion == null) {
paths.push(currentCost);
}else {
findAllPaths(QATree[child.nextQuestion], currentCost+1);
}
}
}
findAllPaths(startNode, 1);
return [Math.min.apply(Math, paths), Math.max.apply(Math, paths)]
}
console.debug('ans',shortAndLong(QAndAObj, QAndAObj[1]));//returns [2, 4]
console.debug('ans',shortAndLong(QAndAObj, QAndAObj[2]));//returns [1, 3]
console.debug('ans',shortAndLong(QAndAObj, QAndAObj[3]));//returns [1, 2]
console.debug('ans',shortAndLong(QAndAObj, QAndAObj[4]));//returns [1, 1]
函数shortAndLong(QATree,startNode){
var路径=[];
函数findAllPath(startNode,currentCost){
对于(变量i=0;i
最基本的是
function shortAndLong(QATree, startNode) {
var paths = [];
function findAllPaths(startNode, currentCost) {
for (var i = 0; i < startNode.answers.length; i++) {
var child = startNode.answers[i];
if (child.nextQuestion == null) {
paths.push(currentCost);
}else {
findAllPaths(QATree[child.nextQuestion], currentCost+1);
}
}
}
findAllPaths(startNode, 1);
return [Math.min.apply(Math, paths), Math.max.apply(Math, paths)]
}
console.debug('ans',shortAndLong(QAndAObj, QAndAObj[1]));//returns [2, 4]
console.debug('ans',shortAndLong(QAndAObj, QAndAObj[2]));//returns [1, 3]
console.debug('ans',shortAndLong(QAndAObj, QAndAObj[3]));//returns [1, 2]
console.debug('ans',shortAndLong(QAndAObj, QAndAObj[4]));//returns [1, 1]
函数shortAndLong(QATree,startNode){
var路径=[];
函数findAllPath(startNode,currentCost){
对于(变量i=0;i
最基本的是
var lengthLongestPath=函数(输入){
设maxLength=0;
设pathLength={0:0}
让lines=input.split(“\n”);
for(设i=0;i
var lengthLongestPath=函数(输入){
设maxLength=0;
设pathLength={0:0}
让lines=input.split(“\n”);
for(设i=0;i