Advertisement
Guest User

Untitled

a guest
Aug 28th, 2015
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. require(igraph)
  2.  
  3. getWeight = function(A, B, graphA, graphB) {
  4. sim <- graph.intersection(graphA, graphB)
  5. adj <- get.adjacency(sim, type="both")
  6. return (adj[A,B]);
  7. }
  8.  
  9. getSimilarity <- function(graphA, graphB) {
  10. # Create a new graph, where each node represents a possible pairing of:
  11. # --a node from graph A (x)
  12. # --to a node from graph B (y)
  13. ig <- graph(n=0, edges=NULL, directed=FALSE)
  14. thresh <- 0.2
  15. for (i in 1:length(V(graphA))){
  16. for (j in 1:length(V(graphB))) {
  17. # If two nodes (x,y) are dissimilar (use association indices), remove the node in the new graph.
  18. # If similar, give node in new graph weight between [0,1]
  19. w <- getWeight(V(graphA)[i], V(graphB)[j], graphA, graphB)
  20. if (w>thresh) {
  21. ig <- add.vertices(ig, 1, name=sprintf("A:%s<->B:%s", V(graphA)[i], V(graphB)[j]), weight=w)
  22. }
  23. }
  24. }
  25. # Connect nodes in new graph if they do not contradict each other. (x,y) and (x,t) contradict each other
  26. first <- list()
  27. second <- list()
  28. for (i in 1:length(V(ig)$name)) {
  29. item <- strsplit(V(ig)$name[i], '<->')
  30. first[i] <- item[1]
  31. second[i] <- item[2]
  32. }
  33. for (i in 1:length(first)) {
  34. for (j in 1:length(second)) {
  35. if (??INSERT CONTRADICTION CHECK HERE!!) {
  36. ig <- add.edges(ig, c(first, second))
  37. }
  38. }
  39. }
  40.  
  41. # Find a clique in the new graph which has the maximum amount of nodes, or
  42. # The clique which has the maximum node weights (sum)
  43. cliq.ig <- maximal.cliques(ig)
  44. maxNodes <- max(sapply(cliq.ig, length);
  45.  
  46. # Similarity between graphs A and B final calculation
  47. # = size of maximum clique OR total weight of maximum clique
  48. # DIVIDED BY
  49. # average clique sizes of A and B OR average weights of A and B
  50. mnCliqSize <- 0
  51. for (i in 1:length(cliquesA)) {
  52. mnCliqSize <- mnCliqSize + length(cliquesA[[i]])
  53. }
  54. for (i in 1:length(cliquesB)) {
  55. mnCliqSize <- mnCliqSize + length(cliquesB[[i]])
  56. }
  57. mnCliqSize <- mnCliqSize / (length(cliquesA)+length(cliquesB))
  58. return (maxNodes/mnCliqSize);
  59. }
  60.  
  61. ig <- graph(n=0, edges=NULL, directed=FALSE)
  62. ig <- add.vertices(ig, 10, name=sprintf("%d", 1:10))
  63. ig <- add.edges(ig, c(1,3, 3,5, 5,7, 7,9, 2,4, 4,6, 6,8, 8,10))
  64. ig2 <- graph(n=0, edges=NULL, directed=FALSE)
  65. ig2 <- add.vertices(ig2, 2, name=sprintf("%d", seq(1,3,2)))
  66. ig2 <- add.edges(ig2, c(1,2))
  67. test <- getSimilarity(ig, ig2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement