JSFiddle - React, Tailwind, and code Playground

JavaScript

//Vertext numbering goes from zero
function findPath(path, infectedNodes, nodesBeforeDeath, initialNode, targetNode, adjacentTable) {
  const lastNode = path.visitedNodes[path.visitedNodes.length - 1];
  
  if (lastNode === targetNode) {
  	return path;
  }

	if (path.nodesLeftBeforeDeath !== null && path.nodesLeftBeforeDeath !== undefined && path.nodesLeftBeforeDeath == 0) {
  	return;
  }
 
  const adjacentNodes = adjacentTable[lastNode];
  
  for (const node of adjacentNodes) {
  	if (path.visitedNodes.indexOf(node) > -1) {
    	continue;
    }
    
  	const newPath = {
    	visitedNodes: [...path.visitedNodes, node]
    };
    
    if (path.nodesLeftBeforeDeath !== null && path.nodesLeftBeforeDeath !== undefined) {
    	newPath.nodesLeftBeforeDeath = path.nodesLeftBeforeDeath - 1;
    }
    else if (infectedNodes.indexOf(node) > - 1) {
    	newPath.nodesLeftBeforeDeath = nodesBeforeDeath;
    }
    
  	const nodeResult = findPath(newPath, infectedNodes, nodesBeforeDeath, initialNode, targetNode, adjacentTable);
    
    if (nodeResult) {
    	return nodeResult;
    }
  }
}

const firstExampleResult = findPath({
	visitedNodes: [0]
	},
  [1, 2, 4],
  1,
  0,
  13,
	[[1,2,3,4],
  [0,4,6],
  [0, 7, 8],
  [0, 9, 10],
  [0, 1, 11, 12],
  [6, 12, 13],
  [1, 5, 7],
  [2,6,14],
  [2,9,14],
  [3,8],
  [3, 11],
  [4,10,12],
  [4,5,11],
  [5],
  [7,8]]
);

console.log(firstExampleResult.visitedNodes);

const secondExampleResult = findPath({
	visitedNodes: [0]
	},
  [4, 6, 7],
  2,
  0,
  9,
	[
  [1],
  [0,2],
  [1,3],
  [2,4,5],
  [3,7],
  [3,7],
  [7,9],
  [4,5,6,8],
  [7,9],
  [6,8]
  ]
);

console.log(secondExampleResult.visitedNodes);