A REPRESENTATION OF BINARY MATRICES * Hristina Kostadinova, Krasimir Yordzhev

Similar documents
arxiv: v1 [cs.ms] 6 Jan 2012

AN EXAMPLE FOR THE USE OF BITWISE OPERATIONS IN PROGRAMMING. Krasimir Yordzhev

An Entertaining Example for the Usage of Bitwise Operations in Programming

arxiv: v2 [math.co] 29 May 2013

Bitwise Operations Related to a Combinatorial Problem on Binary Matrices

Random Permutations, Random Sudoku Matrices and Randomized Algorithms

AN EXAMPLE FOR THE USE OF BITWISE OPERATIONS IN PROGRAMMING

REALIZATION OF OPEN ADDRESSING HASH TABLE IN THE CHAINED ALLOCATED MEMORY. Valentina Dyankova and Rositza Hristova

[Ch 6] Set Theory. 1. Basic Concepts and Definitions. 400 lecture note #4. 1) Basics

CSC Discrete Math I, Spring Sets

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

ON TWO ADAPTIVE SYSTEMS FOR DOCUMENT MANAGEMENT * Vanyo G. Peychev, Ivo I. Damyanov

The Warhol Language Reference Manual

Operators. Java operators are classified into three categories:

EXTRACTING BUSINESS RULES THROUGH STATIC ANALYSIS OF THE SOURCE CODE * Krassimir Manev, Neli Maneva, Haralambi Haralambiev

Bits, Words, and Integers

2.2 Set Operations. Introduction DEFINITION 1. EXAMPLE 1 The union of the sets {1, 3, 5} and {1, 2, 3} is the set {1, 2, 3, 5}; that is, EXAMPLE 2

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

CS2630: Computer Organization Homework 1 Bits, bytes, and memory organization Due January 25, 2017, 11:59pm

Using Templates to Introduce Time Efficiency Analysis in an Algorithms Course

Discrete Mathematics Lecture 4. Harper Langston New York University

Advanced Algorithms and Computational Models (module A)

A flow chart is a graphical or symbolic representation of a process.

JAVA OPERATORS GENERAL

UNIT- 3 Introduction to C++

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

CSC-140 Assignment 5

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

A Method for Construction of Orthogonal Arrays 1

CS/COE 0447 Example Problems for Exam 2 Spring 2011

CSE 20 DISCRETE MATH. Winter

COMP 122/L Lecture 2. Kyle Dewey

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5

Programming in C++ 5. Integral data types

CS 33. Data Representation, Part 1. CS33 Intro to Computer Systems VII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

4&5 Binary Operations and Relations. The Integers. (part I)

C: How to Program. Week /Mar/05

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

Lamé s Theorem. Strings. Recursively Defined Sets and Structures. Recursively Defined Sets and Structures

Arithmetic and Bitwise Operations on Binary Data

Summary of Course Coverage

CSCI 171 Chapter Outlines

Arithmetic and Bitwise Operations on Binary Data

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

arxiv: v1 [math.co] 25 Sep 2015

Chapter 2 - Introduction to C Programming

Computers Programming Course 5. Iulian Năstac

CS201 Some Important Definitions

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

GENETIC ALGORITHMS, FLOATING POINT NUMBERS AND APPLICATIONS

Chapter 3: Operators, Expressions and Type Conversion

Integer Representation

DEPARTMENT OF MATHS, MJ COLLEGE

Arithmetic and Logic Blocks

CT 229. Java Syntax 26/09/2006 CT229

Assignment 1 (concept): Solutions

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

Programming, numerics and optimization

WAREHOUSING AND OLAP ANALYSIS OF DATA ABOUT UNIQUE BULGARIAN BELLS * Tihomir Trifonov, Tsvetanka Georgieva-Trifonova

SMPL - A Simplified Modeling Language for Mathematical Programming

The Further Mathematics Support Programme

Lesson 12 - Operator Overloading Customising Operators

Quick Reference Guide

Imelda C. Go, South Carolina Department of Education, Columbia, SC

Power Set of a set and Relations

Chapter 4. Operations on Data

Review of the C Programming Language for Principles of Operating Systems

Chapter 1 & 2 Introduction to C Language

Matrix algebra. Basics

Characterization of Boolean Topological Logics

Operators in C. Staff Incharge: S.Sasirekha

Chapter 4 Arithmetic Functions

Binary Values. CSE 410 Lecture 02

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Deductive Methods, Bounded Model Checking

gcc o driver std=c99 -Wall driver.c bigmesa.c

REGULAR GRAPHS OF GIVEN GIRTH. Contents

BITS, BYTES, AND INTEGERS

Contents of Lecture 3

To prove something about all Boolean expressions, we will need the following induction principle: Axiom 7.1 (Induction over Boolean expressions):

CS 3410 Ch 5 (DS*), 23 (IJP^)

Computer Organization & Systems Exam I Example Questions

BITWISE OPERATORS. There are a number of ways to manipulate binary values. Just as you can with

Information Science 1

A Survey of Mathematics with Applications 8 th Edition, 2009

CS3102 Theory of Computation Solutions to Problem Set 1, Spring 2012 Department of Computer Science, University of Virginia

Boolean networks, local models, and finite polynomial dynamical systems

Fast Implementation of the ANF Transform

Binary Adders: Half Adders and Full Adders

SECTION II: LANGUAGE BASICS

1. Find f(1), f(2), f(3), and f(4) if f(n) is defined recursively by f(0) = 1 and for n = 0, 1, 2,

Unit 3. Operators. School of Science and Technology INTRODUCTION

CSE 20 DISCRETE MATH. Fall

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

3. Java - Language Constructs I

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

CS 253. January 14, 2017

Transcription:

МАТЕМАТИКА И МАТЕМАТИЧЕСКО ОБРАЗОВАНИЕ, 2010 MATHEMATICS AND EDUCATION IN MATHEMATICS, 2010 Proceedings of the Thirty Ninth Spring Conference of the Union of Bulgarian Mathematicians Albena, April 6 10, 2010 A REPRESENTATION OF BINARY MATRICES * Hristina Kostadinova, Krasimir Yordzhev In this article we discuss the representation of binary matrix using a sequence of positive integers. We examine some advantages and disadvantages of this presentation as an alternative to the standard representation using a two-dimensional matrix. It is shown that the representation of binary matrices using ordered n-tuples of natural numbers makes the algorithms faster and saves a lot of memory. In this work we use object-oriented programming using the syntax and the semantic of C++ programming language. 1. Introduction. One of the basic principles of object-oriented programming is encapsulation, which means that the client knows objects data, properties and algorithms, to which events the object reacts, but it is not necessary to have the information about their realization and the algorithms used in the member functions of the corresponding class. Here we ask the following question: if there are two different classes, which describe one and the same object in mathematics or in the real world, and they have the same properties and methods, the question is which one of these two classes we have to choose. The answer is trivial: the class, which objects use less memory and which algorithms work faster, i.e. which algorithms use less standard computer operations. The present paper is a continuation of the paper [5]. Our aim is to show that the representation of the binary matrices using ordered n-tuple of positive integers and the bitwise operations make the realization of a better class (as it was mentioned above) compared with the standard representation of the binary matrices using two-dimensional n n matrix of positive integers. About the definition and some examples how to use bitwise operations see [2, 3, 5, 7]. We recommend [4] about the object-oriented programming using C++ language in the area of the computer algebra. We examine the set B = {0, 1. B together with the operations conjunction &&, disjunction and negation! form the all known boolean algebra B(&&,,!), which role is very important in the computers and programming. We use the mentioned symbols for the operations in B(&&,,!), have in mind the semantic and the syntax of these operations in the widespread C++ programming language. Binary matrix (or boolean, or (0, 1)-matrix) is a matrix, which elements belong to the set B = {0, 1. We denote by B n the set of all n n square matrices. * 2000 Mathematics Subject Classification: 68N15, 68W40, 15B34. Key words: Binary matrix, object-oriented programming, C++ programming language, bitwise operations, computer algebra. 198

Let A = (a ij ) and B = (b ij ) be matrices of B n. We consider the semantic and syntax of C++ language and the first index is 0, i.e i, j {0, 1, 2,..., n 1. We examine the following operations in B n, defined according to our aim as follows: component conjunction (1) A && B = C = (c ij ) where, by definition c ij = a ij && b ij for every i, j {0, 1, 2..., n 1; component disjunction (2) A B = C = (c ij ) where, by definition c ij = a ij b ij for every i, j {0, 1, 2..., n 1; component negation (3)!A = C = (c ij ) where, by definition c ij =!a ij for every i, j {0, 1, 2..., n 1; transpose (4) t(a) = C = (c ij ) where, by definition c ij = a ji for every i, j {0, 1, 2..., n 1; logical product (5) A B = C = (c ij ) where, by definition c ij = (a ik && b kj ) = (a i 0 && b 0 j ) (a i 1 && b 1 j ) (a i n 1 && b n 1 j ) n 1 k=0 for every i, j {0, 1, 2..., n 1. That is the way B n and the above-described operations &&,,!, t() and to form the algebra B n (&&,,!, t(), ). Here and in the whole article the term algebra means abstract algebra considering the definition given in [1], and, namely, set equipped with various operations, assumed to satisfy some specified system of axiomatic laws. It is naturally to put the linear order, and exactly the lexicographic order in B n (&&,,!, t(), ). We examine the following set of standard operations with integer arguments in the C++ programming language: (6) Op = {+,,, /, %, <<, >>,&,,,, &&,,!, =, if, <, <=, >, >=, ==,!=. These operations mean addition, subtraction, multiplication, division, integer division, bitwise left shift, bitwise right shift, bitwise and, bitwise or, bitwise exclusive or, bitwise negation, conjunction, disjunction, negation, assignment, if check, and comparing. We consider that the time, needed for each of these operations of the set Op are proportional, i.e. if t 1 and t 2 are the times needed to execute arbitraty operations of Op, then t 1 = Ct 2, where C is a const. The algorithms in this paper are evaluated according to the number of the needed operations of the set Op. The present paper is also an appropriate example how to use bitwise operations in the object-oriented programming courses. This matter does not take enough place in the studying literature(see for example [5]). 199

2. Two classes, which describe the algebra B n (&&,,!, t(), ). To create the first class we use the standard realization of the binary matrices: using a two-dimensional n n matrix of positive integers and standard algorithms to execute the operations (1) (5). Let us denote this class by Bn array. A square binary n n matrix, as it is described in [5], can be realized using ordered n-tuple of whole nonnegative numbers, which belong to the closed interval [0, 2 n 1]. There is one to one correspondence between the representation of the integers in decimal and in binary number system. Let us denote by Bn tuple the class which describes the algebra B n (&&,,!, t(), ) using ordered n-tuple of positive integers. These two classes have the same specifications (we use the terminology in [6, 8]), and we describe these specification using the name Bn X, i.e. X means array, or tuple depending on the case. Let the two classes Bn X have the following specification: class Bn_X { int n; int *Matr; public: /* constructor without parameter: */ Bn_X(); /* constructor with parameter n pointing the row of the square matrix: */ Bn_X (unsigned int); /* copy constructor: */ Bn_X (const Bn_X &); /* destructor: */ ~Bn_X(); /* predefines the assignment operator: */ Bn_X & operator = (const Bn_X &); /* returns the size (row) of the matrix: */ int get_n() { return n; ; /* sets value 1 to the element (i,j) of the matrix: */ void set_1 (int,int); /* sets value 0 to the element (i,j) of the matrix: */ void set_0 (int,int); /* fills row i of the matrix using integer number r (just for the class Bn_tuple): */ void set_row(int,int); // must not be included when "X" = "array"! /* gets element (i,j) of the matrix: */ int get_element (int,int); /* gets the row i of the matrix (just for the class Bn_tuple): */ int get_row (int); // must not be included when "X" = "array"! /* transposes matrix : */ Bn_X t (); /* predefines operators according to (1), (2), (3) and (5): */ Bn_X operator && (Bn_X &); Bn_X operator (Bn_X &); 200

Bn_X operator! (); Bn_X operator * (Bn_X &); /* defines order (lexicographical) */ int operator < (Bn_X &); When we predefine the operators &&, and and if the dimensions of the two operands are not equal, then we receive the zero matrix of order the same as the first operand. But this result is not correct. We can say something more: in this case the operation is not defined, i.e. the result of the operation function is not correct and we have to be very careful in such situations. The situation is the same about the entered linear order, i.e. although the lexicographic order can be put for the words with different length, we examine by definition only the matrices of the same order. The result we receive when we compare the matrices with different dimensions is not correct. We give suitable comments in this situations. Since the objects of the two classes get dynamic operation memory, using pointers and the new operator, then to realize the applications, using such objects, it is necessary to predefine the operations of the big three [6] copy constructor, destructor and the assignment operator. The constructor without parameter and the destructor are the same for both classes Bn array and Bn tuple. Actually, when we work with objects of the class Bn X, from the mathematical point of view it is necessary to point the dimension of the matrix and this dimension does not change. In this aspect using a constructor without parameter have not sense and we are not interested in it. But yet we add such a constructor to make our presentation complete and to make, that the big three to become the big four [6]. We use the universal integer type int to save the exactness in testing the program, this type can be changed to any other whole number type. To evaluate the effectiveness and speed of the algorithms, which use objects of the algebra B n (&&,,!, t(), ) it is necessary to evaluate the algorithms, which realize the operations &&,!, t(), including the operation, comparing two elements, and operation = assignment. In that sense we describe in details just these methods, realizing the above mentioned operations. We suppose that the experienced programmer can easyly create the other methods in each of the two classes. In the present work we predefine the operator < too. Using the same model we can predefine the other relational operators: <=, >, >=, == and!=. If the dimensions of the two matrices, we compare, are not equal, then the relation < is not defined and the result we get is the negative number 1. 3. Realization of the class Bn array. When X == array we propose the following (standard) realization of the examined methods in the class Bn array: Bn_array Bn_array :: t () { Bn_array temp(n); for (int i=0; i<n; i++) for (int j=0; j<n; j++) *(temp.matr + i*n+j) = *(Matr + j*n+i); 201

Bn_array Bn_array :: operator && (Bn_array &B) { Bn_array temp(n); int n2 = n*n; for (int p=0; p<n2; p++) *(temp.matr + p) = *(this->matr + p) && *(B.Matr + p); Bn_array Bn_array :: operator (Bn_array &B) { Bn_array temp(n); int n2 = n*n; for (int p=0; p<n2; p++) *(temp.matr + p) = *(this->matr + p) *(B.Matr + p); Bn_array Bn_array :: operator! () { int n2 = n*n; for (int p=0; p<n2; p++) *(this->matr + p) = *(this->matr + p)? 0 : 1; return *this; Bn_array Bn_array :: operator * (Bn_array &B) { Bn_array temp(n); int c; for (int i=0; i<n; i++) for (int j=0; j<n; j++) { c=0; for (int k=0; k<n; k++) c = c (*(this->matr + i*n+k) && *(B.Matr +k*n+j)); *(temp.matr +i*n+j)=c; 202

Bn_array& Bn_array :: operator = (const Bn_array &B) { int n2=n*n; for (int p=0; p<n2; p++) *(this->matr + p) = *(B.Matr + p); return *this; int Bn_array :: operator < (Bn_array &B) { int p = 0; int n2 = n*n; while ((*(Matr +p) == *(B.Matr +p)) && (p<n2-1) ) p++; if ( *(Matr +p) < *(B.Matr +p) ) return 0; return 1; Analogously we can realize the remaining relational operators <=, ==, >, >=, ==,!=. It is easy to convince that the following proposition is true: Proposition 1. For each positive integer n, for the computer representation via the class Bn array, using the C++ programming language, of the algebra B n (&&,,!, t(), ), the following propositions are true: (i) When we use standard realization (standard predefining) of the operation functions &&,,!, <, the operation transpose and the assignment operator, then each of them performs O(n 2 ) operations of the set Op; (ii) When we use standard realization (standard predefining) of the operation function, then this operation performs O(n 3 ) operations of the set Op; (iii) For every object of the class Bn array are necessary O(n 2 ) sizeof (int) bytes of the operating memory; (iv) To make initialization of the object of the class Bn array O(n 2 ) operations are performed of the set Op. We can prove the propositions (i) and (ii) as we count the number of the inner cycles and the maximal number of the iterations in each of the methods. The propositions (iii) and (iv) are obvious. 4. Representing the binary matrices using ordered n-tuples of nonnegative integers. As it is shown in [5] there is one to one corresponding between the set of all n n binary matrices and the set of all ordered n-tuples of whole numbers, which belong to the closed interval [0, 2 n 1], based on the binary representation of the positive integers. This idea takes place in the realization of the class Bn tuple. To create the class Bn tuple we propose the following methods, which realize the examined operations in the algebra B n (&&,,!, t(), ). To create these methods we use bitwise operations: bitwise conjunction &, bitwise disjunction, bitwise exclusive or 203

and bitwise negation, using these operations we raise the effectiveness and make the algorithms work faster. Bn_tuple Bn_tuple :: t() { Bn_tuple temp(n); int k; for (int i=0; i<n; i++) for (int j=0; j<n; j++) { k=get_element(i,j); if (k) temp.set_1(j,i); temp.set_0(j,i); Bn_tuple Bn_tuple :: operator && (Bn_tuple &B) { Bn_tuple temp(n); for (int p=0; p<n; p++) *(temp.matr + p) = *(this->matr + p) & *(B.Matr + p); Bn_tuple Bn_tuple :: operator (Bn_tuple &B) { Bn_tuple temp(n); for (int p=0; p<n; p++) *(temp.matr + p) = *(this->matr + p) *(B.Matr + p); Bn_tuple Bn_tuple :: operator! () { Bn_tuple temp(n); for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { if ( get_element(i,j) ) temp.set_0(i,j); temp.set_1(i,j); 204

Bn_tuple Bn_tuple :: operator * (Bn_tuple &B) { Bn_tuple temp(n), TB(n); TB = t(b); int c, r_i, r_j; for (int i=0; i<n; i++) for (int j=0; j<n; j++) { r_i = this->get_row(i); r_j = TB.get_row(j); c = r_i & r_j; if (c==0) temp.set_0(i,j); temp.set_1(i,j); Bn_tuple& Bn_tuple :: operator = (const Bn_tuple &B) { cout<<"unallowable value\n"; for (int p=0; p<n; p++) *(this->matr + p) = *(B.Matr + p); return *this; int Bn_tuple :: operator < (Bn_tuple &B) { int p = 0; cout<<"unallowable value\n"; while ((get_row(p)==b.get_row(p))&&(p<n-1 )) p++; if (get_row(p)<b.get_row(p) ) return 1; return 0; Analogously to Proposition 1 we are convinced that the following proposition is true: Proposition 2. For each positive integer n, for the computer representation via the class Bn tuple, using the C++ programming language, of the algebra B n (&&,,!, t(), ), the following propositions are true: (i) When we use the above-described realization of the operation functions &&,, < and the predefining of the assignment operator, then each one of them performs O(n) operations of the set Op; (ii) The transpose and negation operation performs O(n 2 ) operations of the set Op. (iii) When we use the above-described realization of the operation functions, then this operation performs O(n 2 ) operations of the set Op; (iv) For every object of the class Bn tuple O(n) sizeof (int) bytes of the operating 205

memory are necessary; (v) Initialization of the object of the class Bn tuple can be performed using O(n) operations of the set Op. We see that to create the class Bn array is easy and it is not a difficult task even for the beginning programmer, when we describe the algorithms we conform to the definitions of the corresponding operations. On the other side, comparing Proposition 1 with Proposition 2 we are convinced that following proposition is true: Theorem 1. The algorithms using objects of the class Bn tuple work faster than the algorithms using objects of the class Bn array and they save a lot of operating memory. REFERENCES [1] J. Daintith, R. D. Nelson. The Penguin Dictionary of Mathemathics. Penguin books, 1989. [2] S. R. Davis. C++ for Dummies. IDG Books Worldwide, 2000. [3] B. W. Kernigan, D. M Ritchie. The C Programming Language. AT&T Bell Laboratories, 1998. [4] Tan Kiat Shi, W.-H. Steeb, Y. Hardy. Symbolic C++: An Introduction to Computer Algebra using Object-Oriented Programming. Springer, 2001. [5] K. Yordzhev. An Example for the Use of Bitwise Operations in programming. Mathematics and Education in Mathematics, 38 (2009), 196 202. [6] П. Азълов. Обектно ориентирано програмиране Структури от данни и STL. София, Сиела, 2008. [7] Е. Л. Романов. Практикум по программированию на C++. БХВ-Петербург, 2004. [8] М. Тодорова. Програмиране на С++. Част I, част II, София, Сиела, 2002. Hristina Kostadinova, Krasimir Yordzhev South-West University N. Rilsky 2700 Blagoevgrad, Bulgaria e-mail: hkostadinova@gmail.com e-mail: yordzhev@swu.bg, iordjev@yahoo.com ВЪРХУ ЕДНО ПРЕДСТАВЯНЕ НА БИНАРНИТЕ МАТРИЦИ Христина Костадинова, Красимир Йорджев В статията се обсъжда представянето на произволна бинарна матрица с помощта на последователност от цели неотрицателни числа. Разгледани са някои предимства и недостатъци на това представяне като алтернатива на стандартното, общоприето представяне чрез двумерен масив. Показано е, че представянето на бинарните матрици с помощта на наредени n-торки от естествени числа води до по-бързи алгоритми и до съществена икономия на оперативна памет. Използуван е апарата на обектно-ориентираното програмиране със синтаксиса и семантиката на езика C++. 206