Guest User

Untitled

a guest
May 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Graph{
  4. LinkedList<Integer>[] adjList;
  5. int visited[];
  6. public Graph(int nodes){
  7. adjList = new LinkedList[nodes];
  8. visited = new int[this.adjList.length];
  9. for(int i = 0; i < nodes; i++){
  10. adjList[i] = new LinkedList<>();
  11. }
  12. }
  13.  
  14. public void addNode(int src, int dest){
  15. adjList[src].addFirst(dest);
  16. // adjList[dest].addFirst(src); //-- uncomment this for undirected graph.
  17. }
  18.  
  19. public static void printG(Graph g1){
  20.  
  21. for(int i = 0; i < g1.adjList.length; i++){
  22. System.out.print("Node :"+i);
  23. for(Integer j : g1.adjList[i]){
  24. System.out.print(" -> "+j);
  25. }
  26. System.out.println();
  27. }
  28. }
  29.  
  30. // this function will not work in disconnected Graphs
  31. public void bfs(int start){
  32.  
  33. Queue<Integer> q = new LinkedList<>();
  34.  
  35. visited[start] = 1;
  36. q.add(start);
  37.  
  38. while(!q.isEmpty()){
  39. int item = q.poll();
  40. System.out.println(item + " ");
  41.  
  42. Iterator<Integer> i = this.adjList[item].listIterator();
  43. while(i.hasNext()){
  44. int temp = i.next();
  45. if (visited[temp] != 1) {
  46. visited[temp] = 1;
  47. q.add(temp);
  48. }
  49. }
  50. }
  51. }
  52.  
  53. // function for BFS in disconnected graph:
  54. public void bfsDisc(int start){
  55. bfs(start);
  56. for (int i = 0; i < this.visited.length; i++) {
  57. if (this.visited[i] == 0) {
  58. this.bfs(i);
  59. }
  60. }
  61. }
  62.  
  63. // function for DFS in Graph
  64. public void dfs(int start) {
  65. Stack <Integer>st = new Stack<Integer>();
  66. visited[start] = 1;
  67. st.push(start);
  68.  
  69. while(!st.isEmpty()){
  70. int item = st.pop();
  71. System.out.println(item);
  72.  
  73. Iterator it = this.adjList[item].iterator();
  74. while(it.hasNext()){
  75. int i = (Integer)it.next();
  76. if(this.visited[i] != 1){
  77. this.visited[i] = 1;
  78. st.push(i);
  79. }
  80. }
  81. }
  82. }
  83.  
  84. public void dfsDisc(int start) {
  85. dfs(start);
  86. for (int i = 0; i < this.visited.length; i++) {
  87. if (this.visited[i] == 0) {
  88. this.dfs(i);
  89. }
  90. }
  91. }
  92.  
  93. // ----------------------- Main --------------
  94. public static void main(String[] args) {
  95.  
  96. // Graph must have '0' node.
  97.  
  98. Graph g = new Graph(6);
  99. g.addNode(0, 2);
  100. g.addNode(0, 5);
  101. g.addNode(1, 2);
  102. g.addNode(1, 4);
  103. g.addNode(2, 0);
  104. g.addNode(2, 1);
  105. g.addNode(2, 4);
  106. g.addNode(2, 5);
  107. g.addNode(3, 2);
  108. g.addNode(3, 4);
  109. g.addNode(4, 1);
  110. g.addNode(4, 2);
  111. g.addNode(4, 3);
  112. g.addNode(4, 5);
  113. g.addNode(5, 4);
  114. g.addNode(5, 0);
  115.  
  116. printG(g);
  117. System.out.println("DFS");
  118. g.dfsDisc(0);
  119.  
  120. Graph g1 = new Graph(7);
  121. g1.addNode(0, 1);
  122. g1.addNode(1, 2);
  123. g1.addNode(1, 3);
  124. g1.addNode(2, 4);
  125. g1.addNode(3, 5);
  126. g1.addNode(3, 6);
  127.  
  128. // printG(g1);
  129. // System.out.println("BFS");
  130. // g.bfsDisc(1);
  131.  
  132. // System.out.println("DFS");
  133. // g1.dfsDisc(1);
  134.  
  135. }
  136. }
Add Comment
Please, Sign In to add comment