|
| 1 | +package DataAndAlgoL.Chpt9GraphAlgorithms; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +import javax.xml.transform.Source; |
| 5 | + |
| 6 | +public class Graph { |
| 7 | + /*//specifying a string that represents a new line character |
| 8 | + Retrieves the value of the line separator property of the system, which is the character sequence used to separate lines in text files |
| 9 | + */ |
| 10 | + private static final String NEWLINE= System.getProperty("line.separator"); |
| 11 | + |
| 12 | + private final int V; // creates vertex number |
| 13 | + private int E; //creates number of edges |
| 14 | + private boolean[][] adjMatrix; //creates a matrix of boolean values for graph and dfs algotrithm |
| 15 | + boolean[] visited; //creates array to store whether vertices where visited previously |
| 16 | + |
| 17 | + //Empty graph with V vertices |
| 18 | + public Graph(int V){ |
| 19 | + if(V < 0) throw new IllegalArgumentException("Too few vertices"); // checks for error as V cannot be < 0 |
| 20 | + this.V= V; |
| 21 | + this.E=0; |
| 22 | + this.adjMatrix= new boolean[V][V]; //creates matrix with number of vertices by number of vertices |
| 23 | + visited= new boolean[V]; //creates an array with size V (# of vertices) |
| 24 | + } |
| 25 | + |
| 26 | + //random graph with V vertices and E edges |
| 27 | + public Graph(int V, int E){ |
| 28 | + this(V); |
| 29 | + if(E > (long) V*(V-1)/2+V) throw new IllegalArgumentException("Too many edges"); |
| 30 | + if(E<0) throw new IllegalArgumentException("Too few eges"); |
| 31 | + Random random= new Random(); |
| 32 | + |
| 33 | + //can be inefficient |
| 34 | + while(this.E != E){ |
| 35 | + int u= random.nextInt(V); |
| 36 | + int v= random.nextInt(V); |
| 37 | + addEdge(u,v); // creates edge between adjacent vertices |
| 38 | + } |
| 39 | + |
| 40 | + visited= new boolean[V]; |
| 41 | + } |
| 42 | + |
| 43 | + //number of vertices and edges |
| 44 | + public int V(){ |
| 45 | + return V; |
| 46 | + } |
| 47 | + |
| 48 | + public int E(){ |
| 49 | + return E; |
| 50 | + } |
| 51 | + |
| 52 | + //adding undirected edge u-v |
| 53 | + public void addEdge(int u, int v){ |
| 54 | + if(!adjMatrix[u][v]){ |
| 55 | + E++; |
| 56 | + } |
| 57 | + adjMatrix[u][v]= true; //undirected edge from u-v |
| 58 | + adjMatrix[v][u]=true; //undirected edge from v-u |
| 59 | + } |
| 60 | + |
| 61 | + //does graph contain the edge u-v |
| 62 | + public boolean contains(int u, int v){ |
| 63 | + return adjMatrix[u][v]; |
| 64 | + } |
| 65 | + |
| 66 | + //return list of neighbors of u |
| 67 | + public Iterable<Integer> adjMatrix(int u){ |
| 68 | + return new AdjIterator(u); |
| 69 | + } |
| 70 | + |
| 71 | + //support iteration over graph vertices |
| 72 | + private class AdjIterator implements Iterator<Integer>, Iterable<Integer>{ |
| 73 | + private int u; |
| 74 | + private int v=0; |
| 75 | + |
| 76 | + AdjIterator(int u){ |
| 77 | + this.u=u; |
| 78 | + } |
| 79 | + |
| 80 | + public Iterator<Integer> iterator() { |
| 81 | + return this; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public boolean hasNext() { |
| 86 | + while(v < V){ |
| 87 | + if(adjMatrix[u][v]) |
| 88 | + return true; |
| 89 | + v++; |
| 90 | + } |
| 91 | + |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public Integer next() { |
| 97 | + if(!hasNext()){ |
| 98 | + throw new NoSuchElementException(); |
| 99 | + } |
| 100 | + |
| 101 | + return v++; |
| 102 | + } |
| 103 | + |
| 104 | + public void remove(){ |
| 105 | + throw new UnsupportedOperationException(); |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + //String representation of Graph - Takes O(n^2) time |
| 111 | + public String toString(){ |
| 112 | + StringBuilder s = new StringBuilder(); |
| 113 | + s.append("Undirected graph" + NEWLINE); |
| 114 | + s.append("Vertices:"+ V + " and edges:" + E + NEWLINE); |
| 115 | + for (int u = 0; u < V; u++) { |
| 116 | + s.append(u + ": "); |
| 117 | + for (int v = 0; v < V; v++) { |
| 118 | + s.append(String.format("%7s", adjMatrix[v][u]) + " "); |
| 119 | + } |
| 120 | + s.append(NEWLINE); |
| 121 | + } |
| 122 | + return s.toString(); |
| 123 | + } |
| 124 | + |
| 125 | + void clearVisited(){ |
| 126 | + for (int i = 0; i < visited.length; i++) |
| 127 | + visited[i] = false; |
| 128 | + } |
| 129 | + |
| 130 | + //ALGORITHM IMPORTANT PART |
| 131 | + public void dfs(){ |
| 132 | + //visit nodes using a stack to store "to visit" nodes |
| 133 | + Stack<Integer> s= new Stack<>(); |
| 134 | + clearVisited(); //set all visited[i]=0; |
| 135 | + s.push(0); // start the "to visit" at node 0 |
| 136 | + |
| 137 | + //LOOP AS LONG AS THERE ARE ACTIVE NODES |
| 138 | + while(!s.isEmpty()){ |
| 139 | + int nextNode; //next Node to visit |
| 140 | + int i; |
| 141 | + nextNode= s.pop(); |
| 142 | + if(!visited[nextNode]){ //current node has't been visited |
| 143 | + visited[nextNode]=true; //mark node as visited |
| 144 | + System.out.println("nextNode "+nextNode); |
| 145 | + for(i=0 ; i< V; i++){ //for i < than the total number of vertices |
| 146 | + if((this.adjMatrix[nextNode][i]==true) && !this.visited[i]){ // if next column assigning to the next node from current node is true in matrix and has not beed visited, push that next node |
| 147 | + s.push(i); //node pushed that has a connection of value true to previous node |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + public static void main(String[] args) { |
| 155 | + int V =10; |
| 156 | + int E =13; |
| 157 | + Graph G= new Graph(V,E); |
| 158 | + System.out.println(G.toString()); |
| 159 | + G.dfs(); |
| 160 | + } |
| 161 | +} |
0 commit comments