Graph_generic.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Java program to implement Graph
  2. // with the help of Generics
  3. import java.util.*;
  4. class Graph<T> {
  5. // We use Hashmap to store the edges in the graph
  6. private Map<T, List<T> > map = new HashMap<>();
  7. // This function adds a new vertex to the graph
  8. public void addVertex(T s)
  9. {
  10. map.put(s, new LinkedList<T>());
  11. }
  12. // This function adds the edge
  13. // between source to destination
  14. public void addEdge(T source, T destination,
  15. boolean bidirectional)
  16. {
  17. if (!map.containsKey(source))
  18. addVertex(source);
  19. if (!map.containsKey(destination))
  20. addVertex(destination);
  21. map.get(source).add(destination);
  22. if (bidirectional == true) {
  23. map.get(destination).add(source);
  24. }
  25. }
  26. // This function gives the count of vertices
  27. public void getVertexCount()
  28. {
  29. System.out.println("The graph has "
  30. + map.keySet().size()
  31. + " vertex");
  32. }
  33. // This function gives the count of edges
  34. public void getEdgesCount(boolean bidirection)
  35. {
  36. int count = 0;
  37. for (T v : map.keySet()) {
  38. count += map.get(v).size();
  39. }
  40. if (bidirection == true) {
  41. count = count / 2;
  42. }
  43. System.out.println("The graph has " + count
  44. + " edges.");
  45. }
  46. // This function gives whether
  47. // a vertex is present or not.
  48. public void hasVertex(T s)
  49. {
  50. if (map.containsKey(s)) {
  51. System.out.println("The graph contains " + s
  52. + " as a vertex.");
  53. }
  54. else {
  55. System.out.println("The graph does not contain "
  56. + s + " as a vertex.");
  57. }
  58. }
  59. // This function gives whether an edge is present or
  60. // not.
  61. public void hasEdge(T s, T d)
  62. {
  63. if (map.get(s).contains(d)) {
  64. System.out.println(
  65. "The graph has an edge between " + s
  66. + " and " + d + ".");
  67. }
  68. else {
  69. System.out.println(
  70. "The graph has no edge between " + s
  71. + " and " + d + ".");
  72. }
  73. }
  74. public void neighbours(T s)
  75. {
  76. if(!map.containsKey(s))
  77. return ;
  78. System.out.println("The neighbours of "+s+" are");
  79. for(T w:map.get(s))
  80. System.out.print(w+",");
  81. }
  82. // Prints the adjancency list of each vertex.
  83. @Override public String toString()
  84. {
  85. StringBuilder builder = new StringBuilder();
  86. for (T v : map.keySet()) {
  87. builder.append(v.toString() + ": ");
  88. for (T w : map.get(v)) {
  89. builder.append(w.toString() + " ");
  90. }
  91. builder.append("\n");
  92. }
  93. return (builder.toString());
  94. }
  95. }
  96. // Driver Code
  97. public class Graph_generic {
  98. public static void main(String args[])
  99. {
  100. // Object of graph is created.
  101. Graph<Integer> g = new Graph<Integer>();
  102. // edges are added.
  103. // Since the graph is bidirectional,
  104. // so boolean bidirectional is passed as true.
  105. g.addEdge(0, 1, true);
  106. g.addEdge(0, 4, true);
  107. g.addEdge(1, 2, true);
  108. g.addEdge(1, 3, true);
  109. g.addEdge(1, 4, true);
  110. g.addEdge(2, 3, true);
  111. g.addEdge(3, 4, true);
  112. // Printing the graph
  113. System.out.println("Graph:\n" + g.toString());
  114. // Gives the no of vertices in the graph.
  115. g.getVertexCount();
  116. // Gives the no of edges in the graph.
  117. g.getEdgesCount(true);
  118. // Tells whether the edge is present or not.
  119. g.hasEdge(3, 4);
  120. // Tells whether vertex is present or not
  121. g.hasVertex(5);
  122. g.neighbours(1);
  123. }
  124. }