How do I merge two nodes into a single node using igraph

timothyjgraham

I am trying to merge two nodes (call them 'V' and 'U') in a graph (G) into a single node (V).

G is a hyperlink network of 779 nodes (websites). Each edge represents a hyperlink. V and U are actually the same website, but unfortunately the webpages from that website have become split into two separate nodes. So I want to put them back together into a single node.

I have researched the contract.vertices function, but I cannot understand how to adapt it here.

Here are the attributes of my graph (G).

> G
IGRAPH D--- 779 3544 -- 
+ attr: Image File (v/c), Ringset (v/n), Country Code TLD (v/n), Generic TLD (v/n), Number of Pages (v/n), Categorical 1 (v/n), Categorical 2 (v/n),
  Categorical 3 (v/n), id (v/c), label (v/c), Width (e/n)

I have two nodes that I want to merge together:

> V(g)$id[8]
[1] "http://www.police.uk/"

and

> V(g)$id[14]
[1] "http://police.uk/"

In total there are 779 nodes and 3544 edges in the graph.

I want these two nodes to become a single node in the graph (i.e. they will have the same "id"). All inlinks and outlinks from/to other nodes will now point only to this new single node.

All other attributes will remain the same, with the exception of Number of Pages (the value of this will be the sum of both the nodes before they are merged).

Tamás

contract.vertices is indeed the right function to try, but its API is a bit complicated since it is designed to be able to merge not only a single pair of nodes but also several pairs in a single pass. (It can also permute vertices). To this end, it requires a mapping from the old vertex IDs to the new ones.

In case you are unfamiliar with vertex IDs: igraph identifies each vertex of the graph with an integer in the range 1 to N where N is the number of vertices. The mapping that contract.vertices requires must be a list of length N where the i-th element of the list contains the new ID of the node corresponding to ID i before merging.

Suppose that your graph contains 10 nodes. The following mapping vector will simply map each node to the same ID that it already has, so it will not do any merging:

c(1,2,3,4,5,6,7,8,9,10)

Now, suppose that you want to merge node 7 into node 4. You have to tell igraph that the new ID of node 7 will be 4, so you have to change the 7th element in the above vector to 4:

c(1,2,3,4,5,6,4,8,9,10)

This will almost do the job; the problem is that igraph requires the node IDs to be in the range 1 to N and since you still have a node with ID 10 according to the above mapping, igraph will not delete the old node 7. You can either delete it manually with delete.vertices after you contracted the vertices, or you can specify a different mapping that not only merges node 7 into node 4 but also changes the ID of node 8 to 7, node 9 to 8 and node 10 to 9:

c(1,2,3,4,5,6,4,7,8,9)

Now, since you also want the Number of Pages attribute of the new node to be the sum of the values of the two old nodes, you must tell igraph what to do with the vertex attributes during the merge. The vertex.attr.comb parameter of contract.vertices serves this purpose. In your case, the value of vertex.attr.comb should be something like this:

list("Number of Pages"="sum", "first")

where "Number of Pages"="sum" means that the new value of the Number of Pages attribute should be calculated by summing the old attribute values, and "first" means that for all other attributes not mentioned here, the new value should be determined by the old value of the first node among the set of nodes that are merged into a single one. See ?attribute.combination in R for more details about the format of this argument.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I merge two nodes into a single node using igraph

From Dev

How do I add a node between two other nodes in a HBox?

From Dev

How do I merge these two regexs into a single one?

From Dev

how to visualize nodes using in igraph

From Dev

How do I merge two lists of XElements using Linq?

From Dev

How to merge two tables using single query

From Dev

How do I combine nodes with the same node type from different matched patterns into a single column or collection?

From Dev

Merge nodes of the same kind to a single node

From Dev

How do I select a node based on one of its edges in igraph?

From Java

How do I merge two dictionaries in a single expression in Python (taking union of dictionaries)?

From Dev

How do I merge two dictionaries in a single expression (taking union of dictionaries)?

From Dev

How do I merge two dictionaries in a single expression (taking union of dictionaries)?

From Dev

How to extract Neighbors of specific nodes using igraph?

From Dev

How do I merge two images into one?

From Dev

How do I merge two components in react

From Dev

How do I merge two rows in oracle

From Dev

How do I merge two components in react

From Dev

How do I merge two *.srt files

From Dev

How do I merge two icon sets?

From Dev

How can I Merge Two LINQ DataSources into a single table for a chart?

From Dev

How do I count all the other nodes related to a specific node

From Dev

How do I count all the other nodes related to a specific node

From Dev

In python pandas, how do i merge two dataframes while spreading values in one using weights of another?

From Dev

How do I merge elements from two XML files into one based on id using Nokogiri?

From Dev

How do I merge multiple Hashes into a single valid JSON file?

From Dev

How to merge two different node groups by the id attribute using XSLT

From Dev

How to merge two different node groups by the id attribute using XSLT

From Dev

How do i get data from two tables using single query in mysql

From Dev

How do I merge multiple, small PDF's into a single one using PDFBox without running out of heap space?

Related Related

  1. 1

    How do I merge two nodes into a single node using igraph

  2. 2

    How do I add a node between two other nodes in a HBox?

  3. 3

    How do I merge these two regexs into a single one?

  4. 4

    how to visualize nodes using in igraph

  5. 5

    How do I merge two lists of XElements using Linq?

  6. 6

    How to merge two tables using single query

  7. 7

    How do I combine nodes with the same node type from different matched patterns into a single column or collection?

  8. 8

    Merge nodes of the same kind to a single node

  9. 9

    How do I select a node based on one of its edges in igraph?

  10. 10

    How do I merge two dictionaries in a single expression in Python (taking union of dictionaries)?

  11. 11

    How do I merge two dictionaries in a single expression (taking union of dictionaries)?

  12. 12

    How do I merge two dictionaries in a single expression (taking union of dictionaries)?

  13. 13

    How to extract Neighbors of specific nodes using igraph?

  14. 14

    How do I merge two images into one?

  15. 15

    How do I merge two components in react

  16. 16

    How do I merge two rows in oracle

  17. 17

    How do I merge two components in react

  18. 18

    How do I merge two *.srt files

  19. 19

    How do I merge two icon sets?

  20. 20

    How can I Merge Two LINQ DataSources into a single table for a chart?

  21. 21

    How do I count all the other nodes related to a specific node

  22. 22

    How do I count all the other nodes related to a specific node

  23. 23

    In python pandas, how do i merge two dataframes while spreading values in one using weights of another?

  24. 24

    How do I merge elements from two XML files into one based on id using Nokogiri?

  25. 25

    How do I merge multiple Hashes into a single valid JSON file?

  26. 26

    How to merge two different node groups by the id attribute using XSLT

  27. 27

    How to merge two different node groups by the id attribute using XSLT

  28. 28

    How do i get data from two tables using single query in mysql

  29. 29

    How do I merge multiple, small PDF's into a single one using PDFBox without running out of heap space?

HotTag

Archive