forked from destiny1020/algorithm_playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
72 lines (60 loc) · 1012 Bytes
/
Graph.java
File metadata and controls
72 lines (60 loc) · 1012 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package chap4;
import java.io.IOException;
import java.util.Scanner;
import chap1.LinkedBag;
public class Graph
{
private final int V;
private int E;
private LinkedBag<Integer>[] adj;
@SuppressWarnings("unchecked")
public Graph(int V)
{
this.V = V;
adj = (LinkedBag<Integer>[]) new LinkedBag[V];
for (int i = 0; i < V; i++)
adj[i] = new LinkedBag<Integer>();
}
public Graph(Scanner scanner) throws IOException
{
this(scanner.nextInt());
int E = 0;
if (scanner.hasNext())
E = scanner.nextInt();
else
E = 0;
if(0 == E)
return;
else
{
for (int i = 0; i < E; i++)
{
int v = scanner.nextInt();
int w = scanner.nextInt();
this.addEdge(v, w);
}
}
}
public void addEdge(int v, int w)
{
this.adj[v].add(w);
this.adj[w].add(v);
E++;
}
public Iterable<Integer> adj(int v)
{
return this.adj[v];
}
public int getV()
{
return V;
}
public int getE()
{
return E;
}
public LinkedBag<Integer>[] getAdj()
{
return adj;
}
}