HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Algorithm to find all paths of length k

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
alllengthpathsalgorithmfind

Problem

Consider the following definition of 3-friends:

person 1 is 3-friends with person 2 if they are direct friends or person 1 is friends with a friend of person 2 or person 1 is friends with a friend of a friend of person 2.

In graph-theoretical terms, person 1 is a 3-friend with another person 2 if there is a path of length 1, 2 or 3 from vertex 1 to vertex 2.

Is there an efficient algorithm to find all the paths between say person 1 and all its 3-friends? (I.e. all the paths of length 3, all the paths of length 2, all the paths of length 1).

Or the only option is an exhaustive search?

Solution

You are asking for all paths of length 3 emanating from a given vertex. There are $O(n^3)$ of these, the worst case being a clique. If you want to actually output all of them, then exhaustive search (on the adjacency matrix) is the perfect algorithm, and it is efficient, since there are only $O(n^3)$ paths (or less).

Context

StackExchange Computer Science Q#42967, answer score: 5

Revisions (0)

No revisions yet.