-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
35 lines (30 loc) · 907 Bytes
/
Graph.java
File metadata and controls
35 lines (30 loc) · 907 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
package Graph;
import java.util.LinkedList;
public class Graph {
int vertices;
LinkedList<LinkedList<Integer>> adjacentList;
public Graph(int vertices)
{
this.vertices = vertices;
adjacentList = new LinkedList<>();
for (int i = 0; i < vertices; i++) {
adjacentList.add(new LinkedList<>());
}
}
public void addEdge(int source, int destination)
{
adjacentList.get(source).add(destination);
//----------for undirected graph uncomment the line below---
//adjacentList.get(destination).add(source);
}
public void printGraph()
{
for (int i = 0; i < vertices; i++) {
System.out.print("|" + i + "| => ");
for (int item: adjacentList.get(i)) {
System.out.print("[" + item + "] ->");
}
System.out.println("null");
}
}
}