Transitive Closure Algorithm using Binary OR Operation: Primes

The SIJ Transactions on Computer Science Engineering & its Applications (CSEA), Vol. 3, No. 4, April 2015
Transitive Closure Algorithm using
Binary OR Operation: Primes Algorithm,
GHK Algorithm
Dr. Shaik Mohiddin Shaw*, Dr. Dharmaiah Gurram**, Hari Krishna Gurram*** & Ramakrishna
Gurram****
*Professor, Department of Mathematics, NarasaraoPeta Engineering College, Andhra Pradesh, INDIA.
E-Mail: mohiddin_shaw26{at}yahoo{dot}co{dot}in
**Associate Professor, Department of Mathematics, NarasaraoPeta Engineering College, Andhra Pradesh, INDIA.
E-Mail: 10072014nec{at}gmail{dot}com
***Master of Computer Applications, National Institute of Technology, Warangal, Andhra Pradesh, INDIA.
E-Mail: ramakrishna.nitw19{at}gmail{dot}com
****M. Tech, Department of Computer Science, University College of Engineering Kakinada, Jawaharlal Nehru Technological University,
Andhra Pradesh, INDIA. E-Mail: harikrishna553{at}gmail{dot}com
Abstract—In real world scenarios, transitive closures play a vital role in computer networks RDBMS and
Graph theory. An efficient algorithm to find transitive closure is in need. Following paper presents new
algorithms to find out Transitive Closure of a Relation Matrix and compares the performance with Warshall
algorithm. Sections 3 and 4 explain primes and GHK algorithm respectively. Primes algorithm uses prime
numbers to find out transitive closures of a relation, and GHK algorithm uses bit sets to find out the transitive
closures of a relation efficiently. Section 4 presents experimental results of Primes and GHK against
Warshall‟s algorithm.
Keywords—Algorithms and Performance; Computer Networks; Design; Forward Scan; Graph Theory; Initial
Scan; Primes Algorithm.
Abbreviations—Relational Data Base Management System (RDBMS).
I.
A
INTRODUCTION
relation R on a binary relation X is transitive if for
every x,y,z in X if xRy, yRz then xRz. Many
algorithms are proposed to find transitive closure of
relation like Warshall [7, 9, 10], Warren [11] algorithms,
Matrix multiplication by Arlazarov et al., [1], the AhoCorasick algorithm by Alfred V. Aho et al., [2], Boolean
matrix multiplication and transitive closure by Fischer et al.,
[3], Linear Graph Algorithms by Tarjan [4] and by Schimitz
[5], Closure Algorithms by Raghu Rama Krishnan [6],
Graphs And Networks by Carre [8], which by Dewitt &
Gerber [13], Breaking through the o(n2) barrier by
Demetrescu & Italiano [14], which by Alsed et al., [15],
which by Blondel et al., [16], Large Graphs and Networks
with stability or stabilizability of linear systems with
parametric uncertainty, robust control, time-varying linear
systems, nonlinear and hybrid systems, and stochastic
optimal control by Blondel et al., [17], which by Bochnak et
al., [18] and by Collins [19] which use a bit-matrix
representation of the graph, the Schmitz [12] algorithm,
which uses Tarjan‟s algorithm to identify strongly connected
components in reverse topological order. Finding the
ISSN: 2321-2381
transitive closure of a directed graph is an important
subproblem in many computational tasks. Recently, transitive
closure computation has been recognized as a signicant
subproblem in evaluating recursive database queries, since
almost all practical recursive queries are transitive. In Section
2, we presented Warshall algorithm and in Section 3, we
presented PRIMES algorithm with an example. In Section 4,
we presented GHK algorithm with an explanation. In section
5, we presented the experimental resuls. Setion 6 concludes
this paper.
The motivation of the research is: Today all the
electronic devices like computers, laptops, mobiles etc., all
connected with internet; there is a need to transfer data
between these devices efficiently. By using efficient
transitive closure algorithm, the data transfer cost is reduced
at the expense of small communication cost.
II.
WARSHALL’S ALGORITHM
Stephen Warshall presented an efficient algorithm to compute
the transitive closure of a relation. Warshall's algorithm to
compute transitive closure is defined below.
© 2015 | Published by The Standard International Journals (The SIJ)
42
The SIJ Transactions on Computer Science Engineering & its Applications (CSEA), Vol. 3, No. 4, April 2015
Let 'n' be the number of nodes in the graph or number of
distinct elements in a relation. Array b[i][j] be the relation
matrix for the given relation, then Warshall's algorithm
computes the transitive closure of a relation in O(n3) time.
Algorithm Warshall( )
{
for k := 1 to n
{
for i := 1 to n
{
for j := 1 to n
{
b[i][j]=b[i][j]||(b[i][k] && b[k][j])
}
}
}
}
If a graph is represented as a relation with each tuple,
representing an arc, the Warshall algorithm can be
implemented in the following manner. For each node n, first
fetch its successor list. Then for each predecessor p of n,
fetch the successor list of p, and add to the successor list of p
the successor list of n (removing duplicates if any). In order
to determine the predecessors of n, the successor list of all
other nodes may be scanned to see if n appears in them. An
alternative would be to maintain, in addition to the successor
list, a predecessor list also with each node. In that case, the
determination of the predecessors of n would become trivial,
but at the time of updating the successor list of the
predecessor p, the predecessor list associated with each of the
successors of n must also be updated to include p in them.
III.
PRIMES ALGORITHM
In number theory, the fundamental theorem of Arithmetic
states that every integer greater than 1 is either Prime itself or
the product of Prime numbers. That means, every composite
integer (n > 1) can be represented in exactly one way as a
product of Prime powers. PRIMES Algorithm uses
Fundamental Theorem of Arithmetic and Simple division
logic which satisfies Transitive property.
In PRIMES algorithm, each vertex is assigned with a
unique Prime number. It works on basic division principle,
“If a divides b and b divides c then a divides c”.
3.1. Notation
n: Number of nodes in a Graph
a[n][n]: Relation matrix for a given relation
prime[n] : Array which contains first 'n' prime numbers
Forward[n]: Array which contains the final values to compute
transitive closures
Hence, Total space required for this algorithm is
1. O(n*n): To store relation matrix
2. O(n): To store first 'n' prime numbers
3. O(n): To store 'n' computed values in forward[n]
So, total space needed is O(n*n + 2n).
ISSN: 2321-2381
3.2. PRIMES Algorithm Pseudo Code
Step 1: Read the relation matrix a[n][n]
Step 2: Calculate first 'n' prime numbers and save these
in prime[n] and initialize forward[n] with 1 by default
Step 3: Call the initial scan procedure. Pseudo code is
given below
Proc initialscan()
{
for j := 1 to n
{
for i := 1 to n
{
if(a[j][i] == 1)
{
forward[i] = forward[i] * prime[j]
}
}
}
}
Step 4: Call the forwardScan method, to compute
transitive closures
Proc forwardScan()
{
for i := 1 to n
{
for j := 1 to n
{
if(forward[j] % prime[i] =0)
{
forward[j]=(forward[j]/gcd(forward[j],forward[i]))
*forward[i]
}
}
}
}
gcd() finds the Greatest common divisor of two
numbers.
Step 5: Call the compute procedure to update the final
transitive closure matrix
Proc Compute()
{
for i := 1 to n
{
for j := 1 to n
{
if(forward[i]%prime[j] == 0)
a[j][i]=1
}
}
}
a[j][i] = 1 means there exist a relation from j to i
a[j][i] = 0 means there is no relation from j to i
Usually multiplication operation consumes more time as
compared to addition, subtraction and bitwise operations.
Implementation results in Section 5 shows both Primes and
© 2015 | Published by The Standard International Journals (The SIJ)
43
The SIJ Transactions on Computer Science Engineering & its Applications (CSEA), Vol. 3, No. 4, April 2015
Warshall‟s algorithms are running with nearly equal time
complexity.
Since Primes algorithm needs Prime numbers
Multiplication so it takes time complexity of O(n3) like
Warshall‟s Algorithm. In Section 4, we reduced the time
complexity of Primes algorithm by modifying Primes
multiplication operation with Binary OR operation in GHK
algorithm.
3.3. Example to Find Transitive Closure using Primes
Algorithm
Consider the below relation matrix:
0
1
0
0
0
0
1
0
0
1
0
1
0
0
0
0
Let a[4][4] is the relation matrix that represents above
relation.
Prime[4] is one dimensional matrix that store first 4
prime numbers.
Prime[4] = {2, 3, 5, 7}
forward[4] is initialized with values 1
forward[4] = {1,1,1,1}
Call the initial scan procedure to update the array
forward[4]
After calling the procedure initial scan the array is like
below
forward[4] = { 1, 10, 3, 5 }
Call the forward scan method to find out transitive
closure relations
After calling forward scan method the forward scan array
is like below
forward[4] = { 1, 30, 30, 30 }
Call the compute method to update the given input
relation matrix
Final transitive relation matrix is
0
1
1
1
0
1
1
1
0
1
1
1
0
0
0
0
IV.
1
GHK ALGORITHM
2
5
3
4
Figure 1: Directed Graph
As shown in Figure 1, Graph has 5 vertices. Initial scan
computes initial Bitsets for every Vertex. Bitset of a Vertex
represents the outgoing edges from the vertex.
Bitset[1] = 01000 since vertex 1 has outgoing edges to
vertex 2 and 3. So, 2nd and 3rd bits of Bitset[1] are set to 1 and
ISSN: 2321-2381
remaining bits are set to 0. Similarly the values for remaining
vertices are shown below.
Bitset[2] = 00011
Bitset[3] = 10010
Bitset[4] = 00001
Bitset[5] = 00000
forwardScan method computes transitive closure in „n‟
iterations. For each iteration, Bitset of every vertex will be
computed and updated accordingly. In ith iteration,
forwardScan method checks is there any incoming edges to
vertex „i‟ from all vertices. In first iteration, we will check for
an incoming edge to vertex 1. If an edge exists from vertex
„v‟, then Bitset[v] will be updated by performing binary OR
operation on Bitsets v and i.
Bitset[v] = Bitset[v] or Bitset[1];
For Example: - After first iteration, Bitsets for the
vertices will be updated as shown below.
Bitset[1] = 01000
Bitset[2] = 00011
Bitset[3] = Bitset[3] or Bitset[1]
= 10010 or 01000
= 11010
Bitset[4] = 00001
Bitset[5] = 00000
Since Vertex 1 has an incoming edge from vertex 3, so
all the paths that vertex 1 can reach, also reachable by vertex
3. Bitset[3] updated accordingly.
4.1. Notation
n: Number of nodes in a Graph
a[n][n]: Relation matrix for a given relation
Bitset[n]: Bitsets representing n vertices.
Hence Total space required for this algoritm is
1. n*n
- To store relation matrix
2. n*n bits
- To define Bitset[]
So total space required is n*n + n*n bits.
4.2. GHK Algorithm Pseudo Code
Step 1: Read the relation matrix a[n][n]
Step 2: Initialize Bitset[n] with 0 by default, i.e, all the
bit values in each element of Bitset[n] are set to 0
Step 3: Call the initial scan procedure. Pseudo code is
given below
Proc initialscan()
{
for i := 1 to n
{
for j := 1 to n
{
if(a[i][j] == 1)
{
Bitset[i].set(j)
}
}
}
}
© 2015 | Published by The Standard International Journals (The SIJ)
44
The SIJ Transactions on Computer Science Engineering & its Applications (CSEA), Vol. 3, No. 4, April 2015
Step 4: Call the forwardScan method, to compute
transitive closures
Proc forwardScan()
{
for i := 1 to n
{
for j := 1 to n
{
if(Bitset[j].get(i))
{
Bitset[j] = Bitset[j] or Bitset[i]
}
}
}
}
Step 5: Call the compute procedure to update the final
transitive closure matrix
Proc Compute()
{
for i := 1 to n
{
for j := 1 to n
{
if(Bitset[i].get(j))
{
a[i][j] = 1
}
}
}
}
a[i][j] = 1 means there exist a relation from i to j
a[i][j] = 0 means there is no relation from i to j
Bitset[i].set(j)
: sets the jth bit of Bitset[i]
Bitset[i].get(j)
: gets the jth bit of Bitset[i]
GHK algorithm is an enhancement to Primes Algorithm
and it runs faster as compared to Warshall‟s algorithm.
Experimental results show it works better than Warshall's
algorithm.
Figure 3: Comparison between Warshal and GHK Algorithm by
taking 10000 random samples of each relation matrix
We took 10000 random samples of relation matrices of
each size from 1000 to 8000, in all the cases GHK Algorithm
works better than Warshall Algorithm.
VI.
We presented two algorithms that computes all transitive
closures of a relation matrix efficiently. We compared the
two algorithms with Warshall's algorithm. GHK algorithm
runs in O(n*n) time which works efficiently as compared to
Warshall's algorithm.
ACKNOWLEDGEMENTS
First two authors express sincere thanks to the management
of NARASARAOPETA ENGINEERING COLLEGE, for
providing necessary Research Facilities. Finally authors
thanks to the referees for their valuable suggestions in
improvement of this research article.
REFERENCES
[1]
[2]
[3]
V.
EXPERIMENTAL RESULTS
[4]
[5]
[6]
[7]
[8]
[9]
[10]
[11]
Figure 2: Comparison between Warshal and Primes Algorithm by
taking 10000 random samples of each relation matrix
ISSN: 2321-2381
CONCLUSION
V. Arlazarov, E. Dinic, M. Kronrod & I. Faradzev (1970), “On
Economical Construction of the Transitive Closure of a
Directed Graph”, Dokl. Akad. Nauk. (in Russian) 194 (11),
English Translation in Soviet MathDokl.
Alfred V. Aho, John E. Hopcroft & Jeffrey D. Ullman (1974),
“The Design and Analysis of Computer Algorithms”, AddisonWesley.
M.J. Fischer & A.R. Meyer (1971), “Boolean Matrix
Multiplication and Transitive Closure”, Proceedings of the 12th
Annual Symposium on Switching and Automata Theory, Pp.
129–131.
Tarjan (1972), “Depth First Search and Linear Graph
Algorithms”, Siam Jour of Computing, Vol. 1, No. 2, Pp. 146–
160.
L. Schimitz (1983), “An Improved Transitive Closure
Algorithm”, Computing, Vol. 30, Pp. 359–371.
Raghu Rama Krishnan (1988), “Efficient Transitive Closure
Algorithms”, Proceedings of the 14th VLDB Conference, Los
Angeles, California, Pp. 382–394.
Warshall (1962), “A Theorem on Boolean Matrices”, JACM,
Vol. 9, No. 1, Pp. 11–12.
B. Carre (1979), “Graphs and Networks”, Clarendon Press
Oxford, England.
http://en.wikipedia.org/wiki/Transitive_closure
http://en.wikipedia.org/wiki/Floyd-Warshall_algorithm
H. S. Warren (1975), “A Modification of Warshall‟s Algorithm
for the Transitive Closure of Binary Relations”,
Communications of the ACM, Vol. 18, No. 4, Pp. 218–220.
© 2015 | Published by The Standard International Journals (The SIJ)
45
The SIJ Transactions on Computer Science Engineering & its Applications (CSEA), Vol. 3, No. 4, April 2015
[12]
[13]
[14]
[15]
[16]
[17]
[18]
[19]
L. Schmitz (1983), “An Improved Transitive Closure
Algorithm”, Computing, Vol. 30, Pp. 359–371.
D.J. Dewitt & R.H. Gerber (1985), “Multiprocessor sh-Based
Join Algorithms”, Proceedings of 11th International
Conference on Very Large Data Bases, Stockholm, Sweden,
Pp. 151–164.
C. Demetrescu & G. Italiano (2000), “Fully Dynamic
Transitive Closure: Breaking through the o(n2) Barrier”,
Proceedings of the 41st Annual Symposium on Foundations of
Computer Science (FOCS), Pp. 381–389.
Ll. Alsed`a, J. Llibre & M. Misiurewicz (1993),
“Combinatorial Dynamics and Entropy in Dimension One”,
Advances Series in Nonlinear Dynamics, World Scientific, Vol.
5.
V.D. Blondel, O. Bournez, P. Koiran, C.H. Papadimitriou &
J.N. Tsitsiklis (2001), “Deciding Stability and Mortality of
Piecewise Affine Dynamical Systems”, Theoretical Computer
Science, Vol. 255, No. 1-2, Pp. 687–696.
V.D. Blondel, O. Bournez, P. Koiran & J.N. Tsitsiklis (2001),
“The Stability of Saturated Linear Dynamical Systems is
Undecidable, Journal of Computer and System Sciences, Vol.
62, No. 3, Pp. 442–462.
J. Bochnak, M. Coste & M.-F. Roy (1998), “Real Algebraic
Geometry”, Ergebenisse der Mathematik und ihrer
Grenzgebiete. Folge 3, Springer, Vol. 36.
G.E. Collins (1975), “Quantifier Elimination for Real Closed
Fields by Cylindrical Algebraic Decomposition”, Lecture Notes
in Computer Science, Vol. 33, Pp. 134–183.
Dr. Dharmaiah Gurram presently working
as an Associate Professor of Mathematics at
Narasaraopeta
Engineering
college,
Narasaraopeta. He has more than 14 years of
teaching and research experience. He worked
in many reputed Universities / Institutions. He
had published more than 18 research/review
papers on Graph Theory, more than 4 on
Applied Mathematics(Fluid Dynamics) and
more than six on Computer Sciences. He attended 10
National/International Conferences and presented Papers. He is
governing body member in many professional bodies.
Hari Krishna Gurram is a post graduate in
computer science from JNTU Kakinada. He
has 4 Yrs of work experience in software
field. He worked in aeronautical, banking and
mobile domain. Areas of interests are
exploring new technologies, solving puzzles.
Rama Krishna Gurram is a post graduate in
Master Of Computer Applications from NIT
Warangal. He has 5 years of work experience
in software development. He has experience
in trading and e-commerce domain. Areas of
interests are mathematics & algorithms.
Dr. Shaik Mohiddin Shaw was awarded
with Ph.D., in Mathematics from Acharya
Nagarjuna University in 2012. He has 9 years
of teaching experience and 11years of
research experience. Presently working as a
Professor of Mathematics at Narasaropeta
Engineering College, Narasaropeta. He had
published more than 12 research papers in
reputed international/National Journals. He
authored / Edited more than 4 books one of his book published by
Dr. Mullar VDM verlog, Germany. He is a life member in different
Professional bodies. Also an editorial / reviewer board member in
different reputed International journals. Areas of interest are
Algebra, Fuzzy Sets and Systems, Graph Theory, Computer
Science. He attended and presented more than 15 papers in National/
International Conferences.
ISSN: 2321-2381
© 2015 | Published by The Standard International Journals (The SIJ)
46