• About Me
  • Blog
  • Home

Eric Hokanson

~ E's little space in cyberspace

Eric Hokanson

Tag Archives: Algorithms

The Value of Algorithmic Problem Solving: A Demonstration

20 Friday Mar 2015

Posted by Eric Hokanson in Algorithms, Computer Science

≈ Leave a comment

Tags

Algorithms, Computer science, magic squares, problem solving

Algorithmic problem solving is a very useful skill to develop.  Once such source you can learn and practice these skills is [1].  Allow me to illustrate with an example with puzzle #29, p. 39 from [1]:


 Magic Squares

A magic square of order 3 is a 3×3 table filled with nine distinct integers from 1 – 9 so that the sum of the numbers in each row, column, and corner-to-corner diagonals is the same.  Find all the magic squares of order 3.


 

image1According to the tutorial on p. 4 of [1], solving this problem is not difficult if we can prove that the common sum must be 15 and that 5 must be placed at the center square.  We should start with these two hints and see where that knowledge takes us.

1.  What should be the common sum?  We have nine distinct integers: 1, 2, 3, …, 9, of which, each sum is a combination of three of those distinct integers.  The sum of the digits 1 to 9, i.e. 1 + 2 + 3 + … + 9 = 45.  Since we are only using three of the digits to form our sum for each row, column, and the diagonals, we divide 45 by 3: 45/3 = 15.  Hence the common sum of any of the three squares — rows, columns, and diagonals — must be 15 because the total sum of all nine digits in the magic square will be 45.

2. Which integer should occupy the center square to get an optimal sum of 15 using the remaining integers?

image2 Start with 9: if 9 occupied the center square, then an 8 will have to occupy any one of the eight remaining boxes.  The sum of 8 + 9 = 17 and 17 > 15 so that won’t work.  The same problem occurs if 8 occupies the center square because a 9 must occupy one of the remaining boxes adjacent to the 8.

Let x, y, and z be any one of the distinct integers in the set of consecutive integers 1 through 9.  We need x + y + z = 15.  Observe that x+y = 15 – z, x+z = 15 – y, and y+z = 15 – x.  Since addition is commutative, x+y = y+x, x+z = z+x, and y+z = z+y.  Hence, x+y < 15, x+z < 15, and y+z < 15.  Since 9 cannot occupy the center, that means 9 will occupy any of the remaining squares, which means x+9 < 15* for all integers in the set of consecutive integers from 1 to 8.  The first such integer to satisfy this condition is 5, i.e. 5+9 = 14 < 15.  Hence, 5 must occupy the center square.

Given our new constraint, we know that 1+5+9 = 15:

image3

image4What about the diagonals?  Say we had the arrangement on the left.  Take the rows containing the 1: x+y+1 = 15.  The only remaining digits to satisfy the equation have to be 6 and 8, i.e. 6+8+1 = 15.  Note that the 6 or the 8 would wind up in the same row as the 9.  In either case, 8+9 = 17 > 15 and 6+9 = 15 leaving the remaining square unfillable.  Given the symmetric nature of squares, we can reason that none of the diagonal 1,5,9 combinations will work.  Given our further constraint, this reduces the possible partially filled squares to one of the four above, which I have labeled (a), (b), (c), and (d).

In the case of (a): for the row containing the 1, our only options are the digits 6 and 8 to make the 15.  These can be placed in either combination:

image5

The remaining digits can then be uniquely positioned to create our magic square.

For case (b), given the symmetric properties of squares, we can obtain our solutions by swapping the top and bottom rows from our solutions to case (a):

image6

Case (c) can be constructed in a similar fashion to case (a):

image7

And again using the symmetry of squres, we can take the case (c) solutions by swapping the first and third columns to obtain a solution for case (d):

image8

for a total of 8 magic squares of order 3!

Q.E.D


What I learned by solving the magic square puzzle:

To exhaustively search all possible 3×3 tables by placing a 1 in one of the possible nine locations, a 2 in any of the possible eight locations remaining, a 3 in any of the possible 7 locations left, and so on, means the total possible combinations would be:

9! = 9x8x7x…x1 = 362,880

ways to arrange the 9 numbers in a 3×3 table.  That is an undaunting task to generate all 362,880 tables checking if the result of our arrangements yields a magic square.  Considering that we just learned there are only 8 such squares of order 3, that is a lot of wasted work.

In order to improve our exhaustive search, we tried constraining our problem to help reduce the search space.  One such discovery was the invariant of the common sum of 15.  This invariant was helpful in allowing us to smartly construct magic squares that fit the constraint.

We further reduced our search space by discovering another invariant: that 5 must always occupy the center square.  Given these two invariant properties, we were able to quickly and smartly reason all the possible magic squares of order 3 without having to waste our time constructing and examining the 362,872 squares that did not fit the constraints.

Bottom line:  Invariants are incredibly useful in problem solving and programming.  Finding an invariant can reduce intractable exhaustive search problems and ensure that our solutions are correct.  The recognition of invariants is an important problem solving skill.  Possibly the most important.


* Note that 9 could be x, y, or z but the results from any of the inequality relationships will be the same.  Therefore, we will show the x+9 < 15 relationship but our “proof” will hold with any of the three inequality relationships.

References

[1] Algorithmic Puzzles, Anany Levitin, Maria Levitin, Oxford University Press, Inc., New York, NY, 2011

Just Three Steps to Success

09 Sunday Mar 2014

Posted by Eric Hokanson in Algorithms, Research, Success

≈ Leave a comment

Tags

Algorithms, hard problem, How to run a successful project, Keys to success, Research, Success, successful project management

I am a computer scientist and I love designing algorithms to solve problems; a series of steps or instructions that one executes until there is a solution.  Wouldn’t it be great to write an algorithm for success — what ever success means for you?  Given a set of inputs, and a set of instructions, that if acted upon correctly and faithfully,  you achieve a successful solution to you problem(s) or in meeting your goal(s).

The above thought actually stems from my concerns and fears of taking on a Principal Investigator role to a research project — a very hard problem with no guarantee of a solution.  Pretty scary venture, right?  I want the project to be successful.  While formulating a team and a plan of attack, I began seeking models of successful project management in the literature.  I thought back on my own successful accomplishments — obtaining a job as a radio announcer, earning a computer science degree, obtaining the rank of Eagle Scout, starting a new career, accepting a job, and moving from one end of the continent to the other, … what did I do consistently that ensured success?

Upon reflection, I discovered that there are three key steps to any successful endeavor.  I could be wrong, but this is based on my own experience.  Therefore, I reserve the right to be wrong.

  1. The first step is a leap of faith.  Accomplishing my goals required some faith that I would succeed even though I could fail.  Despite any fear, I took small steps and built my confidence attempting to get from “here” to “there” — and I wasn’t guaranteed there would even be a there, there.
  2. The second step is personal doubt.  This is the phase of your journey where you feel you have hit a brick wall, you hit rock bottom, and you feel that there is no possible way to go any further.  You are frustrated.  Stymied.  Addled.  This is the point where you may start to feel like giving up — and most people do at this point.  However, I argue that this period of perplexity is a good thing.  It means you are tackling a very hard problem and hard problems yield great rewards.  This is not the time to quit but the time to begin.  You may need to begin by stepping back from the problem.  Take the weekend off.  Don’t think about it.  Do something else; go for a run, play golf, whatever you consider fun to do.  This gives your subconscious some time to work on the problem without your interference.  Ever have an idea suddenly spring out of nowhere?  It usually happens when you are in the shower, on the john, or at 3 A.M.  I believe this is the work of your subconscious; just make sure you have a notebook to write down what ever that flash of inspiration is.  After this period of incubation, you can start fresh.
  3. The third step is perseverance.  This is where you “gut it out”, keep at it to get past your time of doubt.  The key is to start somewhere.  Anywhere.  If you got a flash of inspiration during the incubation or break, start with that.  Otherwise formulate a hypothesis and try it.  Most likely it will be wrong.  But you will learn something.  Apply what you learned in a new hypothesis, try and fail again.  Like a smart missile, you constantly course-correct until you reach your target.

Well, there you have it.  A simple algorithm for success.  The algorithm may be simple; performing it will be hard but you can do it.  All you need to do is take that first step.

Subscribe

  • Entries (RSS)
  • Comments (RSS)

Archives

  • May 2016
  • May 2015
  • April 2015
  • March 2015
  • September 2014
  • August 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013

Categories

  • Alan Turing
  • Algorithms
  • Apollo 17
  • C Programming
  • Christmas
  • Computer Programming
  • Computer Science
  • Computer Security
  • Current Events
  • Cyber Security Research
  • Education
  • Freedom of choice
  • Freewill
  • Hacking
  • Holidaze
  • Learning
  • Malware RE
  • Math
  • NASA
  • Pen-testing
  • Philosophy
  • Pi Day
  • procrastination
  • Programming
  • Python
  • Quine programs
  • Quotes
  • Random Stuff
  • Research
  • Reverse Engineering
  • Shopping
  • Smithsonian National Air and Space Museum
  • Software Development
  • Star Wars
  • Success
  • Uncategorized

Meta

  • Register
  • Log in

Blog at WordPress.com.

  • Follow Following
    • Eric Hokanson
    • Join 44 other followers
    • Already have a WordPress.com account? Log in now.
    • Eric Hokanson
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar
 

Loading Comments...