473,509 Members | 8,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Algorithm optimization for a map of strings

6 New Member
I have a map<string, time_t>; string represents a filesystem path, time_t is a timestamp associated with that path. My problem is to find the best version of a directory and its subdirectories, regarding its timestamp.

For example, if I have:
/home, 1000
/home/user1, 2000
the best version is (/home, 1000) AND (/home/user1, 2000) ( because /home includes /home/user1 and /home/user1 has a timestamp bigger than its parent directory).

If I have:
/home, 2000
/home/user1, 1000
the best version is (/home, 2000) ( because /home includes /home/user1 and /home/user1 has a timestamp smaller than its parent directory).


A larger example:
/home/ ---> 1000
/home/user1 ---> 2000
/home/user1/aaa --> 500
/home/user2 ---> 3000
/mnt --> 1000
/mnt/cdrom -->2000
/usr/ --->3000
/usr/bin -->2000

I have to find the best versions of every directory (that means the most recent time for a directory); in my example, this should be:

/home, 1000
/home/user1, 2000
/home/user2, 3000
(/home/user1/aaa has a timestamp of 500, less than /home/user1 timestamp, so it's no good)

/mnt, 1000
/mnt/cdrom, 2000

/usr, 3000
(usr/bin has a timestamp of 2000, less than /usr timestamp, so it;s no good).

I'm using an algorithm to do my job, but I'm not satisfied with it. Basically, I iterate through map: for every item, find if the next items are included in it and verify their timestamps, keep a stack of parents etc etc etc.

I know that exist a better solution, but I cannot find it :(. Maybe someone could help me, I'm not asking that one writes the code for me, but merely indicates me the algorithm I should use, or how this problem could be decomposed (something like this: "hey, stupid, just use Divide and conquer"; or "you moron, this is a classic example of XXX algorithm").
Thanks.
Sep 24 '07 #1
6 1795
weaknessforcats
9,208 Recognized Expert Moderator Expert
Have you considered a multimap<string, string> where the key is the parent folder and the value is the full path to the folder?

That way you could use the equal_range algorithm passing iterators for the begin and end of the multimap. You will get back a pair of iterators that bracket all occurrences of the key. If I understand correctly you can iterate this range and keep the highest time stamp.
Sep 24 '07 #2
baudolino
6 New Member
Thank you, I've used a multimap and backtracking, now the code looks easier to understand.

This is the initial map (keys)

/mnt/download/Backup/data/Test/faild/root/0/4
/mnt/download/Backup/data/Test/faild/root/0/4/h
/mnt/download/Backup/data/Test/faild/root/1
/mnt/download/Backup/data/Test/faild/root/1/6
/mnt/download/Backup/data/Test/faild/root/1/6/j
/mnt/download/Backup/data/Test/faild/root/1/6/l
/mnt/download/Backup/data/Test/faild/root/1/8
/mnt/download/Backup/data/Test/faild/root/1/9
/mnt/download/Backup/data/Test/faild/root/1/9/m
/mnt/download/Backup/data/Test/faild/root/2/a
/mnt/download/Backup/data/Test/faild/root/2/a/n
/mnt/download/Backup/data/Test/faild/root/2/c
/mnt/download/Backup/data/Test/faild/root/2/c/p
/mnt/download/Backup/data/Test/faild/root/3/f
/mnt/download/Backup/data/Test/faild/root/3/f/q
/mnt/download/Backup/data/Test/faild/root/3/f/v


This is the multimap (keys and values):

/mnt/download/Backup/data/Test/faild/root---->1
/mnt/download/Backup/data/Test/faild/root/0---->4
/mnt/download/Backup/data/Test/faild/root/0/4---->h
/mnt/download/Backup/data/Test/faild/root/1---->8
/mnt/download/Backup/data/Test/faild/root/1---->6
/mnt/download/Backup/data/Test/faild/root/1---->9
/mnt/download/Backup/data/Test/faild/root/1/6---->l
/mnt/download/Backup/data/Test/faild/root/1/6---->j
/mnt/download/Backup/data/Test/faild/root/1/9---->m
mnt/download/Backup/data/Test/faild/root/2---->c
/mnt/download/Backup/data/Test/faild/root/2---->a
/mnt/download/Backup/data/Test/faild/root/2/a---->n
/mnt/download/Backup/data/Test/faild/root/2/c---->p
/mnt/download/Backup/data/Test/faild/root/3---->f
/mnt/download/Backup/data/Test/faild/root/3/f---->q
/mnt/download/Backup/data/Test/faild/root/3/f---->v

My algorithm follows:

for every p in multimap
if p already visited
continue
endif

mark p as visited
equal_range(p);
//backtracking algorithm: for every item in range, make an equal_range(key + value) etc etc
endfor

I have a problem, though: this loop iterates through all multimap's elements, even through the ones that have been processed (hence the test "if p was already visited"). To get rid of this loop, I need to find out the unique keys of multimap (and apply equal_range only for them). How can I do this, let alone the possibility that I could remember those unique keys when multimap is created?

Thank you.
Sep 25 '07 #3
JosAH
11,448 Recognized Expert MVP
If you consider that file system a tree, is it true that you want to find the paths
from the root to a node down the tree as long as the child 'value' is larger than
the parent's value?

kind regards,

Jos
Sep 25 '07 #4
baudolino
6 New Member
you want to find the paths from the root to a node down the tree as long as the child 'value' is larger than
the parent's value
Not exactly. Even if child's value is smaller than root's value, maybe child's child value is larger than root's.

/home 100
/home/user 50
/home/user/file 200

In this case, solution is: (/home,100) and (/home/user/file,200).
/home/user is useless because has a smaller timestamp than its parent.
Sep 25 '07 #5
JosAH
11,448 Recognized Expert MVP
Not exactly. Even if child's value is smaller than root's value, maybe child's child value is larger than root's.

/home 100
/home/user 50
/home/user/file 200

In this case, solution is: (/home,100) and (/home/user/file,200).
/home/user is useless because has a smaller timestamp than its parent.
So if a child has a larger value than its parent it's going to be part of that 'best' list?
If so, a simple depth first recursive traversal can do the job.

kind regards,

Jos
Sep 25 '07 #6
baudolino
6 New Member
If so, a simple depth first recursive traversal can do the job.
Basically, that is what I've done, after using a multimap (only that are several trees, each one corresponding to a unique key): start at every root (every unique key) and explore as far as possible before backtracking.

But I didn't know how to do it when using my initial map. Thanks, weaknessforcats, for the multimap idea.
Sep 25 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
1280
by: Home Newbie | last post by:
I have some stupid questions.... - Why would program 1 run a lot faster than program 2? Can someone explain? If I change the sample_size to be a larger one, program 2 will my browser freeze!! -...
3
3310
by: PWalker | last post by:
Hi, I have written code that I would like to optimize. I need to push it to the limit interms of speed as the accuracy of results are proportional to runtime. First off, would anyone know any...
32
76373
by: Cmorriskuerten | last post by:
HI, is this is this solution to test if a number is a prime number or not: /* * Is n a prime number? * Return TRUE (1): n is a prime number * Return FALSE (0): n is a *not* a prime number...
28
3126
by: joshc | last post by:
If I have an array of data that I know to be sorted in increasing order, and the array is less than 50 elements, and I want to find the first element greater than a certain value, is a simple...
113
12186
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
6
25348
by: aarklon | last post by:
Hi folks, I found an algorithm for calculating the day of the week here:- http://www.faqs.org/faqs/calendars/faq/part1/index.html in the section titled 2.5 what day of the week was 2 august...
51
8566
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
206
8170
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
42
1739
by: Armin | last post by:
Hi, just a dumb question. Let a = Why is the value of a.append(7) equal None and not ?? --Armin
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7347
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7416
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5656
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4732
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3218
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1571
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
779
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.