Giter Club home page Giter Club logo

spiral-matrix-generator's Introduction

Introduction

This project implements a function to return all elements of a given m x n matrix in spiral order. It demonstrates the use of vector manipulation and control flow in C++ to traverse the matrix boundaries efficiently.

Algorithms

1. Spiral Order Matrix Traversal

Logic

  • The algorithm initializes four boundaries (top, bottom, left, right) to traverse the outer perimeter of the matrix and gradually moves inward in a spiral manner. It checks for edge cases (e.g., empty matrix) and uses a loop to iterate over the matrix elements in the specified spiral order: from the top row, right column, bottom row, to the left column, sequentially adjusting the boundaries.

Complexity Analysis

  • Time Complexity: O(M*N), where M is the number of rows and N is the number of columns in the matrix. Each element is visited exactly once.
  • Space Complexity: O(1), disregarding the space used to store the result as it is required for the output. The algorithm uses a constant amount of space for variables.

Code Snippet

vector<int> spiralOrder(vector<vector<int>>& matrix) {
	// Edge Cases
	if (matrix.empty()) { return {}; }

	// Boundaries
	int top = 0;
	int bottom = matrix.size() - 1;
	int left = 0;
	int right = matrix[0].size() - 1;

	// Store spiral numbers in vector
	vector<int> spiralVec;

	// Traverse the matrix in a spiral
	while (top <= bottom && left <= right) {
		// Begin from the top left to top right
		for (int i = left; i <= right && top <= bottom; ++i) {
			spiralVec.push_back(matrix[top][i]);
		}
		++top;

		// Then, traverse the rightmost column, from the updated top boundary to the bottom boundary
		for (int i = top; i <= bottom && left <= right; ++i) {
			spiralVec.push_back(matrix[i][right]);
		}
		--right;

		// Now, traverse the bottom row, right to left
		for (int i = right; i >= left && top <= bottom; --i) {
			spiralVec.push_back(matrix[bottom][i]);
		}
		--bottom;

		// Finally, traverse the left column
		for (int i = bottom; i >= top && left <= right; --i) {
			spiralVec.push_back(matrix[i][left]);
		}
		++left;
	}

	return spiralVec;
}

Usage

To utilize this function, include it in your C++ project and invoke spiralOrder with a 2D vector (matrix) parameter. The function returns a vector containing the matrix elements in spiral order, which can be useful for various applications requiring matrix manipulation or data presentation in a specific sequence.

Test Cases

The main function includes several test cases, ranging from empty matrices to rectangular and square matrices, demonstrating the versatility and correctness of the spiralOrder function. Users are encouraged to add more test cases to validate the function's behavior under different scenarios.

Verification

After running the program, it prints the outcome of each test case to the console, indicating whether it passed or failed based on the expected output. This immediate feedback helps in quickly identifying any issues or edge cases that may not have been adequately addressed.

Memory Management

The code utilizes standard C++ vectors, which automatically manage memory, ensuring that dynamically allocated memory is appropriately freed up when it's no longer needed, avoiding memory leaks.

Conclusion

This project provides a robust solution for traversing and returning elements of a matrix in spiral order. It's designed for easy integration into larger projects and can be extended or modified to meet various requirements.

spiral-matrix-generator's People

Contributors

danieldotwav avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.