Use deep-first-search. Find out all the articulation points and biconnected components. Show the change process of Low[v] for each node - depth-first-search

Given the following undirected graph G = (V, E). Using vertex s as the source vertex.
If multiple neighbors are available, visit them using alphabetical order.

Related

Acyclic across cuts in a graph

Given we have a graph G = (V,E) and a subset F with only V in it, for each connected component S of F, add the minimum weight edge in the cut (S, V \ S) to F.
Why is it that every time the minimum weight edge is added to F, F remains acyclic?
To create cycle, you have to create edge which connects vertices which are already connected.
If you add edge between vertices that are not connected, you don't create new cycle. You connect two unconnected components. But graph remains acyclic.
To get better understanding how it works, you could represent connected component of graph as single vertex. And then, when your add edge between unconnected components, your just merge vertices.
By the way, this question is not related to weights (and MST algorithm). It's still valid without weights.

Shortest path with even number of edges

Given a weighted undirected graph G and two nodes U,V to get the shortest path. How can i get the shortest path from U to V that uses a even number of edges (if possible to get it) ?
I've found some articles on the web speaking that a modification on the original graph is necessary. But i can't understand how to do it.
There is some good material to study on this problem ?
You'll need to build an intermediate graph and run Dijkstra's on that graph.
Given a graph G = (V, E), create a new graph G' = (V', E'), with V' a new set of vertices v_even and v_odd for every vertex v in V and E' the set of vertices as follows:
If (u, v) is an edge in G, then (u_odd, v_even) and (u_even, v_odd) are edges in G', with the same weight.
Obviously, the new graph has twice as many edges and vertices as the original graph.
Now, if you wanted to find the shortest path between s and t in G, simply run Dijkstra's on G' to find the shortest path between s_even and t_even.
The running time is still O(|V| log |E|).
Make a copy of your graph with the same weights and call it G'.
Connect every vertex of G to the corresponding vertex in G' and set the weight of the new edges to zero.
Delete copy of u from G' and delete v from G.
Now, the set of edges you added between G and G' constitute a matching M. Take that matching and find a minimum augmenting path for that matching.
Such a path must have u as one of its end points and copy of v as the other endpoint, because they are the only uncovered vertices. If you merge back the copies and get rid of the added edges, the path you found corresponds to an even path, because it starts at one copy and ends at another. Also, every even path corresponds to an augmenting path (by same argument), therefore the minimum of one is also the minimum of the other. This is explained in here.
What about running Dijkstra where each node has two values. One is odd (coming from even value) and other is even value.

DFS on directed graph & Kosaraju's algorithm

I'm having trouble to understand Kosaraju's algorithm for finding the strongly connected components of a directed graph. Here's what I have on my notebook (I'm a student :D):
Start from an arbitrary vertex (label it with #1) and perform a DFS. When you can't go any further, label the last visited vertex with #2, and start another DFS (skipping vertices already labeled), and so on.
Transpose the graph.
Do DFS starting from each vertex in reverse order, those vertices which end visited after each DFS belong to the same SCC.
I have this example:
And after the first step starting from E, the labels are:
E
G
K
J
I
H
F
C
D
B
A
So here comes the thing: Is there a difference for DFS in directed/undirected graphs?
I did a mental test of the first step on my mind ignoring the arrows (just like it was undirected) and only got correct #1 for E (of course) and #2 for G, but #3 fell onto J, not K. So I thought maybe I should respect the arrows, and did a DFS considering that, but after the first pass starting from E, I can't go anywhere from G (which is #2), so I'm stuck there.
Is there anything about DFS on directed graphs that I'm not aware of? I've been taught DFS only on undirected graphs!
Your second step is incomplete. See Wikipedia:
Kosaraju's algorithm works as follows:
Let G be a directed graph and S be an empty stack.
While S does not contain all vertices:
Choose an arbitrary vertex v not in S. Perform a depth-first search starting at v. Each time that depth-first search finishes expanding a vertex u, push u onto S.
Reverse the directions of all arcs to obtain the transpose graph.
While S is nonempty:
Pop the top vertex v from S. Perform a depth-first search starting at v in the transpose graph. The set of visited vertices will give the strongly connected component containing v; record this and remove all these vertices from the graph G and the stack S. Equivalently, breadth-first search (BFS) can be used instead of depth-first search.
So you shouldn't only do something with the last vertex and first vertices, but with each vertex in the DFS.
Also note that you should be backtracking - when you can't go further, you go to the previous vertex and continue from there.
And no, you can't treat it as an undirected graph - the direction of the edges matter significantly.
So, starting from E, you'd, for example, go F, then G, then back to F, then H, then K, then I, then J, then back to I, K, H, F, and finally E, having pushed all visited vertices onto the stack.

Transform a simple directed graph to a simple undirected graph

How would I convert a simple directed graph to a simple undirected one?
Is this possible?
Assuming: (from here)
in a simple digraph loops are disallowed. (A loop is an arc that pairs a vertex to itself.)
and: (from here)
a simple [undirected] graph is an undirected graph that has no loops (edges connected at both ends to the same vertex) and no more than one edge between any two different vertices.
I'm assuming the edges are unweighted, otherwise this can't be done without specifying how this needs to be done.
AM = Adjacency matrix
AL = Adjacency list
Remove the direction of all edges.
AM: For each edge A->B, row A, column B would already be set. Also set row B, column A.
AL: For each edge A->B, the edge already appears in A's AL, also add the edge to B's AL.
Go through all edges, removing all edges for which we already found an edge between the two vertices that are the end-points of that edge (so if A-B has already been processed, if we find another A-B (or equivalently B-A), we must remove that edge). This needs to be done since there can be multiple edges between vertices in a simple directed graph, but this can't happen in a simple undirected graph.
AM: Nothing really needs to be done since an AM forces there to only be a single edge between vertices, so we'd just be overwriting the edges.
AL: For each vertex A, for each edge A-B or B-A in its adjacency list, remove all other edges A-B or B-A in the list.
Source and details...
For each directed edge e=(x,y), add new vertices ve1,…,ve5 and replace e with the edges xve1, ve1ve2, ve1ve3, ve3ve4, ve4ve5, ve3y.
To decode, every leaf (degree-1 vertex) whose neighbour has degree 2 must be ve5 for some edge e=(x,y); its neighbour is ve4 and the other neighbour of ve4 is ve3. ve3 has a unique neighbour that has both degree 3 and is adjacent to a leaf: the neighbour is ve1 and its leaf is ve2 (if ve1 has two leaf neighbours, pick one arbitrarily to be ve2). The other neighbour of ve1 is x and the other neighbour of ve3 is y. Restore the directed edge (x,y) and delete the vertices ve1,…,ve5.
G=(V,E) is a directed graph. (a,b) \in E is a directed edge.
G' = (V, E') is an equivalent undirected graph where (a,b) is transformed as (a,b) and (b,a).
Thus, edge directed edge becomes two edge (one in each direction).

Shortest Paths in undirected cyclic graphs

Can someone please explain how given an undirected graph G = (V; E); edge lengths le > 0; and edge edges in E.
We can generate the length of the shortest cycle containing edge e.
I understand how to do this in directed graphs, but im not sure how to approach the problem with an undirected graph.
Without modifying the graph: Let e be an edge (u, v). Choose one of the two nodes—I'll choose u—and run an ordinary Dijkstra/BFS starting from u with one minor modification: When making the first hop, you must not add v to the queue. Now search for v.

Resources