Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Give a graph:
Input:
0 -> 1
2 -> 1
3 -> 1
Representation:
0 -> 1 <- 2
^
|
3
This graph is not strongly connected because not every vertex u can reach vertex v and vice versa (path u to v and v to u)
The algorithm I am currently using for checking if the directed graph is strongly connected is applying DFS from each vertex O(n3), if I can find N-1 vertices from the N vertices, then the digraph is strongly connected.
Alternative algorithm is Tarjan's algorithgm.
But is this graph considered connected (not strongly) ?
If yes, what would be a good algorithm to apply.
Thank you
If you want to figure out your digraph is strongly connected there are several algorithm for that and in wiki you can find this three:
Kosaraju's algorithm
Tarjan's algorithm
Path-based strong component algorithm
If you want to check if your digraph is just connected or not, you can simply assume that it is a graph not a digraph then apply connected component algorithm with O(|V|+|E|). if it just have one connected component then your graph is connected.
The term "Connected (not strongly connected" is usually made out for undirected graphs. In your case the directed graph is not connected (strongly).
One of the algorithms that can evaluate if a digraph (directed graph) is strongly connected in O (V + E) time is named after Kosaraju who discovered it.
The wiki: http://en.wikipedia.org/wiki/Kosaraju%27s_algorithm is pretty instructive. I have implemented the algorithm and it is available here. Runs in O (v + E ). We are assuming that the graph is backed as an adjacency list.
Link for Kosaraju Implementation in Java
Try it out, hopefully you would find no bugs :-)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to solve the problem of node/vertices disjoint paths in a directed graph and came to know about the idea of splitting nodes into in and out nodes respectively. I got the idea and how it works and all the related theorem's like menger theorem but still, I'm not sure how to code it in an efficient manner.
Which data structure should I use so that I can split the vertices and still manage to balance the time complexity? Is there any algorithm existing which tells how to approach the code.
Please help or suggest some appropriate link which may help me out.
Thanks
It's quite simple actually. Let's say you have graph as an edge list of pairs u v meaning theres an edge from u to v
If nodes are not integers already use a dictionary/hash/map to reduce them to integers in range 1..n where n is number of nodes.
Now we "split" all nodes, for each node i it will become 2 nodes i and i+n. where i is considered in-node and i+n out-node.
Now graph edges are modified, for every edge u --> v we instead store edge u+n --> v
Also we add edges from each nodes in-node to out-node, i.e. from node i to i+n
We can assign infinity capacities to all edges and capacities of 1 to edges that connect in-node to out-node
Now Node-disjoint paths from some node s to t can be found using any max-flow algorithm (Ford-Fulkerson, Edmonds-Karp, Dinic, etc)
pesudo-code for building residual network:
n = #nodes
for each node i in 1..n:
residual_graph.addEdge(i, i+n, capacity=1);
residual_graph.addEdge(i+n, i, capacity=0);
for each edge (u,v) in graph
residual_graph.addEdge(u+n, v, capacity=+Infinity);
residual_graph.addEdge(v, u+n, capacity=0);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
excuse me if my question is repeated but i couldn't find a complete answer to prove that a connected graph which all vertices has degree = 2 is a hamiltonian graph.
I have read this and this
Let the given graph be G. Starting from a vertex v in the graph, let us trace an arbitrary walk(path with repeated vertices allowed), P, by repeatedly picking a vertex adjacent to the last vertex added to P, without repeated any edges. Terminate if you cannot add any more vertices or if you reach a vertex that was already visited before. This process will eventually terminate since there are finitely many vertices. Note that since every vertex has degree two, the termination will be caused by a vertex repeating. Let this termination vertex be t. What we have found is really a cycle containing t. Let this subgraph consisting of just this cycle containing t be C. Let V(C) be the set of all vertices of C. Since all the vertices have degree 2 in G and C, every edge in G involving any of the vertices in V(C) is already in C. Now, let us suppose there is a vertex of G, say u, not present in V(C). There will be no path from u to any vertex of V(C), because if there was one, you would end up with an edge going from V(C) to a vertex outside, which as we just saw isn't possible. But you know that G is connected, implying that there is no such vertex u. Thus, G = C and hence G is just a cycle. Trivially, it is Hamiltonian.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
A connected graph is vertex biconnected if there is no vertex whose removal disconnects the graph. A connected graph is edge biconnected if there is no edge whose removal disconnects the graph.
Give a proof or counterexample for each for the following statements:
(a) A vertex biconnected graph is edge biconnected.
(b) An edge biconnected graph is vertex biconnected.
For A)My attempt is that it should be the case, since I don't see how removing a vertex will affect the biconnection of the edge.
For B)My attempt is NO, since if we have a bridge, connecting two graphs, removing that edge will no longer have the graph vertex biconnected.
Perhaps I am totally wrong here, any assistance would be greatly appreciated.
Proof for a): by contradiction. Let G = (V,E) be vertex biconnected. Assume it is not edge biconnected. Then there exists an edge e = {v,w} we can remove such that G' = (V, E \ {e}) is disconnected. But then we can also remove v or w from G and disconnect the graph (since the removal of either end vertex of an edge will also remove that edge), which is a contradiction to G being vertex biconnected; thus G must also be edge biconnected.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I have a simple question on DFS and I'm trying to understand how to use it and not how to solve the whole problem. I'm really looking for an explanation and not a solution to my homework.
I'll write down the question first.
"Suppose you have an undirected graph G=(V,E) and let three of its
vertices to be called v1, v2 and v3. Find an algorithm which
determines if these three vertices are part of a clique
(complete graph) (k>=3)"
Now I suppose to use DFS in order to solve it. As far as I understand DFS will let me know if v1, v2 and v3 are in the same strongly connected component. If I'm correct I should also determine if G is also a clique(complete graph).
I read in the internet and I found out that asserting if a graph is clique or not is NP and cannot be solved easily. Am I correct? Am I missing anything? Is there any propery I can use to determine immediately if a graph is comeplete ?
To clarify the confusion about the NP-completeness: checking whether a graph is a clique is not NP-complete; just count the edges and see whether there are n(n-1)/2. What is NP-complete is to find a maximum clique (meaning the subgraph that has the biggest number of vertices and is a clique) or a clique of k vertices in a graph of n vertices (if k is part of the input instead of a fixed number); the latter case is called the clique decision problem.
EDIT: I just realized you asked something regarding strongly connected components as well; that term only applies to directed graphs (i.e. the edges have a direction, which means for two vertices v and w, the edge v->w is not the same as the edge w->v). Cliques are commonly defined on undirected graphs, for which there are only connected components.
If I understood it properly, all you have to check whether these three vertices are connected, i.e., the edges v1-v2, v2-v3 and v3-v1 exists. If they exist, they form a clique of K=3. If at least one of them does not, these three vertices together can not be in a clique of size k>=3.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I am working on solving this problem:
Professor Gaedel has written a program that he claims implements Dijkstra’s algorithm.
The program produces v.d and v.π for each vertex v in V. Give an
O.(V + E)-time algorithm to check the output of the professor’s program. It should
determine whether the d and π attributes match those of some shortest-paths tree.
You may assume that all edge weights are nonnegative.
v.d is the shortest distance from starting node to v.
v.π is v's predecessor in the shortest path from starting node to v
My idea is:
For every vertex (i), compare i.d with (i.π).d. If i's predecessor has a larger d value then we cannot have a shortest-path tree.
I believe this can check if the professor's output is not a shortest-path tree, but I don't think it can confirm that the output is a shortest-path tree. I cannot think of a way to do this without more information.
Am I on the right track?
I think this would work
Do a DFS, but instead of following the regular graph edges, follow only the π value for each vertex. You're doing this to produce a topological ordering, so that the first vertex to finish will be the first vertex in the topological ordering. Note that the first vertex in the topo sort you produce will be the "source" vertex that was given to Gaedel's algorithm.
Now that you have a topo ordering, you can relax edges in the most efficient order, just like how you would do it on a DAG.
for each v in topoSortedVerts
if v.d_verify != v.d_Gaedel
//fail
for each u in v.adjacencies
relax(v, u)
if v.d_verify != v.d_Gaedel
//fail
I think you may also need to make sure all V verts are considered, and that the source vertex matches. Maybe. Also, I guess Gaedel's predecessor subgraph induced by the π values could be real jacked up and have all kinds of crazy things wrong with it, but I assume it doesn't.
It is O(V + E) because the outer loop runs V times, and the inner loop runs E times using aggregate analysis.