Algorithm: Find if a there's a path from a vertex to every other vertices in a directed graph? - graph

Given a directed graph. I want an algorithm that finds, if there is a path between a vertex v to all other vertices in the graph? In time complexity (|V| + |E|).
I'm not sure how to proceed with this problem. A little help will be greatly appreciated.
Thanks.

Recursive version for check connected vertex Pseudocode:
Object Vertex {position, list_of_connected_vertex, already_checked}
Object Graph {list_of_all_vertex}
function checkConnected(initial_vertex):
if initial_vertex is already_checked:
return
else:
initial_vertex.already_checked = true
for vertex in initial_vertex.list_of_connected_vertex:
checkConnected(vertex)
if all_are_checked(Graph.list_of_all_vertex) then Vertex is connected.
(Clear the flags already_checked for other vertex and graph check)
Is a natural way of think about the problem, not the best performance way for it probably.

If the problem is just finding out if a path between a single pair of vertices exists, depth-first search can be used. If the existence of a path for a specific fixed graph, but various combinations of vertices is to be repeated, a connected component analysis might be more suitable.

Related

Gremlin query to find vertices which are connected in both directions

Gremlin: Count connections ignoring edges with a parallel edge in the opposing direction In this question, I like to know if there is a way to find vertices which are connected in both ways from a given vertex. We know dedup() is there to avoid duplicate. But is there any way to find the vertices which have parallel edges?
Searching vertices with parallel edges is a special case of cycle detection.
You can find the recipe for the cycle detection here.
A simplified version of the code to match your case:
g.V().as('a').
out().simplePath().where(out().as('a')).
path().dedup().by(unfold().
order().by(id).
dedup().fold())
example: https://gremlify.com/8c

How to find all the paths in a undirected graph touching all the nodes of a given set

I have an undirected graph and i'd like to find all the possible paths in it connecting all the nodes of a given set.
Is it an NP problem? Is there an algorithm to doing it, or a good way to accomplish it? I don't care about the order by which each paths touches the nodes in the set, i just need it to go through each of them.
It's called Hamiltonian Path Problem and it is NP-complete.
For detailed info: https://en.wikipedia.org/wiki/Hamiltonian_path_problem

Take certain edges in Dijkstra (or DFS)

i have a question about Dijkstra and/or DFS.
Let's assume I have a graph, with several nodes and edges. Now i want to find a Path from Node A to Node B. On this way i have to take certain Edges, for example the Edge (C,D).
Edit:
Sorry if it was a little bit unclear. My question is : I want a path from A to B. Is there a Path so that all edges {a,b}, {b,c}... and so on are taken? I'm interested if that's possible with dfs. And if thats also possible with Dijkstra under the same constraint if i want the shortest path from A to B and some edges {a,b}, {b,c} in the graph are needed to be taken. Also the graph is directed.
Help would be really appreciated!
Create a list of the all the edges that you want to make sure must be navigated.
Perform a DFS starting from the source.
Everytime you pass though an edge that's in the list, mark is as "traversed".
When you reach the destination, check if all the edges in the list are marked as traversed.
a.) If yes, goal state reached.
b.) Else, backtrack and unmark each edge in the list while passing through them.

Graph - Dijkstra's algorithm - Tile-based map - Is there "blocking" term in graph?

Let's suppose there is tile-based map. Each tile(vertex) has edges to 8 neighbour tiles. On one of these tiles, there is a wall(which is completly blocking).
From mathematical point of view, does it mean that vertex(on which there is a wall):
Does not exists?
There are no edges to this vertex?
Vertex is just blocking - is there this term in graph teory?
Theoretically this is normally modeled as (1) the node not existing. In an implementation of Dijkstra's algorithm, however, usually there is a function that maps a node to its sequence of neighbors that the algorithm uses to traverse the graph. That function would simply not return any "blocking" neighbors, which could be interpreted as either (1) the node not existing or (2) no edges being incident to the "blocking" node. How you choose to interpret it doesn't really matter—the implementation ends up the same.

How can I find all 'long' simple acyclic paths in a graph?

Let's say we have a fully connected directed graph G. The vertices are [a,b,c]. There are edges in both directions between each vertex.
Given a starting vertex a, I would like to traverse the graph in all directions and save the path only when I hit a vertex which is already in the path.
So, the function full_paths(a,G) should return:
- [{a,b}, {b,c}, {c,d}]
- [{a,b}, {b,d}, {d,c}]
- [{a,c}, {c,b}, {b,d}]
- [{a,c}, {c,d}, {d,b}]
- [{a,d}, {d,c}, {c,b}]
- [{a,d}, {d,b}, {b,c}]
I do not need 'incomplete' results like [{a,b}] or [{a,b}, {b,c}], because it is contained in the first result already.
Is there any other way to do it except of generating a powerset of G and filtering out results of certain size?
How can I calculate this?
Edit: As Ethan pointed out, this could be solved with depth-first search method, but unfortunately I do not understand how to modify it, making it store a path before it backtracks (I use Ruby Gratr to implement my algorithm)
Have you looked into depth first search or some variation? A depth first search traverses as far as possible and then backtracks. You can record the path each time you need to backtrack.
If you know your graph G is fully connected there is N! paths of length N when N is number of vertices in graph G. You can easily compute it in this way. You have N possibilities of choice starting point, then for each starting point you can choose N-1 vertices as second vertex on a path and so on when you can chose only last not visited vertex on each path. So you have N*(N-1)*...*2*1 = N! possible paths. When you can't chose starting point i.e. it is given it is same as finding paths in graph G' with N-1 vertices. All possible paths are permutation of set of all vertices i.e. in your case all vertices except starting point. When you have permutation you can generate path by:
perm_to_path([A|[B|_]=T]) -> [{A,B}|perm_to_path(T)];
perm_to_path(_) -> [].
simplest way how to generate permutations is
permutations([]) -> [];
permutations(L) ->
[[H|T] || H <- L, T <- permutations(L--[H])].
So in your case:
paths(A, GV) -> [perm_to_path([A|P]) || P <- permutations(GV--[A])].
where GV is list of vertices of graph G.
If you would like more efficient version it would need little bit more trickery.

Resources