DEV Community

Cover image for Spiral traversing a matrix
Kenny
Kenny

Posted on

Spiral traversing a matrix

Hello readers!

This is week 4 of bootcamp and we're currently learning a little bit of the back-end side of development, and also a bit of databases like mySQL and its slowly breaking me inside and if there's a way to get a better understanding of mySQL please send help! Its been a rough month, but we're here with another toy-problem with spiral traversal! to get a grasp of the concept, our input is a matrix which is a two-dimensional array and our output is a single array.

A matrix would normally look like this:

let matrix = [
  [ 1, 2, 3],
  [ 4, 5, 6],
  [ 7, 8, 9]
];

Our output wants the elements from the matrix in a toilet-flush like formation.
so our output should look something like:

[1, 2, 3, 6, 9, 8, 7, 4, 5];

Lets jump right into it! and try to break it down!
There are many ways to solve this particular toy problem, but for this one I used a function that'll iterate through the matrix in a spiraling motion.

Lets Begin!
Since our output will be an array, its common to say we can make our output variable an empty array soon to be filled with elements! And since we're creating a recursive function within the function we can also input that as well!

function spiralTraversal(matrix) {
  let output = [];
  let spiral = function(matrix) {
 };

In the function we're going start by shifting the matrix and concatting it into the output afterwards we can start our conditionals statement. For this conditional we just want to check if there's anything left in our matrix and if there's nothing left we can just simply return it.

let spiral = function(matrix) {
  output = output.concat(matrix.shift());
  if(!matrix.length){
   return;
  }
 };

Now within the function we can start the looping process, or iterating process.
Lets create a simple loop to go forward in the matrix. Having i start from 0 and the stopping condition when that i hits the matrix's maximum length. Afterwards we're going to pop the matrix at the i index and concatting it to output.

}
    for (let i = 0; i < matrix.length - 1; i++) {
      output = output.concat(matrix[i].pop());
    }

if we console.log the output we should get [1, 2, 3, 6]

Now we're gonna go reverse the matrix and pop off each elements in the matrix and concat it our output. Next we're going to check if the matrix's elements are undefined, in order to access a matrix, we'd have to test the matrix[0][0].

    output = output.concat(matrix.pop().reverse());
     if (matrix[0][0] === undefined) {
      return;
    }

Our final step to the spiral function is to create a decrementing loop, starting from the length of the matrix. Afterwards we lastly shift the matrix at the iterated index and also concat it to our output.

for (let j = matrix.length- 1; j >= 0; j--) {
      output = output.concat(matrix[j].shift());
    }

Now that we've finish the functionality part of the function lastly outside of the loop we can just simply recursively call the function inside of itself. Since we've finally finshed the function we can call it once more in our overall function and return our output.

Thank you for taking the time to read over my post, Please let me know other ways to spiral traverse a matrix I'd love to know other ways!

Top comments (0)