Tarjan's Lowest Common Ancestor Algorithm

Total Page:16

File Type:pdf, Size:1020Kb

Tarjan's Lowest Common Ancestor Algorithm Tarjan's Lowest Common Ancestor Algorithm Maria Mahbub Algorithms - COSC 581 04/20/2021 Test Questions 1. What is the difference between static and off-line LCA finding problems? 2. Which data structure is used in Tarjan’s lowest common ancestor algorithm? 3. What is the overall time complexity of Tarjan's lowest common ancestor algorithm? 2 About me ● Phd Student ○ Computer Science Major -- EECS, UTK ○ Joined the Data Science Program @UTK in Fall 2018. ○ Migrated to Computer Science in Fall 2020 ● Research interest: Natural Language Processing, more specifically vulnerability assessment of NLP models ● Research Collaborator -- Oak Ridge National Laboratory (NSSD) ○ Currently working on the REACHVET project, focusing on suicide prevention ● Advisors: ○ Dr. Gregory Peterson (primary advisor at UTK) ○ Dr. Edmon Begoli (co-advisor at ORNL) 3 Hometown & Interests ● Home country: Bangladesh ● Home town: Dhaka, the capital of BD 8,354 ● BS in Mathematics & MS in Appl. Mathematics miles ● Love to travel places with friends and family ● LOVE Biryani (my comfort food!) My undergrad institution: University of Dhaka (the oldest, largest and one of the most prestigious universities in our country) 4 Some Pictures! 5 Outline ● Overview ● Motivation ● History ● Some Developments since Tarjan’s Algorithm ● Algorithm ● Implementation ● Comparison with Another Frequently Used Algorithm: RMQ-LCA ● Applications ● References ● Discussion 6 Overview ● Lowest Common Ancestor (LCA) ○ Consider two nodes x and y in a tree ○ LCA(x,y) is the lowest node in the tree that has both x and y as descendants. ○ A node can be a descendant of itself. ○ LCA of z and y would be z, since y has a direct connection from z x z y 7 Overview ● Problem definition: If we have a rooted tree, how fast can we find answers for Lowest Common Ancestor (LCA) queries for any pair of nodes? ● The LCA problem was first formulated by Aho, Hopcroft, and Ullman in 1973 ● Applications: ○ In object-oriented programming to find superclass in an inheritance hierarchy ○ In compiler design to facilitate some common basic computation for two basic blocks through their ancestors 8 Motivation ● Aho, Hopcroft, and Ullman introduced 3 versions of the LCA problem: ● Online LCA: Find LCA for node pairs as the queries are made ● Static LCA: Require the answers on line, but all tree merging instructions precede the information requests ● Off-line LCA: First get all queries and then find LCA for node pairs ○ Time complexity: O(nlog*n) in the 1973 version ○ In 1976, Aho, Hopcroft, and Ullman used the set union algorithm. They used an intermediate problem, called the off-line min problem. ○ Time complexity becomes O(nα(n)) ● The motivation behind Tarjan’s LCA algorithm was to implement a cleaner approach using set union algorithm directly 9 History ● Developed in 1979 by Robert Endre Tarjan ● Published in “Applications of path compression on balanced trees” paper ● Uses disjoint-set/union-find data structure ● Time complexity: ○ Barely slower than linear Robert Endre Tarjan ○ For a rooted tree with N nodes and Q queries, total runtime is O(Nα (N)+Q). ○ α is the Inverse Ackermann function 10 Chronological Developments 1983 1984 1988 ● off-line ● off-line ● off-line ● for special case of disjoint set ● uses the observation that on ● uses EREW PRAM union problem, time complete binary trees the LCA ● q queries take O(logn) time to complexity is O(n) can be solved in O(1) time by process in (n+q)/logn ● slight, but theoretically direct calculation. processors significant improvement ● O(n) to preprocess the tree ● Read conflicts are allowed ● the theory was too ● also not easily implementable complicated to implement effectively Gabow and Tarjan Gabow and Tarjan Schieber and Vishkin 11 Chronological Developments 1989 2000 1998 ● static ● static ● off-line ● based on the observation by ● simplification of the previous ● pointer-machine Gabow, Bently and Tarjan that approach implementation computing minimum over any ● implemented without the ● uses pointer-based radix sort interval can be reduced to PRAM ● time complexity: O(n+q) answering an LCA query in a ● sequential approach Cartesian data structure and ● uses RMQ the RMQ problem can be ● O(n) for pre-processing and solved serially in linear time O(1) for query ● proposed an algorithm using CRCW PRAM that takes O(ɑ Bender and Farach-Colton Buchsbaum, Kaplan, Rogers, and (n)) to preprocess and answer Westbrook LCA queries ● Complexities arises due to PRAM Berkman, Breslauer, Galil, Schieber, and Vishkin 12 Algorithm Basics ● Disjoint-set/Union-find Data Structure ○ Keeps track of a set of elements partitioned into several disjoint subsets ○ Supports two basic operations: e a ■ Find: Determines which subset a particular element is in f b c ■ Union: Merges two subsets into a single subset ○ Represented by rooted trees: Node: Member, Tree: Set d ○ A member points only to its parent. Root contains the representative and is its own parent. ● Time complexity O(mα(n)), for a sequence of m union, or find operations on a disjoint-set forest with n nodes, where α(n) is the extremely slow-growing inverse Ackermann function. 13 Algorithm Basics ● Disjoint-set Union implementation w/ Path Compression & Union Rank: ○ MAKE-SET: creates a tree with just one node. Time Complexity: O(1) ○ FIND-SET: follows parent pointers until the root of the tree is found. ■ path compression: points all the nodes on the search path directly to the root ○ UNION: causes the root of one tree to point to the root of the other. ■ union by rank: attaches the shorter tree to the root of the taller tree. f a a union (e, g) find(e) = find(g) g b c b c e f a f d e d g 14 Algorithm LCA(u) 1. MAKE-SET(u) 2. FIND-SET(u).ancestor = u 3. for each child v of u in T 4. LCA(v) 5. UNION(u,v) 6. FIND-SET(u).ancestor = u 7. u.color = BLACK 8. for each node v such that {u,v} ∈ P 9. if v.color = BLACK 10. print “The least common ancestor of” 11. u and v is FIND-SET(v).ancestor 15 Implementation ● Find Answers to the Queries: LCA (2,5), LCA (6,7), LCA (5,6) in this tree 1 2 3 4 5 6 7 16 Implementation LCA walk from 1 towards its 1 1 left-child 2 LCA walk from 2 towards its 2 3 left-child 4 2 3 4 5 6 7 4 5 6 7 ➢ ➢ create disjoint set for node 1 1 create disjoint set for node 2 2 4 ➢ ancestor[1] = 1 ➢ ancestor[2] = 2 17 Implementation 1 1 LCA walk Return back from 2 from 4 to 2 2 3 2 towards its 3 and color 4 right-child 5 BLACK 4 5 6 7 4 5 6 7 ➢ return disjoint set for node 4 2 1 5 ➢ UNION (2,4) ➢ ancestor[4] = 2 4 18 Implementation Return back 1 from 2 to 1 1 and color 2 BLACK Return back 2 from 5 to 2 3 2 3 and color 5 BLACK 4 5 6 7 4 5 6 7 ➢ return disjoint set for node 5 ➢ LCA (2,5) = FIND-SET(5).ancestor 2 1 ➢ UNION (2,5) = ancestor [FIND(5)] = ancestor[2] ➢ ancestor[5] = 2 = 2 4 5 19 Implementation LCA walk from 1 1 towards its 1 right-child 3 LCA walk 2 3 2 from 3 3 towards its left-child 6 4 5 6 7 4 5 6 7 ➢ return disjoint set for node 2 2 3 6 ➢ UNION (1,2) ➢ ancestor[2] = 1 4 5 1 20 Implementation 1 1 LCA walk from 3 towards its right-child 7 2 Return back 3 2 3 from 6 to 3 and color 6 BLACK 4 5 6 7 4 5 6 7 ➢ return disjoint set for node 6 2 3 7 ➢ UNION (3,6) ➢ ancestor[6] = 3 4 5 1 6 21 Implementation Return back 1 1 from 3 to 1 and color 3 BLACK Return back 2 3 from 7 to 3 2 3 and color 7 BLACK 4 5 6 7 4 5 6 7 ➢ return disjoint set for node 7 ➢ LCA (6,7) = FIND-SET(7).ancestor 2 3 ➢ UNION (3,7) = ancestor [FIND(7)] ➢ ancestor[7] = 3 = ancestor[3] 4 5 1 6 7 = 3 22 Implementation color 1 2 1 BLACK 4 5 1 2 3 3 4 5 6 7 6 7 ➢ UNION (1,3) ❏ LCA (2,5) = 2 ➢ LCA (5,6) = FIND-SET(6).ancestor ❏ LCA (6,7) = 3 = ancestor [FIND(6)] ❏ LCA (5,6) = 1 = ancestor [3] = 1 23 Tarjan’s LCA vs. RMQ-LCA ● Find Answers to the Queries: LCA (2,5), LCA (6,7), LCA (5,6) in T 1 2 3 4 5 6 7 24 RMQ-LCA Algorithm ● Uses Range Minimum Query & Euler tour to find LCA on static tree ● For this Range Minimum Query Algorithm, in LCA(u,v), u must be smaller than v. ● Range Minimum Query: Used to find the position of an element with the minimum value between two specified indices in an array ● Euler Tour: way of traversing tree starting from root and then reaching back to root after visiting all vertices without lifting pencil. ● Time Complexity: 1 ○ Preprocessing: O(n) ○ RMQ w/ segment tree data structure: O(logn) 2 3 4 5 6 7 25 RMQ-LCA Algorithm ● Perform a Euler tour on the tree, and fill three arrays: ○ Euler Tour Array - tracks nodes visited in order during Euler tour ○ Level Array - tracks each node’s respective level during Euler tour ○ First Occurrence Array - tracks index of the first occurrence of nodes in Euler tour ● Using the first occurrence array, get the indices corresponding to the two given nodes which will be the corners of the range in the level array that is fed to the RMQ algorithm for the minimum value.
Recommended publications
  • Lowest Common Ancestors in Trees and Directed Acyclic Graphs1
    Lowest Common Ancestors in Trees and Directed Acyclic Graphs1 Michael A. Bender2 3 Martín Farach-Colton4 Giridhar Pemmasani2 Steven Skiena2 5 Pavel Sumazin6 Version: We study the problem of finding lowest common ancestors (LCA) in trees and directed acyclic graphs (DAGs). Specifically, we extend the LCA problem to DAGs and study the LCA variants that arise in this general setting. We begin with a clear exposition of Berkman and Vishkin’s simple optimal algorithm for LCA in trees. The ideas presented are not novel theoretical contributions, but they lay the foundation for our work on LCA problems in DAGs. We present an algorithm that finds all-pairs-representative : LCA in DAGs in O~(n2 688 ) operations, provide a transitive-closure lower bound for the all-pairs-representative-LCA problem, and develop an LCA-existence algorithm that preprocesses the DAG in transitive-closure time. We also present a suboptimal but practical O(n3) algorithm for all-pairs-representative LCA in DAGs that uses ideas from the optimal algorithms in trees and DAGs. Our results reveal a close relationship between the LCA, all-pairs-shortest-path, and transitive-closure problems. We conclude the paper with a short experimental study of LCA algorithms in trees and DAGs. Our experiments and source code demonstrate the elegance of the preprocessing-query algorithms for LCA in trees. We show that for most trees the suboptimal Θ(n log n)-preprocessing Θ(1)-query algorithm should be preferred, and demonstrate that our proposed O(n3) algorithm for all- pairs-representative LCA in DAGs performs well in both low and high density DAGs.
    [Show full text]
  • Minimum Cut in $ O (M\Log^ 2 N) $ Time
    Minimum Cut in O(m log2 n) Time Pawe lGawrychowski1, Shay Mozes2, and Oren Weimann3 1 University of Wroc law, Poland, [email protected] 2 The Interdisciplinary Center Herzliya, Israel, [email protected] 3 University of Haifa, Israel, [email protected] Abstract We give a randomized algorithm that finds a minimum cut in an undirected weighted m-edge n-vertex graph G with high probability in O(m log2 n) time. This is the first improvement to Karger's celebrated O(m log3 n) time algorithm from 1996. Our main technical contribution is a deterministic O(m log n) time algorithm that, given a spanning tree T of G, finds a minimum cut of G that 2-respects (cuts two edges of) T . arXiv:1911.01145v5 [cs.DS] 3 Aug 2020 1 Introduction The minimum cut problem is one of the most fundamental and well-studied optimization problems in theoretical computer science. Given an undirected edge-weighted graph G = (V; E), the problem asks to find a subset of vertices S such that the total weight of all edges between S and V n S is minimized. The vast literature on the minimum cut problem can be classified into three main approaches: The maximum-flow approach. The minimum cut problem was originally solved by computing the maximum st-flow [3] for all pairs of vertices s and t. In 1961, Gomory and Hu [10] showed that only O(n) maximum st-flow computations are required, and in 1994 Hao and Orlin [11] showed that in fact a single maximum st-flow computation suffices.
    [Show full text]
  • Compressed Range Minimum Queries⋆
    Compressed Range Minimum Queries? Seungbum Jo1, Shay Mozes2, and Oren Weimann3 1 University of Haifa [email protected] 2 Interdisciplinary Center Herzliya [email protected] 3 University of Haifa [email protected] Abstract. Given a string S of n integers in [0; σ), a range minimum query RMQ(i; j) asks for the index of the smallest integer in S[i : : : j]. It is well known that the problem can be solved with a succinct data structure of size 2n + o(n) and constant query-time. In this paper we show how to preprocess S into a compressed representation that allows fast range minimum queries. This allows for sublinear size data struc- tures with logarithmic query time. The most natural approach is to use string compression and construct a data structure for answering range minimum queries directly on the compressed string. We investigate this approach using grammar compression. We then consider an alternative approach. Even if S is not compressible, its Cartesian tree necessar- ily is. Therefore, instead of compressing S using string compression, we compress the Cartesian tree of S using tree compression. We show that this approach can be exponentially better than the former, and is never worse by more than an O(σ) factor (i.e. for constant alphabets it is never asymptotically worse). 1 Introduction Given a string S of n integers in [0; σ), a range minimum query RMQ(i; j) returns the index of the smallest integer in S[i : : : j]. A range minimum data structure consists of a preprocessing algorithm and a query algorithm.
    [Show full text]
  • Common Ancestor Algorithms in Dags
    TUM INSTITUTF URINFORMATIK¨ A Path Cover Technique for LCAs in Dags Andrzej Lingas Miroslaw Kowaluk Johannes Nowak ABCDE FGHIJ KLMNO TUM-I0809 April 08 TECHNISCHEUNIVERSIT ATM¨ UNCHEN¨ TUM-INFO-04-I0809-0/1.-FI Alle Rechte vorbehalten Nachdruck auch auszugsweise verboten c 2008 Druck: Institut f¨urInformatik der Technischen Universit¨atM¨unchen A Path Cover Technique for LCAs in Dags Mirosław Kowaluk ∗ Andrzej Lingas † Johannes Nowak ‡ Abstract We develop a path cover technique to solve lowest common ancestor (LCA for short) problems in a directed acyclic graph (dag). Our method yields improved upper bounds for two recently studied problem variants, com- puting one (representative) LCA for all pairs of vertices and computing all LCAs for all pairs of vertices. The bounds are expressed in terms of the number n of vertices and the so called width w(G) of the input dag G. For the first problem we achieve Oe(n2w(G)) time which improves the upper bound of [?] for dags with w(G) = O(n0.376−δ) for a constant δ > 0. For the second problem our Oe(n2w(G)2) upper time bound subsumes the O(n3.334) bound established in [?] for w(G) = O(n0.667−δ). As a second major result we show how to combine the path cover technique with LCA solu- tions for dags with small depth [?]. Our algorithm attains the best known upper time bound for this problem of O(n2.575). However, most notably, the algorithm performs better on a vast amount of input dags, i.e., dags that do not have an almost linear-sized subdag of extremely regular structure.
    [Show full text]
  • Race Detection in Two Dimensions
    Race Detection in Two Dimensions Dimitar Dimitrov Martin Vechev Vivek Sarkar Department of Computer Department of Computer Department of Computer Science, ETH Zürich Science, ETH Zürich Science, Rice University Universitätstrasse 6 Universitätstrasse 6 6100 Main St., 8092 Zürich, Switzerland 8092 Zürich, Switzerland Houston, TX, USA [email protected] [email protected] [email protected] ABSTRACT Race detection challenges. Dynamic data race detection is a program analysis technique Automatic data race detection techniques are extremely for detecting errors provoked by undesired interleavings of valuable in detecting potentially harmful sources of concur- concurrent threads. A primary challenge when designing rent interference, and therefore in ensuring that a parallel efficient race detection algorithms is to achieve manageable program behaves as expected. Designing precise race detec- space requirements. tion algorithms (i.e., not reporting false positives) which scale State of the art algorithms for unstructured parallelism to realistic parallel programs is a very challenging problem. require Θ(n) space per monitored memory location, where n In particular, it is important that a race detection algorithm is the total number of tasks. This is a serious drawback when continues to perform well as the number of concurrently analyzing programs with many tasks. In contrast, algorithms executing threads increases. for programs with a series-parallel (SP) structure require only Unfortunately, state of the art race detection techniques [13] Θ(1) space. Unfortunately, it is currently poorly understood that handle arbitrary parallelism suffer from scalability issues: if there are classes of parallelism beyond SP that can also their memory usage is Θ(n) per monitored memory location, benefit from and be analyzed with Θ(1) space complexity.
    [Show full text]
  • Arxiv:2001.07765V1 [Cs.DS] 21 Jan 2020 749022 and Has Been Supported by Pareto-Optimal Parameterized Algorithms, ERC Starting Grant 715744
    Faster and Enhanced Inclusion-Minimal Cograph Completion ? ?? Christophe Crespelle1, Daniel Lokshtanov1, Thi Ha Duong Phan2, and Eric Thierry3 1 University of Bergen, Department of Informatics, N-5020 Bergen, NORWAY [email protected], [email protected] 2 Institute of Mathematics, Vietnam Academy of Science and Technology, 18 Hoang Quoc Viet, Hanoi, Vietnam [email protected] 3 Univ Lyon, ENS de Lyon, UCB Lyon 1, CNRS, Inria, LIP UMR 5668, 15 parvis Ren´eDescartes, F-69342, Lyon, FRANCE [email protected] Abstract. We design two incremental algorithms for computing an inclusion-minimal completion of an arbitrary graph into a cograph. The first one is able to do so while providing an additional property which is crucial in practice to obtain inclusion-minimal completions using as few edges as possible : it is able to compute a minimum-cardinality com- pletion of the neighbourhood of the new vertex introduced at each in- cremental step. It runs in O(n + m0) time, where m0 is the number of edges in the completed graph. This matches the complexity of the al- gorithm in [41] and positively answers one of their open questions. Our second algorithm improves the complexity of inclusion-minimal comple- tion to O(n + m log2 n) when the additional property above is not re- quired. Moreover, we prove that many very sparse graphs, having only O(n) edges, require Ω(n2) edges in any of their cograph completions. For these graphs, which include many of those encountered in applications, the improvement we obtain on the complexity scales as O(n= log2 n).
    [Show full text]
  • A Scalable Approach to Computing Representative Lowest Common Ancestor in Directed Acyclic Graphs
    View metadata, citation and similar papers at core.ac.uk brought to you by CORE provided by University of Hertfordshire Research Archive Accepted Manuscript A scalable approach to computing representative Lowest Common Ancestor in Directed Acyclic Graphs Santanu Kumar Dash, Sven-Bodo Scholz, Stephan Herhut, Bruce Christianson PII: S0304-3975(13)00722-6 DOI: 10.1016/j.tcs.2013.09.030 Reference: TCS 9477 To appear in: Theoretical Computer Science Received date: 27 June 2012 Revised date: 14 August 2013 Accepted date: 23 September 2013 Please cite this article in press as: S.K. Dash et al., A scalable approach to computing representative Lowest Common Ancestor in Directed Acyclic Graphs, Theoretical Computer Science (2013), http://dx.doi.org/10.1016/j.tcs.2013.09.030 This is a PDF file of an unedited manuscript that has been accepted for publication. As a service to our customers we are providing this early version of the manuscript. The manuscript will undergo copyediting, typesetting, and review of the resulting proof before it is published in its final form. Please note that during the production process errors may be discovered which could affect the content, and all legal disclaimers that apply to the journal pertain. A scalable approach to computing representative Lowest Common Ancestor in Directed Acyclic Graphs Santanu Kumar Dash 1a, Sven-Bodo Scholz 2a, Stephan Herhut 3b,Bruce Christianson 4a aSchool of Computer Science, University of Hertfordshire, Hatfield, UK bProgramming Systems Labs, Intel Labs, Santa Clara, CA., USA Abstract LCA computation for vertex pairs in trees can be achieved in constant time after linear-time preprocessing.
    [Show full text]
  • Limits of Structures and the Example of Tree-Semilattices Pierre Charbit, Lucas Hosseini, Patrice Ossona De Mendez
    Limits of Structures and the Example of Tree-Semilattices Pierre Charbit, Lucas Hosseini, Patrice Ossona de Mendez To cite this version: Pierre Charbit, Lucas Hosseini, Patrice Ossona de Mendez. Limits of Structures and the Example of Tree-Semilattices. 2015. hal-01150659v2 HAL Id: hal-01150659 https://hal.archives-ouvertes.fr/hal-01150659v2 Preprint submitted on 17 Sep 2015 HAL is a multi-disciplinary open access L’archive ouverte pluridisciplinaire HAL, est archive for the deposit and dissemination of sci- destinée au dépôt et à la diffusion de documents entific research documents, whether they are pub- scientifiques de niveau recherche, publiés ou non, lished or not. The documents may come from émanant des établissements d’enseignement et de teaching and research institutions in France or recherche français ou étrangers, des laboratoires abroad, or from public or private research centers. publics ou privés. LIMITS OF STRUCTURES AND THE EXAMPLE OF TREE-SEMILATTICES PIERRE CHARBIT, LUCAS HOSSEINI, AND PATRICE OSSONA DE MENDEZ Abstract. The notion of left convergent sequences of graphs intro- duced by Lov´aszet al. (in relation with homomorphism densities for fixed patterns and Szemer´edi'sregularity lemma) got increasingly stud- ied over the past 10 years. Recently, Neˇsetˇriland Ossona de Mendez in- troduced a general framework for convergence of sequences of structures. In particular, the authors introduced the notion of QF -convergence, which is a natural generalization of left-convergence. In this paper, we initiate study of QF -convergence for structures with functional symbols by focusing on the particular case of tree semi-lattices. We fully char- acterize the limit objects and give an application to the study of left convergence of m-partite cographs, a generalization of cographs.
    [Show full text]
  • Succinct Representations of Weighted Trees Supporting Path Queries ✩ ∗ Manish Patil, Rahul Shah, Sharma V
    View metadata, citation and similar papers at core.ac.uk brought to you by CORE provided by Elsevier - Publisher Connector Journal of Discrete Algorithms 17 (2012) 103–108 Contents lists available at SciVerse ScienceDirect Journal of Discrete Algorithms www.elsevier.com/locate/jda Succinct representations of weighted trees supporting path queries ✩ ∗ Manish Patil, Rahul Shah, Sharma V. Thankachan Department of Computer Science, Louisiana State University, 291 Coates Hall, Baton Rouge, LA 70803, USA article info abstract Article history: We consider the problem of succinctly representing a given vertex-weighted tree of n Received 28 May 2012 vertices, whose vertices are labeled by integer weights from {1, 2,...,σ } and supporting Received in revised form 3 August 2012 the following path queries efficiently: Accepted 16 August 2012 Available online 28 August 2012 • Path median query: Given two vertices i, j, return the median weight on the path from i Keywords: to j. Succinct data structures • Path selection query: Given two vertices i, j and a positive integer k, return the kth smallest weight on the path from i to j. • Path counting/reporting query: Given two vertices i, j and a range [a, b], count/report the vertices on the path from i to j whose weights are in this range. The previous best data structure supporting these queries takes O (n logn) bits space and can perform path median/selection/counting in O (log σ ) time and path reporting in O (log σ + occ log σ ) time, where occ represents the number of outputs [M. He, J.I. Munro, G.
    [Show full text]
  • Scribe Notes
    6.851: Advanced Data Structures Spring 2012 Lecture 15 | April 12, 2012 Prof. Erik Demaine Scribes: Jelle van den Hooff (2012), Yuri Lin (2012) Anand Oza (2012), Andrew Winslow (2010) 1 Overview In this lecture, we look at various data structures to solve problems about static trees: given a static tree, we perform some preprocessing to construct our data structure, then use the data structure to answer queries about the tree. The three problems we look at in this lecture are range minimum queries (RMQ), lowest common ancestors (LCA), and level ancestor (LA); we will support all these queries in constant time per operation, using linear space. 1.1 Range Minimum Query (RMQ) In the range minimum query problem, we are given an array A of n numbers (to preprocess). In a query, the goal is to find the minimum element in a range spanned by A[i] and A[j]: RMQ(i; j) = (arg)minfA[i];A[i + 1];:::;A[j]g = k; where i ≤ k ≤ j and A[k] is minimized We care not only about the value of the minimum element, but also about the index k of the minimum element between A[i] and A[j]; given the index, it is easy to look up the actual value of the minimum element, so it is a more general problem to find the index of the minimum element between A[i] and A[j]. The range minimum query problem is closely related to the lowest common ancestor problem. 1.2 Lowest Common Ancestor (LCA) In the lowest common ancestor problem (sometimes less accurately referred to as \least" common ancestor), we want to preprocess a rooted tree T with n nodes.
    [Show full text]
  • The Algorithm of Heavy Path Decomposition Based on Segment Tree
    2020 5th International Workshop on Materials Engineering and Computer Sciences (IWMECS 2020) The Algorithm of Heavy Path Decomposition Based on Segment Tree Jinpeng Xu1, Weixing Zhang2 1School of Software of Zheng Zhou University, Zheng Zhou 450000, Henan Province, China; 2School of Software of Zheng Zhou University, Zheng Zhou 450000, Henan Province, China; Keywords: Heavy Path Decomposition; Segment Tree; data structure; algorithm Abstract: The time complexity or space complexity of the problem between any two nodes on the classic tree is high, and it is not suitable for the situation where modification and query coexist. The solution in this paper is to discuss a new algorithm- Heavy Path Decomposition, which has good time complexity and wide applicability. 1. Introduction In the field of computer algorithms, we often encounter the problem of Tree structure. Among them, some types of problems are required to take two points on the tree and perform a series of operations on the path between the two points, others are required to calculate some answers by only one point. For example, given a tree containing N nodes connected and acyclic, each node contains a value, the following operations need to be supported: the first requirement is to add a number K to the value of all the nodes in the shortest path from the X node to the Y node; The second is to find the sum of the values of all the nodes in the shortest path from X node to the Y node on the tree; The third one is to add a number K to all the node values in the subtree with the X node as the root node; The last requirement is to find the sum of all the node values in the subtree with the X node as the root node.
    [Show full text]
  • Faster and Enhanced Inclusion-Minimal Cograph Completion
    Faster and Enhanced Inclusion-Minimal Cograph Completion Christophe Crespelle1, Daniel Lokshtanov2, Thi Ha Duong Phan3, and Eric Thierry1 1 Univ Lyon, UCB Lyon 1, ENS de Lyon, CNRS, Inria, LIP UMR 5668, 15 parvis Ren´eDescartes, F-69342, Lyon, FRANCE [email protected], [email protected] 2 University of Bergen, Department of Informatics, N-5020 Bergen, NORWAY [email protected] 3 Institute of Mathematics, Vietnam Academy of Science and Technology, 18 Hoang Quoc Viet, Hanoi, Vietnam [email protected] Abstract. We design two incremental algorithms for computing an inclusion-minimal completion of an arbitrary graph into a cograph. The first one is able to do so while providing an additional property which is crucial in practice to obtain inclusion-minimal completions using as few edges as possible : it is able to compute a minimum-cardinality com- pletion of the neighbourhood of the new vertex introduced at each in- cremental step. It runs in O(n + m0) time, where m0 is the number of edges in the completed graph. This matches the complexity of the al- gorithm in [24] and positively answers one of their open questions. Our second algorithm improves the complexity of inclusion-minimal comple- tion to O(n + m log2 n) when the additional property above is not re- quired. Moreover, we prove that many very sparse graphs, having only O(n) edges, require Ω(n2) edges in any of their cograph completions. For these graphs, which include many of those encountered in applications, the improvement we obtain on the complexity scales as O(n= log2 n).
    [Show full text]