Advertisement
Guest User

Untitled

a guest
May 25th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. # Matrix
  2. generateMatrix <- function(n){
  3.  
  4. mat <- matrix(sample(c(0,1), n*n, T), n, n)
  5.  
  6. for(i in 1:n){
  7. for(j in i:n){
  8. mat[i, j] <- mat[j,i]
  9. }
  10. }
  11.  
  12. diag(mat) <- 0
  13. return(mat)
  14. }
  15.  
  16.  
  17. # Reset colors
  18. resetColors <- function(n){
  19. colors <- c()
  20. for(i in 1:n){
  21. colors[i] <- "gray"
  22. }
  23.  
  24. return(colors)
  25. }
  26.  
  27. # Find edge number
  28. findEdgeNumber <- function(a, from, to){
  29.  
  30. minNum <- min(c(from, to))
  31. maxNum <- max(c(from, to))
  32.  
  33. count <- 0
  34.  
  35. for(i in 1:sqrt(length(a))){
  36. for(j in 1:sqrt(length(a))){
  37. if(a[i,j] == 1 && i <= j){
  38. count <- count+1
  39. }
  40.  
  41. if(i == minNum && j == maxNum){
  42. return(count)
  43. break
  44. }
  45. }
  46. }
  47. }
  48.  
  49. # Delete from arr
  50. deleteFromArr <- function(arr, value){
  51. if(length(arr) > 0){
  52. for(i in 1:length(arr)){
  53. if(arr[i] == value){
  54. arr <- arr[-i]
  55. return(arr)
  56. }
  57. }
  58. }
  59. }
  60.  
  61. # Draw graph
  62. drawGraph <- function(a, g, vertexColors, edgesColors){
  63.  
  64. igraph_options(
  65. vertex.color = vertexColors,
  66. vertex.size = 30,
  67. vertex.shape = "sphere",
  68. vertex.label.cex = 3,
  69. vertex.label.color = "white",
  70. vertex.label.family = "sans-serif",
  71.  
  72. edge.color = edgesColors,
  73. width = 3
  74. )
  75.  
  76. roots <- c(1)
  77. rootsLevel <- c(1)
  78.  
  79. for(i in (1:sqrt(length(a)))){
  80. roots[length(roots)+1] <- i
  81. rootsLevel[length(rootsLevel)+1] <- i
  82. }
  83.  
  84. plot(g, layout=layout_as_tree(g, root=roots, rootlevel = rootsLevel))
  85. }
  86.  
  87. deepStart <- function(n, searchFrom){
  88.  
  89. # Settings
  90. a <- generateMatrix(n)
  91. g <- graph.adjacency(a, mode="undirected")
  92. available <- 1:n
  93. available <- available[-searchFrom]
  94.  
  95. # Colors
  96. vertexColors <- resetColors(n)
  97. edgesColors <- resetColors(n*n)
  98.  
  99. print(a)
  100.  
  101.  
  102. saveHTML({
  103.  
  104. # Draw canvas
  105. drawGraph(a, g, vertexColors, edgesColors)
  106. vertexColors[searchFrom] = "green"
  107. drawGraph(a, g, vertexColors, edgesColors)
  108.  
  109. deepSearch(a, g, searchFrom, available, vertexColors, edgesColors)
  110. },
  111.  
  112. img.name = "eaGraph", imgdir = "eaGraph7", htmlfile = "eaGraph3.html",
  113. autobrowse = TRUE, autoplay = FALSE, title = "Ershov Andrew's Graph",
  114. description = "Deep search graph")
  115. }
  116.  
  117.  
  118. # Search
  119. deepSearch <- function(a, g, searchFrom, available, vertexColors, edgesColors){
  120.  
  121. if(length(available > 0)){
  122. for(i in 1:n){
  123. # There edge
  124. if( (a[searchFrom, i]) == 1){
  125. # Is free?
  126. check <- 0
  127. for(b in 1:length(available)){
  128. if(length(available) > 0){
  129. if(available[b] == i){
  130. check <- 1
  131. available <- available[-b]
  132.  
  133.  
  134. edgeNumber <- findEdgeNumber(a, searchFrom, i)
  135.  
  136. edgesColors[edgeNumber] = "green"
  137. vertexColors[i] = "green"
  138. vertexColors[searchFrom] = "red"
  139.  
  140. # Save animation
  141. drawGraph(a, g, vertexColors, edgesColors)
  142. vertexColors[i] = "red"
  143. edgesColors[edgeNumber] = "gray"
  144.  
  145. # Remove from list
  146. available <- deepSearch(a, g, i, available, vertexColors, edgesColors)
  147.  
  148. break
  149. }
  150. }
  151. }
  152. }
  153. }
  154. }
  155.  
  156. return(available)
  157.  
  158. }
  159.  
  160. # Code
  161. n <- 5
  162. searchFrom <- 1
  163.  
  164. deepStart(n, searchFrom)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement