Guest User

Untitled

a guest
Oct 16th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. //Patrick Cullom
  2. //4/23/10
  3. //Algorithms
  4.  
  5. import java.util.ArrayList;
  6. import java.util.Random;
  7.  
  8.  
  9. public class SmallWorldNetwork {
  10.  
  11. ArrayList<Integer> path = new ArrayList();
  12. int counter123 = 0;
  13. public SmallWorldNetwork(){
  14. int nodes = 256;
  15. int k = 8;
  16. int[][] graph = new int[nodes][nodes];
  17. graph = createGraph(nodes, k);
  18. graph = randomizeNeighbors(graph);
  19. System.out.println("C: " + 1 / (findC(graph, 0) / nodes));
  20. if(isConnected(graph))
  21. System.out.println("L: " + (findL(graph) / nodes));
  22. else
  23. System.out.println("Graph is unconnected. L not calculated.");
  24. // for(int i = 0; i < graph.length; i++){
  25. // for(int j = 0; j < graph.length; j++){
  26. // System.out.print(graph[i][j]);
  27. // }
  28. // System.out.println();
  29. // }
  30. }
  31.  
  32. public int[][] createGraph(int nodes, int k){
  33. int[][] graph = new int[nodes][nodes];
  34.  
  35. for(int i = 0; i < nodes; i++){
  36. for(int j = 1; j < k / 2 + 1; j++){
  37. if((i + j) >= nodes){
  38. graph[i][i + j - nodes] = 1;
  39. graph[i + j - nodes][i] = 1;
  40. } else {
  41. graph[i][i + j] = 1;
  42. graph[i + j][i] = 1;
  43. }
  44. }
  45. }
  46. return graph;
  47. }
  48.  
  49. public int[][] randomizeNeighbors(int[][] graph){
  50. Random random = new Random();
  51. int temp = 0;
  52. boolean noChange = true;
  53. for(int i = 0; i < graph.length; i++){
  54. for(int j = 0; j < graph.length; j++){
  55. if(random.nextDouble() < 1 && graph[i][j] == 1 && i != j){
  56. graph[i][j] = 0;
  57. graph[j][i] = 0;
  58. temp = random.nextInt(graph.length);
  59. noChange = true;
  60. while(noChange){
  61. temp = random.nextInt(graph.length);
  62. if(temp != i && graph[i][temp] != 1){
  63. graph[i][temp] = 1;
  64. graph[temp][i] = 1;
  65. noChange = false;
  66. }
  67. }
  68. }
  69. }
  70. }
  71. return graph;
  72. }
  73.  
  74. public int findL(int[][] graph){
  75. int count = 0;
  76. for(int i = 0; i < graph.length; i++){
  77. for(int j = 0; j < graph.length; j++){
  78. if(i != j)
  79. count += findShortestPath(graph, i, j);
  80. path.clear();
  81. }
  82. }
  83.  
  84. return count / graph.length;
  85. }
  86. public int findShortestPath(int graph[][], int node1, int node2){
  87. int shortest = 0;
  88. ArrayList<Integer> pathCount = new ArrayList();
  89.  
  90. if(graph[node1][node2] == 1){
  91. return 1;
  92. }
  93.  
  94. path.add(node1);
  95.  
  96. for(int i = 0; i < graph.length; i++){
  97. if(graph[node1][i] == 1 && !path.contains(i) && node1 != i){
  98. path.add(i);
  99. pathCount.add(findShortestPath(graph, i, node2));
  100. }
  101. }
  102. for(int i = 0; i < pathCount.size(); i++){
  103. if(i == 0){
  104. shortest = pathCount.get(i);
  105. } else if(pathCount.get(i) < shortest){
  106. shortest = pathCount.get(i);
  107. }
  108. }
  109. return 1 + shortest;
  110. }
  111.  
  112. public double findC(int[][] graph, int start){
  113. double total = 0;
  114. ArrayList<Integer> nodesToCheck = new ArrayList();
  115. if(start == graph.length)
  116. return 0;
  117. for(int i = 0; i < graph.length; i++){
  118. if(graph[start][i] == 1){
  119. nodesToCheck.add(i);
  120. }
  121. }
  122. for(int j = 0; j < nodesToCheck.size(); j++){
  123. for(int k = j + 1; k < nodesToCheck.size(); k++){
  124. if(graph[nodesToCheck.get(j)][nodesToCheck.get(k)] == 1){
  125. total++;
  126. }
  127. }
  128. }
  129. return (double) (nodesToCheck.size()*(nodesToCheck.size()-1) / 2 ) - total + findC(graph, ++start);
  130. }
  131.  
  132. public boolean isConnected(int[][] graph){
  133. ArrayList<Integer> visited = new ArrayList();
  134. ArrayList<Integer> neighbors = new ArrayList();
  135. visited.add(0);
  136. for(int i = 0; i < visited.size(); i++){
  137. neighbors = findNeighbors(graph, visited.get(i));
  138. for(int j = 0; j < neighbors.size(); j++){
  139. if(!visited.contains(neighbors.get(j))){
  140. visited.add(neighbors.get(j));
  141. }
  142. }
  143. }
  144. if(visited.size() == graph.length){
  145. return true;
  146. }
  147. return false;
  148. }
  149.  
  150. public ArrayList<Integer> findNeighbors(int[][] graph, int node){
  151. ArrayList<Integer> neighbors = new ArrayList();
  152.  
  153. for(int i = 0; i < graph.length; i++){
  154. if(graph[node][i] == 1){
  155. neighbors.add(i);
  156. }
  157. }
  158. return neighbors;
  159. }
  160.  
  161. public static void main(String[] args){
  162. new SmallWorldNetwork();
  163. }
  164. }
Add Comment
Please, Sign In to add comment