| |

VerySource

 Forgot password?
 Register
Search
View: 941|Reply: 5

An algorithm about 6 degrees

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-2 17:20:01
| Show all posts |Read mode
The table structure is as follows, just two fields

 user char (10) // user
 friend char (10) // friend (s) of this user


Data demo:
   user friend
--------------------
   alex tom
   alex john
   alex diana
   john simeno
   john cooker
   john coco
   wendy kk47
   wendy stephen

Take two users from the user column randomly, how to determine whether they can be contacted from friends, friends of friends, or friends of friends of friends?
   For example, alex can know coco through his friend john, but alex has no way to contact stephen. When the amount of data is relatively large, what algorithm should be used?

   This should be considered a connectivity problem for the graph?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-3 17:33:01
| Show all posts
Well, it's connectivity!
1. If you want to know if any two people can reach O (1) time, I only think of O (n ^ 3) algorithm. Passing closures can be, Freud can also, just a little Change it!
2. Of course, if you don't need to judge in O (1) time, you can divide the graph into several connected sets. This can consider the spanning tree method (the minimum spanning tree is not necessary), and dfs can also, depending on the situation The complexity of this creation is low
If the graph does not change much, it is mainly for query, or use 1, otherwise use 2

I can only think of these, expecting more efficient algorithms
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Invalid IP Address

Post time: 2020-1-18 11:09:01
| Show all posts
And check the set, the search time is almost O (1)
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-12 07:30:01
| Show all posts
I messed up ...


If only for querying O (1)
You can first build a matrix

Figure:
  A B C D E F
A
B
C
D
E
F

Then calculate their relationship
Then
Just check the matrix for the next query
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-8-19 00:00:01
| Show all posts
Just like it
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Invalid IP Address

Post time: 2020-8-19 02:15:02
| Show all posts
ivor1982positive solution
And check set.
Find time is O(1)

The input is O(n). Haha.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list