An Optimal Algorithm for Prufer Codes *

Similar documents
Parallelism for Nested Loops with Non-uniform and Flow Dependences

Related-Mode Attacks on CTR Encryption Mode

Non-Split Restrained Dominating Set of an Interval Graph Using an Algorithm

Compiler Design. Spring Register Allocation. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Load Balancing for Hex-Cell Interconnection Network

Solving two-person zero-sum game by Matlab

Problem Set 3 Solutions

Sequential search. Building Java Programs Chapter 13. Sequential search. Sequential search

Sorting Review. Sorting. Comparison Sorting. CSE 680 Prof. Roger Crawfis. Assumptions

An Application of the Dulmage-Mendelsohn Decomposition to Sparse Null Space Bases of Full Row Rank Matrices

Design and Analysis of Algorithms

Course Introduction. Algorithm 8/31/2017. COSC 320 Advanced Data Structures and Algorithms. COSC 320 Advanced Data Structures and Algorithms

On Some Entertaining Applications of the Concept of Set in Computer Science Course

6.854 Advanced Algorithms Petar Maymounkov Problem Set 11 (November 23, 2005) With: Benjamin Rossman, Oren Weimann, and Pouya Kheradpour

ON SOME ENTERTAINING APPLICATIONS OF THE CONCEPT OF SET IN COMPUTER SCIENCE COURSE

Fast Computation of Shortest Path for Visiting Segments in the Plane

For instance, ; the five basic number-sets are increasingly more n A B & B A A = B (1)

The Greedy Method. Outline and Reading. Change Money Problem. Greedy Algorithms. Applications of the Greedy Strategy. The Greedy Method Technique

The Shortest Path of Touring Lines given in the Plane

F Geometric Mean Graphs

CMPS 10 Introduction to Computer Science Lecture Notes

NUMERICAL SOLVING OPTIMAL CONTROL PROBLEMS BY THE METHOD OF VARIATIONS

CHAPTER 2 DECOMPOSITION OF GRAPHS

Module Management Tool in Software Development Organizations

CS1100 Introduction to Programming

GSLM Operations Research II Fall 13/14

An Iterative Solution Approach to Process Plant Layout using Mixed Integer Optimisation

Cluster Analysis of Electrical Behavior

A Facet Generation Procedure. for solving 0/1 integer programs

Greedy Technique - Definition

CE 221 Data Structures and Algorithms

Constructing Minimum Connected Dominating Set: Algorithmic approach

Problem Definitions and Evaluation Criteria for Computational Expensive Optimization

Insertion Sort. Divide and Conquer Sorting. Divide and Conquer. Mergesort. Mergesort Example. Auxiliary Array

Sum of Linear and Fractional Multiobjective Programming Problem under Fuzzy Rules Constraints

CSCI 104 Sorting Algorithms. Mark Redekopp David Kempe

A mathematical programming approach to the analysis, design and scheduling of offshore oilfields

R s s f. m y s. SPH3UW Unit 7.3 Spherical Concave Mirrors Page 1 of 12. Notes

Learning the Kernel Parameters in Kernel Minimum Distance Classifier

Programming in Fortran 90 : 2017/2018

Determining the Optimal Bandwidth Based on Multi-criterion Fusion

Concurrent Apriori Data Mining Algorithms

A New Approach For the Ranking of Fuzzy Sets With Different Heights

Private Information Retrieval (PIR)

NGPM -- A NSGA-II Program in Matlab

A fast algorithm for color image segmentation

CSE 326: Data Structures Quicksort Comparison Sorting Bound

CHARUTAR VIDYA MANDAL S SEMCOM Vallabh Vidyanagar

The Research of Ellipse Parameter Fitting Algorithm of Ultrasonic Imaging Logging in the Casing Hole

A Five-Point Subdivision Scheme with Two Parameters and a Four-Point Shape-Preserving Scheme

Cordial and 3-Equitable Labeling for Some Star Related Graphs

Classifier Selection Based on Data Complexity Measures *

INTEGER PROGRAMMING MODELING FOR THE CHINESE POSTMAN PROBLEMS

SLAM Summer School 2006 Practical 2: SLAM using Monocular Vision

the nber of vertces n the graph. spannng tree T beng part of a par of maxmally dstant trees s called extremal. Extremal trees are useful n the mxed an

Today s Outline. Sorting: The Big Picture. Why Sort? Selection Sort: Idea. Insertion Sort: Idea. Sorting Chapter 7 in Weiss.

Meta-heuristics for Multidimensional Knapsack Problems

Bridges and cut-vertices of Intuitionistic Fuzzy Graph Structure

A Binarization Algorithm specialized on Document Images and Photos

Kent State University CS 4/ Design and Analysis of Algorithms. Dept. of Math & Computer Science LECT-16. Dynamic Programming

A Topology-aware Random Walk

K-means and Hierarchical Clustering

Cracking of the Merkle Hellman Cryptosystem Using Genetic Algorithm

CS221: Algorithms and Data Structures. Priority Queues and Heaps. Alan J. Hu (Borrowing slides from Steve Wolfman)

Repeater Insertion for Two-Terminal Nets in Three-Dimensional Integrated Circuits

Virtual Machine Migration based on Trust Measurement of Computer Node

CS 534: Computer Vision Model Fitting

Sorting. Sorting. Why Sort? Consistent Ordering

Type-2 Fuzzy Non-uniform Rational B-spline Model with Type-2 Fuzzy Data

3D vector computer graphics

CSE 326: Data Structures Quicksort Comparison Sorting Bound

Quality Improvement Algorithm for Tetrahedral Mesh Based on Optimal Delaunay Triangulation

CHAPTER 10: ALGORITHM DESIGN TECHNIQUES

Available online at Available online at Advanced in Control Engineering and Information Science

Computation of a Minimum Average Distance Tree on Permutation Graphs*

Can We Beat the Prefix Filtering? An Adaptive Framework for Similarity Join and Search

5 The Primal-Dual Method

International Journal of Scientific & Engineering Research, Volume 7, Issue 5, May ISSN Some Polygonal Sum Labeling of Bistar

Distributed Middlebox Placement Based on Potential Game

Virtual Memory. Background. No. 10. Virtual Memory: concept. Logical Memory Space (review) Demand Paging(1) Virtual Memory

Mathematics 256 a course in differential equations for engineering students


An Efficient Algorithm for Minimum Vertex Cover Problem

Machine Learning: Algorithms and Applications

Intra-Parametric Analysis of a Fuzzy MOLP

MULTISPECTRAL IMAGES CLASSIFICATION BASED ON KLT AND ATR AUTOMATIC TARGET RECOGNITION

S1 Note. Basis functions.

Approximations for Steiner Trees with Minimum Number of Steiner Points

UNIT 2 : INEQUALITIES AND CONVEX SETS

EVALUATION OF THE PERFORMANCES OF ARTIFICIAL BEE COLONY AND INVASIVE WEED OPTIMIZATION ALGORITHMS ON THE MODIFIED BENCHMARK FUNCTIONS

Line Clipping by Convex and Nonconvex Polyhedra in E 3

Determining the Optimal Network Partition and Kanban Allocation in JIT Production Lines

Harvard University CS 101 Fall 2005, Shimon Schocken. Assembler. Elements of Computing Systems 1 Assembler (Ch. 6)

An Application of Network Simplex Method for Minimum Cost Flow Problems

Simulation Based Analysis of FAST TCP using OMNET++

Support Vector Machines

X- Chart Using ANOM Approach

Loop Transformations, Dependences, and Parallelization

Priority queues and heaps Professors Clark F. Olson and Carol Zander

AN IMPROVED GENETIC ALGORITHM FOR RECTANGLES CUTTING & PACKING PROBLEM. Wang Shoukun, Wang Jingchun, Jin Yihui

Transcription:

J. Software Engneerng & Applcatons, 2009, 2: 111-115 do:10.4236/jsea.2009.22016 Publshed Onlne July 2009 (www.scrp.org/journal/jsea) An Optmal Algorthm for Prufer Codes * Xaodong Wang 1, 2, Le Wang 3, Yngje Wu 1 1 Department of Computer Scence, Fuzhou Unversty, Fuzhou, Chna; 2 Department of Computer Scence, Quanzhou Normal Unversty, Quanzhou, Chna; 3 College of Computng, Georga Insttute of Technology, Atlanta GA 30332, USA. Emal: wangxd@fzu.edu.cn Receved February 19 th, 2009; revsed February 23 rd, 2009; accepted February 24 th, 2009. ABSTRACT Ths paper studes the algorthms for codng and decodng Prufer codes of a labeled tree. The algorthms for codng and decodng Prufer codes of a labeled tree n the lteratures requre Onlogn) ( tme usually. Although there exst lnear tme algorthms for Prufer-lke codes [1,2,3], the algorthms utlze the nteger sortng algorthms. The specal range of the ntegers to be sorted s utlzed to obtan a lnear tme nteger sortng algorthm. The Prufer code problem s reduced to nteger sortng. In ths paper we consder the Prufer code problem n a dfferent angle and a more drect manner. We start from a naïve algorthm, then mproved t gradually and fnally we obtan a very practcal lnear tme algorthm. The technques we used n ths paper are of nterest n ther own rght. Keywords: Desgn of Algorthm, Labeled Trees, Prufer Codes, Integer Sortng 1. Introducton * Supported by Natural Scence Foundaton of Chna under Grant No. 60172017 and Natural Scence Foundaton of Fujan under Grant No. A0510008. Labeled trees are of nterest n practcal and theoretcal areas of computer scence. For example, Ethernet has a unque path between termnal devces, thus beng a tree: labelng the tree nodes s necessary to unquely dentfy each devce n the network. An nterestng alternatve to the usual representatons of tree data structures n computer memores s based on codng labeled trees by means of strngs of node labels. Ths representaton was frst used n the proof of Cayley s theorem [4] to show a one-to-one correspondence between free labeled trees on n nodes and strngs of length n-2. In addton to ths purely mathematcal use, strng-based codng of trees has many practcal applcatons. For nstance, they make t possble to generate random unformly dstrbuted trees and random connected graphs: the generaton of a random strng followed by the use of a fast decodng algorthm s typcally more effcent than random tree generaton by the addton of edges, snce n the latter case one must pay attenton not to ntroduce cycles. In addton, tree codes are employed n genetc algorthms, where chromosomes n the populaton are represented as strngs of ntegers, and n heurstcs for computng mnmum spannng trees wth addtonal constrants, e.g., on the number of leaves or on the dameter of the tree tself. Not last, tree codes are used for data compresson and for computng the tree and forest volumes of graphs. Let T be a labeled tree whose nodes are numbered from 0 to n-1. For some vertex v n T, the degree of v, denoted by d[v], s the number of edges ncdent to v. If d[v]=1, then v s called a leaf. Accordng to Prufer s proof, any sequence of n-2 numbers, each number n {0,1,,n-1} can determne a unque labeled tree of n nodes. Such a number sequence s the Prufer code of a labeled tree. Algorthm A s the straghtforward mplementaton of Prufer s proof. Algorthm A Input: A labeled tree T of n nodes as a lst of n-1 edges. Output: c, the Prufer code of T. Method: Step 1. B {0,1,,n-1}. Step 2. For 0 to n-3 do Step 2.1. x mn{k B: k s a leaf }. Step 2.2. B B-{x}. Step 2.3. Remove x and ts ncdent edge (x, y) from T. Step 2.4. c[] y. Step 3. Return c. End of Algorthm For example, let the nput labeled tree T be the graph depcted n Fgure 1. After the Algorthm A termnates, c = [2,4,0,1,3,3] whch s the Prufer code of T. Copyrght 2009 ScRes

112 An Optmal Algorthm for Prufer Codes Fgure 1. A labelled tree The algorthms for codng and decodng Prufer codes of a labeled tree n the lteratures requre Onlogn) ( tme usually. As stated n [3], although the problem of producng a Prufer code n lnear tme s an exercse n two books [5, 6], there exsts no explct publcaton of a soluton. In [3] an On ( ) tme algorthm for generatng a Prufer code from a labeled tree was descrbed, but the On ( ) tme algorthm utlzed the nteger sortng algorthms. The specal range of the ntegers to be sorted was utlzed to obtan a lnear tme nteger sortng algorthm. The Prufer code problem s reduced to nteger sortng. In ths paper we consder the Prufer code problem n a dfferent angle and present a very practcal lnear tme algorthm drectly. The technques we used n ths paper are of nterest n ther own rght. 2. A Lnear Tme Algorthm for Codng The most tme consumng step n Algorthm A s Step 2. The leaf elmnaton scheme of the Prufer code mplctly defnes a root for the labeled tree T. Actually, t s easy to see that the last element n the code s n-1 whch s the root of the labeled tree T. Gven a lst of n-1 edges, we can buld the parent array f and the degree array d of the labeled tree T wth only On ( ) preprocessng tme, whch allows checkng and updatng a node's degree n O(1) tme. Wth ths preprocessng, the Step 2.3 of Algorthm A can be mplemented n O(1) tme. Usng a heap, the leaf wth the smallest number can be found n Onl ( ogn) tme. Hence, Step 2 takes Onlogn ( ) tme totally. The tme complexty of Algorthm A s also Onl ( ogn). We can mprove the algorthms further. An nsght nto the problem s that for the heap operatons n the Step 2.1 of Algorthm A, there s a specal knd of nodes that they are deleted from the heap mmedately after they are nserted nto the heap. Ths knd of nodes can be treated easly wthout heap operaton. The remaned heap operaton can be replaced by a lnear scan of the degree array d. Therefore the total tme to fnd x n the Step 2.1 s reduced to O(n). The lnear tme algorthm can be presented detaled as follows. Codng Algorthm: 1: ndex x mn{0 k<n: d[k]=1} 2: for 0 to n-3 do 3: y f[x] 4: c[] y 5: d[y] d[y]-1 6: f y<ndex and d[y]=1 then x y 7: else ndex x mn{ndex<k<n: d[k]=1} In the algorthm descrbed above, d[v] s the degree of node v and f[v] s the parent node of the node v. The varable ndex s a cursor of degree array d. On lne 6, when node y becomes a leaf and the label of y s less than the current cursor ndex, the node y must be the knd of nodes that are deleted from the heap mmedately after they are nserted nto the heap. In ths case, the node y becomes the next node wth mnmal label. Otherwse, on lne 7 the cursor ndex moves to the next leaf node n the degree array d. The computng tme of lne 1 and lne 7 s On ( ), snce the cursor ndex goes through the degree array d from left to rght once. The remanng tme s clearly On) (, leadng to On ( ) complexty for the entre algorthm. 3. A Lnear Tme Algorthm for Decodng Decodng s to buld the tree T correspondng to the gven Prufer code c. As far as c s computed, each node label n t represents the parent of a leaf elmnated from T. Hence, n order to reconstruct T, t s suffcent to compute the ordered sequence of labels of the elmnated leaves, say s: for each {0,1,,n-1}, the par (c[], s[]) wll thus be an edge n the tree. We frst observe that the leaves of T are exactly those nodes that do not appear n the code, as they are not parents of any node. Each nternal node, say v, n general may appear n c more than once; each appearance corresponds to the elmnaton of one of ts chldren, and therefore to decreasng the degree of v by 1. After the rghtmost occurrence n the code, v s clearly a leaf and thus becomes a canddate for beng elmnated. Therefore, the tmes of a node v appears n c s exactly the degree of v mnus 1. A lnear scan of code array c can determne the degree array d. Usng a heap, the leaf wth the smallest number can be found n O( nlogn ) tme, leadng an Onlogn ( ) tme decodng algorthm. Lke the codng algorthm, we can mprove the algorthms further. An nsght nto the problem s also that for the heap operatons of the decodng algorthm, there s a specal knd of nodes that they are deleted from the heap mmedately after they are nserted nto the heap. Ths knd of nodes can be treated easly wthout heap operaton. The remaned heap operaton can be replaced by a lnear scan of the degree array d. Copyrght 2009 ScRes

An Optmal Algorthm for Prufer Codes 113 The lnear tme algorthm can be presented detaled as follows. Decodng Algorthm: 1: ndex x mn{0 k<n: d[k]=1} 2: for 0 to n-2 do 3: y c[] 4: add edge (x,y) to T 5: d[y] d[y]-1 6: f y<ndex and d[y]=1 then x y 7: else ndex x mn{ndex<k<n: d[k]=1} Lke the codng algorthm, the tme requred by the decodng s also On ( ). 4. Examples We use the labeled tree T depcted n Fgure 1 as an example to demonstrate the algorthms for codng and decodng Prufer codes descrbed above. The nput of the labeled tree T s an edge lst {0,1}{0,4}{1,3}{4,2}{3,6} {3,7}{2,5}. There are 8(n) nodes labeled 0,1,2,3,4,5,6,7 and 7(n-1) edges. It s easy to see that the last element 7 s the root of the labeled tree T. For the gven edge lst, we can buld the parent array f and the degree array d of the labeled tree T by a depth frst search wth only On ( ) tme. Wth ths two arrays, the Step 2.3 of Algorthm A can be mplemented n O(1) tme. For our lnear tme codng algorthm, we frst go through the degree array d to fnd an ndex such that ndex=mn{0 k<n: d[k]=1}. It s clear that the ndex equals 5 for the frst tme n our example as shown n Table 1. Table 1. The parent array f and the degree array d of T 0 1 2 3 4 5 6 7 f[] 1 3 4 7 0 2 3-1 d[] 2 2 2 3 2 1 1 1 ndex Table 2. After edge {2,5} s deleted 0 1 2 3 4 [5] 6 7 f[] 1 3 4 7 0 2 3-1 d[] 2 2 1 3 2 1 1 1 ndex Table 3. After edge {4,2} s deleted 0 1 [2] 3 4 [5] 6 7 f[] 1 3 4 7 0 2 3-1 d[] 2 2 1 3 1 1 1 1 ndex Table 4. After the edges {0,4}, {0,1} and {1,3} are deleted [0] [1] [2] 3 [4] [5] 6 7 f[] 1 3 4 7 0 2 3-1 d[] 1 1 1 2 1 1 1 1 ndex Table 5. After the edge {3,6} s deleted [0] [1] [2] 3 [4] [5] [6] 7 f[] 1 3 4 7 0 2 3-1 d[] 1 1 1 1 1 1 1 1 ndex f[ndex]=f[5]=2 s the frst value of the Prufer code c. The edge {2,5} s deleted and d[2] s decreased by 1 on lne 5 of the codng algorthm. The status of the tree T now becomes Table 2. Follows the codng algorthm on lne 6 the next node to be deleted s 2. The father node of 2 s node 4, the next value of the Prufer code c. The edge {4,2} s deleted and d[4] s decreased by 1 on lne 5 of the codng algorthm. The status of the tree T now becomes Table 3. Smlarly, n the next three steps we obtan the next three values 0,1 and 3 of the Prufer code c. The edges {0,4}, {0,1} and {1,3} are deleted accordngly. The Prufer code we have obtaned up to now s (2,4,0,1,3). The status of the tree T now becomes Table 4. Look at the Table 4, the next node to be deleted s 6 whch s determned on lne 7 of our codng algorthm. We do not scan the degree array d from the begnnng. We scan the degree array d from ndex (5) to rght and the ndex s moved to 6. Ths s a key pont to see the algorthm runnng n lnear tme. The father node of 6 s node 3, the next value of the Prufer code c. The edge {3,6} s deleted and d[3] s decreased by 1 on lne 5 of the codng algorthm. The status of the tree T now becomes Table 5. Copyrght 2009 ScRes

114 An Optmal Algorthm for Prufer Codes Table 6. The nto status of the decodng algorthm 0 1 2 3 4 5 6 7 c[] 2 4 0 1 3 3 7-1 d[] 2 2 2 3 2 1 1 1 ndex Table 7. After edge {2,5} s added 0 1 2 3 4 [5] 6 7 c[] 2 4 0 1 3 3 7-1 d[] 2 2 1 3 2 1 1 1 ndex Table 8. After edge {2,4} s added 0 1 [2] 3 4 [5] 6 7 c[] 2 4 0 1 3 3 7-1 d[] 2 2 1 3 1 1 1 1 ndex We now have the n-2 values of the Prufer code (2,4,0,1,3,3). The algorthm termnates. Usng the same labeled tree T depcted n Fgure 1 as an example we can demonstrate the algorthm for decodng Prufer codes as follows. The nput of the decodng algorthm s the Prufer code c of the tree T. In our example the Prufer code of the tree T s (2,4,0,1,3,3). We fst noted that the tmes of a node v appears n the Prufer code c s exactly the degree of v mnus 1. A lnear scan of code array c can determne the degree array d as shown n Table 6. For our lnear tme decodng algorthm, we frst go through the degree array d to fnd an ndex such that ndex=mn{0 k<n:d[k]=1}. It s clear that the ndex equals 5 for the frst tme n our example as shown n Table 6 whch s the frst leaf node deleted n the codng algorthm. c[0]=2 s the other node label of the edge to be added. The edge {2,5} s added and d[2] s decreased by 1 on lne 5 of the decodng algorthm. The status of the tree T now becomes Table 7. Follows the decodng algorthm on lne 6 the next node to be added s 2. c[1]=4 s the other node label of the edge to be added. The edge {2,4} s added and d[4] s decreased by 1 on lne 5 of the decodng algorthm. The status of the tree T now becomes Table 8. Smlarly, n the next three steps we obtan the next three edges {0,4}, {0,1} and {1,3}. The status of the tree T now becomes Table 9. Look at the Table 9. The next node to be added s 6 whch s determned on lne 7 of our decodng algorthm. We do not scan the degree array d from the begnnng. We scan the degree array d from ndex (5) to rght and the ndex s moved to 6. Ths s a key pont to see the decodng algorthm runnng n lnear tme. The other node label of the edge to be added s c[5]=3. The edge {3,6} s added and d[3] s decreased by 1 on lne 5 of the decodng algorthm. The status of the tree T now becomes Table 10. Follows the decodng algorthm on lne 6 the next node to be added s 3. c[6]=7 s the other node label of the edge to be added. The edge {3,7} s added. We now have the n-1 edges of the tree T. The decodng algorthm termnates. 5. Conclusons Prufer codes for labeled trees have many practcal applcatons. The algorthms for codng and decodng Prufer codes of a labeled tree are of nterest n practcal and theoretcal areas of computer scence. The exstng lnear tme algorthms for Prufer-lke codes utlzed the nteger sortng algorthms. The specal range of the ntegers to be sorted s utlzed to obtan a lnear tme nteger sortng algorthm. The optmal algorthms for codng and decodng Prufer codes presented n ths paper are very practcal lnear tme algorthms. The technques used n these algorthms are of nterest n ther own rght. Table 9. After the edges {0,4}, {0,1} and {1,3} are added [0] [1] [2] 3 [4] [5] 6 7 c[] 2 4 0 1 3 3 7-1 d[] 1 1 1 2 1 1 1 1 ndex Table 10. After the edge {3,6} s added [0] [1] [2] 3 [4] [5] [6] 7 c[] 2 4 0 1 3 3 7-1 d[] 1 1 1 1 1 1 1 1 ndex Copyrght 2009 ScRes

An Optmal Algorthm for Prufer Codes 115 6. Acknowledgments Ths work was supported by Natural Scence Foundaton of Chna under Grant No. 60172017 and Natural Scence Foundaton of Fujan under Grant No. A0510008. REFERENCES [1] S. Camnt, I. Fnocch, and R. Petresch, A unfed approach to codng labeled trees, n Proceedngs of the 6th Latn Amercan Symposum on Theoretcal Informatcs (LATIN 04), LNCS 2976, pp. 339-348, 2004. [2] S. Camnt, I. Fnocch, and R. Petresch, On codng labeled trees, To appear on Theoretcal Computer Scence, 2006. [3] H. C. Chen and Y. L. Wang, An effcent algorthm for generatng Prufer codes from labeled trees, Theory of Computng Systems, Vol. 33, pp. 97 105, 2000. [4] A. Cayley, A theorem on trees, Quarterly Journal of Mathematcs, Vol. 23, pp. 376 378, 1889. [5] L. Devroye, Non-unform random varate generaton, Sprnger-Verlag, New York, Exercse 2, pp. 666, 1986. [6] A. Njenhus and H. S. Wlf, Combnatoral algorthms for computers and calculators, Second Edton, Academc Press, New York, Exercse 46, pp. 293, 1978. Copyrght 2009 ScRes