0

I have this 2 dimensional array of tasks

const graph = [
    ['tie your shoes', 'put on your shoes'],
    ['put on your jacket', 'put on your shirt'],
    ['put on your shoes', 'put on your shorts'],
    ['put on your jacket', 'put on your shorts'],
    ['buy eggs from store', 'cook eggs'],
    ['heat up skillet', 'cook eggs']
]

I need to split up this graph into two separate arrays if the they have no association to each other. (i.e., putting shoes on and cooking eggs have no association/relation within the graph)

Each array within the graph is an association of tasks. An association can be traced between two tasks if they can be traced back to a single common task - putting on your jacket can be traced back to putting on your socks

The result should look like this

const graph_1 = [
    ['tie your shoes', 'put on your shoes'],
    ['put on your jacket', 'put on your shirt'],
    ['put on your shoes', 'put on your shorts'],
    ['put on your jacket', 'put on your shorts']
]

const graph_2 = [
    ['buy eggs from store', 'cook eggs'],
    ['cook eggs', 'heat up skillet']
]

I'm not worried about sorting them at the moment - only concerned with splitting them up

Clay Banks
  • 4,483
  • 15
  • 71
  • 143
  • Can use the Array method `slice` to split them up. – async await Apr 26 '22 at 16:14
  • 1
    @asyncawait How would that help with this? He's not splitting up by a range of indexes. – Barmar Apr 26 '22 at 16:17
  • What do you mean by dependencies? – Barmar Apr 26 '22 at 16:18
  • How do you expect to detect the "if they have no dependencies on each other"?Are you looking for a simple way to parse natural human language strings and categorize them by intent? – Daniel Beck Apr 26 '22 at 16:22
  • @Barmar dependency was the wrong phrase, association is a better one – Clay Banks Apr 26 '22 at 16:22
  • 1
    @Barmar to split the example array at the desired value, could split at index 4, i.e. `slice(0,4)` and `slice(4)` to get the two output. Then it's just finding the index to split on which could use a loop to determine those. Other ways to solve too. As for dependencies, I interpreted it as including the substring "put on your" or "cook eggs" so maybe partial matches? Though not super clear ♥ – async await Apr 26 '22 at 16:24
  • 2
    @asyncawait I don't see any reason to assume that all the related elements would be adjacent so that splitting at an index would work. – Barmar Apr 26 '22 at 16:28
  • @Barmar I believe they intend to sort them, so we are to assume the order is significant and the elements would be adjacent. We would be comparing to elements preceding the index – async await Apr 26 '22 at 16:30
  • @ClayBanks It's still not clear what you mean by association. I'm wondering the same thing as Daniel Beck, is this a natural language processing problem? – Barmar Apr 26 '22 at 16:30
  • 1
    Ah, this is a graph partitioning problem. Try starting [here](https://en.wikipedia.org/wiki/Graph_partition) – Barmar Apr 26 '22 at 16:31
  • @Barmar I've updated the question to explain how an association is made – Clay Banks Apr 26 '22 at 16:33
  • So what you really mean is "if the second item in one of the subarrays is the same as the first item in one of the other subarrays, those two subarrays are associated"? – Daniel Beck Apr 26 '22 at 16:36
  • But that doesn't work either because the first chain will end at "put on your shorts" – Daniel Beck Apr 26 '22 at 16:37
  • Maybe some variation of a fuzzy search for finding strength of groupings without getting into some kind of ai api? Is the question trying to determine how to programmatically categorize strings into being related to eachother? – async await Apr 26 '22 at 16:43
  • @asyncawait not trying to do any string searches for this problem – Clay Banks Apr 26 '22 at 16:50
  • What _are_ you trying to do? How specifically do you want to determine whether a task can be traced back to another? None of your updates has clarified that at all – Daniel Beck Apr 26 '22 at 17:04

1 Answers1

1

You could use a combo of loops to check against an array that contains keywords.

const graph = [
    ['tie your shoes', 'put on your shoes'],
    ['put on your jacket', 'put on your shirt'],
    ['put on your shoes', 'put on your shorts'],
    ['put on your jacket', 'put on your shorts'],
    ['buy eggs from store', 'cook eggs'],
    ['heat up skillet', 'cook eggs']
]

// Keywords
const clothes = ['shoes', 'jacket', 'shorts']
const foods = ['eggs', 'heat', 'skillet']

// Creating graph 1
const graph_1 = graph.filter(array => {
  return array.reduce((arr, str) => {
    clothes.forEach(item => {
      if (str.includes(item)) {
        arr.push(str)
      }
    })
    return arr
  }, []).length
})

// Creating graph 2
const graph_2 = graph.filter(array => {
  return array.reduce((arr, str) => {
    foods.forEach(item => {
      if (str.includes(item)) {
        arr.push(str)
      }
    })
    return arr
  }, []).length
})

console.log(graph_1)
console.log(graph_2)
Ludolfyn
  • 1,806
  • 14
  • 20