Lesson 2
Exploring Special Traversals of Matrices in PHP
Introduction

Hello, fellow coder! Are you excited to dive into a new, intriguing coding challenge? In this lesson, we're going to explore special traversals of matrices. Matrices are rectangular 2D arrays, and in PHP, arrays are quite flexible since inner arrays don't need to maintain the same size. We'll navigate through a matrix using a zigzag pattern, climbing up and down the columns. Sound exciting? Buckle up, then, and get ready!

Task Statement

Here's the task: you've been given a 2D array consisting of individual cells, each holding a unique integer value. Your goal is to create a function that will traverse this matrix, starting at the bottom-right cell. From there, you'll travel up to the top of the same column, move left to the next column, and continue downward from the top of this new column. After reaching the bottom of the column, you move left and start moving upward. This unique traversal pattern will be performed until all the cells have been visited.

Consider this small 3x4 matrix as an example:

php
1$matrix = [ 2 [1, 2, 3, 4], 3 [5, 6, 7, 8], 4 [9, 10, 11, 12] 5];

With the described traversal pattern, your function should return this list: [12, 8, 4, 3, 7, 11, 10, 6, 2, 1, 5, 9].

Solution Building: Step 1

The first step towards a solution is understanding the dimensions of the matrix with which we're working. We can do this using PHP's count() function to determine array lengths. Let's set up our function and identify the matrix size:

php
1function columnTraverse($matrix) { 2 $rows = count($matrix); 3 $cols = count($matrix[0]); 4}
Solution Building: Step 2

Now that we're aware of the matrix dimensions, we should establish the starting point (bottom-right) and the direction of travel (upward initially). Additionally, we'll need an array to keep track of the cells we've visited in order:

php
1function columnTraverse($matrix) { 2 $rows = count($matrix); 3 $cols = count($matrix[0]); 4 $direction = "up"; 5 $row = $rows - 1; 6 $col = $cols - 1; 7 $output = []; 8 $index = 0; 9}
Solution Building: Step 3

It's time to go exploring! We'll now implement a while loop to traverse the matrix. This loop will continue until we have covered all the cells in the matrix. As we "visit" each cell, we'll add the value in the cell to our array.

php
1<?php 2function columnTraverse($matrix) { 3 $rows = count($matrix); 4 $cols = count($matrix[0]); 5 $direction = "up"; 6 $row = $rows - 1; 7 $col = $cols - 1; 8 $output = []; 9 $index = 0; 10 11 while ($index < $rows * $cols) { 12 $output[] = $matrix[$row][$col]; 13 14 if ($direction === "up") { 15 if ($row - 1 < 0) { 16 $direction = "down"; 17 $col -= 1; 18 } else { 19 $row -= 1; 20 } 21 } else { 22 if ($row + 1 == $rows) { 23 $direction = "up"; 24 $col -= 1; 25 } else { 26 $row += 1; 27 } 28 } 29 $index++; 30 } 31 32 return $output; 33} 34 35$matrix = [ 36 [1, 2, 3, 4], 37 [5, 6, 7, 8], 38 [9, 10, 11, 12] 39]; 40 41$result = columnTraverse($matrix); 42foreach ($result as $num) { 43 echo $num . " "; 44}
Traverse Using Decreasing Range

Let's explore one more way of traversal. We can leverage PHP's for loop to traverse a 2D matrix in reverse order. This flexibility can also create a sequence that decrements.

To achieve this, we use for loops with decrementing indices.

Consider our familiar 3x4 matrix:

php
1$matrix = [ 2 [1, 2, 3, 4], 3 [5, 6, 7, 8], 4 [9, 10, 11, 12] 5];

Using decrementing loops, the reverse traverse pattern would produce this list: [12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1].

Here's how we can implement this reverse traversal:

php
1function reverseTraverse($matrix) { 2 $rows = count($matrix); 3 $cols = count($matrix[0]); 4 $output = []; 5 $index = 0; 6 7 for ($row = $rows - 1; $row >= 0; $row--) { 8 for ($col = $cols - 1; $col >= 0; $col--) { 9 $output[] = $matrix[$row][$col]; 10 } 11 } 12 13 return $output; 14} 15 16$matrix = [ 17 [1, 2, 3, 4], 18 [5, 6, 7, 8], 19 [9, 10, 11, 12] 20]; 21 22$result = reverseTraverse($matrix); 23foreach ($result as $num) { 24 echo $num . " "; 25}

In this function, we start the loop for row from rows - 1 and run it to 0, decrementing it at each step. Similarly, our nested loop for col starts from cols - 1 and goes to 0. This allows us to start from the bottom-right corner and traverse leftward, then upward, covering the entire matrix in reverse order.

Lesson Summary

Congratulations! You have made it through a challenging task where you maneuvered through complex matrix traversal patterns. The functions you've designed not only test your PHP coding skills but also your ability to visualize spatial patterns.

It's time to put your new knowledge to the test! The next step is to tackle similar challenges on your own. I encourage you to use this lesson as your guide and don't forget to experiment with different matrix sizes and cell values. With plenty of practice, you'll soon master these traversal patterns in PHP!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.