<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://visone.ethz.ch/wiki/index.php?action=history&amp;feed=atom&amp;title=Graph_difference_%28tutorial%29</id>
	<title>Graph difference (tutorial) - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://visone.ethz.ch/wiki/index.php?action=history&amp;feed=atom&amp;title=Graph_difference_%28tutorial%29"/>
	<link rel="alternate" type="text/html" href="https://visone.ethz.ch/wiki/index.php?title=Graph_difference_(tutorial)&amp;action=history"/>
	<updated>2026-10-15T20:20:18Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.3</generator>
	<entry>
		<id>https://visone.ethz.ch/wiki/index.php?title=Graph_difference_(tutorial)&amp;diff=1294&amp;oldid=prev</id>
		<title>Nagelu: Created page with &quot;This tutorial shows, how the R console can be used to manipulate and compare graphs beyond the functionality that visone provides.  == Scenario ==  We&#039;ll assume in the following,...&quot;</title>
		<link rel="alternate" type="text/html" href="https://visone.ethz.ch/wiki/index.php?title=Graph_difference_(tutorial)&amp;diff=1294&amp;oldid=prev"/>
		<updated>2012-08-31T12:30:40Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;This tutorial shows, how the R console can be used to manipulate and compare graphs beyond the functionality that visone provides.  == Scenario ==  We&amp;#039;ll assume in the following,...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;This tutorial shows, how the R console can be used to manipulate and compare graphs beyond the functionality that visone provides.&lt;br /&gt;
&lt;br /&gt;
== Scenario ==&lt;br /&gt;
&lt;br /&gt;
We&amp;#039;ll assume in the following, that you have two networks on the same set of nodes and want to find out which of their edges differ.&lt;br /&gt;
An important precondition is, that there exist an identifying attribute that:&lt;br /&gt;
* gives every node a value&lt;br /&gt;
* has the same value for each node in both networks&lt;br /&gt;
* has within one network a different value for every node.&lt;br /&gt;
&lt;br /&gt;
This attribute will be used in the following to establish the relation between the two networks.&lt;br /&gt;
For this tutorial we assume it is called &amp;#039;&amp;#039;&amp;#039;nodeid&amp;#039;&amp;#039;&amp;#039; and is of type &amp;#039;&amp;#039;&amp;#039;text&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Creating the difference network ==&lt;br /&gt;
&lt;br /&gt;
Select the tab containing your first network and open the R console.&lt;br /&gt;
Give your network a name in the input field next to the send button and press &amp;quot;send&amp;quot;.&lt;br /&gt;
For this tutorial we&amp;#039;ll assume that it is named &amp;#039;&amp;#039;&amp;#039;g1&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Now the command &amp;#039;&amp;#039;&amp;#039;ls()&amp;#039;&amp;#039;&amp;#039; should list &amp;#039;&amp;#039;&amp;#039;g1&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;ls()&amp;#039;&amp;#039;&amp;#039; can always be used to list all objects in the current R environment.&lt;br /&gt;
&lt;br /&gt;
Send your second network to the R console: select it in the main window, name it (&amp;#039;&amp;#039;&amp;#039;g2&amp;#039;&amp;#039;&amp;#039; in this tutorial), and press &amp;quot;send&amp;quot;.&lt;br /&gt;
The command &amp;#039;&amp;#039;&amp;#039;ls()&amp;#039;&amp;#039;&amp;#039; should now show the two networks.&lt;br /&gt;
&lt;br /&gt;
The idea of this tutorial is, to create the difference network by subtraction of the networks adjacency matrices.&lt;br /&gt;
Therefore, it is first of all necessary, to get those in the correct node order.&lt;br /&gt;
We&amp;#039;ll achieve this by deriving a node order from the attribute identifying the nodes (&amp;#039;&amp;#039;&amp;#039;nodeid&amp;#039;&amp;#039;&amp;#039;).&lt;br /&gt;
&lt;br /&gt;
The order of the nodes in &amp;#039;&amp;#039;&amp;#039;g1&amp;#039;&amp;#039;&amp;#039; is derived by: &amp;#039;&amp;#039;&amp;#039;order1&amp;lt;-sort(V(g1)$nodeid,index.return=T)$ix&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
This command combines a number of things, let&amp;#039;s visit them in detail:&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;V(g1)&amp;#039;&amp;#039;&amp;#039; yields the nodes (vertices) of g1&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;V(g1)$nodeid&amp;#039;&amp;#039;&amp;#039; then provides the list of &amp;#039;&amp;#039;&amp;#039;nodeid&amp;#039;&amp;#039;&amp;#039; attribute values for those nodes, i.e. gives a list of node ids in the order of the nodes in the graph&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;sort()&amp;#039;&amp;#039;&amp;#039; does what it says, it sorts. &lt;br /&gt;
** As first parameter we provide the list of values to sort (&amp;#039;&amp;#039;&amp;#039;V(g1)$nodeid&amp;#039;&amp;#039;&amp;#039;, the node ids).&lt;br /&gt;
** Since we are not interested in the sorted list but in their order, we tell &amp;#039;&amp;#039;&amp;#039;sort()&amp;#039;&amp;#039;&amp;#039; to return this order with the parameter &amp;#039;&amp;#039;&amp;#039;index.return=T&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
** Sort will then return a list with two fields &amp;#039;&amp;#039;&amp;#039;x&amp;#039;&amp;#039;&amp;#039; our sorted list and &amp;#039;&amp;#039;&amp;#039;ix&amp;#039;&amp;#039;&amp;#039; the indices of the original list in sorted order. Of those we only need the second and consequently store that in &amp;#039;&amp;#039;&amp;#039;order1&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
We do the same for the second network with &amp;#039;&amp;#039;&amp;#039;order2&amp;lt;-sort(V(g2)$nodeid,index.return=T)$ix&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
The two lists &amp;#039;&amp;#039;&amp;#039;order1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;order2&amp;#039;&amp;#039;&amp;#039; can now be used as ordering permution on the node sets of the two networks, i.e. they put the nodes of the two networks in the same order.&lt;br /&gt;
As an example &amp;#039;&amp;#039;&amp;#039;V(g1)$nodeid[order1]&amp;#039;&amp;#039;&amp;#039; lists the node ids in the just established order and should be exactly the same as &amp;#039;&amp;#039;&amp;#039;V(g2)$nodeid[order2]&amp;#039;&amp;#039;&amp;#039;, i.e. the node ids of the second network in correct order.&lt;br /&gt;
&lt;br /&gt;
To get the adjacency matrix of a network we would use the command &amp;#039;&amp;#039;&amp;#039;a1&amp;lt;-get.adjacency(g1)&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Here we want these matrices in our node order and thus reorder them accordingly by &amp;#039;&amp;#039;&amp;#039;a1&amp;lt;-get.adjacency(g1)[order1,order1]&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
The &amp;quot;&amp;#039;&amp;#039;&amp;#039;[order1,order1]&amp;#039;&amp;#039;&amp;#039;&amp;quot;-part just reorders rows and columns of the matrix.&lt;br /&gt;
Use the two commands &amp;#039;&amp;#039;&amp;#039;a1&amp;lt;-get.adjacency(g1)[order1,order1]&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;a2&amp;lt;-get.adjacency(g2)[order2,order2]&amp;#039;&amp;#039;&amp;#039; to create &amp;#039;&amp;#039;&amp;#039;a1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;a2&amp;#039;&amp;#039;&amp;#039;, the two adjacency matrices of your networks in correct node order.&lt;br /&gt;
&lt;br /&gt;
The difference between those can then easily be derived as the entrywise matrix difference:&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;diff&amp;lt;-a2-a1&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;diff&amp;#039;&amp;#039;&amp;#039; is the matrix with entry &amp;#039;&amp;#039;&amp;#039;-1&amp;#039;&amp;#039;&amp;#039; for an edge that was deleted, &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; for an added edge and &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; if the edge did not change.&lt;br /&gt;
&lt;br /&gt;
Now we can create the network with only the changed edges: &amp;#039;&amp;#039;&amp;#039;diffnetwork&amp;lt;-graph.adjacency(diff, weighted=&amp;quot;change&amp;quot;)&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
The function &amp;#039;&amp;#039;&amp;#039;graph.adjacency()&amp;#039;&amp;#039;&amp;#039; creates a graph from an adjacency matrix with an edge for every entry that does not equal zero. In that the parameter &amp;#039;&amp;#039;&amp;#039;weighted=&amp;quot;change&amp;quot;&amp;#039;&amp;#039;&amp;#039; stores the corresponding values in an edge attribute with the name &amp;quot;change&amp;quot;.&lt;br /&gt;
Before we load that back into visone we want to reattach the node ids with: &amp;#039;&amp;#039;&amp;#039;V(diffnetwork)$nodeid&amp;lt;-V(g1)$nodeid[order1]&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Finally, you can select the newly created network &amp;quot;diffnetwork&amp;quot; in the combobox of the dialog (next to the &amp;quot;load network&amp;quot; button) and load it into visone using the &amp;quot;load network&amp;quot; button.&lt;br /&gt;
&lt;br /&gt;
== Visualizing the difference ==&lt;br /&gt;
Close the console and you should see your network of changed edges.&lt;br /&gt;
Assuming, that your networks where undirected, every edge will be shown as to opposing, directed edges.&lt;br /&gt;
Use the &amp;quot;transformation&amp;quot;-tab and the &amp;quot;links&amp;quot; and &amp;quot;merge&amp;quot; with the option &amp;quot;contrary directed&amp;quot; to merge those into undirected edges.&lt;br /&gt;
&lt;br /&gt;
Back to the undirected graph we do know, which edges changed but not, what happened to them.&lt;br /&gt;
Using the &amp;quot;change&amp;quot; attribute created in R we can easily visualize what happened:&lt;br /&gt;
* Select the &amp;quot;visualization&amp;quot; tab.&lt;br /&gt;
* Go to category &amp;quot;mapping&amp;quot;, type &amp;quot;color&amp;quot; and property &amp;quot;link color&amp;quot;.&lt;br /&gt;
* As attribute choose &amp;quot;change&amp;quot; and method &amp;quot;color table&amp;quot;.&lt;br /&gt;
* Now you can select the color for &amp;#039;&amp;#039;&amp;#039;-1&amp;#039;&amp;#039;&amp;#039;, i.e. edges that were deleted and &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; (the added edges). For this example we will use red and blue.&lt;br /&gt;
* Use the &amp;quot;visualize&amp;quot; button to apply your color setting and you should get a graph with a red edge for every edge that was present in the first network but not in the second and a blue edge for every edge that is present in the second, but not in the first.&lt;/div&gt;</summary>
		<author><name>Nagelu</name></author>
	</entry>
</feed>