Skip to content

Commit bf088a3

Browse files
authored
Merge pull request careermonk#14 from narkarum/patch-1
Update GraphAdjacencyMatrix.java
2 parents 8c28a8b + eb79331 commit bf088a3

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,126 @@
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+
114
package chapter09graphs;
15+
import java.util.Iterator;
16+
import java.util.Random;
17+
import java.util.NoSuchElementException;
18+
19+
public class Graph {
20+
private static final String NEWLINE = System.getProperty("line.separator");
21+
22+
private final int V;
23+
private int E;
24+
private boolean[][] adjMatrix;
25+
26+
// empty graph with V vertices
27+
public Graph(int V) {
28+
if (V < 0) throw new IllegalArgumentException("Too few vertices");
29+
this.V = V;
30+
this.E = 0;
31+
this.adjMatrix = new boolean[V][V];
32+
}
33+
34+
// random graph with V vertices and E edges
35+
public Graph(int V, int E) {
36+
this(V);
37+
if (E > (long) V*(V-1)/2 + V) throw new IllegalArgumentException("Too many edges");
38+
if (E < 0) throw new IllegalArgumentException("Too few edges");
39+
Random random = new Random();
40+
41+
// can be inefficient
42+
while (this.E != E) {
43+
int u = random.nextInt(V);
44+
int v = random.nextInt(V);
45+
addEdge(u, v);
46+
}
47+
}
48+
49+
// number of vertices and edges
50+
public int V() { return V; }
51+
public int E() { return E; }
52+
53+
// add undirected edge u-v
54+
public void addEdge(int u, int v) {
55+
if (!adjMatrix[u][v]) E++;
56+
adjMatrix[u][v] = true;
57+
adjMatrix[v][u] = true;
58+
}
59+
60+
// does the graph contain the edge u-v?
61+
public boolean contains(int u, int v) {
62+
return adjMatrix[u][v];
63+
}
64+
65+
// return list of neighbors of u
66+
public Iterable<Integer> adjMatrix(int u) {
67+
return new AdjIterator(u);
68+
}
69+
70+
// support iteration over graph vertices
71+
private class AdjIterator implements Iterator<Integer>, Iterable<Integer> {
72+
private int u;
73+
private int v = 0;
74+
75+
AdjIterator(int u) {
76+
this.u = u;
77+
}
78+
79+
public Iterator<Integer> iterator() {
80+
return this;
81+
}
82+
83+
public boolean hasNext() {
84+
while (v < V) {
85+
if (adjMatrix[u][v]) return true;
86+
v++;
87+
}
88+
return false;
89+
}
90+
91+
public Integer next() {
92+
if (!hasNext()) {
93+
throw new NoSuchElementException();
94+
}
95+
return v++;
96+
}
97+
98+
public void remove() {
99+
throw new UnsupportedOperationException();
100+
}
101+
}
102+
103+
// string representation of Graph - takes quadratic time
104+
public String toString() {
105+
StringBuilder s = new StringBuilder();
106+
s.append("Undirected graph" + NEWLINE);
107+
s.append("Vertices:"+ V + " and edges:" + E + NEWLINE);
108+
for (int u = 0; u < V; u++) {
109+
s.append(u + ": ");
110+
for (int v = 0; v < V; v++) {
111+
s.append(String.format("%7s", adjMatrix[v][u]) + " ");
112+
}
113+
s.append(NEWLINE);
114+
}
115+
return s.toString();
116+
}
117+
118+
// test code
119+
public static void main(String[] args) {
120+
int V = 5;
121+
int E = 7;
122+
Graph G = new Graph(V, E);
123+
System.out.println(G.toString());
124+
}
125+
126+
}

0 commit comments

Comments
 (0)