![]() |
Feedback On Algorithm With C++
This is my first time trying to use the features of C++ to write an algorithm. I program in C. But I came across a question that required hashing, and thought it would be smartest to use C++'s associative containers.
The problem itself is pretty simple. You just need to compute the distance of every node from one node in particular. I chose to implement a BFS. So I gave myself a crash course on hash maps, vectors, and queues. This is the question in case you're curious: http://www.programming-challenges.co...06&format=html Now, could any one give me some constructive feedback on my code? I want succinct, simple, and stable code that can get all of the following jobs done quickly. So are these aspects of the code optimal?
Mine took 2 seconds to solve all of the input files. Which is fairly decent, but I'd like to know if I'm using some slow bits of code, or if there are ways to shorten what I have. Any feedback pertaining to my use of C++'s features in general would also be appreciated. I'm a C programmer, so I'm not aiming to "learn" C++, but I'd be more than happy to learn whether or not I'm using the features of C++ effectively and correctly. Thanks. Note: To test the program, it's easiest to redirect the input/output to a file by uncommented the two lines at the top of main, and using the input file at the bottom. :
/* Programming Challenge #110206 - Erdos Numbers Example Input: :
6Example Output: :
Scenario 1 |
Re: Feedback On Algorithm With C++
A few things to consider:
The vector<string> in the tree map may end up with the same name in it several times (e.g. if two authors publish together more than once). It might be better to use a set<string> instead, although the overhead of the set might end up costing more than the data duplication. Its not actually a problem, but I would normally declare the temporary variables local to the loop (such as names, in the paper reading loop). Again, this may end up begin less efficient as the variable has to be recreated each time, rather than just being cleared. It does prevent you accidentally using it later on though. Looking up entries in a map can be a little costly, so it might be worth caching the result of a lookup. e.g. since you know tree and bfs.front() don't change in this loop: :
s = tree[bfs.front()].size();:
vector<string> &coAuthors = tree[bfs.front()]; |
Re: Feedback On Algorithm With C++
I appreciate the reply.
I don't think switching from a map of vectors to a map of maps would help. Data repetition would be less harmful, wouldn't it? The map needs to check for collisions during every single insertion. But data repetition only results in an extra lookup for every repeated vertex, into the map called "depths" inside the for loop (since same verticies are not visited twice in a BFS). |
Re: Feedback On Algorithm With C++
It probably won't help, given the size of the input data. If you were talking millions of names, I think it would be faster though.
Note that vectors have to maintain their data in a contiguous block, so every time a vector has to grow (i.e gets too big for its capacity), it has to reallocate a larger block and then copy the whole vector over. This can be very costly, when you are talking about large vectors. I sometimes use the deque class instead, which doesn't keep everything in one block, so adding to the end (or the start) of the deque is fast. Again, it won't impact this problem, but it can cause problems with large data sets. |
| All times are GMT -5. The time now is 4:17 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC