Algorithms – Quick Algorithm to Find Matches Between Two Arrays

algorithmsarrayefficiency

I'm faced with the following problem:

I have an array which is produced by the parsing of a yaml file which contains patterns in the following structure.

"Programming books"
    Title:
          a list of titles
    Author:
"Art Books"
    Title:
          foo
          bar
    Author:
          Mrs. Foo
          Mr. Bar
    Year:

the program is supposed to be given random input and find if there is a book with that particular Title,Author and Year.

So far I'm using the following

 foreach book_type,values from config{
       tag_match = 0
       foreach tag from values{
            tag_no = values.length()
            foreach value from tag{
                 if value in input
                      tag_match++
            }
      }
      if tag_match == values.length()
           /* tag the book matched continue matching*/
 }

The program is not supposed to receive many matching lines bu it is supposed to receive a huge amount of data.
So far it has to do
book_type.length() * tag.length() * value.length() iterations for each line of input, is there a better way to do this?

Best Answer

Use a dictionary or collection, which has a Key-value pair, if the size of the data allows, if not a database - as stated above.

There are lots of different collections, dictionary, hash table etc implementations, and the right/fastest one to use will depend on the nature of its indented use, so you will have to look around to find the most suitable one. For example some will load faster, but take longer to read, some load slower, but have faster access times, and so on.

On other thing, might be, currently you are O(3n), by which I mean type X tag X value so if that's loops it might be 10 + 10 + 10 = 30 worst case. You could combine Type and Tag, and Value, which would be 10 worst case - just a thought.

Heres a link to an articular I found useful, but is .Net specific. IDictionary Options - Performance Test - SortedList vs. SortedDictionary vs. Dictionary vs. Hashtable

Related Topic