labgraph.Rnw
- % NOTE
- ONLY EDIT THE .Rnw FILE!!! The .tex file is % likely to be overwritten. % \VignetteDepends{Biobase,graph,RBGL} % \VignetteIndexEntry{Introductory Graph Lab} % \VignetteKeywords{tutorial} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \documentclass[a4paper,11pt]{article} \usepackage{a4wide} \usepackage{times}
\newcommand{\Robject}[1]{\texttt{#1}} \newcommand{\Rpackage}[1]{\textit{#1}}
\begin{document} \title{Introductory Graph Lab} \author{Robert Gentleman, Wolfgang Huber, Vince Carey} \maketitle
<
\textbf{Note for MS Windows users:} The R package \Rpackage{Rgraphviz} is an interface to the graph layout program \textit{graphviz}. With this, you can directly visualize graphs from within R, for example using the \Robject{plot} method. Currently, \Rpackage{Rgraphviz} does not run under MS-Windows. However, \textit{graphviz} does. Here, we provide a simple function that uses files for a uni-directional communication from R to \textit{graphviz}.
<
%------------------------------------------------------------ \section{The \Rpackage{graph} package} %------------------------------------------------------------
First, we create a simple example graph:
<
and plot it:
<
We can find out about the nodes, edges, and node degrees of $\Robject{g}$:
<
The functions \Robject{adj} and \Robject{acc} provide the names of the \textit{adjacent} and \textit{accessible} nodes:
<
adj(g, c("b", "c")) acc(g, c("b", "c")) @
From the directed graph, we can construct the corresponding \textit{undirected graph}:
<
... and a \textit{subgraph}
<
... and the \textit{boundary} of the subgraph within the larger graph.
<
We can also define \textit{edge weights} on our graphs:
<
\textit{Graph manipulation} functions allow to add and remove edges:
<
## addEdge(from, to, graph, weights) g3 <- addEdge("e", "a", g1, pi/2)
## removeEdge(from, to, graph) g4 <- removeEdge("e", "a", g3)
identical(g4, g1)
## par(mfrow=c(2,1)) ## plot(ug) ## g5 <- combineNodes(c("b", "c"), ug, "w") ## plot(g5) @
\textit{Graph algebra}: complement, union, intersection
<
%------------------------------------------------------------ \section{The \Rpackage{RBGL} package} %------------------------------------------------------------
The \Rpackage{tsort} function calculates the \textit{topological sort order} for a directed acyclic graph. The topological sort order is defined as follows: if edge $(u,v)$ appears in the graph, then $u$ comes before $v$ in the ordering.
<
ts <- tsort(FileDep) nodes(FileDep)[ts+1] @
The function \Rpackage{mstree.kruskal} provides Kruskal's minimum spanning tree:
<
<
\textit{Breadth first} and \textit{depth first} search:
<
br <- bfs(dd, "r") nodes(dd)[br]
bs <- bfs(dd, "s") nodes(dd)[bs] @
<
df <- dfs(dd, "u") nodes(dd)[df$discovered] nodes(dd)[df$finish] @
<
sp.between(g, "E", "C") dijkstra.sp(g) @
<A
, C
, g)
g1 <- removeEdge(D
, E
, g1)
g1 <- removeEdge(B
, E
, g1)
g1 <- removeEdge(E
, B
, g1)
connectedComp(g)
connectedComp(g1)
par(mfrow=c(1,2)) plot(g, main="g") plot(g1, main="g1") @
<
strongComp(km) connectedComp(ugraph(km)) plot(km, main="km: connected components example graph") @
<
<red
attrs$node$height <- 1
attrs$node$label <-
myplot <- function(m)
plot(FileDep, m, main=m, attrs=attrs)
par(mfrow=c(1,3)) myplot("dot") myplot("neato") myplot("twopi") @
\end{document}