-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathGraph_Problem_01_ii.java
More file actions
42 lines (30 loc) · 961 Bytes
/
Graph_Problem_01_ii.java
File metadata and controls
42 lines (30 loc) · 961 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
package graphs;
import java.util.*;
// Problem Title :-> Create a Graph, print it using Linked List
public class Graph_Problem_01_ii {
final LinkedList<Integer>[] adj;
public Graph_Problem_01_ii(int v) {
//arrays.array of Linked List
adj = new LinkedList[v];
for(int i = 0; i < v; i++)
adj[i] = new LinkedList<>();
}
public void addEdge(int source, int destination) {
adj[source].add(destination);
adj[destination].add(source);
}
public static void main(String[] args) {
System.out.println("Enter number of vertices and edges");
Scanner sc = new Scanner(System.in);
int v = sc.nextInt();
int e = sc.nextInt();
Graph_Problem_01_ii graph_Problem_01_ii = new Graph_Problem_01_ii(v);
System.out.println("Enter " + e + " edges");
for(int i = 0; i < e; i++) {
int source = sc.nextInt();
int destination = sc.nextInt();
graph_Problem_01_ii.addEdge(source, destination);
}
sc.close();
}
}