How to unite two graphs in JUNG? - graph

I have to graphs which I want to unite, that is, create a new graph composed by the union of both graph's edges and nodes (without repetition). Is there an implementation for that avaliable in JUNG or do I have do so on my own?

There isn't an implementation for that in JUNG, but it's about six lines of code assuming that the graphs, vertices, and edges are of the same types:
// given Graph g1, g2
Graph g = new [appropriate Graph implementation]
for (V v : Collections.union(g1.getVertices(), g2.getVertices())) {
g.addVertex(v);
}
for (E e : g1.getEdges()) {
g.addEdge(e, g1.getEndpoints(e));
}
for (E e : g2.getEdges()) {
g.addEdge(e, g2.getEndpoints(e));
}
You can skip the vertex adding if there are no isolated vertices (i.e., vertices that have no incident edges); addEdge() will add any incident vertices.
If the graph is directed, you'll want to change the above to
g.addEdge(e, g1.getSource(e), g1.getDest(e));
Duplicates are silently ignored (if you want to know whether an add had an effect, check the return value).

Related

Convert random tree to directed tree in netwokx

I have generated a random tree using networkx.
A = nx.random_tree(15)
I am trying to convert it to a directed graph (i.e. tree).
G = nx.to_directed(A)
However, the result is a graph with two directions.
I would like to get the output as one direction tree.
According to official documentation:
Returns: G – A directed graph with the same name, same nodes, and with each edge (u, v, data) replaced by two directed edges (u, v, data) and (v, u, data).
If you want to delete reversed edges, you can write something like this:
G = nx.random_tree(10)
H = nx.DiGraph([(u,v) for (u,v) in G.edges() if u<v])
So H will be the tree you needed:

Split undirected graph by negative edges

Wondering if there exists an algorithm to split an undirected connected component graph given negative edges.
Essentially the vertices provided in negative edges should be unreachable.
If you want the connected components of your graph that contains only positive edges, then first delete all negative edges from your graph. Then run a DFS (depth-first-search) to find all connected components.
Here is the algorithm.
Begin
For each edge e in graph G, if e is negative, remove e from G.
For each vertex v in G, do:
If v is not labeled as visited
Let c be a new component, and add c to set C.
Call procedure DFS(v, c) to find one component of positive edges only.
End If
End For
The set C contains all the connected components consisting of positive edges only.
End
Procedure DFS(v, c)
Add v to c, and label v as visited.
For each edge from v to w, do
If vertex w is not labeled as visited then
Call DFS(G,w)
End If
End For
End Procedure

how to create a complete list of diads form a vertex list

how can I create a complete list of dyads from a vertex list?
I have a list (1, 2, 3...) and I need to generate a list containing all possible dyads from that list (1-1, 1-2, 1-3, 2-1, 2-2,...).
I've tried with get.edgelist, but it doesn't work, because the graph is not fully connected (all nodes are connected among them).
Thanks
Using igraph, you can grab all edges of a graph using E(g). If you'd want all possible edges, you can apply it on a complete graph (a graph that is fully connected). If the vertices in your graph are indeed in sequence from 1 to n, you can use make_full_graph() to make a Kn - that is to say a fully connected graph. In this example, the graph has 14 vertices.
g <- make_full_graph(14, directed=F)
el <- as_edgelist(g)
edges <- E(g)
edges_list <- split(el, rep(1:nrow(el), each = ncol(el)))
edges_vert <- unlist(list(t(el)))
edges will be the igraph-object, but I think what you're after is a list in R, like edges_list.
As you see, length(edges_list) is 91 since it is an undirected graph, and the number of edges in complete graphs is a function of the number of vertices.
A complete graph with n vertices is commonly written Kn and has these many edges:
Note that in igraph dyads are called edges and nodes are called vertices.

Choosing vertices by edge weight

I have a network with two types of edges, one with edge$weight=1 and the other edge$weight=2 (to define different relationships e.g. marriage vs. kinship). I want to choose the neighbors of nodes that are linked by edge$weight=2. Then, after getting the vertex names of the neighbors, I will calculate their degree only for edge$weight=1.
It seems simple, but the following code does not give the correct neighbor names:
V(net)[E(net)[E(net)$weight==2]]
Is there another way to get vertex names with respect to the edge type?
I think you are looking for the adj function which will return the vertices adjacent to a given edge set. In your case you can choose all the relevant vertices by using V(net)[ adj(E(net)$weight==2) ]
For example:
# make a sample graph with edge weights 1 or 2
net <- graph.full(20)
E(net)$weight <- sample(1:2,ecount(net),replace=T)
# now select only those vertices connected by an edge with weight == 2
V(net)[ adj(E(net)$weight==2) ]

Bipartite connected graph proof

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.

Resources