<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mary</title>
    <description>The latest articles on DEV Community by Mary (@mary71).</description>
    <link>https://dev.to/mary71</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1224525%2F1695327c-5ef5-47f6-965b-83eaa460fdc5.png</url>
      <title>DEV Community: Mary</title>
      <link>https://dev.to/mary71</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mary71"/>
    <language>en</language>
    <item>
      <title>Error with DLX algorithm</title>
      <dc:creator>Mary</dc:creator>
      <pubDate>Sat, 02 Dec 2023 18:05:34 +0000</pubDate>
      <link>https://dev.to/mary71/error-with-dlx-algorithm-g9l</link>
      <guid>https://dev.to/mary71/error-with-dlx-algorithm-g9l</guid>
      <description>&lt;p&gt;Hi.Welcome back everyone&lt;br&gt;
My script is:&lt;br&gt;
`class Node {&lt;br&gt;
  constructor(row, col) {&lt;br&gt;
      this.row = row;&lt;br&gt;
      this.col = col;&lt;br&gt;
      this.up = null;&lt;br&gt;
      this.down = null;&lt;br&gt;
      this.left = null;&lt;br&gt;
      this.right = null;&lt;br&gt;
      this.header = null;&lt;br&gt;
  }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;class Matrix {&lt;br&gt;
  constructor(rows, columns) {&lt;br&gt;
    this.rows = rows;&lt;br&gt;
    this.columns = columns;&lt;br&gt;
    this.matrix = [];&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (let i = 0; i &amp;lt; rows; i++) {
  const row = new Array(columns).fill(0);
  this.matrix.push(row);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;getColumnNodes(col) {&lt;br&gt;
    const nodes = [];&lt;br&gt;
    for (let i = 0; i &amp;lt; this.rows; i++) {&lt;br&gt;
      if (this.matrix[i][col] === 1) {&lt;br&gt;
        nodes.push(new Node(i, col));&lt;br&gt;
      }&lt;br&gt;
    }&lt;br&gt;
    return nodes;&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;setEntry(row, col, value) {&lt;br&gt;
    this.matrix[row][col] = value;&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;getEntry(row, col) {&lt;br&gt;
    return this.matrix[row][col];&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;print() {&lt;br&gt;
    for (let i = 0; i &amp;lt; this.rows; i++) {&lt;br&gt;
      console.log(this.matrix[i].join(' '));&lt;br&gt;
    }&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;getRow(row) {&lt;br&gt;
    return this.matrix[row];&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;getColumn(col) {&lt;br&gt;
    const column = [];&lt;br&gt;
    for (let i = 0; i &amp;lt; this.rows; i++) {&lt;br&gt;
      column.push(this.matrix[i][col]);&lt;br&gt;
    }&lt;br&gt;
    return column;&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;getNumRows() {&lt;br&gt;
    return this.rows;&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;getNumColumns() {&lt;br&gt;
    return this.columns;&lt;br&gt;
  }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;class DLX {&lt;br&gt;
 constructor(matrix) {&lt;br&gt;
   this.root = new Node(-1, -1);&lt;br&gt;
   this.root.left = this.root;&lt;br&gt;
   this.root.right = this.root;&lt;br&gt;
   this.matrix = matrix;&lt;br&gt;
   this.cols = [];&lt;br&gt;
   this.solutionRows = [];&lt;br&gt;
   this.solutions = [];&lt;br&gt;
   this.initialize();&lt;br&gt;
 }&lt;/p&gt;

&lt;p&gt;initialize() {&lt;br&gt;
   const numRows = this.matrix.getNumRows();&lt;br&gt;
   const numCols = this.matrix.getNumColumns();&lt;br&gt;
   const headerNodes = [];&lt;/p&gt;

&lt;p&gt;for (let i = 0; i &amp;lt; numCols; i++) {&lt;br&gt;
     const header = new Node(-1, i);&lt;br&gt;
     header.left = this.root.left;&lt;br&gt;
     header.right = this.root;&lt;br&gt;
     this.root.left.right = header;&lt;br&gt;
     this.root.left = header;&lt;br&gt;
     header.up = header;&lt;br&gt;
     header.down = header;&lt;br&gt;
     headerNodes.push(header);&lt;br&gt;
     this.cols.push(0);&lt;br&gt;
   }&lt;/p&gt;

&lt;p&gt;for (let i = 0; i &amp;lt; numRows; i++) {&lt;br&gt;
     let last = null;&lt;br&gt;
     for (let j = 0; j &amp;lt; numCols; j++) {&lt;br&gt;
       if (this.matrix.getEntry(i, j) === 1) {&lt;br&gt;
         const node = new Node(i, j);&lt;br&gt;
         node.header = headerNodes[j];&lt;br&gt;
         node.up = node.header.up;&lt;br&gt;
         node.down = node.header;&lt;br&gt;
         node.header.up = node;&lt;br&gt;
         node.up.down = node;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     if (last === null) {
       last = node;
     } else {
       node.left = last;
       node.right = last.right;
       last.right = node;
       node.right.left = node;
       last = node;
     }

     this.cols[j]++;
   }
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;br&gt;
 }&lt;/p&gt;

&lt;p&gt;cover(col) {&lt;br&gt;
   const header = col.header;&lt;br&gt;
   header.left.right = header.right;&lt;br&gt;
   header.right.left = header.left;&lt;/p&gt;

&lt;p&gt;for (let node = header.down; node !== header; node = node.down) {&lt;br&gt;
     for (let j = node.right; j !== node; j = j.right) {&lt;br&gt;
       j.down.up = j.up;&lt;br&gt;
       j.up.down = j.down;&lt;br&gt;
       this.cols[j.col]--;&lt;br&gt;
     }&lt;br&gt;
   }&lt;br&gt;
 }&lt;/p&gt;

&lt;p&gt;uncover(col) {&lt;br&gt;
   if (col.header) {&lt;br&gt;
     const header = col.header;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; for (let node = header.up; node !== header; node = node.up) {
   for (let j = node.left; j !== node; j = j.left) {
     j.down.up = j;
     j.up.down = j;
     j.header.cols++;
   }
 }

 if (col.right) {
   col.right.left = col;
 }

 if (col.left) {
   col.left.right = col;
 }

 header.left = col;
 header.right = col;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;br&gt;
 }&lt;/p&gt;

&lt;p&gt;search() {&lt;br&gt;
   if (this.root.right === this.root) {&lt;br&gt;
     this.solutions.push([...this.solutionRows]);&lt;br&gt;
     return;&lt;br&gt;
   }&lt;/p&gt;

&lt;p&gt;let minCols = Infinity;&lt;br&gt;
   let colNode = null;&lt;/p&gt;

&lt;p&gt;for (let node = this.root.right; node !== this.root; node = node.right) {&lt;br&gt;
     if (this.cols[node.col] &amp;lt; minCols) {&lt;br&gt;
       minCols = this.cols[node.col];&lt;br&gt;
       colNode = node;&lt;br&gt;
     }&lt;br&gt;
   }&lt;/p&gt;

&lt;p&gt;this.cover(colNode);&lt;/p&gt;

&lt;p&gt;for (let rowNode = colNode.down; rowNode !== colNode; rowNode = rowNode.down) {&lt;br&gt;
     this.solutionRows.push(rowNode.row);&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; for (let j = rowNode.right; j !== rowNode; j = j.right) {
   this.cover(j.header);
 }

 this.search();
 this.solutionRows.pop();

 for (let j = rowNode.left; j !== rowNode; j = j.left) {
   this.uncover(j.header);
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;this.uncover(colNode);&lt;br&gt;
 }&lt;/p&gt;

&lt;p&gt;getSolutions() {&lt;br&gt;
   while (this.root.right !== this.root) {&lt;br&gt;
     this.search();&lt;br&gt;
     this.solutionRows = [];&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; if (this.root.right === this.root) {
   break;
 }

 for (let i = 0; i &amp;lt; this.cols.length; i++) {
   if (this.cols[i] === 0) {
     continue;
   }

   const nodes = this.matrix.getColumnNodes(i);
   const candidates = [];

   for (const node of nodes) {
     candidates.push(node.row);

     if (candidates.length === 2) {
       let isValid = true;

       for (const solution of this.solutions) {
         if (
           solution.indexOf(candidates[0]) === -1 ||
           solution.indexOf(candidates[1]) === -1
         ) {
           isValid = false;
           break;
         }
       }

       if (isValid) {
         this.solutionRows.push([...candidates]);
         break;
       }

       candidates.pop();
     }
   }
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;return this.solutions;&lt;br&gt;
 }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;function getCoveringDesign() {&lt;br&gt;
  const v = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];&lt;br&gt;
  const k = 3;&lt;br&gt;
  const t = 2;&lt;br&gt;
  const matrix = new Matrix(v.length * (v.length - 1) * (v.length - 2), v.length);&lt;/p&gt;

&lt;p&gt;for (let i = 0; i &amp;lt; v.length; i++) {&lt;br&gt;
    for (let j = i + 1; j &amp;lt; v.length; j++) {&lt;br&gt;
      for (let l = j + 1; l &amp;lt; v.length; l++) {&lt;br&gt;
        const row = new Array(v.length).fill(0);&lt;br&gt;
        row[i] = 1;&lt;br&gt;
        row[j] = 1;&lt;br&gt;
        row[l] = 1;&lt;br&gt;
        matrix.matrix.push(row);&lt;br&gt;
      }&lt;br&gt;
    }&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;const dlx = new DLX(matrix);&lt;br&gt;
  const solutions = dlx.getSolutions();&lt;br&gt;
  let minSize = Infinity;&lt;br&gt;
  let bestSolution = null;&lt;/p&gt;

&lt;p&gt;for (const solution of solutions) {&lt;br&gt;
    if (solution.length &amp;lt; minSize) {&lt;br&gt;
      minSize = solution.length;&lt;br&gt;
      bestSolution = solution;&lt;br&gt;
    }&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;const blocks = [];&lt;/p&gt;

&lt;p&gt;for (const indices of bestSolution) {&lt;br&gt;
    const block = [];&lt;br&gt;
    for (const index of indices) {&lt;br&gt;
      block.push(v[index]);&lt;br&gt;
    }&lt;br&gt;
    blocks.push(block);&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;return blocks;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;console.log(getCoveringDesign());`&lt;/p&gt;

&lt;p&gt;It gives error:&lt;br&gt;
Uncaught TypeError: Cannot read properties of null (reading 'left')&lt;br&gt;
at DLX.uncover&lt;br&gt;
at DLX.search&lt;br&gt;
at DLX.getSolutions&lt;br&gt;
at getCoveringDesign&lt;/p&gt;

&lt;p&gt;Any suggestions? Thanks&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>errors</category>
      <category>dlx</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
