Suggestions of the easiest algorithms for some Graph operations

后端 未结 3 1454
粉色の甜心
粉色の甜心 2021-02-04 16:29

The deadline for this project is closing in very quickly and I don\'t have much time to deal with what it\'s left. So, instead of looking for the best (and probably more complic

3条回答
  •  悲哀的现实
    2021-02-04 17:22

    This assumes O(E log V) is an acceptable running time, if you're doing something online, this might not be, and it would require some higher powered machinery.

    • List all users in the graph network given a distance X

    Djikstra's algorithm is good for this, for one time use. You can save the result for future use, with a linear scan through all the vertices (or better yet, sort and binary search).

    • List all users in the graph network given a distance X and the type of relation

    Might be nearly the same as above -- just use some function where the weight would be infinity if it is not of the correct relation.

    • Calculate the shortest path between 2 users on the graph network given a type of relation

    Same as above, essentially, just determine early if you match the two users. (Alternatively, you can "meet in the middle", and terminate early if you find someone on both shortest path spanning tree)

    • Calculate the maximum distance between 2 users on the graph network

    Longest path is an NP-complete problem.

    • Calculate the most distant connected users on the graph network

    This is the diameter of the graph, which you can read about on Math World.

    As for the adjacency list vs adjacency matrix question, it depends on how densely populated your graph is. Also, if you want to cache results, then the matrix might be the way to go.

提交回复
热议问题