Showing posts with label Conceptual blocks. Show all posts
Showing posts with label Conceptual blocks. Show all posts

Friday, January 30, 2009

Conceptual blocks and programming tasks. Part II.

Preface
In the previous post I offered the following problem to solve:
The input data is the graph representation, for example the instance of LinkedList class. The output is Boolean value which is true if the graph has the cycle and false otherwise. The constraints are no additional memory should be used.
Let me publish the solution and the comments.
   

The solution.
To show the solution, I would like to use C# language. I realized that LinkedList<T> class which I referred in the puzzle definition can't represent the graph that has cycles because the methods of this class don't allow to set the next and previous node to the node which already belongs the list. So, the following class is used to show the solution.

internal class GraphNode<T>
{
   private T _value;
   private GraphNode<T> _next;
   public GraphNode(T value)
   {
      _value = value;
   }
   public GraphNode<T> Next
   {
      get{ return _next;}
      set{ _next = value;}
   }
   public override string ToString()
   {
      return _value.ToString();
   }
}

And here is the solution:

static bool HasCycle<T>(List<GraphNode<T>> list)
{
   if (list == null)
      throw new ArgumentNullException("list");
   if (list.Count == 0)
      throw new ArgumentException("empty list", "list");
   GraphNode<T> current1 = list[0];
   GraphNode<T> current2 = list[0];
   do
   {
      if (current1.Next == null || current2.Next == null || current2.Next.Next == null)
      {
         return false;
      }
      current1 = current1.Next;
      current2 = current2.Next.Next;
      if (ReferenceEquals(current1, current2))
      {
         return true;
      }
   } while (current1 != null && current2 != null);
   return false;
}


The Analysis.
The idea behind the scene is very simple. The solution is using two pointers to the graph nodes, the first pointer goes through the graph with step 1(current1=current.Next) and the second pointer goes through the graph with step 2(current2=current.Next.Next). If the graph has the cycle, these two pointers will refer to the same node at some point which is obvious.However, I seen a lot of situations when people were not able to solve this puzzle quickly or didn't solve it at all.When I was asked to solve this puzzle several years ago I didn't solve it as well. And the reason of that is the conceptual blocks.
When I studied at the university, we learned the common algorithms and implemented them quite intensively. A lot of the algorithms dealt with the graphs - visiting the graph nodes, finding the shortest way between two graph nodes, etc. I used to use single pointer to the graph nodes for these algorithms. After more than 5 years this habit still persisted. This is some sort of the stereotype which is definitely the conceptual block. The second block is the same as I shown in the previous post - the tendency to delimit the problem area poorly. Sometimes people restrict themselves to using the single pointer. If one would know about the conceptual blocks and control the process of the problem solving, the "finding the cycle in the graph" become the pretty trivial task.

Monday, January 26, 2009

Conceptual blocks and programming tasks.

Preface
One of my favorite nonfictional books is “Conceptual Blockbusting” by James L. Adams. The book describes the conceptual blocks which are “mental walls that blocks the problem solver from correctly perceiving a problem or conceiving its solution”. There is the set of puzzles that intended to understand how the conceptual blocks can hamper the problem solving. I believe that reading this book can really help you to become a better thinker. Indeed it’s helpful if you aren’t a genius like Albert Einstein. Every time I read this book I try to figure out how the conceptual blocks can hamper the solving of the problems that I face doing my job every day. And recently I found the interesting issue…

Delimiting the problem area poorly.
One of the perceiving conceptual blocks is tendency to delimit the problem too closely. It is demonstrated using the following puzzle:
Draw no more than four straight lines without lifting the pencil from the paper which will cross through all nine dots:
Work on it a while. One possible solution is shown here
A lot of people don’t exceed the imaginary boundary even though it is not in the definition of the problem at all. The overly strict limits are a block in the mind of the solver.

Find cycle in the graph puzzle
Consider the following puzzle.
The input data is the graph representation, for example the instance of LinkedList class. The output is Boolean value which is true if the graph has the cycle and false otherwise. The constraints are no additional memory should be used.
Try to solve this puzzle. In the next post I will explain the issues that hamper this puzzle solving and why they appear.