code_junkie

Graph Algorithm To Find All Connections Between Two Arbitrary Vertices

Nov 10th, 2011
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.LinkedHashSet;
  3. import java.util.LinkedList;
  4. import java.util.Map;
  5. import java.util.Set;
  6.  
  7. public class Graph {
  8. private Map<String, LinkedHashSet<String>> map = new HashMap();
  9.  
  10. public void addEdge(String node1, String node2) {
  11. LinkedHashSet<String> adjacent = map.get(node1);
  12. if(adjacent==null) {
  13. adjacent = new LinkedHashSet();
  14. map.put(node1, adjacent);
  15. }
  16. adjacent.add(node2);
  17. }
  18.  
  19. public void addTwoWayVertex(String node1, String node2) {
  20. addEdge(node1, node2);
  21. addEdge(node2, node1);
  22. }
  23.  
  24. public boolean isConnected(String node1, String node2) {
  25. Set adjacent = map.get(node1);
  26. if(adjacent==null) {
  27. return false;
  28. }
  29. return adjacent.contains(node2);
  30. }
  31.  
  32. public LinkedList<String> adjacentNodes(String last) {
  33. LinkedHashSet<String> adjacent = map.get(last);
  34. if(adjacent==null) {
  35. return new LinkedList();
  36. }
  37. return new LinkedList<String>(adjacent);
  38. }
  39. }
  40.  
  41. import java.util.HashMap;
  42. import java.util.LinkedHashSet;
  43. import java.util.LinkedList;
  44. import java.util.Map;
  45. import java.util.Set;
  46.  
  47. public class Graph {
  48. private Map<String, LinkedHashSet<String>> map = new HashMap();
  49.  
  50. public void addEdge(String node1, String node2) {
  51. LinkedHashSet<String> adjacent = map.get(node1);
  52. if(adjacent==null) {
  53. adjacent = new LinkedHashSet();
  54. map.put(node1, adjacent);
  55. }
  56. adjacent.add(node2);
  57. }
  58.  
  59. public void addTwoWayVertex(String node1, String node2) {
  60. addEdge(node1, node2);
  61. addEdge(node2, node1);
  62. }
  63.  
  64. public boolean isConnected(String node1, String node2) {
  65. Set adjacent = map.get(node1);
  66. if(adjacent==null) {
  67. return false;
  68. }
  69. return adjacent.contains(node2);
  70. }
  71.  
  72. public LinkedList<String> adjacentNodes(String last) {
  73. LinkedHashSet<String> adjacent = map.get(last);
  74. if(adjacent==null) {
  75. return new LinkedList();
  76. }
  77. return new LinkedList<String>(adjacent);
  78. }
  79. }
  80.  
  81. (A,B)
  82.  
  83. Definition
  84.  
  85. Atomic Paths A -> B
  86.  
  87. Freeing the list
  88.  
  89. Atomic Cycle A
  90.  
  91. static bool[] visited;//all false
  92. Stack<int> currentway; initialize empty
  93.  
  94. function findnodes(int nextnode)
  95. {
  96. if (nextnode==destnode)
  97. {
  98. print currentway
  99. return;
  100. }
  101. visited[nextnode]=true;
  102. Push nextnode to the end of currentway.
  103. for each node n accesible from nextnode:
  104. findnodes(n);
  105. visited[nextnode]=false;
  106. pop from currenteay
  107. }
Add Comment
Please, Sign In to add comment