I was learning about DfS trees from:
http://rosalind.info/glossary/algo-depth-first-search/
In the "Depth-first search in directed graphs" section if we add a node Z with no edges pointing towards it but only one edge from Z to C, then where would Z appear in the resulting DFS tree?
Would its edge from Z to C be considered a tree edge or a cross edge?
Thanks!
When you build the new DFS tree for the given question it will be obvious:
The current DFS tree for exploration of A to H is the same. Z is not part of it since it is not connected by any Edge to Z. Due to lexicographic order in selecting vertices, it won't be selected before A.
After visiting A to H, Z is not visited and those will be - in lexicographic order - selected for exploration. It will be a new Tree in the DFS forest.
Be aware that in the unidirectional case, this is only a DFS forest when you ignore the non-tree edges as shown in the sample.
The Terminology states that Cross Edges ... lead to a node that has already been completely explored. Z has an Edge to the previously explored C, those it's edge is a Cross Edge.
You should check this answer for yourself.
How the tree (or forest) looks and what type of edge ZC is depends on where the DFS is run from, and what nodes are chosen first. If the DFS visits Z before it visits C, then ZC will be a tree edge as it is the only and Z will be a part of a tree with multiple nodes. If DFS starts at Z, then there will be one tree with Z as the root node. However, if Z is only discovered once C has already been visited, then C must also have been finished (completely explored) because Z cannot be reached by any other nodes. Thus in this case, ZC is a cross edge and Z would be in a tree by itself.
Related
Say that I have a big network with 10^4 nodes. And then I want to analyse the neighbourhood associated with a random node, say node 10. I can see which are the nodes connected to that node by looking at the 10th row entries of the adjacency matrix, and then I can repeat this if I want to look at the neighbours of those neighbours (second shell) and so on and so forth.
Is there an efficient way to do this - or even an inefficient but better than writing the whole thing from the scratch-? The actual network that I have is a Random Regular Graph and I am interested on the tree-like local structure for large networks.
If I understand your use case, there is a good way of doing this: the egonet function. You give it a graph, a starting vertex, and number of hops, and it will return an induced subgraph of the graph starting at the vertex and going out that number of hops. Here's the docstring:
egonet(g, v, d, distmx=weights(g))
Return the subgraph of g induced by the neighbors of v up to distance d, using weights (optionally) provided by distmx. This is equivalent to
induced_subgraph(g, neighborhood(g, v, d, dir=dir))[1].
Optional Arguments
––––––––––––––––––––
• dir=:out: if g is directed, this argument specifies the edge direction
with respect to v (i.e. :in or :out).
Edited to add: if all you need are the vertex indices, then neighborhood() is what you want:
neighborhood(g, v, d, distmx=weights(g))
Return a vector of each vertex in g at a geodesic distance less than or equal to d, where distances may be specified by distmx.
Optional Arguments
––––––––––––––––––––
• dir=:out: If g is directed, this argument specifies the edge direction
with respect to v of the edges to be considered. Possible values: :in or :out.
Given a graph G,which are the sufficient and necessary conditions , so that this graph has a unique minimum spanning tree?In addition , how can I proove these conditions?
So far , I had found that those conditions are :
1)For every partition of V(G) into two subsets, the minimum weight edge with one endpoint in each subset is unique.
2)The maximum-weight edge in any cycle of G is unique.
But I am not sure if this is correct.Even in case this is correct,I cannot prove its correctness.
Actually, there is a necessary and sufficient condition for unique MST. In the book A First Course In Graph Theory, it is given as an exercise:
Exercise 4.30
Let G be a connected weighted graph and T a minimal spanning tree of G. Show that T is a unique minimal spanning tree of G if and only if the weight of each edge e of G that is not in T exceeds the weight of every other edge on the cycle in T+e.
I write my proof here.
This is false because at least the first condition is not necessary. The proof is by counterexample (source).
Take G to be any tree where all edge weights are 1. Then G has a
unique MST (itself), but any partition with more than one edge
crossing it has several minimum weight edges.
EDIT:
In response to your modified question...
There is a well-known sufficient (but not necessary) condition for the uniqueness of a MST:
If the weight of each edge in a connected graph is distinct, then the graph contains exactly one (unique) minimum spanning tree.
The proof is as follows (source):
For the sake of contradiction, suppose there are two different MSTs of
G, say T1 and T2. Let e = v-w be the min weight edge of G that is in
one of T1 or T2, but not both. Let's suppose e is in T1. Adding e to
T2 creates a cycle C. There is at least one edge, say f, in C that is
not in T1 (otherwise T1 would be cyclic). By our choice of e, w(e) ≤
w(f). Since all of the edge weights are distinct, w(e) < w(f). Now,
replacing f with e in T2 yields a new spanning tree with weight less
than that of T2 (contradicting the minimality of T2).
However, regarding "sufficient and necessary" conditions for the uniqueness of a MST, I do not believe any are known to exist.
I am reading the book called, "VLSI Physical Design: From Graph Partitioning to Timing Closure" by Andrew B. Kahng, Jens Lienig, Igor L. Markov, and Jin Hu.
In that book, there is a picture of a combinational circuit like shown in Fig 1.
For the combinational circuit in Fig 1, the authors show the connectivity graph as shown in Fig 2 below.
My question is, there is no connectivity directly between gates x and y. In that case, why does the graph show two edges between gates (or nodes) x and y ?
Thanks for your help.
While there is no direct connectivity between x and y (such as x feeding y), the net N1 connects three nodes: a, x, and y. So, as all three are electrically equivalent, you must preserve the connections for the relationship among all three nodes. Therefore, for N1, you need an edge between a and x, an edge between a and y, and an edge between x and y. Similarly for N2, as it connects b, x, and y, you need an edge between every pin pair among b, x, and y.
In the general case, if you have a multi-pin net, a net that connects multiple nodes, then you will need to have an edge between every pin pair:
"A p-pin net is represented by (p choose 2) total connections between its nodes"
-- connectivity graph definition on p.28.
As an aside, you can see that this is a tedious process and the number of edges can quickly grow in this model. If you use a hyperedge and hypergraph model, however, then you only need one hyperedge to represent N1 and one hyperedge to represent N2 (versus the three regular edges).
I do also want to point out that this connectivity definition is very general, and in some cases, the edges between x and y can be removed. For instance, if you are performing timing propagation (e.g., arrival time) on N1, then you only need a directed edge between a and x and a directed edge between a and y.
I hope this helps.
Given an undirected graph in which each node has a Cartesian coordinate in space that has the general shape of a tree, is there an algorithm to convert the graph into a tree, and find the appropriate root node?
Note that our definition of a "tree" requires that branches do not diverge from parent nodes at acute angles.
See the example graphs below. How do we find the red node?
here is a suggestion on how to solve your problem.
prerequisites
notation:
g graph, g.v graph vertices
v,w,z: individual vertices
e: individual edge
n: number of vertices
any combination of an undirected tree g and a given node g.v uniquely determines a directed tree with root g.v (provable by induction)
idea
complement the edges of g by orientations in the directed tree implied by g and the yet-to-be-found root node by local computations at the nodes of g.
these orientations will represent child-parent-relationsships between nodes (v -> w: v child, w parent).
the completely marked tree will contain a sole node with outdegree 0, which is the desired root node. you might end up with 0 or more than one root node.
algorithm
assumes standard representation of the graph/tree structure (eg adjacency list)
all vertices in g.v are marked initially as not visited, not finished.
visit all vertices in arbitrary sequence. skip nodes marked as 'finished'.
let v be the currently visited vertex.
2.1 sweep through all edges linking v clockwise starting with a randomly chosen e_0 in the order of the edges' angle with e_0.
2.2. orient adjacent edges e_1=(v,w_1), e_2(v,w_2), that enclose an acute angle.
adjacent: wrt being ordered according to the angle they enclose with e_0.
[ note: the existence of such a pair is not guaranteed, see 2nd comment and last remark. if no angle is acute, proceed at 2. with next node. ]
2.2.1 the orientations of edges e_1, e_2 are known:
w_1 -> v -> w_2: impossible, as a grandparent-child-segment would enclose an acute angle
w_1 <- v <- w_2: impossible, same reason
w_1 <- v -> w_2: impossible, there are no nodes with outdegree >1 in a tree
w_1 -> v <- w_2:
only possible pair of orientations. e_1, e_2 might have been oriented before. if the previous orientation violates the current assignment, the problem instance has no solution.
2.2.2 this assignment implies a tree structure on the subgraphs induced by all vertices reachable from w_1 (w_2) on a path not comprising e_1 (e_2`). mark all vertices in both induced subtrees as finished
[ note: the subtree structure might violate the angle constraints. in this case the problem has no solution. ]
2.3 mark v visited. after completing steps 2.2 at vertex v, check the number nc of edges connecting that have not yet been assigned an orientation.
nc = 0: this is the root you've been searching for - but you must check whether the solution is compatible with your constraints.
nc = 1: let this edge be (v,z).
the orientation of this edge is v->z as you are in a tree. mark v as finished.
2.3.1 check z whether it is marked finished.
if it is not, check the number nc2 of unoriented edges connecting z.
nc2 = 1: repeat step 2.3 by taking z for v.
if you have not yet found a root node, your problem instance is ambiguous:
orient the remaining unoriented edges at will.
remarks
termination:
each node is visited at max 4 times:
once per step 2
at max twice per step 2.2.2
at max once per step 2.3
correctness:
all edges enclosing an acute angle are oriented per step 2.2.1
complexity (time):
visiting every node: O(n);
the clockwise sweep through all edges connecting a given vertex requires these edges to be sorted.
thus you need O( sum_i=1..m ( k_i * lg k_i ) ) at m <= n vertices under the constraint sum_i=1..m k_i = n.
in total this requires O ( n * lg n), as sum_i=1..m ( k_i * lg k_i ) <= n * lg n given sum_i=1..m k_i = n for any m <= n (provable by applying lagrange optimization).
[ note: if your trees have a degree bounded by a constant, you theoretically sort in constant time at each node affected; grand total in this case: O(n) ]
subtree marking:
each node in the graph is visited at max 2 times by this procedure if implemented as a dfs. thus a grand total of O(n) for the invocation of this subroutine.
in total: O(n * lg n)
complexity (space):
O(n) for sorting (with vertex-degree not constant-bound).
problem is probably ill-defined:
multiple solutions: e.g. steiner tree
no solution: e.g. graph shaped like a double-tipped arrow (<->)
A simple solution would be to define a 2d rectangle around the red node or the center of your node and compute each node with a moore curve. A moore curve is a space-filling curve, more over a special version of a hilbert curve where the start and end vertex is the same and the coordinate is in the middle of the 2d rectangle. In generell your problem looks like a discrete addressing space problem.
A friend presented me with a conjecture that seems to be true but neither of us can come up with a proof. Here's the problem:
Given a connected, bipartite graph with disjoint non-empty vertex sets U and V, such that |U|<|V|, all vertices are in either U or V, and there are no edges connecting two vertices within the same set, then there exists at least one edge which connects vertices a∈U and b∈V such that degree(a)>degree(b)
It's trivial to prove that there is at least one vertex in U with degree higher than one in V, but to prove that a pair exists with an edge connecting them is stumping us.
For any edge e=(a,b) with a∈U and b∈V, let w(e)=1/deg(b)-1/deg(a). For any vertex x, the sum of 1/deg(x) over all edges incident with x equals 1, because there are deg(x) such edges. Hence, the sum of w(e) over all edges e equals |V|-|U|. Since |V|-|U|>0, w(e)>0 for som edge e=(a,b), which means that deg(a)>deg(b).
Prove it by contradiction, i.e. suppose that deg(a) ≤ deg(b) ∀(a,b)∈E, where E is the edgeset of the graph (with the convention that the first element is in U and the second in V).
For F⊆E, designate by V(F) the subset of V which is reachable through edgeset F, that is:
V(F) = { b | (a,b)∈F }
Now build an edgeset F as follows:
F = empty set
For a ∈ U:
add any edge (a,b)∈E to F
Keep adding arbitrary edges (a,b)∈E to F until |V(F)| = |U|
The set V(F) obtained is connected to all nodes in U, hence by our assumption we must have
∑a∈U deg(a) ≤ ∑b∈V(F) deg(b)
However, since |U|=|V(F)| and |U|<|V| we know that there must be at least one "unreached" node v∈V\V(F), and since the graph is connected, deg(v)>0, so we obtain
∑a∈U deg(a) < ∑b∈V deg(b)
which is impossible; this should be an equality for a bipartite graph.