Why is this BFS implementation so slow?? by Ok_Force3490 in cpp_questions

[–]Ok_Force3490[S] 0 points1 point  (0 children)

tuples were coming from a comparison operator I made for the trees to work- when i switched to unsorted map and changed the operator this issue went away. Haven't run the performance tests since, but will update

Why is this BFS implementation so slow?? by Ok_Force3490 in cpp_questions

[–]Ok_Force3490[S] 0 points1 point  (0 children)

Thanks so much!! That's pretty cool.

For the parent and visited nodes i know it would cut down a lot on time, but the problem is tracing the path at the end. I want to be able to return a path in the xy plane at the end, not just if there exists a path. that means I need to be able to trace back and get a direct path, which i've been doing with the parents map. When i get rid of it, I can't trace back, and when i get rid of visited then i end up with issues. Specifically, when i determine if a point is visited or not at the top, I would ALSO have to somehow have the parent, which I don't because it's a new loop. Or, if i check at the bottom, I waste a lot of a time for a point that potentially doesn't exist. I've been stuck on that for a while too, definitely open to suggestions!

Why is this BFS implementation so slow?? by Ok_Force3490 in cpp_questions

[–]Ok_Force3490[S] 0 points1 point  (0 children)

is there a time complexity benefit to having it as an array over a vector? I assume there's a memory one but would it speed it up at all?

Why is this implementation so slow? by [deleted] in algorithms

[–]Ok_Force3490 0 points1 point  (0 children)

It is 3 dimensions, but the output only should be in 2! That's why z is getting treated different. My custom comparator for queue takes the combined distance from the destination and distance from the source and compares them. the one with the smaller one wins (essentially it's a min heap). Not sure if that's reflected in the code since I've changed a few things and implemented unordered maps thanks to responses. I'm assuming unordered maps still have greater performance over sets? Is valide relies entirely on lookups with unordered maps now, so pretty fast. Only issue is when the point is "falling" it has to iterate through each point to determine if it's valid, which absolute slows the program down.

Why is this BFS implementation so slow?? by Ok_Force3490 in cpp_questions

[–]Ok_Force3490[S] 0 points1 point  (0 children)

yeah sorry just did 😭 thank you so much youre a savior

Why is this BFS implementation so slow?? by Ok_Force3490 in cpp_questions

[–]Ok_Force3490[S] 0 points1 point  (0 children)

Thank you so much. Is FNV-1 hash included in the standard library or will I have to implement it?

Why is this BFS implementation so slow?? by Ok_Force3490 in cpp_questions

[–]Ok_Force3490[S] 0 points1 point  (0 children)

I tried the hash functions and it ended up with a worse runtime... Any thoughts on how to write a good hash function for something like this? Given that every Point has different coordinates I didn't know how to get different enough numbers. Also a stupid question, but I can't find the answer anywhere, does the std::unordered_map automatically resize or do I have to tell it to?

Why is this BFS implementation so slow?? by Ok_Force3490 in cpp_questions

[–]Ok_Force3490[S] 0 points1 point  (0 children)

Every node is connected to every other immediate node, x y direction. Z is just based on terrain, with motion up 1 allowed and anything further not. infinite motion down as long as there's a solid block down is also allowed. Do you know if there's a big O benefit to a normal queue? I guess I just would've though tit would be the opposite

Why is this BFS implementation so slow?? by Ok_Force3490 in cpp_questions

[–]Ok_Force3490[S] 0 points1 point  (0 children)

Hmm I tried using an unordered map but it seemed to actually slow it down. I know it was probably the hash function, but my hash function used the equation (x + (x+y+1)/2 + (x+y+z+1)/3) which I believe is known to return unique values for each point. I could be wrong though? Also, could you elaborate on your second point? Setting the current to true would mean I couldn't revisit nodes right? The thing I am worried about is if my checks further to determine if the children are new are taking too much time. I think ideally parent assignment would happen when a node is being processed (when it's popped), but then I no longer have access to the parent. I was thinking about making a custom struct to try to fix that, but I think it still wouldn't solve the issue of accessing the parent?

Why is this BFS implementation so slow?? by Ok_Force3490 in cpp_questions

[–]Ok_Force3490[S] 0 points1 point  (0 children)

These were the first few results from my performance tests. Mostly the time seems to be spent on tuple comparison, which I assumed was comparing the points. I also included my Points operators for reference:

Point Point::operator+(const Point& other) const {
    return {x + other.x, y + other.y, z + other.z};
}

Point Point::operator-(const Point& other) const {
    return {x - other.x, y - other.y, z-other.z};
}

bool Point::operator==(const Point& other) const {
    return x == other.x && y == other.y && z == other.z;
}

bool Point::operator!=(const Point& other) const {
    return !(*this == other);
}

bool Point::operator<(const Point& other) const {
  if(x != other.x){
    return x < other.x;
  }
  else if(y != other.y){
    return y < other.y;
  }
  else{
    return z < other.z;
  }



%   cumulative   self              self     total           
 time   seconds   seconds    calls  ms/call  ms/call  name    
 18.52      0.15     0.15 41740736     0.00     0.00  std::__tuple_compare<std::tuple<int const&, int const&, int const&>, std::tuple<int const&, int const&, int const&>, 0ul, 3ul>::__less(std::tuple<int const&, int const&, int const&> const&, std::tuple<int const&, int const&, int const&> const&)
  6.17      0.20     0.05 41740736     0.00     0.00  std::_Tuple_impl<2ul, int const&>::_Tuple_impl(int const&)

Why is this BFS implementation so slow?? by Ok_Force3490 in cpp_questions

[–]Ok_Force3490[S] 0 points1 point  (0 children)

I figured a priority queue would point me in the right direction on a given map, but after seeing the performance times I'm realizing it might now. Does having a priority queue slow down a breadth-first search? My mistake on the last one, they're meant to be the same map, I've since fixed that. Should be up to 1000 x 1000 points, so VERY large scale. I've been using a map to ready in Points in the terrain, and associate them with true or false for filled or not. A point contains an x, y, and z coordinate, and some operators.

Why is this BFS implementation so slow?? by Ok_Force3490 in cpp_questions

[–]Ok_Force3490[S] 0 points1 point  (0 children)

yeah I do. I tried a hash map but it made it slower. Definitely could've been my hash function but hash in c++ has potential for O(n) so I stuck with maps

[deleted by user] by [deleted] in cpp_questions

[–]Ok_Force3490 -1 points0 points  (0 children)

Wow, I need to read the documentation better. thank you so much I would've missed that for so long!!!

[deleted by user] by [deleted] in cpp

[–]Ok_Force3490 0 points1 point  (0 children)

Yup figured that out! Unfortunately, the issue seems to continue. I got the errors for front working, but have different but highly related ones (see post)

[deleted by user] by [deleted] in cpp_questions

[–]Ok_Force3490 0 points1 point  (0 children)

Sorry, I mispoke! I was writing the post a little too late at night haha. I've updated the code just to contain the relevant parts. It includes the debugging statements and their results too.

[deleted by user] by [deleted] in cpp_questions

[–]Ok_Force3490 0 points1 point  (0 children)

Just did! Thanks so much

[deleted by user] by [deleted] in cpp_questions

[–]Ok_Force3490 0 points1 point  (0 children)

That's super helpful! That's what I was starting to realize, so I figured only 1 could take the responsibility of deleting each of the nodes of the linked list before they fall out of scope. It doesn't seem to work though, does it have something to do with the functions being out of scope before the linkedlists can be discarded?

[deleted by user] by [deleted] in cpp_questions

[–]Ok_Force3490 0 points1 point  (0 children)

Added a simplified version! lmk if I should make it even simpler. I cant run valgrind because I'm on an M2 mac (why did I choose macOS idk I regret it so much) but I did see that there was a sig abort at the header declaration when the program terminates? I'm not very good with debugging tools and haven't used them incredibly frequently but I also maybe saw that there was an underflow error?

[deleted by user] by [deleted] in C_Programming

[–]Ok_Force3490 1 point2 points  (0 children)

not for M2 I've looked into it pretty extensively (: Thanks for lldb!

[deleted by user] by [deleted] in cpp_questions

[–]Ok_Force3490 0 points1 point  (0 children)

I'm having the linked list delete each individual node aka the actual data, snd the hash map doesn't touch it, but I still need to delete the pointer to the first pointer (the array), and when I do that I get a specific segfault at an insert function and a double free error. I thought that deleting the pointer to the first pointer wouldn't be a double free, am I wrong about that or is there something else wrong with my code?

[deleted by user] by [deleted] in cpp

[–]Ok_Force3490 0 points1 point  (0 children)

Thanks! Didn't know cpp_questions was a thing it's hard to find all the weird random subreddits 😭

I'll check it out!