What is the preferred method for checking whether an edge exists in a graph in the Graphs.jl package?
Say we have a GenericGraph G and we want to check if an edge a->b is in the Graph. I would like to have something similar to has_edge(G, a, b)
but that does not appear to exist.
I am currently using in(a, in_neighbors(b, G))
to check, but that may be quite inefficient.
We can always find if an undirected is connected or not by finding all reachable vertices from any vertex. If count of reachable vertices is equal to number of vertices in graph, then the graph is connected else not. We can find all reachable vertices either using BFS or DFS.
In an adjacency list each vertex u∈V is associated with a list of adjacent vertices. Given a graph G=(V,E), in order to check if the edge (u,v)∈E you need to check whether v∈adjacent[u]. A node can have at most O(|V|) neighbors, from here the complexity follows.
1- perform a depth first search starting from u, determine if v is found and a back edge exist along the path to v. 2- perform a depth first search from v, if u is found and back edge exist to u, then there's a cycle that includes both u and v.
For a given graph, in order to check for an edge we need to check for vertices adjacent to given vertex. A vertex can have at most O(|V|) neighbours and in worst can we would have to check for every adjacent vertex. Therefore, time complexity is O(|V|) .
As far as I understand it, that is the normal way to check for an edge in a graph. It's not really inefficient either.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With