Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package customgraph;
- import java.util.LinkedList;
- //Contributed by Sumit Ghosh, GeeksForGeeks
- //Graph implementation : Undirected, unweighted, LinkedList
- public class Graph {
- private int V;
- private LinkedList<Integer> adjListArray[];
- private int[] vColors; // Stores the colors of the vertices
- // constructor
- Graph(int V)
- {
- this.V = V;
- // define the size of array as
- // number of vertices
- adjListArray = new LinkedList[V];
- // Create a new list for each vertex
- // such that adjacent nodes can be stored
- for(int i = 0; i < V ; i++){
- adjListArray[i] = new LinkedList<>();
- }
- }
- public int getV() {
- return V;
- }
- public LinkedList<Integer>[] getAdjListArray() {
- return adjListArray;
- }
- // Adds an edge to an undirected graph
- public void addEdge(Graph graph, int src, int dest)
- {
- // Add an edge from src to dest.
- graph.adjListArray[src].add(dest);
- // Since graph is undirected, add an edge from dest
- // to src also
- graph.adjListArray[dest].add(src);
- }
- // A utility function to print the adjacency list
- // representation of graph
- public void printGraph(Graph graph)
- {
- for(int v = 0; v < graph.V; v++)
- {
- System.out.println("Adjacency list of vertex "+ v);
- System.out.print("head");
- for(Integer pCrawl: graph.adjListArray[v]){
- System.out.print(" -> "+pCrawl);
- }
- System.out.println("\n");
- }
- }
- public void color(int vertex, int color) {
- vColors[vertex] = color;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement