I have recently discovered graphs and algorithms and am trying to solve a specific problem involving two different types of vertices: Users and Entities. Details are as follows:
- The graph is directed
- I am trying to find all paths from A to B
- A is always a User
- B can be a User or an Entity
- If B is a User, maximum depth for the search is 3 edges
- If B is an Entity, maximum depth is 2 edges
- I can not traverse any edges which are outbound from a User, unless the user is A
Although the graph has two types of vertices, it is not bipartite.
So far I have managed to create a graph object which holds a vertex-indexed array of adjacency lists. The adjacency lists are based on linked lists.
I think I require some kind of variation on an All Paths algorithm, but I’m not quite sure. In addition, not sure whether I should be looking at DFS or BFS.
I am working in PHP, which complicates matters, since most examples are in Java. What I’d really like is the pseudo code.
Any ideas? Thanks!
It sounds like you’re traversing some sort of LDAP implementation. If you need a generic algorithm, just use a DFS, since it’s easier to code. Doing this is overkill though unless the depth will change.
Most generic way