dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Looking for a better Search Algorithm

  1. #1
    Join Date
    Apr 2004
    Posts
    123

    Looking for a better Search Algorithm

    I'm looking for an algoritham which helps me in retrieving data in a faster way..

    The following is my XML

    Code:
    <IntrestedBookSet>
    <IntrestedBook AuthorName="XXX" Book="YYY" ID="1"/>
    <IntrestedBook AuthorName="XXX" Book="AAA" ID="2"/>
    <IntrestedBook AuthorName="XXX" Book="BBB" ID=" "/>
    <IntrestedBook AuthorName="CCC" Book="YYY" ID="5" Link="tttt"/>
    <IntrestedBook AuthorName="CCC" Book="YZY" ID="7"/>
    <IntrestedBook AuthorName="XXX" Book="XDY" ID="10"/>
    <IntrestedBook AuthorName="CCC" Book="BBB" ID="3" Link="ssss"/>
    </IntrestedBookSet>
    I need to convert the above XML into a list of objects ,in which each object should basically be having the below structure..

    Code:
    public class IntrestedBook
    {
    
    public string authorName;
    
    public list books;
    
    public int Id;
    
    public string link;
    
    }
    The objects of my IntrestedBook class will be then held within a list,and could be around 50,000 objects(as the XML is very huge)..I need a better algoritham which can give me back the object from the list if authorname or id is specified.


    I know i can iterate through the list saying foreach...but then i have to iterate through each object until the object is found..Is there a better way of doing it???


    Thanks,
    Mmx

  2. #2
    Join Date
    Apr 2008
    Posts
    5

    Re: Looking for a better Search Algorithm

    "System.Collections.Generics.Dictionary" will help you search for InterestingBooks by ID, but you will not be able to use it to search by Author because "Dictionary" requires that the "Key" is unique.

    Really, you should save these 50000 objects in a database. Then you can index the fields ID and Author so that you can search for them quickly, and they will be saved so that your program can close without losing the data.

  3. #3
    Join Date
    Apr 2004
    Posts
    123

    Re: Looking for a better Search Algorithm

    I'm already pulling out the records from the DB..

    Imagine a real world scenario like capital market historical data..

    I'm pulling out the entire data in one shot,and giving offline support in GUI..rather than pinging the application server every now and then...I know dictionary was an option...if i need to pull the object based on only one key...but in my case..i have to go for some other custom implementation..

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    Re: Looking for a better Search Algorithm

    A dictionary is probably the fastest way of finding a specific item based on a 'key'. If you want to search be either Author *or* Id, just create two dictionaries. One which is keyed by author, one which is keyed by Id.

    For author, you could have the value as a List<Book> so you can store multiple objects under the one key.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  5. #5
    Join Date
    Apr 2004
    Posts
    123

    Re: Looking for a better Search Algorithm

    That is exactly my problem...

    If i'm maintaining two different dictionaries with 50K objects in each that too parsed and populated at the app startup...my application memory consumption goes for a toss(increases > 200 MB).

  6. #6
    Join Date
    May 2007
    Posts
    1,546

    Re: Looking for a better Search Algorithm

    A 50,000 object dictionary takes roughly 800kB of memory. I'm assuming that a dictionary is represented by what is essentially sorted list of keys and a corresponding list of values and then doubling the memory that would take. So, two dictionaries takes less than 2MB of memory. So the overhead of having your objects stored in two dictionaries is quite small as compared to the size of those objects.

    If you want to use less memory, don't load 50,000 objects into memory. That's the only solution. You can't load 50,000 objects worth of XML into memory, parse it (in memory), convert it into 50,000 separate objects (in memory) and still expect memory usage to be low.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)