forked from Shreyasheeetal20/HACKTOBERFEST22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphImplementation.java
120 lines (120 loc) · 3.39 KB
/
GraphImplementation.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
import java.util.*;
class Graph<T>
{
//creating an object of the Map class that stores the edges of the graph
private Map<T, List<T> > map = new HashMap<>();
//the method adds a new vertex to the graph
public void addNewVertex(T s)
{
map.put(s, new LinkedList<T>());
}
//the method adds an edge between source and destination
public void addNewEdge(T source, T destination, boolean bidirectional)
{
//
if (!map.containsKey(source))
addNewVertex(source);
if (!map.containsKey(destination))
addNewVertex(destination);
map.get(source).add(destination);
if (bidirectional == true)
{
map.get(destination).add(source);
}
}
//the method counts the number of vertices
public void countVertices()
{
System.out.println("Total number of vertices: "+ map.keySet().size());
}
//the method counts the number of edges
public void countEdges(boolean bidirection)
{
//variable to store number of edges
int count = 0;
for (T v : map.keySet())
{
count = count + map.get(v).size();
}
if (bidirection == true)
{
count = count / 2;
}
System.out.println("Total number of edges: "+ count);
}
//checks a graph has vertex or not
public void containsVertex(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.");
}
}
//checks a graph has edge or not
//where s and d are the two parameters that represent source(vertex) and destination (vertex)
public void containsEdge(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("There is no edge between "+ s + " and " + d + ".");
}
}
//prints the adjacencyS list of each vertex
//here we have overridden the toString() method of the StringBuilder class
@Override
public String toString()
{
StringBuilder builder = new StringBuilder();
//foreach loop that iterates over the keys
for (T v : map.keySet())
{
builder.append(v.toString() + ": ");
//foreach loop for getting the vertices
for (T w : map.get(v))
{
builder.append(w.toString() + " ");
}
builder.append("\n");
}
return (builder.toString());
}
}
//creating a class in which we have implemented the driver code
public class GraphImplementation
{
public static void main(String args[])
{
//creating an object of the Graph class
Graph graph=new Graph();
//adding edges to the graph
graph.addNewEdge(0, 1, true);
graph.addNewEdge(0, 4, true);
graph.addNewEdge(1, 2, true);
graph.addNewEdge(1, 3, false);
graph.addNewEdge(1, 4, true);
graph.addNewEdge(2, 3, true);
graph.addNewEdge(2, 4, true);
graph.addNewEdge(3, 0, true);
graph.addNewEdge(2, 0, true);
//prints the adjacency matrix that represents the graph
System.out.println("Adjacency List for the graph:\n"+ graph.toString());
//counts the number of vertices in the graph
graph.countVertices();
//counts the number of edges in the graph
graph.countEdges(true);
//checks whether an edge is present or not between the two specified vertices
graph.containsEdge(3, 4);
graph.containsEdge(2, 4);
//checks whether vertex is present or not
graph.containsVertex(3);
graph.containsVertex(5);
}
}