forked from Vanshikapandey30/Hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.java
150 lines (124 loc) · 3.79 KB
/
Graph.java
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Java program to implement Graph
// with the help of Generics
import java.util.*;
class Graph<T> {
// We use Hashmap to store the edges in the graph
private Map<T, List<T> > map = new HashMap<>();
// This function adds a new vertex to the graph
public void addVertex(T s)
{
map.put(s, new LinkedList<T>());
}
// This function adds the edge
// between source to destination
public void addEdge(T source, T destination,
boolean bidirectional)
{
if (!map.containsKey(source))
addVertex(source);
if (!map.containsKey(destination))
addVertex(destination);
map.get(source).add(destination);
if (bidirectional == true) {
map.get(destination).add(source);
}
}
// This function gives the count of vertices
public void getVertexCount()
{
System.out.println("The graph has "
+ map.keySet().size()
+ " vertex");
}
// This function gives the count of edges
public void getEdgesCount(boolean bidirection)
{
int count = 0;
for (T v : map.keySet()) {
count += map.get(v).size();
}
if (bidirection == true) {
count = count / 2;
}
System.out.println("The graph has " + count
+ " edges.");
}
// This function gives whether
// a vertex is present or not.
public void hasVertex(T s)
{
if (map.containsKey(s)) {
System.out.println("The graph contains " + s
+ " as a vertex.");
}
else {
System.out.println("The graph does not contain "
+ s + " as a vertex.");
}
}
// This function gives whether an edge is present or
// not.
public void hasEdge(T s, T d)
{
if (map.get(s).contains(d)) {
System.out.println(
"The graph has an edge between " + s
+ " and " + d + ".");
}
else {
System.out.println(
"The graph has no edge between " + s
+ " and " + d + ".");
}
}
public void neighbours(T s)
{
if(!map.containsKey(s))
return ;
System.out.println("The neighbours of "+s+" are");
for(T w:map.get(s))
System.out.print(w+",");
}
// Prints the adjancency list of each vertex.
@Override public String toString()
{
StringBuilder builder = new StringBuilder();
for (T v : map.keySet()) {
builder.append(v.toString() + ": ");
for (T w : map.get(v)) {
builder.append(w.toString() + " ");
}
builder.append("\n");
}
return (builder.toString());
}
}
// Driver Code
public class Main {
public static void main(String args[])
{
// Object of graph is created.
Graph<Integer> g = new Graph<Integer>();
// edges are added.
// Since the graph is bidirectional,
// so boolean bidirectional is passed as true.
g.addEdge(0, 1, true);
g.addEdge(0, 4, true);
g.addEdge(1, 2, true);
g.addEdge(1, 3, true);
g.addEdge(1, 4, true);
g.addEdge(2, 3, true);
g.addEdge(3, 4, true);
// Printing the graph
System.out.println("Graph:\n" + g.toString());
// Gives the no of vertices in the graph.
g.getVertexCount();
// Gives the no of edges in the graph.
g.getEdgesCount(true);
// Tells whether the edge is present or not.
g.hasEdge(3, 4);
// Tells whether vertex is present or not
g.hasVertex(5);
g.neighbours(1);
}
}