Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. package customgraph;
  2. import java.util.LinkedList;
  3. //Contributed by Sumit Ghosh, GeeksForGeeks
  4. //Graph implementation : Undirected, unweighted, LinkedList
  5. public class Graph {
  6. private int V;
  7. private LinkedList<Integer> adjListArray[];
  8. private int[] vColors; // Stores the colors of the vertices
  9.  
  10. // constructor
  11. Graph(int V)
  12. {
  13. this.V = V;
  14. // define the size of array as
  15. // number of vertices
  16. adjListArray = new LinkedList[V];
  17.  
  18. // Create a new list for each vertex
  19. // such that adjacent nodes can be stored
  20. for(int i = 0; i < V ; i++){
  21. adjListArray[i] = new LinkedList<>();
  22. }
  23. }
  24. public int getV() {
  25. return V;
  26. }
  27. public LinkedList<Integer>[] getAdjListArray() {
  28. return adjListArray;
  29. }
  30.  
  31.  
  32. // Adds an edge to an undirected graph
  33. public void addEdge(Graph graph, int src, int dest)
  34. {
  35. // Add an edge from src to dest.
  36. graph.adjListArray[src].add(dest);
  37.  
  38. // Since graph is undirected, add an edge from dest
  39. // to src also
  40. graph.adjListArray[dest].add(src);
  41. }
  42.  
  43. // A utility function to print the adjacency list
  44. // representation of graph
  45. public void printGraph(Graph graph)
  46. {
  47. for(int v = 0; v < graph.V; v++)
  48. {
  49. System.out.println("Adjacency list of vertex "+ v);
  50. System.out.print("head");
  51. for(Integer pCrawl: graph.adjListArray[v]){
  52. System.out.print(" -> "+pCrawl);
  53. }
  54. System.out.println("\n");
  55. }
  56. }
  57.  
  58. public void color(int vertex, int color) {
  59. vColors[vertex] = color;
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement