Skip to main content
gamedev.net
🔒 Locked

Linked Lists Problem

Started by runtime Feb 28, 2003 at 9:51 AM 2 replies 200+ views
Original Post
runtime
runtime
How can i determine the longest series of items present in 2 lists? for example: L1: a>b>a>c>d>e>c L2: a>a>b>d>b>a>c the longest series: b>a>c Length: 3 Help will be much apreciated!
"Don''t think you are, know you are!''
NiteLordz
NiteLordz
Not sure, but isn''t the longest series a>c>d>e if not, don''t mind me, it''s been a long night and time for bed.



Code makes the man
Code makes the man
guitarplayer
guitarplayer
That sounds like graduation homework. hehe

You should make an method that check if two list are equal.
Then make an sub list with the serie.
Then traverse by one(you should keep a variable to know what position in the list you are), copy a sub list with the same lenght of the serie and compare. increment the counter if its equal.
Now let''s analyze the complexity in the worst case of of the algorithm in an unordered list:

there is the lenght of the serie -> LS
there is the lenght of the list -> LL

you make (LL-LS) list comparasion, with is based on LS comparasion.
The creation and copy of list involves (LS+LS) comparasions (serie and sublist).
Being the worst case, presence of 1 serie at the end with (LL-1) lenght.
F=O((2LL-1)*(2LL-2)*(LL-1))
complexity O(n^3).

Now its easy..,(NOT)! You make an grafh with all the possibilities of series in L1. Traverser..er.. travel all over the grafh doing the check above, and keep track of the occurence of series in an balanced tree. Then you do a search on the tree, and it will tell you the result and graph with indicates the serie.

Actually there are easier ways, but this is the funniest and will make you learn a lot about data structures and search algorithms.
----------------------------I would rather burn dollars than USA flags... but they are too expensive!
civguy
civguy
Extract all subsequences from the first list, put them in a hash map, compare them with subsequences in the other list and you''ll get O(n^2) performance (at the expense of some memory usage). But someone''s bound to come up with better implementation..

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.