DFS Tree of an Undirected Graph - algorithm

So I am given what is a DFS Tree of an Undirected Graph.
Here is the problem:
Now I already know that the answer is (4,3)
But what other edges not listed would be impossible?
Would (3,6) be a valid edge?
What about (2,4) or (3,5)
Would it be correct to assume that nodes on different branches of a DFS tree cannot have an edge connecting them?

The graph G(V, E), as stated in the original question, is undirected. Consider any pair of nodes u, v \in V such that there is an edge (u, v) \in E. Now lets traverse the graph in DFS (depth-first search):
if we reach u first, we will eventually visit all nodes that are reachable from u, including v, and therefore v will be a child node of u (or of its child nodes) in the DFS tree;
if we reach v first, the case is analogous, as the graph is undirected.
So, for any edge (u, v) \in E, there will be a path in the DFS tree connecting u to v. Now lets see your cases:
1) Would (3,6) be a valid edge? What about (2,4) or (3,5)?
(3, 6): is not a valid edge. If there were such edge, 3 would be a child node of 6;
(2, 4): is not a valid edge. If there were such edge, 2 would be a child node of 4;
(3, 5): is not a valid edge. If there were such edge, 3 would be a child node of 5.
2) Would it be correct to assume that nodes on different branches of a DFS tree cannot have an edge connecting them?
If there is an edge connecting two nodes u and v in an undirected graph, there will be a path e1 e2 ... en connecting u to v (or v to u) in the associate DFS tree. So, if two nodes from the DFS tree are on different branches, there is no edge inbetween them.

Related

Graph and MST, Some Facts and Validity

My notes tell me that the first and last is false. I need some idea of how to understand the validity of these sentences in a more simple and concise manner.
Suppose M is an MST (minimum spanning tree) of the Weighted Graph - GR.
Let A be a vertex of GR then M-{A} is also MST of GR-{A}.
Let A be a leaf of M then M-{A} is also MST of GR-{A}.
If e is a edge of M then (M-{e}) is a forest of M1 and M2 trees such that for M_i, i=1,2 is a MST of Induced Graph GR on vertexes T_i.
Let A be a vertex of GR, then M-{A} is also MST of GR-{A}.
This is false.
If A is not a leaf, then M-{A} is not a connected graph, and so it cannot be a MST.
In more detail: A has at least two neighbors, and the single path (MST property) that exists between two of them includes A. If A is removed, then there is no more path between those two other vertices.
Let A be a leaf of M, then M-{A} is also MST of GR-{A}.
This is true.
As A is a leaf, M-{A} is a connected graph, and has one fewer edge than M: it does not have the edge e that connects with A in M.
Now assume that M-{A} is not a MST of GR-{A}. We know that M-{A} is a connected graph, and has no cycles -- otherwise M would not be an MST. So if it is not an MST, it must be that its weight is not minimised. Then there is a different graph P that is a MST of GR-{A}. But then P+e would have less total weight than M, but still be a spanning tree of GR. So then M could not have been a MST of GR. This is a contradiction, and so the original statement is true.
If e is a edge of M then M-{e} is a forest of M1 and M2 trees such that for Mi, i=1,2 is a MST of the induced graph GR on vertexes Ti.
This is true. Your notes are wrong on this one.
Let's assume that Mi (for either i=1,2) is not a MST of the induced graph on vertexes Ti. We know that Mi is not disconnected (the removal of e creates exactly 2 connected graphs), and that it has no cycles (otherwise M would have had those as well). So if it is not a MST, then the reason would be that its weight is not minimised, and another graph Pi is a MST on that induced graph, and has less total weight than Mi.
If you would then reintroduce the edge e, and so connect Pi with M3-i, we would have a spanning tree for GR which would have less weight than M, and so M is not a MST. This is again a contradiction and so the original statement is true.

Shortest path distance from source(s) to all nodes in the graph - O(m + n log(n)) time

Let G(V,E) be a directed weighted graph with edge lengths, where all of the edge lengths are positive except two of the edges have negative lengths. Given a fixed vertex s, give an algorithm computing shortest paths from s to any other vertex in O(e + v log(v)) time.
My work:
I am thinking about using the reweighting technique of Johnson's algorithm. And then, run Belford Algo once and apply Dijkstra v times. This will give me the time complexity as O(v^2 log v + ve).
This is the standard all pair shortest problem, As I only need one vertex (s) - my time complexity will be O(v log v + e) right?
For this kind of problem, changing the graph is often a lot easier than changing the algorithm. Let's call the two negative-weight edges N1 and N2; a path by definition cannot use the same edge more than once, so there are four kinds of path:
A. Those which use neither N1 nor N2,
B. Those which use N1 but not N2,
C. Those which use N2 but not N1,
D. Those which use both N1 and N2.
So we can construct a new graph with four copies of each node from the original graph, such that for each node u in the original graph, (u, A), (u, B), (u, C) and (u, D) are nodes in the new graph. The edges in the new graph are as follows:
For each positive weight edge u-v in the original graph, there are four copies of this edge in the new graph, (u, A)-(v, A) ... (u, D)-(v, D). Each edge in the new graph has the same weight as the corresponding edge in the original graph.
For the first negative-weight edge (N1), there are two copies of this edge in the new graph; one from layer A to layer B, and one from layer C to layer D. These new edges have weight 0.
For the second negative-weight edge (N2), there are two copies of this edge in the new graph; one from layer A to layer C, and one from layer B to layer D. These new edges have weight 0.
Now we can run any standard single-source shortest-path problem, e.g. Dijkstra's algorithm, just once on the new graph. The shortest path from the source to a node u in the original graph will be one of the following four paths in the new graph, whichever corresponds to a path of the lowest weight in the original graph:
(source, A) to (u, A) with the same weight.
(source, A) to (u, B) with the weight in the new graph minus the weight of N1.
(source, A) to (u, C) with the weight in the new graph minus the weight of N2.
(source, A) to (u, D) with the weight in the new graph minus the weights of N1 and N2.
Since the new graph has 4V vertices and 4E - 2 edges, the worst-case performance of Dijkstra's algorithm is O((4E - 2) + 4V log 4V), which simplifies to O(E + V log V) as required.
To ensure that a shortest path in the new graph corresponds to a genuine path in the original graph, it remains to be proved that a path from e.g. (source, A) to (u, B) will not use two copies of the same edge from the original graph. That is quite easy to show, but I'll leave it to you as something to think about.

Find all pairs of nodes whose deletion disconnects graph

Given undirected, connected graph, find all pairs of nodes (connected by an edge) whose deletion disconnects the graph.
No parallel edges and no edges connecting node to itself.
The problem seems similar to finding articulation points (or bridges) of a connected, undirected graph - yet with a twist, that we have to remove a pair of vertices connected by an edge (and all other edges connected to that pair).
This is a homework question. I've been trying to solve it, read about DFS and articulation points algorithms (that bookkeap depth and lowpoint of each node) - but none of these approaches help this particular problem. I've checked through Cormen's Intro to Algorithms, but no topic suggested itself as appropriate (granted, book does have 1500 pages).
While it's true that finding articulation point would also (most of the time) find such a pair, there are a lot of pairs that are not articulation points - consider a graph with 4-vertices,5-edges (square with a single diagonal): it has one such pair but no articulation points (nor bridges).
I'm lost. Help me, stack overflow, you are my only hope.
Rather straightforward, maybe not the most efficient:
Let the graph be G=(V,E) with V := {v_1, ..., v_n}. For each subset V' of V let G_V' be the node induced subgraph comprising the nodes V \ V'. Let further N>_v_i := {v_j in V : {v_i,v_j} in E and j > i} be the set of all neighbors of v_i in G with index greater than i. Finally, let c(G) be the set of connected components of a graph.
Compute the pairs as follows:
pairs = {}
for each v in V:
compute G_{v}
if G_{v} is unconnected:
for each v' in N>_v:
# Ensures that removal of v' does not render subgraph connected
# (Note comment by MkjG)
if |c(G_{v})| > 2 or {v'} not in c(G_{v}):
add {v,v'} to pairs
else:
for each v' in N>_v:
compute G_{v,v'}
if G_{v,v'} unconnected:
add {v,v'} to pairs
Connectivity can be checked via DFS or BFS in O(m+n). The runtime should hence be O(n * k * (m+n)), where k is the maximum degree of G.
Update to my previous answer based on the suggestion by #MkjG to use DFS for computing articulation points.
Let the graph be G=(V,E) with V := {v_1, ..., v_n}_. For each subset V' of V let G_V' be the node induced subgraph comprising the nodes V \ V'. For G connected, we call v in V an articulation point if G_{v} is unconnected. Let N_v be the set of neighbors of v in G.
Articulation points can be computed via DFS, read here for more information on the algorithm. In short:
compute a DFS tree T for some root node r in V
r is an articulation point, iff it has more than one child in T
any other node v in V is an articulation point, iff it has a child v' in T that satisfies the following condition: no node in the subtree T' of T rooted at v' has a back edge to an ancestor of v
Let the result of a DFS on graph G be a function c on the nodes v in V. c(v) is a subset of N_v, it holds v' in c(v) iff both of the following conditions are met:
v' is a child of v in T
no node in the subtree T' of T rooted at v' has a back edge to an ancestor of v
Note that for the root node r of T, c(r) is the set of all children of r. Function c can be computed in time O(n+m).
Compute the separator pairs as follows:
# performs DFS on G for some root node r
c = DFS(G,r)
# computes articulation points of G and corresponding number of components
aps = {}
compCounts = {}
for each v in V:
numComps = |c(v)|
if v != r:
++numComps
if numComps > 1:
add v to aps
compCounts[v] = numComps
# computes the set of all separator pairs containing at least on ap
S = {}
for each v in aps:
numComps = compCounts[v]
for each v' in N_v:
if numComps > 2:
# G_{v,v'} has at least two connected components
add {v,v'} to S
else:
# if v' is an isolated node in G_{v}, then G_{v,v'} is connected
if N_v' != {v}:
add {v,v'} to S
# computes remaining separator pairs
for each v in V \ aps:
compute G_{v}
# performs DFS on G_{v} for some root r_v != v
c_v = DFS(G_{v},r_v)
# adds separator pairs for articulation points of G_{v} in N_v
for each v' in N_v:
numComps = |c(v')|
if v' != r_v:
++numComps
if numComps > 1:
add{v,v'} to S
Runtime is in O(n * (n+m))
A set of k edges disconnecting a graph is called a k-cut. You are trying to enumerate all 2-cuts of a graph.
This paper describes an efficient algorithm to enumerate all cuts of a graph. It should be possible to adapt it to find all 2-cuts of a graph.

Minimum Spanning Tree Graph

I have a connected graph G=(V,E) V={1,2,...,n} and a cost function c:E->R
and a second partial graph G'=(V,T) where T={ for every vertex v∈ V find the neighbor with the minimum cost and add the new edge to T}
If G' graph has at least 2 connected components with the set of vertices we consider the graph H where
iff the set of edges (from the initial graph G) is not null.We define over the edges of H a cost function.
Let's say I choose V(H)={a,e,f} and E(H)={ae,af,fe} and
E12={ab,bc,bd,ed}
E23={eg,ef} E31={fc,fd}
c'(ae)=min{c(ab),c(bc),c(bd),c(ed)}=4
c'(af)=min{c(fc),c(fd)}=9
c'(fe)=min{c(eg),c(ef)}=8
Now for every edge e ∈ E(H) we note with e' the edge (from the original graph G)
for which this minimum is attained.
So e'={bc,df,eg} because bc=4 , df=9 and eg=8 and are the min edges that connect my components.
And I have a minimum spanning tree in H relative to the cost function c' and A' is the set of edges for this tree.
So A'={ae,fe} (I deleted the edge with the maximum cost=af from my graph H to create a min spanning tree)
and I have another set of edges A'={e'|e∈A'} and
is a min spanning tree in G relative to the function cost c.
But none of my edges from A' are the same with the ones from e'.
What I'm I doing wrong?
Looks like you're implementing Boruvka's algorithm. If you look at the notation, it says there's an edge from one new node vC1 to a new node vC2 if there are a pair of nodes x ∈ C1 and y ∈ C2 that are adjacentnin the original graph G. In other words, there's an edge between two new nodes if the connected components they correspond to in G' are adjacent in G. The cost of the edge running between them is then the lowest of the costs of any of the edges running between those CC's in the original graph G.

Algorithm for finding weight of path with lowest weight in weighted directed graph

I am given a G=(V,E) directed graph, and all of its edges have weight of either "0" or "1".
I'm given a vertex named "A" in the graph, and for each v in V, i need to find the weight of the path from A to v which has the lowest weight in time O(V+E).
I have to use only BFS or DFS (although this is probably a BFS problem).
I though about making a new graph where vertices that have an edge of 0 between them are united and then run BFS on it, but that would ruin the graph direction (this would work if the graph was undirected or the weights were {2,1} and for an edge of 2 i would create a new vertex).
I would appreciate any help.
Thanks
I think it can be done with a combination of DFS and BFS.
In the original BFS for an unweighted graph, we have the invariant that the distance of nodes unexplored have a greater or equal distance to those nodes explored.
In our BFS, for each node we first do DFS through all 0 weighted edges, mark down the distance, and mark it as explored. Then we can continue the other nodes in our BFS.
Array Seen[] = false
Empty queue Q
E' = {(a, b) | (a, b) = 0 and (a, b) is of E}
DFS(V, E', u)
for each v is adjacent to u in E' // (u, v) has an edge weighted 0
if Seen[v] = false
v.dist = u.dist
DFS(V, E', v)
Seen[u] = true
Enqueue(Q, u)
BFS(V, E, source)
Enqueue(Q, source)
source.dist = 0
DFS(V, E', source)
while (Q is not empty)
u = Dequeue(Q)
for each v is adjacent to u in E
if Seen[v] = false
v.dist = u.dist + 1
Enqueue(Q, v)
Seen[u] = true
After running the BFS, it can give you all shortest distance from the node source. If you only want a shortest distance to a single node, simply terminate when you see the destination node. And yes, it meets the requirement of O(V+E) time complexity.
This problem can be modified to the problem of Single Source Shortest Path.
You just need to reverse all the edge directions and find the minimum distance of each vertex v from the vertex A.
It could be easily observed that if in the initial graph if we had a minimal path from some vertex v to A, after changing the edge directions we would have the same minimal path from A to v.
This could be simply done either by Dijkstra OR as the edges just have two values {0 and 1}, it could also be done by modified BFS (first go to vertexes with distance 0, then 1, then 2 and so on.).

Resources