Is it possible to assign edge direction based on a edge attribute in R igraph?
Suppose I have a following edge table. I can either created a directed graph or an undirected graph using graph_from_data_frame(). But as seen here some are directed edges while others are undirected. Is it possible to create it?
Node1 Node2 Directional
Node2 Node3 Undirected
Node1 Node3 Directional
Node1 Node4 Undirected
Node4 Node2 Directional
One method could be to separate the directed and undirected edges, create a graph separately and then merge them. Any other efficient method?
When using igraph, I don't think you can enclose both directed and undirected edges on the same plot. However, we can use bidirectional edges as an alternative of undirected edges, and create a directed graph in turn.
Below is a possible option
g <- graph_from_data_frame(
rbind(
df,
transform(
subset(
df,
V3 == "Undirected"
),
V1 = V2,
V2 = V1
)
)
)
and plot(g) gives
data
> dput(df)
structure(list(V1 = c("Node1", "Node2", "Node1", "Node1", "Node4"
), V2 = c("Node2", "Node3", "Node3", "Node4", "Node2"), V3 = c("Directional",
"Undirected", "Directional", "Undirected", "Directional")), class = "data.frame", row.names = c(NA,
-5L))
> df
V1 V2 V3
1 Node1 Node2 Directional
2 Node2 Node3 Undirected
3 Node1 Node3 Directional
4 Node1 Node4 Undirected
5 Node4 Node2 Directional
Related
Given a .dot representation of a graph, I'd like to be able to compile some statistics about each node. Statistics could be: # of edges, # of levels, # of nodes.
Is there a package available that lets me do this?
Yes, this comes with graphviz out of the box.
General statistics about a graph can be obtained by feeding your graph into gc - count graph components:
gc (...) prints to standard output the number of nodes, edges, connected
com- ponents or clusters contained in the input files.
If you would like to generate more specific stats about your graph, you can use the tool gvpr - graph pattern scanning and processing language.
gvpr allows executing a custom script against your graph. The script may simply gather custom statistics like in your case, or it may even modify the input graph.
The above linked documentation is very complete and explains all the available properties and features better than I can do here. Below just a simple example to get you started.
If we have the following graph graph.gv:
digraph graphinfotest {
a -> {b; c; d} -> e;
b -> c;
}
The following gvpr script (in file graphinfo.gvpr):
BEG_G {
int n = nNodes($G);
int e = nEdges($G);
printf("There are %d nodes and %d edges in %s\n", n, e, $G.name);
}
N {
printf("Node %s - indegree %d, outdegree %d\n", $.name, $.indegree, $.outdegree);
}
Called with
gvpr -f graphinfo.gvpr graph.gv
Will produce the following output:
There are 5 nodes and 7 edges in graphinfotest
Node a - indegree 0, outdegree 3
Node b - indegree 1, outdegree 2
Node c - indegree 2, outdegree 1
Node d - indegree 1, outdegree 1
Node e - indegree 3, outdegree 0
I am not able to comprehend the documentation for this function, I have seen several times the following
tie (ei,ei_end) = out_edges(*(vi+a),g);
**g**<-graph
**vi**<-beginning vertex of graph
**a**<- a node
**ei and ei_end** <- edge iterators
What does the function return,and what does it do,when could I use?
Can I find all edges from a node for example?
Provides iterators to iterate over the out-going edges of node u from graph g, e.g.:
typename graph_traits < Graph >::out_edge_iterator ei, ei_end;
for (boost::tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei) {
auto source = boost::source ( *ei, g );
auto target = boost::target ( *ei, g );
std::cout << "There is an edge from " << source << " to " << target << std::endl;
}
where Graph is your type definition of the graph an g is an instance of that. However, out_edges is only applicable for graphs with directed edges. The opposite of out_edges is in_edges that provides you iterators to compute in-coming edges of a node.
In an undirected graph both out_edges and in_edges will return all the edges connecting to the node in question.
However, more information can be easily found on http://www.boost.org/doc/libs/1_55_0/libs/graph/doc/graph_concepts.html or just in the Boost.Graph examples/tests.
As explained above, for a directed graph, out_edges accepts a "vertex_descriptor and the graph(adjacency list) to be examined" and returns "all the edges that emanate (directed from) the given vertex_descriptor", by means of an iterator-range.
As described in https://www.boost.org/doc/libs/1_69_0/libs/graph/doc/adjacency_list.html
std::pair<out_edge_iterator, out_edge_iterator>
out_edges(vertex_descriptor u, const adjacency_list& g)
Returns an iterator-range providing access to the out-edges of vertex
u in graph g. If the graph is undirected, this iterator-range provides
access to all edges incident on vertex u. For both directed and
undirected graphs, for an out-edge e, source(e, g) == u and target(e,
g) == v where v is a vertex adjacent to u.
In short, to answer some of your questions,
Yes, you can use it to find all edges from a node.
For undirected graphs, the behavior is as explained in the link above, it returns all the edges incident on the vertex (all edges connected to it)
I have a graph that is processed by neato and I'm adding new edges to it. However, I don't want that the old nodes to be moved. I want that neato computes the new positions of the new nodes and the old nodes stay in the same place.
strict graph {
node0 [pos="1,2"];
node1 [pos="2,3"];
}
and I add new edges:
strict graph {
node0 [pos="1,2"];
node1 [pos="2,3"];
node1 -- node2 [len="3"];
...
}
I want to get the same positions on the old nodes. For example:
strict graph {
node0 [pos="1,2"];
node1 [pos="2,3"];
node2 [pos="3,4"];
...
}
How can I do that?
You can pin a node's position by setting the node attribute pin=true.
Or put a '!' at the end of the pos attribute: pos="34,12!"
Running it with -n option should do the trick.
I would like these two nodes to appear on the same level:
digraph G {
subgraph cluster1 {
label="Local Datacenter";
router1;
host1;
}
subgraph cluster2 {
label="Remote Datacenter";
router2;
host2;
}
router1 -> router2;
router2 -> host2;
router1 -> host1;
}
I have tried using rank=same and rank=min, but they aren't giving me what I need.
Interestingly, if I set rankdir=LR and comment out the two router-to-host edges, it gives me exactly the look I want - but I would like to leave the edges intact.
You may use the newrank graph attribute (added in GraphViz 2.30) to activate the new ranking algorithm which allows defining rank=same for nodes which belong to clusters.
Add the following line at the top:
newrank=true;
Add the following line after the cluster definitions:
{ rank=same; router1; router2; }
Here's the resulting graph:
You may simply modify the edge between the routers:
router1 -> router2[constraint=false];
constraint indicates whether the edge should be used in the ranking of the nodes.
What is the difference between the terms edge and path in graph data structure?
An edge is something that connects two nodes. A path is a series of edges in sequence that defines a "path" from node A to node B.
http://en.wikipedia.org/wiki/Graph_(data_structure)
Edge: connects node one node to another. So there no nodes present between node A and B.
eg. A<-->B or A-->B or A<---B.
Path: Connects 1 or more nodes to each other. So path contains 1 or more edges.
eg. 1.) A---B---C : here path is ABC
2.)
A
/ \
B C
/
D
Here different paths are A-B-C and A-C.
Different edges are: A-B, B-C, A-C.
I hope this clears your doubt
Edge is a connection between two vertices of the graph.
Consider the graph a b
6---4----5
| | \ e
c | d| 1
| | / f
3----2
g
a,b,c,d,e represents the edges of the graphs where as a path can be path from a to g that can be a,b,d,g or a,c,g.
Edge is a point/dot ( maybe starting point, mid point, ending point).
Path is a line( sequence of point/dot makes a line).
A graph is two tuple G = (V, E), where:
V -> set of vertices (points/nodes or whatever you call it)
E -> set of edges (a line which connects any two vertices)
Such that: (v,u) belongs to E (set of edges) => v, u belongs to V (set of vertices).
Now, when we talk about paths: These are series of connected edges, which starts from a vertex and ends in another vertex.
Then you have several types of graphs : i.e. Connected/disconnected directed/undirected weighted/unweighted graphs.
Further reading : http://en.wikipedia.org/wiki/Graph_(mathematics)
Hope it helps!!
An edge connects two nodes and path is sequence of nodes and edges.