Advertisement
Guest User

Untitled

a guest
May 19th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.LinkedList;
  3. import java.util.Queue;
  4. import java.util.Scanner;
  5. import java.util.Stack;
  6.  
  7. public class JavaApplication14 {
  8. /*
  9. 9 11
  10. 0 1
  11. 0 8
  12. 1 5
  13. 2 3
  14. 2 6
  15. 2 7
  16. 4 5
  17. 4 8
  18. 5 6
  19. 5 8
  20. 6 7
  21.  
  22. 4 4
  23. 0 2
  24. 1 2
  25. 1 3
  26. 2 3
  27. */
  28.  
  29. public static void main(String[] args) {
  30. Scanner sc=new Scanner(System.in);
  31.  
  32. int V=sc.nextInt();
  33. int E=sc.nextInt();
  34. //Declare a graph.
  35. int counter=0;
  36. int G[][]=new int[V][V];
  37. //Connect the edges.
  38. // G[0][2]=1; G[2][0]=1;
  39. // G[1][2]=1; G[2][1]=1;
  40. // G[1][3]=1; G[3][1]=1;
  41. // G[2][3]=1; G[3][2]=1;
  42. while(E-->0){
  43. int src=sc.nextInt();
  44. int dest=sc.nextInt();
  45. G[src][dest]=1;
  46. G[dest][src]=1;
  47. }
  48. //Print Adjacency Matrix
  49. // System.out.println(Arrays.deepToString(G));
  50. //degree(G,V);
  51. display(G,V);
  52. // System.out.println("DFS: ");
  53. // DFS(G,V,0);
  54.  
  55.  
  56. }
  57. public static void BFS(int[][] adj,int V, int begin){
  58. Queue<Integer> Q=new LinkedList<>();
  59. boolean[] visited=new boolean[V];
  60. int num=V;
  61. visited[begin]=true;
  62. Q.add(begin);
  63. while(!Q.isEmpty()){
  64. int element=Q.remove();
  65. System.out.print(element+" ");
  66. int temp=0;
  67. while(temp<num){
  68. if((!visited[temp])&&(adj[element][temp]==1)){
  69. Q.add(temp);
  70. visited[temp]=true;
  71. }
  72. temp++;
  73. }
  74. }
  75. }
  76. public static void DFS(int[][] adj,int V, int begin){
  77. Stack<Integer> Q=new Stack<>();
  78. boolean[] visited=new boolean[V];
  79. int num=V;
  80. visited[begin]=true;
  81. Q.push(begin);
  82. while(!Q.isEmpty()){
  83. int element=Q.pop();
  84. System.out.print(element+" ");
  85. int temp=0;
  86. while(temp<num){
  87. if((!visited[temp])&&(adj[element][temp]==1)){
  88. Q.push(temp);
  89. visited[temp]=true;
  90. }
  91. temp++;
  92. }
  93. }
  94.  
  95. }
  96. public static void degree(int g[][],int V){
  97. int counter=0;
  98. for (int row = 0; row < V; row++) {
  99. for (int col = 0; col < V; col++) {
  100. if(g[row][col]==1){
  101. counter++;
  102. }
  103.  
  104.  
  105.  
  106. }
  107. System.out.print(row+" "+counter);
  108. counter=0;
  109. System.out.println();
  110. }
  111. }
  112. public static void display(int g[][],int V){
  113. for (int row = 0; row < V; row++) {
  114. for (int col = 0; col < V; col++) {
  115. System.out.print(g[row][col]+" ");
  116.  
  117.  
  118.  
  119. }
  120.  
  121. System.out.println();
  122. }
  123.  
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement