我们已经了解了Java的泛型类。我们还可以使用它们在Java为 Graph 编码。 Graph 类是使用Java的HashMap 实现的。正如我们所知 HashMap 包含一个键和一个值,我们将节点表示为键及其在图中的值中的邻接列表。
示例:具有 5 个顶点的无向无权图。
邻接矩阵:
邻接表:
方法:
与 C++ 一样,我们在创建泛型类时使用 <> 来指定参数类型。要创建泛型类的对象,我们使用以下语法。
// To create an instance of generic class
BaseType obj = new BaseType ()
Note: In Parameter type, we cannot use primitives like
'int','char' or 'double'.
下面是上述方法的实现:
// Java program to implement Graph
// with the help of Generics
import java.util.*;
class Graph {
// We use Hashmap to store the edges in the graph
private Map > map = new HashMap<>();
// This function adds a new vertex to the graph
public void addVertex(T s)
{
map.put(s, new LinkedList());
}
// 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 + ".");
}
}
// 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 g = new Graph();
// 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);
// print 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);
}
}
输出:
Graph:
0: 1 4
1: 0 2 3 4
2: 1 3
3: 1 2 4
4: 0 1 3
The graph has 5 vertex
The graph has 7 edges.
The graph has an edge between 3 and 4.
The graph does not contain 5 as a vertex.
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。