GithubHelp home page GithubHelp logo

poor-mans-floyd's Introduction

Poor Man's Floyd

Floyd's algorithm implemented for an array as well as a directed graph where every node has a maximum of one successor (or simple: a linked list that might have a cycle).

Seen in this video: https://www.youtube.com/watch?v=pKO9UjSeLew

While I was browsing GitHub, I found this repository. It uses floyd's cycle detection to find hash collisions. It might be interesting as further reading and practical application of this algorithm.

Benchmarking Result

Memory Usage

$ pipenv install
$ pipenv shell

$ python3 -m memory_profiler profile.py

Results on my machine (Windows 10, running python3 in legacy WSL, i5-3570, 16GB RAM)

Filename: profile.py

Line #    Mem usage    Increment   Line Contents
================================================
     9    400.0 MiB    400.0 MiB   @profile
    10                             def main():
    11    401.2 MiB      1.2 MiB       find_cycle_sort(data)
    12    402.6 MiB      1.4 MiB       find_cycle_set(data)
    13    402.6 MiB      0.0 MiB       find_cycle_floyd(data)

Wallclock Time

$ python3 main_array.py

Results on my machine (same setup as above):

find_cycle_sort(data):  95.93714189999992s
find_cycle_set(data):   0.2260710000000472s
find_cycle_floyd(data): 0.7033440999998675s

Conclusion

Although find_cycle_set and find_cycle_floyd have the same time complexity on paper, it depends on the input. floyd genereally seemed a bit slower than the naive set implementation. On large inputs however, floyd used a lot less memory compared to the other two methods. Floyd uses only two pointers whereas the set implementation uses a set (of size up to n) and the sort implementation an array (of size exactly n).

Take this with a grain of salt as the profiling was done without deep knowledge on how to measure these things properly. Also, it was done inside Legacy WSL which leads to pretty poor performance in general.

poor-mans-floyd's People

Contributors

nikeee avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.