I'm looking for verification on the following in an effort of sorting out some basic properties of BFS & DFS.
Take the graph G=(V,E), V={a, b, c, d}, E={(a,b), (b,c), (c,d)}.
Suppose first G is a directed graph. Then, running BFS on
a as the source returns the subgraph V={a, b, c, d}, E={(a,b), (b,c), (c,d)},
b as the source returns the subgraph V={b, c, d}, E={(b,c), (c,d)},
c as the source returns the subgraph V={c, d}, E={(c,d)},
d as the source returns the subgraph V={d}, E={},
Running DFS on G can return, among others, one of the following 2 forests F. What
DFS returns depends on the ordering of nodes/edges on the representation of G:
i.) F={T_1}, T_1: V_{T_1}=V_G, E_{T_1}=E_G,
ii.) F={T_1, T_2, T_3}, V_{T_1}=V_{T_2}=V_{T_3}=V_G},
E_{T_1}={(a,b)}}, E_{T_2}={(b,c)}}, E_{T_3}={(c,d)}},
When G is undirected, Both BFS and DFS return a single tree, of which the edge- and vertex-sets are the same as those of G itself, and the tree roots/structures are potentially different depending the order of the elements of these sets and the nodes the algorithms started processing at.
Related
I have a pretty general question about algorithms:
Is there a shortest path algorithm in a graph (directed or undirected) with the following twist: each added node in the path is influenced by ALL of the other already existing nodes, and that "influence" (which is in other words - the weight of the edge between the two verteces, which is only created for that single purpose) is added to the path total weight?
Thanks in advance!
Edit: another attemp to explain: let's say I want to find the shortest path from A to Z, where in between there all kind of other nodes. The thing is, If im looking into the path that starts from A and then B, and I try to calculate the total distance from A to C, the distance is actually: distance(C) = weight(A, B) + weight(B,C) + weight(A, C), even though there isn't a direct edge between A and C, and its calculated just for that purpose.
Edit2: Another explanation: lets say we have: A, B1, B2, C1, C2, D. with verteces: (A, B1)=1, (A, B2)=2, (B1, C1)=1, (B2, C2)=2, (C1, D)=1, (C2, D)=2. (right to the equal sign is the edge weight). Shortest path from A to D is of course (A->B1->C1->D), but now lets add my bizzare demand: each node added to the path is adding his weight to all other nodes in the current path, so the total distance of the following is actually: 1+1+1+ w(A->C1) +w(A->D)+ w(B1->D). those weight can be known, but can't be used for the path itself (the A, B1, C1, D). one of those could effect the algorithms wanted result.
Your problem description (without further specifying weight(I, J)) is breaking one of the assumptions in the standard shortest path algorithms, namely that the shortest path from A to I might not be part of any shortest path A to Z via I.
A -> B(2), C(1)
B -> D(2)
C -> D(1)
D -> E(2)
with non-edge costs weight(I, J) := 0, except weight(C, E) := 100
The shortest path from A to D is A, C, D with cost 2=1+1, while the path A, B, D has cost 4=2+2. Even so the path A, C, D, E is more expensive than A, B, D, E because of weight(C, E).
In this case you're left with computing all possible paths from the start to the end node in your network, since the latest most expensive seeming path might still be cheaper when reaching the end node because of some weight(X, <end>): for each node, compute the cost of all incoming paths (from the start node) until you've computed all paths, then select the cheapest one at the end node. This algorithm assumes weight(I,J) >= 0 or that the input is a DAG.
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.
I want to find the weight of the edges of a graph by using the output of the Prim's algorithm.
Note: In a graph has n edges, each edge is different and between 1-n.
For example:
Vertices = {A, B, C, D, E}
Edges = {B-D, D-E, E-A, C-B, A-D, D-C, A-C}
Extract_Min() Order = B D C A E
By using the information above, I want to find the weight of each edge. Do you have any ideas?
Thanks in advance.
Edit: The solution does not have to be unique.
By your example:
Vertices = {A, B, C, D, E}
Edges = {B-D, D-E, E-A, C-B, A-D, D-C, A-C}
Extract_Min() Order = B D C A E
Look at the order given by Extract_Min().
The edge with weight 1 is surely B-D.
Assign weight 2 to some single edge from the set {B,D} to C.
Assign weight 3 to some single edge from the set {B,D,C} to A.
Assign weight 4 to some single edge from the set {B,D,C,A} to E.
Assign the remaining weights to the remaining edges in any order.
Given is a bipartite graph, and we want to list all maximal complete bipartite sub-graph.
For instance,
vertex set L = {A, B, C, D}
vertex set R = {a, b, c, d, e}
edges: A-a, A-b, B-a, B-b, C-c, C-d, D-c, D-d, D-e
The maximal complete bipartite are:
{A,B}-{a, b}
{C,D}-{c, d}
{D} - {c, d, e}
I have found a brute force algorithm, O(2^n).
I don't know if some approximation algorithm or randomized algorithm.
You can transform the problem to finding maximal cliques by adding edges between every pair of vertices in each part of the bipartite graph.
The Bron-Kerbosch algorithm can be used to list all maximal cliques in a graph (not necessarily bipartite). It is pretty easy to implement and has a slightly better worst-case time bound of O(3^(n/3)). There is also a fix-parameter tractable time bound in term of the degeneracy of the graph.
We're given an unweighted undirected graph G = (V, E) where |V| <= 40,000 and |E| <= 106. We're also given four vertices a, b, a', b'. Is there a way to find two node-disjoint paths a -> a' and b -> b' such that the sum of their lengths is minimum?My first thought was to first find the shortest path a -> a', delete it from the graph, and then find the shortest path b -> b'. I don't think this greedy approach would work.
Note: Throughout the application, a and b are fixed, while a' and b' change at each query, so a solution that uses precomputing in order to provide efficient querying would be preferable. Note also that only the minimum sum of lengths is needed, not the actual paths.
Any help, ideas, or suggestions would be extremely appreciated. Thanks a lot in advance!
This may be reduced to the shortest edge-disjoint paths problem:
(Optionally) Collapse all chains in the graph into single edges. This produces a smaller weighted graph (if there are any chains in the original graph).
Transform undirected graph into digraph by substituting each edge by a pair of directed edges.
Split each node into the pair of nodes: one with only incoming edges of the original node, other with only its outgoing edges. Connect each pair of nodes with a single directed edge. (For example, node c in the diagram below should be split into c1 and c2; now every path containing node c in the original graph should pass through the edge c1 -> c2 in the transformed graph; here x and y represent all nodes in the graph except node c).
Now if a = b or a' = b', you get exactly the same problem as in your previous question (which is Minimum-cost flow problem and may be solved by assigning flow capacity for each edge equal to 1, then searching for a minimum-cost flow between a and b with flow=2). If a != b, you just create a common source node and connect both a and b to it. If a' != b', do the same with a common destination node.
But if a != b and a' != b', minimum-cost flow problem is not applicable. Instead this problem may be solved as Multi-commodity flow problem.
My previous (incorrect) solution was to connect both pairs of (a, b) and (a', b') to common source/destination nodes, then to find a minimum-cost flow. Following graph is a counter-example for this approach:
How about this? Do BFS (breadth first search) traversal from a1 -> a2 and remove the path and compute BFS b1 -> b2. Now reset the graph and do same with b1->b2 first and remove path and then a1->a2.
Whatever sum is minumum is the answer.