-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphsTutorial.java
More file actions
44 lines (43 loc) · 1.26 KB
/
GraphsTutorial.java
File metadata and controls
44 lines (43 loc) · 1.26 KB
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
import java.util.ArrayList;
import java.util.Scanner;
public class GraphsTutorial{
public static class Node{
int data;
ArrayList<Node> adjcNodes;
Node(int data){
this.data=data;
adjcNodes=new ArrayList<Node>();
}
}
public static void initGraph(Node[] nodes,int V){
for(int i=0;i<V;i++){
nodes[i]=new Node(i+1);
}
}
public static void addEdge(Node[] nodes,int u,int v){
nodes[u-1].adjcNodes.add(nodes[v-1]);
}
public static void printGraph(Node[] nodes){
int V=nodes.length;
for(int i=0;i<V;i++){
System.out.print((i+1)+": ");
for(int j=0;j<nodes[i].adjcNodes.size();j++){
System.out.print(nodes[i].adjcNodes.get(j).data+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int V=4;
Node[] nodes=new Node[V];
initGraph(nodes,V);
System.out.println("Graph initialized...");
addEdge(nodes,1,2);
addEdge(nodes,1,4);
addEdge(nodes,2,3);
addEdge(nodes,3,1);
printGraph(nodes);
sc.close();
}
}