|
| 1 | +/*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. |
| 2 | + * E-Mail : info@careermonk.com |
| 3 | + * Creation Date : 2015-01-10 06:15:46 |
| 4 | + * Last modification : 2006-05-31 |
| 5 | + by : Narasimha Karumanchi |
| 6 | + * File Name : CLLNode.java |
| 7 | + * Book Title : Data Structures And Algorithms Made In Java |
| 8 | + * Warranty : This software is provided "as is" without any |
| 9 | + * warranty; without even the implied warranty of |
| 10 | + * merchantability or fitness for a particular purpose. |
| 11 | + * |
| 12 | + */ |
| 13 | + |
| 14 | +import java.io.*; |
| 15 | +import java.util.*; |
| 16 | + |
| 17 | +class LinkedList<Integer> implements Iterable<Integer> { |
| 18 | + private ListNode<Integer> head; // beginning of linked list |
| 19 | + private int n; // number of elements in linked list |
| 20 | + |
| 21 | + // helper linked list class |
| 22 | + private static class ListNode<Integer> { |
| 23 | + private Integer data; |
| 24 | + private ListNode<Integer> next; |
| 25 | + } |
| 26 | + |
| 27 | + public LinkedList() { |
| 28 | + head = null; |
| 29 | + n = 0; |
| 30 | + } |
| 31 | + |
| 32 | + public boolean isEmpty() { |
| 33 | + return head == null; |
| 34 | + } |
| 35 | + |
| 36 | + public int size() { |
| 37 | + return n; |
| 38 | + } |
| 39 | + |
| 40 | + public void add(Integer data) { |
| 41 | + ListNode<Integer> oldfirst = head; |
| 42 | + head = new ListNode<Integer>(); |
| 43 | + head.data = data; |
| 44 | + head.next = oldfirst; |
| 45 | + n++; |
| 46 | + } |
| 47 | + |
| 48 | + public Iterator<Integer> iterator() { |
| 49 | + return new ListIterator(head); |
| 50 | + } |
| 51 | + |
| 52 | + // an iterator, doesn't implement remove() since it's optional |
| 53 | + private class ListIterator implements Iterator<Integer> { |
| 54 | + private ListNode<Integer> current; |
| 55 | + |
| 56 | + public ListIterator(ListNode<Integer> head) { |
| 57 | + current = head; |
| 58 | + } |
| 59 | + |
| 60 | + public boolean hasNext() { return current != null; } |
| 61 | + public void remove() { throw new UnsupportedOperationException(); } |
| 62 | + |
| 63 | + public Integer next() { |
| 64 | + if (!hasNext()) throw new NoSuchElementException(); |
| 65 | + Integer data = current.data; |
| 66 | + current = current.next; |
| 67 | + return data; |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | +public class Graph { |
| 72 | + private static final String NEWLINE = System.getProperty("line.separator"); |
| 73 | + |
| 74 | + private final int V; |
| 75 | + private int E; |
| 76 | + private LinkedList<Integer>[] adjList; |
| 77 | + |
| 78 | + // Initializes an empty graph with V vertices and 0 edges. |
| 79 | + public Graph(int V) { |
| 80 | + if (V < 0) throw new IllegalArgumentException("Number of vertices must be nonnegative"); |
| 81 | + this.V = V; |
| 82 | + this.E = 0; |
| 83 | + adjList = (LinkedList<Integer>[]) new LinkedList[V]; |
| 84 | + for (int u = 0; u < V; u++) { |
| 85 | + adjList[u] = new LinkedList<Integer>(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // random graph with V vertices and E edges |
| 90 | + public Graph(int V, int E) { |
| 91 | + this(V); |
| 92 | + if (E > (long) V*(V-1)/2 + V) throw new IllegalArgumentException("Too many edges"); |
| 93 | + if (E < 0) throw new IllegalArgumentException("Too few edges"); |
| 94 | + Random random = new Random(); |
| 95 | + |
| 96 | + // can be inefficient |
| 97 | + while (this.E != E) { |
| 98 | + int u = random.nextInt(V); |
| 99 | + int v = random.nextInt(V); |
| 100 | + addEdge(u, v); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + // Initializes a new graph. |
| 106 | + public Graph(Graph G) { |
| 107 | + this(G.V()); |
| 108 | + this.E = G.E(); |
| 109 | + for (int u = 0; u < G.V(); u++) { |
| 110 | + // reverse so that adjacency list is in same order as original |
| 111 | + Stack<Integer> reverse = new Stack<Integer>(); |
| 112 | + for (int v : G.adjList[u]) { |
| 113 | + reverse.push(v); |
| 114 | + } |
| 115 | + for (int v : reverse) { |
| 116 | + adjList[u].add(v); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Returns the number of vertices in this graph. |
| 122 | + public int V() { |
| 123 | + return V; |
| 124 | + } |
| 125 | + |
| 126 | + // Returns the number of edges in this graph. |
| 127 | + public int E() { |
| 128 | + return E; |
| 129 | + } |
| 130 | + |
| 131 | + // throw an IllegalArgumentException unless {@code 0 <= u < V} |
| 132 | + private void validateVertex(int u) { |
| 133 | + if (u < 0 || u >= V) |
| 134 | + throw new IllegalArgumentException("vertex " + u + " is not between 0 and " + (V-1)); |
| 135 | + } |
| 136 | + |
| 137 | + public void addEdge(int u, int v) { |
| 138 | + validateVertex(u); |
| 139 | + validateVertex(v); |
| 140 | + E++; |
| 141 | + adjList[u].add(v); |
| 142 | + adjList[v].add(u); |
| 143 | + } |
| 144 | + |
| 145 | + // Returns the vertices adjacent to vertex {@code u}. |
| 146 | + public Iterable<Integer> adjList(int u) { |
| 147 | + validateVertex(u); |
| 148 | + return adjList[u]; |
| 149 | + } |
| 150 | + |
| 151 | + public int degree(int u) { |
| 152 | + validateVertex(u); |
| 153 | + return adjList[u].size(); |
| 154 | + } |
| 155 | + |
| 156 | + // Returns a string representation of this graph. |
| 157 | + public String toString() { |
| 158 | + StringBuilder s = new StringBuilder(); |
| 159 | + s.append("Undirected graph" + NEWLINE); |
| 160 | + s.append(V + " vertices, " + E + " edges " + NEWLINE); |
| 161 | + for (int u = 0; u < V; u++) { |
| 162 | + s.append(u + ": "); |
| 163 | + for (int v : adjList[u]) { |
| 164 | + s.append(v + " "); |
| 165 | + } |
| 166 | + s.append(NEWLINE); |
| 167 | + } |
| 168 | + return s.toString(); |
| 169 | + } |
| 170 | + |
| 171 | + // test code |
| 172 | + public static void main(String[] args) { |
| 173 | + Graph G = new Graph(5, 7); |
| 174 | + System.out.println(G.toString()); |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments