-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay25.java
More file actions
42 lines (36 loc) · 1.2 KB
/
Day25.java
File metadata and controls
42 lines (36 loc) · 1.2 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
import java.util.*;
class Graph {
private int vertices;
private Map<Integer, List<Integer>> adjacencyList;
public Graph(int vertices) {
this.vertices = vertices;
this.adjacencyList = new HashMap<>();
for (int i = 0; i < vertices; i++) {
adjacencyList.put(i, new LinkedList<>());
}
}
public void addEdge(int source, int destination) {
adjacencyList.get(source).add(destination);
adjacencyList.get(destination).add(source);
}
public void printGraph() {
for (Map.Entry<Integer, List<Integer>> entry : adjacencyList.entrySet()) {
System.out.print("Vertex " + entry.getKey() + " is connected to: ");
for (int neighbor : entry.getValue()) {
System.out.print(neighbor + " ");
}
System.out.println();
}
}
}
public class Day25 {
public static void main(String[] args) {
Graph graph = new Graph(5);
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(1, 3);
graph.addEdge(2, 4);
System.out.println("Graph representation:");
graph.printGraph();
}
}