-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph Implementation In Java
More file actions
45 lines (44 loc) · 939 Bytes
/
Graph Implementation In Java
File metadata and controls
45 lines (44 loc) · 939 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
import java.util.*;
class Graph{
ArrayList<ArrayList<Integer>> grp;
int v;
Graph(int node)
{
v = node;
grp = new ArrayList<ArrayList<Integer>>();
for(int i=0;i<v;i++)
{
grp.add(new ArrayList<Integer>());
}
}
void addEdge(int V, int U)
{
grp.get(V).add(U);
grp.get(U).add(V);
}
void printGraph()
{
for(int i=0;i<v;i++)
{
System.out.print("Node "+i+" = ");
for(int j: grp.get(i))
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
public class GraphImplementation {
public static void main(String arg[])
{
Graph g = new Graph(5);
g.addEdge(0,2);
g.addEdge(4,1);
g.addEdge(1,0);
g.addEdge(2,3);
g.addEdge(3,4);
g.addEdge(3,0);
g.printGraph();
}
}