Detect cycle in an undirected graph using BFS - graph

Getting wrong output,
In my created graph there is no cycle present still it returns true insted of false.
Main Function
BFS function
BFS Utility function

Related

Trying to reverse a MetaGraph in Julia

I am currently working on Julia with the MetaGraphs.jl package.
I have created the Graph with a dataset, the characteristic of the graph is that there's one start vertex and one end vertex. However the graph is created by creating all possible options so some edges which "take too much time" don't reach the final vertex. In order to have a decent graph for my next step/ an optimization problem.
I am cleaning the graph by simply going through all edges and those who don't reach the end vertex are deleted. But this method is costly in time and I know a wiser way to do it.
The method is simple:
1.reversing the graph (end vertex becomes start vertex)
2.Calculating the distance of all vertices from the start vertex
3.erase the vertices which don't have a defined distance with the start vertex
I have created an example of a small digraph that would be the same type as mine:
module EssaiModule
using LightGraphs, MetaGraphs
g = DiGraph(8)
mg = MetaDiGraph(g, 1.0)
add_vertex!(mg)
add_edge!(mg,1,2)
add_edge!(mg,1,3)
add_edge!(mg,1,4)
add_edge!(mg,2,4)
add_edge!(mg,2,5)
add_edge!(mg,3,5)
add_edge!(mg,5,6)
add_edge!(mg,4,6)
add_edge!(mg,3,7)
add_edge!(mg,4,8)
rg=reverse(mg)
end
the end vertex is number 6 so normally I would like to erase edges 3->7 and 4->8
But I can't even start my function because I simply cannot reverse this graph.
I get the error message "LoadError: type MetaDiGraph has no field badjlist"
I know it's because the graph is a metaGraph and not a lightGraph but shouldn't we be able to reverse a metagraph? it seems like a basic function that could be useful on many occasions while working with graphs.
Thanks for your help!

Directed Graph to DAG

Does anyone have an algorithm for this problem? :
"Suppose we have a directed graph with n vertices and we want to make it acyclic.
How many sets of edges can we pick so that reversing the edges inside any one of those sets will make the graph acyclic?"
I can find the loops inside a graph,but i'm wondering if reversing one of edges of a loop , will produce another loop.

Algorithm: Find if a there's a path from a vertex to every other vertices in a directed 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.

Finding all maximal transitive closured subgraphs in given graph

I'm trying to solve the following problem:
I have an directed graph G = (E,V) which has a low number of edges. Now I try to find all subgraphs in it which are transitive closured and maximal which means there should be no subgraph which ist part of another subgraph.
The first idea I had was to start at every Node doing a DFS and on every step look if all edges for the closure exists but the performance is horrible. So I wonder if there is any faster algorithm

Directed Graph BFS without all nodes reachable

I'm doing a breadth-first search on a digraph. I'm lost at nodes c and f, and I'm not sure if and how they should be in the BF-tree or if you only go as far as reachable from the source node and don't start at another node in order to get all the vertices.
Here's what I'm getting so far. As you can see, letters mark the nodes. Distance and predecessor are marked by d and pi:
This was helpful BFS traversal of directed graph from a given node but I'm not familiar with graphs enough to understand how that applies to this situation. From what I'm getting from that question, it seems like in this case I would not include c and f at all.
In fact, it seems like I have the maximal number of nodes included already, just because I started at i. I think that the d=4 at the g node (also at k but that doesn't even connect to any other nodes), this is the greatest distance and max-depth possible in BFS in this graph.

Resources