Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl
Imagine you have a tree - a series of leaves which join together as you head towards the root into bigger and bigger branches. The "Least Common Ancestor" is the point at which the branches from two leaves you have chosen come together. In computing, such a tree structure is very common - files and folders / directories, and heirarcies of classes, being two common examples.
Over the past couple of days, I've received several requests through
our enquiry page if it's possible to write a Least Common Ancestor program in Perl. It's rather odd to have received such a batch of questions, and I think that - somewhere in the world where our web site is used as a reference - this has been set as a class exercise. The easy answer is "Yes - you can write anything in Perl"; my longer answer the first time I was asked was "How much Perl do you know / can I help / do you even need to come and learn Perl on
one of our Perl courses?". But I got no reply - not even an acknowldegement (and, by the way, for readers from non-English cultures, it's regarded as impolite to ask a question, have someone spend a lot of time writing and answer, and not even acknoledge the help / say "Thank you").
But ... I admit ... I was very interested in how easy the question was ... so when I woke this morning I set myself the challenge of writing a Least Common Ancestor program in Perl. The source code is
[here]. Now - there are plenty of learned papers on efficient ways of doing this, with recursive code that looks very clever - but I came up with something much simpler.
1. Define all of the branch relationships in a hash
2. Generate two list of ancestor - one for the two start points you're interested in
3. Staring at the end of those lists, compare individual members until you find ones which are NOT common
... and the last common ancestor you found was also the LEAST common one.
The sample program worked with a single test case that I rushed through it; note that it needs further testing especially for special cases such as for leaves that have no common ancestor at all, and for leaves where one of the leaves actually given as an input is an ancestor of the other.
Should I have written the code? Should I pass on the URL of this item and the sample to my new enquirers and the original one? Well - I'm not going to start sending more emails to the first (rude?) chap / chapess. But this code is a useful example / algorithm I can make plenty of re-use of during demonstrations and courses. It illustrates topicalisation,
named blocks of code (subs)i,
hashes,
lists, "here" documents, passing parameters by address, scoping of variable using "my" and much more. So it will be useful to other people who need it in a far wider area. If people who are looking for help with homework take a copy and hand it in (they should add my copyright notice to it, but probably wouldn't), then they only have themselves to blame when they end up learning little about Perl, and lots about how to steal other people's code. But - come to think of it - it's pretty efficient to re-use code provided that you do it legally!
(written 2010-11-11)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q110 - Object Orientation and General technical topics - Programming Algorithms [202] Searching for numbers - (2005-02-04)
[227] Bellringing and Programming and Objects and Perl - (2005-02-25)
[642] How similar are two words - (2006-03-11)
[1157] Speed Networking - a great evening and how we arranged it - (2007-04-21)
[1187] Updating a page strictly every minute (PHP, Perl) - (2007-05-14)
[1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
[1840] Validating Credit Card Numbers - (2008-10-14)
[1949] Nuclear Physics comes to our web site - (2008-12-17)
[2189] Matching disparate referencing systems (MediaWiki, PHP, also Tcl) - (2009-05-19)
[2259] Grouping rows for a summary report - MySQL and PHP - (2009-06-27)
[2509] A life lesson from the accuracy of numbers in Excel and Lua - (2009-11-21)
[2586] And and Or illustrated by locks - (2010-01-17)
[2617] Comparing floating point numbers - a word of caution and a solution - (2010-02-01)
[2894] Sorting people by their names - (2010-07-29)
[2951] Lots of way of converting 3 letter month abbreviations to numbers - (2010-09-10)
[2993] Arrays v Lists - what is the difference, why use one or the other - (2010-10-10)
[3072] Finding elements common to many lists / arrays - (2010-11-26)
[3093] How many toilet rolls - hotel inventory and useage - (2010-12-18)
[3102] AND and OR operators - what is the difference between logical and bitwise varieties? - (2010-12-24)
[3451] Why would you want to use a Perl hash? - (2011-09-20)
[3620] Finding the total, average, minimum and maximum in a program - (2012-02-22)
[3662] Finding all the unique lines in a file, using Python or Perl - (2012-03-20)
[4325] Learning to program - what are algorithms and design patterns? - (2014-11-22)
[4401] Selecting RECENT and POPULAR news and trends for your web site users - (2015-01-19)
[4402] Finding sum, minimum, maximum and average in Python (and Ruby) - (2015-01-19)
[4410] A good example of recursion - a real use in Python - (2015-02-01)
[4652] Testing new algorithms in PHP - (2016-02-20)
[4656] Identifying the first and last records in a sequence - (2016-02-26)
[4707] Some gems from an introduction to Python - (2016-10-29)
P251 - Perl Review [2242] So what is this thing called Perl that I keep harping on about? - (2009-06-15)
[3007] Setting up a matrix of data (2D array) for processing in your program - (2010-10-21)
[3407] Perl - a quick reminder and revision. Test yourself! - (2011-08-26)
[3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
P211 - Perl - Hashes [240] Conventional restraints removed - (2005-03-09)
[386] What is a callback? - (2005-07-22)
[738] (Perl) Callbacks - what are they? - (2006-05-30)
[930] -> , >= and => in Perl - (2006-11-18)
[968] Perl - a list or a hash? - (2006-12-06)
[1334] Stable sorting - Tcl, Perl and others - (2007-09-06)
[1705] Environment variables in Perl / use Env - (2008-07-11)
[1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
[1856] A few of my favourite things - (2008-10-26)
[1917] Out of memory during array extend - Perl - (2008-12-02)
[2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
[2836] Perl - the duplicate key problem explained, and solutions offered - (2010-06-28)
[2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11)
[2920] Sorting - naturally, or into a different order - (2010-08-14)
[3106] Buckets - (2010-12-26)
[3400] $ is atomic and % and @ are molecular - Perl - (2011-08-20)
Some other Articles
Java Beans, tag libraries and JSPs - what and why.After Course Resources - do we publish sample answers. Example from Java Exceptions module.Changing a Servlet - more that just editing and compilingGathering information - logging - with log4j. First steps.Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in PerlJava - basic rules for arithmetic, variables and conversionDoing several things at the same time - Java threadsFresh Paint - Java ArraysSetting up individual variables, and arrays, in Java - some commented examplesLooking back and forward personally - 6 years each way