feat: Produce an O(n) to O(log2(n)) improvement in findPaths

Details: The findPathsInner function in lib/findPaths.js is too
expensive, and has a tendency to throw "too much recursion" errors.
However, it can be optimized. In particular, instead of just
going through all paths, we could go in the paths in the
right direction.

Note that: The current improvements don't do that yet. I was trying
to do that at the findDistance level, but I was being dumb.
This commit is contained in:
NunoSempere 2021-12-07 20:40:43 +01:00
parent f8791450a2
commit 74d1f2be23
4 changed files with 141 additions and 102 deletions

View File

@ -1,6 +1,3 @@
/* Definitions */
const elementsDocument = null//
/* Imports */ /* Imports */
import Head from 'next/head' import Head from 'next/head'
import React, { useState } from "react"; import React, { useState } from "react";
@ -13,6 +10,9 @@ import { TextAreaForJson } from "./textAreaForJson"
import { pushToMongo } from "./pushToMongo.js" import { pushToMongo } from "./pushToMongo.js"
import { maxMergeSortSteps, expectedNumMergeSortSteps } from "./utils.js" import { maxMergeSortSteps, expectedNumMergeSortSteps } from "./utils.js"
/* DEFINTIONS */
const DEFAULT_COMPARE = 2 // 1, unless you're testing smth.
/* Helpers */ /* Helpers */
let increasingList = (n) => Array.from(Array(n).keys()) let increasingList = (n) => Array.from(Array(n).keys())
let buildLinks = quantitativeComparisons => quantitativeComparisons.map(([element1, element2, distance, reasoning]) => ({ source: element1, target: element2, distance: distance, reasoning: reasoning })) let buildLinks = quantitativeComparisons => quantitativeComparisons.map(([element1, element2, distance, reasoning]) => ({ source: element1, target: element2, distance: distance, reasoning: reasoning }))
@ -76,7 +76,7 @@ export default function ComparisonView({ listOfElementsForView }) {
let [showAdvancedOptions, changeShowAdvanceOptions] = useState(initialShowAdvancedOptions); let [showAdvancedOptions, changeShowAdvanceOptions] = useState(initialShowAdvancedOptions);
let [showComparisons, changeShowComparisons] = useState(initialShowComparisons); let [showComparisons, changeShowComparisons] = useState(initialShowComparisons);
let [showChangeDataSet, changeshowChangeDataSet] = useState(initialShowChangeDataSet); let [showChangeDataSet, changeshowChangeDataSet] = useState(initialShowChangeDataSet);
let [numSteps, increaseNumSteps] = useState(initialNumSteps); let [numSteps, changeNumSteps] = useState(initialNumSteps);
let [maxSteps, changeMaxSteps] = useState(initialMaxSteps) let [maxSteps, changeMaxSteps] = useState(initialMaxSteps)
let [expectedSteps, changeExpectedSteps] = useState(initialExpectedSteps) let [expectedSteps, changeExpectedSteps] = useState(initialExpectedSteps)
@ -87,6 +87,7 @@ export default function ComparisonView({ listOfElementsForView }) {
setQuantitativeComparisons(initialQuantitativeComparisons) setQuantitativeComparisons(initialQuantitativeComparisons)
setIsListOrdered(initialIsListOdered) setIsListOrdered(initialIsListOdered)
setOrderedList(initialOrderedList) setOrderedList(initialOrderedList)
changeNumSteps(0)
removeOldSvg() removeOldSvg()
} }
@ -203,9 +204,9 @@ export default function ComparisonView({ listOfElementsForView }) {
let newQuantitativeComparisons = [...quantitativeComparisons, newQuantitativeComparison] let newQuantitativeComparisons = [...quantitativeComparisons, newQuantitativeComparison]
setQuantitativeComparisons(newQuantitativeComparisons) setQuantitativeComparisons(newQuantitativeComparisons)
setSliderValue(1) setSliderValue(DEFAULT_COMPARE)
setReasoning('') setReasoning('')
increaseNumSteps(numSteps + 1) changeNumSteps(numSteps + 1)
if (successStatus) { if (successStatus) {
let jsObject = nicelyFormatLinks(quantitativeComparisons, listOfElements) let jsObject = nicelyFormatLinks(quantitativeComparisons, listOfElements)
await pushToMongo(jsObject) await pushToMongo(jsObject)

View File

@ -3,30 +3,28 @@ import React from "react";
import { toLocale, truncateValueForDisplay, numToAlphabeticalString, formatLargeOrSmall } from "../lib/utils.js" import { toLocale, truncateValueForDisplay, numToAlphabeticalString, formatLargeOrSmall } from "../lib/utils.js"
/* Utilities */ /* Utilities */
let avg = arr => arr.reduce((a,b) => (a+b)) / arr.length let avg = arr => arr.reduce((a, b) => (a + b), 0) / arr.length
/* Main function */ /* Main function */
function findPathsInner({sourceElementId, targetElementId, pathSoFar, links, nodes, maxLengthOfPath}){ function findPathsInner({ sourceElementId, targetElementId, pathSoFar, links, nodes, maxLengthOfPath }) {
// THis has a tendency to produce too much recursion errors
// could be refactored
let paths = [] let paths = []
if(maxLengthOfPath > 0){ if (maxLengthOfPath > 0) {
for(let link of links){ for (let link of links) {
if( if (
((link.source == sourceElementId) && (link.target == targetElementId)) || ((link.source == sourceElementId) && (link.target == targetElementId)) ||
((link.source == targetElementId) && (link.target == sourceElementId)) ((link.source == targetElementId) && (link.target == sourceElementId))
){ ) {
paths.push(pathSoFar.concat(link).flat()) paths.push(pathSoFar.concat(link).flat())
} else if((link.source == sourceElementId)){ } else if ((link.source == sourceElementId)) {
let newPaths = findPathsInner({sourceElementId:link.target, targetElementId, pathSoFar: pathSoFar.concat(link).flat(), links, nodes, maxLengthOfPath: (maxLengthOfPath-1)}) let newPaths = findPathsInner({ sourceElementId: link.target, targetElementId, pathSoFar: pathSoFar.concat(link).flat(), links, nodes, maxLengthOfPath: (maxLengthOfPath - 1) })
if(newPaths.length != 0){ if (newPaths.length != 0) {
paths.push(...newPaths) paths.push(...newPaths)
} }
}else if((link.target == sourceElementId)){ } else if ((link.target == sourceElementId)) {
let newPaths = findPathsInner({sourceElementId:link.source, targetElementId, pathSoFar: pathSoFar.concat(link).flat(), links, nodes, maxLengthOfPath: (maxLengthOfPath-1)}) let newPaths = findPathsInner({ sourceElementId: link.source, targetElementId, pathSoFar: pathSoFar.concat(link).flat(), links, nodes, maxLengthOfPath: (maxLengthOfPath - 1) })
if(newPaths.length != 0){ if (newPaths.length != 0) {
paths.push(...newPaths) paths.push(...newPaths)
} }
} }
@ -36,38 +34,34 @@ function findPathsInner({sourceElementId, targetElementId, pathSoFar, links, nod
return paths return paths
} }
function findPaths({sourceElementId, targetElementId, links, nodes}){ function findPaths({ sourceElementId, targetElementId, links, nodes }) {
let positionSourceElement = nodes.map((element, i) => (element.id)).indexOf(sourceElementId) let positionSourceElement = nodes.map((element, i) => (element.id)).indexOf(sourceElementId)
let positionTargetElement = nodes.map((element, i) => (element.id)).indexOf(targetElementId) let positionTargetElement = nodes.map((element, i) => (element.id)).indexOf(targetElementId)
let maxLengthOfPath = Math.abs(positionSourceElement - positionTargetElement) let maxLengthOfPath = Math.abs(positionSourceElement - positionTargetElement)
let paths = [] return findPathsInner({ sourceElementId, targetElementId, pathSoFar: [], links, nodes, maxLengthOfPath })
try{
paths = findPathsInner({sourceElementId, targetElementId, pathSoFar: [], links, nodes, maxLengthOfPath})
}catch(error){
console.log("Error: probably too much recursion.")
}
return paths
} }
function findDistance({sourceElementId, targetElementId, nodes, links}){ function findDistance({ sourceElementId, sourceElementPosition, targetElementId, targetElementPosition, nodes, links, direction }) {
let paths = findPaths({sourceElementId, targetElementId, nodes, links}) let paths = findPaths({ sourceElementId, targetElementId, nodes, links })
console.log(`findDistance from ${sourceElementPosition} to ${targetElementPosition}`)
console.log(targetElementId) console.log(targetElementId)
console.log(direction)
console.log(paths) console.log(paths)
let weights = [] let weights = []
for(let path of paths){ for (let path of paths) {
let currentSource = sourceElementId let currentSource = sourceElementId
let weight = 1 let weight = 1
for(let element of path){ for (let element of path) {
let distance = 0 let distance = 0
if(element.source == currentSource){ if (element.source == currentSource) {
distance = element.distance distance = element.distance
currentSource = element.target currentSource = element.target
}else if(element.target == currentSource){ } else if (element.target == currentSource) {
distance = 1/Number(element.distance) distance = 1 / Number(element.distance)
currentSource = element.source currentSource = element.source
} }
weight = weight*distance weight = weight * distance
} }
weights.push(weight) weights.push(weight)
@ -78,52 +72,97 @@ function findDistance({sourceElementId, targetElementId, nodes, links}){
//return weights.map(weight => Math.round(weight*100)/100) //return weights.map(weight => Math.round(weight*100)/100)
} }
function findDistancesForAllElements({nodes, links}){ function getDirectionalLinks({ nodes, links }) {
console.log("getDirectionalLinks")
// direction: 1 for upwards, -1 for downwards
let upwardsLinks = []
let downwardsLinks = []
links.forEach(link => {
console.log(link)
let sourceElementId = link.source
let targetElementId = link.target
if (link.distance < 1) {
// We already deal with this case upstream, but whatever.
[sourceElementId, targetElementId] = [targetElementId, sourceElementId]
}
let sourceElementPosition = nodes.find(element => element.id == sourceElementId).position
let targetElementPosition = nodes.find(element => element.id == targetElementId).position
if (link.distance == 1) {
// If two elements are the same, then they belong to both upwards and downwards paths!!
upwardsLinks.push(link)
downwardsLinks.push(link)
} else if (sourceElementPosition < targetElementPosition) {
upwardsLinks.push(link)
} else {
downwardsLinks.push(link)
}
})
console.log([upwardsLinks, downwardsLinks])
return [upwardsLinks, downwardsLinks]
}
function findDistancesForAllElements({ nodes, links }) {
let referenceElements = nodes.filter(x => x.isReferenceValue) let referenceElements = nodes.filter(x => x.isReferenceValue)
let midpoint = Math.round(nodes.length/2) let midpoint = Math.round(nodes.length / 2)
let referenceElement = referenceElements.length > 0 ? referenceElements[0] : nodes[midpoint] let referenceElement = referenceElements.length > 0 ? referenceElements[0] : nodes[midpoint]
// console.log(nodes) // console.log(nodes)
let distances = nodes.map(node => let [upwardsLinks, downwardsLinks] = getDirectionalLinks({ nodes, links })
node.isReferenceValue || (node.id == referenceElement.id) ? [1] : findDistance({sourceElementId: referenceElement.id, targetElementId: node.id, nodes, links}) console.log(`referenceElement.position: ${referenceElement.position}`)
) let distances = nodes.map(node => {
if (node.isReferenceValue || (node.id == referenceElement.id)) {
return [1]
} else {
console.log("node")
console.log(node)
let isUpwardsDirection = referenceElement.position < node.position;
let distance = findDistance({
sourceElementId: referenceElement.id,
sourceElementPosition: referenceElement.position,
targetElementId: node.id,
targetElementPosition: node.position,
nodes: nodes,
links: links, // isUpwardsDirection ? upwardsLinks : downwardsLinks, // links
direction: isUpwardsDirection ? "upwards" : "downwards"
})
return distance
}
})
return distances return distances
} }
function abridgeArrayAndDisplay(array){ function abridgeArrayAndDisplay(array) {
let newArray let newArray
let formatForDisplay let formatForDisplay
if(array.length > 10){ if (array.length > 10) {
newArray = array.slice(0,9) newArray = array.slice(0, 9)
formatForDisplay= newArray.map(d => formatLargeOrSmall(d)) formatForDisplay = newArray.map(d => formatLargeOrSmall(d))
formatForDisplay[9] = "..." formatForDisplay[9] = "..."
}else{ } else {
newArray=array newArray = array
formatForDisplay= newArray.map(d => formatLargeOrSmall(d)) formatForDisplay = newArray.map(d => formatLargeOrSmall(d))
} }
let result = JSON.stringify(formatForDisplay, null, 2).replaceAll(`"`, "") let result = JSON.stringify(formatForDisplay, null, 2).replaceAll(`"`, "")
return result return result
} }
function CreateTableWithDistances({isListOrdered, orderedList, listOfElements, links}){ export function CreateTableWithDistances({ isListOrdered, orderedList, listOfElements, links }) {
// Not used anywhere because it's too resource intensive if (!isListOrdered || orderedList.length < listOfElements.length) {
// The culprit is findPathsInner, a recursive function which
// has a tendency to produce "Maximum call stack size exceeded"
// or "too much recursion" errors
if(!isListOrdered || orderedList.length < listOfElements.length){
return (<div>{""}</div>) return (<div>{""}</div>)
} else { } else {
let nodes = orderedList.map(i => listOfElements[i]) let nodes = orderedList.map((id, pos) => ({ ...listOfElements[id], position: pos }))
let distances = findDistancesForAllElements({nodes, links}) let distances = findDistancesForAllElements({ nodes, links })
let rows = nodes.map((element, i) => ({id: numToAlphabeticalString(element.id), name: element.name, distances: distances[i]})) let rows = nodes.map((element, i) => ({ id: numToAlphabeticalString(element.position), position: element.position, name: element.name, distances: distances[i] }))
console.log("rows@CreateTableWithDistances") console.log("rows@CreateTableWithDistances")
console.log(rows) console.log(rows)
return( return (
<div> <div>
<table className=""> <table className="">
<thead > <thead >
<tr > <tr >
<th >Id</th> <th >Id</th>
<th>&nbsp;&nbsp;&nbsp;</th> <th>&nbsp;&nbsp;&nbsp;</th>
<th >Position</th>
<th>&nbsp;&nbsp;&nbsp;</th>
<th >Element</th> <th >Element</th>
<th> &nbsp;&nbsp;&nbsp;</th> <th> &nbsp;&nbsp;&nbsp;</th>
<th >Possible relative values</th> <th >Possible relative values</th>
@ -132,9 +171,11 @@ function CreateTableWithDistances({isListOrdered, orderedList, listOfElements, l
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{rows.filter(row => row.distances.length > 0).map(row => <tr key={row.id}> {rows.map(row => <tr key={row.id}>
<td className="" >{row.id}</td> <td className="" >{row.id}</td>
<td>&nbsp;&nbsp;&nbsp;</td> <td>&nbsp;&nbsp;&nbsp;</td>
<td className="" >{row.position}</td>
<td>&nbsp;&nbsp;&nbsp;</td>
<td className="">{row.name}</td> <td className="">{row.name}</td>
<td>&nbsp;&nbsp;&nbsp;</td> <td>&nbsp;&nbsp;&nbsp;</td>
<td className="">{abridgeArrayAndDisplay(row.distances)}</td> <td className="">{abridgeArrayAndDisplay(row.distances)}</td>
@ -142,10 +183,6 @@ function CreateTableWithDistances({isListOrdered, orderedList, listOfElements, l
<td className="">{formatLargeOrSmall(avg(row.distances))}</td> <td className="">{formatLargeOrSmall(avg(row.distances))}</td>
</tr> </tr>
)} )}
<tr className={rows[0].distances.length > 0 ? "hidden": ""}>
"Maximum compute exceeded, rely on the graph instead"
</tr>
{}
</tbody> </tbody>
</table> </table>
</div> </div>
@ -154,16 +191,15 @@ function CreateTableWithDistances({isListOrdered, orderedList, listOfElements, l
} }
function CreateTableWithoutDistances({ isListOrdered, orderedList, listOfElements, links }) {
function CreateTableWithoutDistances({isListOrdered, orderedList, listOfElements, links}){ if (!isListOrdered || orderedList.length < listOfElements.length) {
if(!isListOrdered || orderedList.length < listOfElements.length){
return (<div>{""}</div>) return (<div>{""}</div>)
} else { } else {
let nodes = orderedList.map(i => listOfElements[i]) let nodes = orderedList.map(i => listOfElements[i])
let rows = nodes.map((element, i) => ({id: numToAlphabeticalString(element.id), name: element.name})) let rows = nodes.map((element, i) => ({ id: numToAlphabeticalString(element.id), name: element.name }))
console.log("rows@CreateTableWithoutDistances") console.log("rows@CreateTableWithoutDistances")
console.log(rows) console.log(rows)
return( return (
<div> <div>
<table className=""> <table className="">
<thead > <thead >
@ -181,7 +217,7 @@ function CreateTableWithoutDistances({isListOrdered, orderedList, listOfElements
<td className="">{row.name}</td> <td className="">{row.name}</td>
</tr> </tr>
)} )}
{} { }
</tbody> </tbody>
</table> </table>
</div> </div>
@ -190,7 +226,7 @@ function CreateTableWithoutDistances({isListOrdered, orderedList, listOfElements
} }
export const CreateTable = CreateTableWithoutDistances; export const CreateTable = CreateTableWithDistances // CreateTableWithoutDistances;
/* Testing */ /* Testing */
//import fs from 'fs'; //import fs from 'fs';

View File

@ -67,7 +67,7 @@ function drawGraphInner({ nodes, links }) {
.append("text") .append("text")
.attr("x", function (d) { return (x(d.id)) }) .attr("x", function (d) { return (x(d.id)) })
.attr("y", height - 10) .attr("y", height - 10)
.text(function (d) { return numToAlphabeticalString(d.id) }) .text(function (d) { return numToAlphabeticalString(d.position) })
.style("text-anchor", "middle") .style("text-anchor", "middle")
// Add the links // Add the links
@ -119,7 +119,7 @@ function drawGraphInner({ nodes, links }) {
}) })
.text(function (d) { .text(function (d) {
return formatLargeOrSmall(Number(d.distance)) return formatLargeOrSmall(Number(d.distance))
return (truncateValueForDisplay(Number(d.distance))) // return (truncateValueForDisplay(Number(d.distance)))
//return(Number(d.distance).toPrecision(2).toString()) //return(Number(d.distance).toPrecision(2).toString())
}) })
.style("text-anchor", "middle") .style("text-anchor", "middle")
@ -127,7 +127,7 @@ function drawGraphInner({ nodes, links }) {
export function DrawGraph({ isListOrdered, orderedList, listOfElements, links }) { export function DrawGraph({ isListOrdered, orderedList, listOfElements, links }) {
if (isListOrdered) { if (isListOrdered) {
let nodes = orderedList.map(i => listOfElements[i]) let nodes = orderedList.map((id, pos) => ({ ...listOfElements[id], position: pos }))
drawGraphInner({ nodes, links }); drawGraphInner({ nodes, links });
} }
return ( return (

View File

@ -1,9 +1,11 @@
import axios from "axios" import axios from "axios"
export async function pushToMongo(data){ export async function pushToMongo(data){
/*
let response = await axios.post('http://metaforecast-twitter-bot.herokuapp.com/utility-function-extractor', { let response = await axios.post('http://metaforecast-twitter-bot.herokuapp.com/utility-function-extractor', {
data: data data: data
}) })
console.log(response) console.log(response)
*/
} }
// pushToMongo() // pushToMongo()