|
| 1 | +/* |
| 2 | +Copyright (C) Deepali Srivastava - All Rights Reserved |
| 3 | +This code is part of DSA course available on CourseGalaxy.com |
| 4 | +*/ |
| 5 | + |
| 6 | +package adjacencyListRepresentation; |
| 7 | + |
| 8 | +public class LinkedDigraph |
| 9 | +{ |
| 10 | + VertexNode start; |
| 11 | + int n; |
| 12 | + int e; |
| 13 | + |
| 14 | + public int vertices() |
| 15 | + { |
| 16 | + return n; |
| 17 | + } |
| 18 | + |
| 19 | + public int edges() |
| 20 | + { |
| 21 | + return e; |
| 22 | + } |
| 23 | + |
| 24 | + public void insertVertex(String s) |
| 25 | + { |
| 26 | + VertexNode temp = new VertexNode(s); |
| 27 | + if(start==null) |
| 28 | + start=temp; |
| 29 | + else |
| 30 | + { |
| 31 | + VertexNode p=start; |
| 32 | + while(p.nextVertex!=null) |
| 33 | + { |
| 34 | + if(p.name.equals(s)) |
| 35 | + { |
| 36 | + System.out.println("Vertex already present"); |
| 37 | + return; |
| 38 | + } |
| 39 | + p=p.nextVertex; |
| 40 | + } |
| 41 | + if(p.name.equals(s)) |
| 42 | + { |
| 43 | + System.out.println("Vertex already present"); |
| 44 | + return; |
| 45 | + } |
| 46 | + p.nextVertex=temp; |
| 47 | + } |
| 48 | + n++; |
| 49 | + } |
| 50 | + |
| 51 | + public void deleteVertex(String s) |
| 52 | + { |
| 53 | + deletefromEdgeLists(s); |
| 54 | + deletefromVertexList(s); |
| 55 | + } |
| 56 | + |
| 57 | + /*Delete incoming edges*/ |
| 58 | + private void deletefromEdgeLists(String s) |
| 59 | + { |
| 60 | + for(VertexNode p=start; p!=null; p=p.nextVertex) |
| 61 | + { |
| 62 | + if(p.firstEdge==null) |
| 63 | + continue; |
| 64 | + |
| 65 | + if(p.firstEdge.endVertex.name.equals(s)) |
| 66 | + { |
| 67 | + p.firstEdge = p.firstEdge.nextEdge; |
| 68 | + e--; |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + EdgeNode q=p.firstEdge; |
| 73 | + while(q.nextEdge!=null) |
| 74 | + { |
| 75 | + if(q.nextEdge.endVertex.name.equals(s)) |
| 76 | + { |
| 77 | + q.nextEdge = q.nextEdge.nextEdge; |
| 78 | + e--; |
| 79 | + break; |
| 80 | + } |
| 81 | + q=q.nextEdge; |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + private void deletefromVertexList(String s) |
| 89 | + { |
| 90 | + if(start==null) |
| 91 | + { |
| 92 | + System.out.println("No vertices to be deleted"); |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + if(start.name.equals(s)) /* Vertex to be deleted is first vertex of list*/ |
| 97 | + { |
| 98 | + for(EdgeNode q=start.firstEdge; q!=null; q=q.nextEdge) |
| 99 | + e--; |
| 100 | + start=start.nextVertex; |
| 101 | + n--; |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + VertexNode p=start; |
| 106 | + while(p.nextVertex!=null) |
| 107 | + { |
| 108 | + if(p.nextVertex.name.equals(s)) |
| 109 | + break; |
| 110 | + p=p.nextVertex; |
| 111 | + } |
| 112 | + |
| 113 | + if(p.nextVertex==null) |
| 114 | + { |
| 115 | + System.out.println("Vertex not found"); |
| 116 | + return; |
| 117 | + } |
| 118 | + else |
| 119 | + { |
| 120 | + for(EdgeNode q=p.nextVertex.firstEdge; q!=null; q=q.nextEdge) |
| 121 | + e--; |
| 122 | + p.nextVertex = p.nextVertex.nextVertex; |
| 123 | + n--; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private VertexNode findVertex(String s) |
| 129 | + { |
| 130 | + VertexNode p=start; |
| 131 | + while(p!=null) |
| 132 | + { |
| 133 | + if(p.name.equals(s)) |
| 134 | + return p; |
| 135 | + p=p.nextVertex; |
| 136 | + } |
| 137 | + return null; |
| 138 | + }/*End of findVertex()*/ |
| 139 | + |
| 140 | + /*Insert an edge (s1,s2) */ |
| 141 | + public void insertEdge(String s1, String s2) |
| 142 | + { |
| 143 | + if(s1.equals(s2)) |
| 144 | + { |
| 145 | + System.out.println("Inavlid Edge : Start and end vertices are same"); |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + VertexNode u = findVertex(s1); |
| 150 | + VertexNode v = findVertex(s2); |
| 151 | + |
| 152 | + if(u==null) |
| 153 | + { |
| 154 | + System.out.println("Start vertex not present, first insert vertex " + s1); |
| 155 | + return; |
| 156 | + } |
| 157 | + if(v==null) |
| 158 | + { |
| 159 | + System.out.println("End vertex not present, first insert vertex " + s2); |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + EdgeNode temp = new EdgeNode(v); |
| 164 | + if(u.firstEdge==null) |
| 165 | + { |
| 166 | + u.firstEdge=temp; |
| 167 | + e++; |
| 168 | + } |
| 169 | + else |
| 170 | + { |
| 171 | + EdgeNode p=u.firstEdge; |
| 172 | + while(p.nextEdge!=null) |
| 173 | + { |
| 174 | + if(p.endVertex.name.equals(s2)) |
| 175 | + { |
| 176 | + System.out.println("Edge present"); |
| 177 | + return; |
| 178 | + } |
| 179 | + p=p.nextEdge; |
| 180 | + } |
| 181 | + if(p.endVertex.name.equals(s2)) |
| 182 | + { |
| 183 | + System.out.println("Edge present"); |
| 184 | + return; |
| 185 | + } |
| 186 | + p.nextEdge=temp; |
| 187 | + e++; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + /* Delete the edge (s1,s2) */ |
| 192 | + public void deleteEdge(String s1, String s2) |
| 193 | + { |
| 194 | + VertexNode u = findVertex(s1); |
| 195 | + |
| 196 | + if(u==null) |
| 197 | + { |
| 198 | + System.out.println("Start vertex not present"); |
| 199 | + return; |
| 200 | + } |
| 201 | + if(u.firstEdge == null) |
| 202 | + { |
| 203 | + System.out.println("Edge not present"); |
| 204 | + return; |
| 205 | + } |
| 206 | + |
| 207 | + if(u.firstEdge.endVertex.name.equals(s2)) |
| 208 | + { |
| 209 | + u.firstEdge = u.firstEdge.nextEdge; |
| 210 | + e--; |
| 211 | + return; |
| 212 | + } |
| 213 | + EdgeNode q = u.firstEdge; |
| 214 | + while(q.nextEdge != null) |
| 215 | + { |
| 216 | + if(q.nextEdge.endVertex.name.equals(s2)) |
| 217 | + { |
| 218 | + q.nextEdge = q.nextEdge.nextEdge; |
| 219 | + e--; |
| 220 | + return; |
| 221 | + } |
| 222 | + q=q.nextEdge; |
| 223 | + } |
| 224 | + System.out.println("Edge not present"); |
| 225 | + } |
| 226 | + |
| 227 | + public void display() |
| 228 | + { |
| 229 | + EdgeNode q; |
| 230 | + for(VertexNode p=start; p!=null; p=p.nextVertex) |
| 231 | + { |
| 232 | + System.out.print(p + "->"); |
| 233 | + for(q=p.firstEdge; q!=null; q=q.nextEdge) |
| 234 | + System.out.print(" " + q.endVertex); |
| 235 | + System.out.println(); |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + /* Returns true if s2 is adjacent to s1, i.e. if edge (s1,s2) exists */ |
| 240 | + public boolean edgeExists(String s1, String s2) |
| 241 | + { |
| 242 | + VertexNode u = findVertex(s1); |
| 243 | + EdgeNode q=u.firstEdge; |
| 244 | + while(q!=null) |
| 245 | + { |
| 246 | + if(q.endVertex.name.equals(s2)) |
| 247 | + return true; |
| 248 | + q=q.nextEdge; |
| 249 | + } |
| 250 | + return false; |
| 251 | + } |
| 252 | + /*Returns number of edges going out from vertex s*/ |
| 253 | + public int outDegree(String s) |
| 254 | + { |
| 255 | + VertexNode u = findVertex(s); |
| 256 | + if(u==null) |
| 257 | + throw new IllegalArgumentException("Vertex not present"); |
| 258 | + |
| 259 | + int out=0; |
| 260 | + EdgeNode q = u.firstEdge; |
| 261 | + while(q!=null) |
| 262 | + { |
| 263 | + q=q.nextEdge; |
| 264 | + out++; |
| 265 | + } |
| 266 | + return out; |
| 267 | + } |
| 268 | + |
| 269 | + /*Returns number of edges coming to vertex s*/ |
| 270 | + public int inDegree(String s) |
| 271 | + { |
| 272 | + VertexNode u = findVertex(s); |
| 273 | + if(u==null) |
| 274 | + throw new IllegalArgumentException("Vertex not present"); |
| 275 | + |
| 276 | + int in=0; |
| 277 | + for(VertexNode p=start; p!=null; p=p.nextVertex) |
| 278 | + { |
| 279 | + for(EdgeNode q=p.firstEdge; q!=null; q=q.nextEdge) |
| 280 | + if(q.endVertex.name.equals(s)) |
| 281 | + in++; |
| 282 | + } |
| 283 | + return in; |
| 284 | + } |
| 285 | +} |
0 commit comments