说明
- 以邻接表作为存储结构
- 以用户指定的结点分别进行广度搜索和深度搜索
- 相应的生成树的边集
运行截图

源代码
import java.util.*;
public class AdjacencyList {
public static void main(String[] args) {
CreateGraph createGraph=new CreateGraph();
createGraph.initGraph();
createGraph.outputGraph();
createGraph.DFSTraverse();
createGraph.BFSTraverse();
createGraph.show();
}
}
class Vertex{
String vername;
Vertex nextNode;
}
class Graph{
Vertex[] vertices;
int verNum = 0;
int edgeNum = 0;
}
class Arc {
String start;
String end;
}
class CreateGraph{
static private Graph graph = new Graph();
static private boolean[] visited;
public Vertex getVertex(String str){
for(int i = 1;i<=graph.verNum;i++){
if(graph.vertices[i].vername.equals(str))
return graph.vertices[i];
}
return null;
}
public void initGraph(){
Scanner in = new Scanner(System.in);
System.out.println("输入顶点的数量");
graph.verNum = in.nextInt();
graph.vertices = new Vertex[graph.verNum+1];
visited = new boolean[graph.verNum+1];
System.out.println("输入弧的数量");
graph.edgeNum = in.nextInt();
System.out.println("请输入各个顶点的名称:");
for (int i=1;i<=graph.verNum;i++){
Vertex vertex = new Vertex();
vertex.vername = in.next();
vertex.nextNode = null;
graph.vertices[i] = vertex;
}
System.out.println("请依次输入无向图的各条弧的两个顶点:");
for (int i=1;i<=graph.edgeNum;i++) {
String v1 = in.next();
String v2 = in.next();
Vertex vertex1 = getVertex(v1);
Vertex vertex2 = getVertex(v2);
if (vertex1 == null) {
System.out.println(v1 + "不是图中的一个顶点,请重新输入");
i--;continue;
} else if (vertex2 == null) {
System.out.println(v2 + "不是图中的一个顶点,请重新输入");
i--;continue;
} else {
Vertex newVertex1 = new Vertex();
newVertex1.vername = v2;
newVertex1.nextNode = vertex1.nextNode;
vertex1.nextNode = newVertex1;
Vertex newVertex2 = new Vertex();
newVertex2.vername =v1;
newVertex2.nextNode = vertex2.nextNode;
vertex2.nextNode = newVertex2;
}
}
}
public void outputGraph(){
System.out.println("输入的无向图图的邻接链表为:");
for(int i=1;i<=graph.verNum;i++){
Vertex vertex=graph.vertices[i];
System.out.print(vertex.vername);
Vertex current=vertex.nextNode;
while(current!=null){
System.out.print("-->"+current.vername);
current=current.nextNode;
}
System.out.println();
}
}
ArrayList<Arc> Dset = new ArrayList<>();
public int find(Vertex node){
for (int i =1;i<=graph.vertices.length;i++)
if (graph.vertices[i].vername.equals(node.vername)==true)
return i;
return -1;
}
public void DFSTraverse(){
System.out.println("请输入从哪个顶点开始进行深度优先搜索(输入顶点名称)");
Vertex first = new Vertex();
Scanner in = new Scanner(System.in);
first.vername = in.next();
int index = find(first);
for (int i=1;i<=graph.verNum;i++)
visited[i]=false;
System.out.println("深度优先搜索遍历的顺序为:");
for (int i=index;i<=graph.verNum;i++){
if (!visited[i]) DFS(i);
}
for(int i=1;i<index;i++){
if (!visited[i]) DFS(i);
}
System.out.println();
}
public void DFS(int v){
visited[v]=true;
System.out.print(graph.vertices[v].vername+" ");
Vertex CurrentNode_NextNode = graph.vertices[v].nextNode;
while (CurrentNode_NextNode != null){
int index = find(CurrentNode_NextNode);
if (!visited[index]) {
Arc asc = new Arc();
asc.start = graph.vertices[v].vername;
asc.end =CurrentNode_NextNode.vername;
Dset.add(asc);
DFS(index);
}
else
CurrentNode_NextNode = CurrentNode_NextNode.nextNode;
}
}
ArrayList<Arc> Bset = new ArrayList<>();
public void BFSTraverse(){
System.out.println("请输入从哪个顶点开始进行广度优先搜索(输入顶点名称)");
Vertex first = new Vertex();
Scanner in = new Scanner(System.in);
first.vername = in.next();
int index = find(first);
for (int i=0;i<=graph.verNum;i++)
visited[i] = false;
System.out.println("广度优先搜索遍历的顺序为:");
for (int i=index;i<=graph.verNum;i++){
if (!visited[i]) BFS(i);
}
for(int i=1;i<index;i++){
if (!visited[i]) BFS(i);
}
System.out.println();
}
public void BFS(int v){
visited[v]=true;
System.out.print(graph.vertices[v].vername+" ");
Queue<Integer> queue = new LinkedList<>();
queue.offer(v);
while (!queue.isEmpty()){
v = queue.poll();
Vertex CurrentNode_NextNode = graph.vertices[v].nextNode;
while (CurrentNode_NextNode!=null){
if (visited[find(CurrentNode_NextNode)]==false){
System.out.print(CurrentNode_NextNode.vername+" ");
visited[find(CurrentNode_NextNode)]=true;
queue.offer(find(CurrentNode_NextNode));
Arc arc = new Arc();
arc.start = graph.vertices[v].vername;
arc.end = CurrentNode_NextNode.vername;
Bset.add(arc);
}
CurrentNode_NextNode = CurrentNode_NextNode.nextNode;
}
}
}
public void show(){
System.out.println("深度搜索生成树边集:");
Iterator Diterator= Dset.iterator();
while (Diterator.hasNext()){
Arc arc = (Arc) Diterator.next();
System.out.print("("+arc.start+","+arc.end+") ");
}
System.out.println();
System.out.println("广度搜索生成树边集:");
Iterator Biterator= Bset.iterator();
while (Biterator.hasNext()){
Arc arc = (Arc) Biterator.next();
System.out.print("("+arc.start+","+arc.end+") ");
}
}
}