Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learn Data Structures and Algorithms with Golang

You're reading from  Learn Data Structures and Algorithms with Golang

Product type Book
Published in Mar 2019
Publisher Packt
ISBN-13 9781789618501
Pages 336 pages
Edition 1st Edition
Languages
Author (1):
Bhagvan Kommadi Bhagvan Kommadi
Profile icon Bhagvan Kommadi

Table of Contents (16) Chapters

Preface 1. Section 1: Introduction to Data Structures and Algorithms and the Go Language
2. Data Structures and Algorithms 3. Getting Started with Go for Data Structures and Algorithms 4. Section 2: Basic Data Structures and Algorithms using Go
5. Linear Data Structures 6. Non-Linear Data Structures 7. Homogeneous Data Structures 8. Heterogeneous Data Structures 9. Dynamic Data Structures 10. Classic Algorithms 11. Section 3: Advanced Data Structures and Algorithms using Go
12. Network and Sparse Matrix Representation 13. Memory Management 14. Next Steps 15. Other Books You May Enjoy

Homogeneous Data Structures

Homogeneous data structures contain similar types of data, such as integers or double values. Homogeneous data structures are used in matrices, as well as tensor and vector mathematics. Tensors are mathematical structures for scalars and vectors. A first-rank tensor is a vector. A vector consists of a row or a column. A tensor with zero rank is a scalar. A matrix is a two-dimensional cluster of numbers. They are all used in scientific analysis.

Tensors are used in material science. They are used in mathematics, physics, mechanics, electrodynamics, and general relativity. Machine learning solutions utilize a tensor data structure. A tensor has properties such as position, shape, and a static size.

This chapter covers the following homogeneous data structures:

  • Two-dimensional arrays
  • Multi-dimensional arrays

The following scenarios are shown to demonstrate...

Technical requirements

Two-dimensional arrays

Two-dimensional arrays were presented briefly in Chapter 2, Getting Started with Go for Data Structures and Algorithms. To recap, for dynamic allocation, we use slice of slices, which is a two-dimensional array. A two-dimensional array, is a list of single-dimensional arrays. Every element in a two-dimensional array arr, is identified as arr[i][j], where arr is the name of the array and i and j represent rows and columns, and their values ranging from 0 to m and 0 to n, respectively. Traversing a two-dimensional array is of O(m*n) complexity.

The following code shows how to initialize an array:

var arr = [4][5] int{
{4,5,7,8,9},
{1,2,4,5,6},
{9,10,11,12,14},
{3,5,6,8,9}
}

An element in a two-dimensional array is accessed with a row index and column index. In the following example, the array's value in row 2 and column 3 is retrieved as an...

Multi-dimensional arrays

An array is a homogeneous collection of data elements. An array's indexes range from index 0 to index m-1, where m is the fixed length of the array. An array with multiple dimensions is an array of an array. The following code initializes a multi-dimensional array. A three-dimensional array is printed:

///main package has examples shown
// in Go Data Structures and algorithms book
package main

// importing fmt package
import (
"fmt"
"math/rand"
)
//main method
func main() {

var threedarray [2][2][2]int

var i int

var j int

var k int


for i=0; i < 2; i++ {

for j=0; j < 2; j++ {

for k=0; k < 2; k++ {

threedarray[i][j][k] = rand.Intn(3)
}
}
}

fmt.Println(threedarray)
}

Run the following command to execute the preceding code snippet:

go run multidarray.go

The output is as follows:

The next section talks about tensor...

Summary

This chapter covered homogeneous data structures such as two-dimensional arrays and multi-dimensional arrays. Matrix operations such as sum, subtraction, multiplication, inverse, and determinant have been explained with code examples. Spiral matrices, zig-zag matrices, and Boolean matrices have been explained using two-dimensional arrays. Tensors and operations such as folding were also covered.

In the next chapter, heterogeneous data structures such as linked lists, ordered lists, and unordered lists will be covered.

Questions

  1. What is 2-mode unfolding of a tensor array?
  2. Write a two-dimensional array of strings and initialize it. Print the strings.
  3. Give an example of a multi-dimensional array and traverse through it.
  4. For a 3 x 3 matrix, write code that calculates the determinant of the matrix.
  5. What is a transpose of a 3 x 3 matrix?
  6. What is a zig-zag matrix?
  7. Write code with an example of a spiral matrix.
  8. Which dimension is typically unfolded for tensor arrays?
  9. How do you define a Boolean matrix?
  10. Choose two 3 x 3 matrices and find the product of the matrices.

Further reading

The following books are recommended if you want to learn more about arrays, matrices, and tensors:

  • Advanced Data Structures, by Peter Brass
  • Dynamic Data Structures: Lists, Stacks, Queues, and Trees, by Bogdan Patrut, and Tiberiu Socaciu
  • Data structures and Algorithms: An Easy Introduction, by Rudolph Russell
lock icon The rest of the chapter is locked
You have been reading a chapter from
Learn Data Structures and Algorithms with Golang
Published in: Mar 2019 Publisher: Packt ISBN-13: 9781789618501
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}