Optimized algorithm to compare templates of two websites My task is to compare templates of two websites. I am ready with my algorithm. But it takes too much time to give a final answer. 
Here, "template" means the way any page presents its contents.

Example:

Any shopping website have page of any Shoes, that contains,

Images in the left.
Price and Size in the right.
Reviews in the bottom.


If two websites are of any specific product, then it returns "Both are from same templates". Example, this link and this link have the same template.

If one website shows any product and another website shows any category, then it shows "No match".
Example, this link and this link are from different template. 

I think that this algorithm requires some optimization, that's why I am posting this question in this forum.

My algorithm


Fetch, parse two input URLS and make their DOM trees.
Then if any page contains , UL and TABLE , then remove that tag. I done this because, may be two pages contains different number of items.
Then, I count number of tags in both URLS. say, initial_tag1, initial_tag2.
Then, I start removing tags that have same position on corresponding pages and same Id and their below subtree, if that tree has number of nodes less than 10.
Then, I start removing tags that have same position on coresponding pages and same Class name and their below subtree, if that tree has number of nodes less than 10..
Then, I start removing tags that have no Id ,and No Class name and their below subtree, if that tree has number of nodes less than 10.
Steps 4, 5, 6 have (N*N) complexity. Here, N, is number of tags. [In this way, in every step DOM tree going to shrink]
When it comes out from this recursion, then I check final_tag1 and final_tag2.
If final_tag1 and final_tag2 is less than initial_tag1*(0.2) and initial_tag2*(0.2) then I can say that Two URL matched, otherwise not.


I wrote my code in Java using Jsoup and Selenium. I asked before on Stack Overflow, but the answers did not help me.

I think a lot about this algorithm, and I found that removing node from DOM tree is pretty slow process. This may be the culprit for slowing this algorithm.

I discussed with some geeks, and 


  they said that use a score for every tag instead of removing them, and add them , and 
  at the end return (score I Got)/(accumulatedPoints) or something similar, and on the 
  basis of that you decide two websites are either similar or not.


But I didn't understand this. So can you explain this statement, or can you give any other algorithm that solves this problem efficiently?
Reading between the lines, here is what I propose. I assume that you are interested in the structure of websites, so the first step is to expose this structure.


Throw away all non-structure tags as well as attributes (maybe keep class) and text. 
 I imagine we would be left with &lt;div&gt;, &lt;table&gt;, &lt;ul&gt;/&lt;ol&gt;¹, HTML5 structure tags and little else, depending on coding style.
 For the sake of efficiency, use a SAX-style parser for this step.
Create a tree of the remaining tags, either directly or in a second run. 
 The structure of this tree should hold the information you want.
 You may want to cut everything that is nested deeper than some level;
 the overall structure of some website is likely to be clear from the first
 five to ten levels.
Compare the trees. What is useful here depends heavily on the kind of 
 similarity you want and on the degree of sophistication the used websites 
 display.

Equality of trees would be simplest measure but also coarse. There 
 has been work on tree similarity measures, see e.g. here. Some research may unearth a measure that suits your purpose.


Finetuning the steps will require experiments and will most likely depend on the class of websites you compare.




You say you remove lists and tables because the may contain different numbers of elements. While that is true, the action is overcompensating since the occurrence of a list or table can be significant. Instead, remove all contained tags but keep the main ones. But then again, maybe the templates differ in the amount of columns in some table?