博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(HW)DFS and BFS(Java)
阅读量:6573 次
发布时间:2019-06-24

本文共 5184 字,大约阅读时间需要 17 分钟。

1 import java.util.HashMap;  2 import java.util.LinkedList;  3 import java.util.Queue;  4 import java.util.Stack;  5   6 public class task1  7 {  8     public static void main(String[] args)  9     {     10         Graph g = new Graph(5); 11         g.createGraph(); 12          13         System.out.println("DFS(递归)"); 14         DFS(g, 0); 15         System.out.println(); 16         for(Vertex p : g.vertex) //数组元素的拷贝 17             p.isvisited = false; 18         System.out.println("DFS(迭代)"); 19         DFS_iterative(g, 0); 20         for(Vertex p : g.vertex) 21             p.isvisited = false; 22         System.out.print('\n' + "BFS" + '\n'); 23         BFS(g, 0); 24     } 25      26     //递归 27     public static void DFS(Graph g, int index) 28     { 29         System.out.print(g.vertex[index].name + " "); 30         g.vertex[index].isvisited = true; 31          32         for(Vertex v : g.adjacencyMap.get(g.vertex[index])) 33             if(v.isvisited == false) 34             { 35                 int i = g.locateVex(v.name); 36                 DFS(g, i); 37             } 38     } 39      40     //非递归 41     public static void DFS_iterative(Graph g, int index) 42     { 43         Stack
stack = new Stack<>(); 44 stack.push(g.vertex[index]); 45 Vertex p; 46 47 while(!stack.isEmpty()) 48 { 49 p = stack.pop(); 50 if(p.isvisited == false) 51 { 52 System.out.print(p.name + " "); 53 p.isvisited = true; 54 for(Vertex q : g.adjacencyMap.get(p)) 55 if(q.isvisited == false) //可避免重复入栈 56 stack.push(q); 57 } 58 } 59 } 60 61 //BFS 62 public static void BFS(Graph g, int index) 63 { 64 Queue
queue = new LinkedList<>(); 65 Vertex p; 66 System.out.print(g.vertex[index].name + " "); 67 g.vertex[index].isvisited = true; 68 queue.add(g.vertex[index]); //每个元素仅入队一次 69 70 while(!queue.isEmpty()) 71 { 72 p = queue.poll(); 73 for(Vertex q : g.adjacencyMap.get(p)) 74 if(q.isvisited == false) 75 { 76 System.out.print(q.name + " "); 77 q.isvisited = true; 78 queue.add(q); 79 } 80 } 81 } 82 } 83 84 class Vertex 85 { 86 char name; 87 boolean isvisited; 88 89 public Vertex(char name) 90 { 91 this.name = name; 92 this.isvisited = false; 93 } 94 } 95 96 class Graph 97 { 98 Vertex[] vertex; //顶点数组 99 int vexnum; //顶点数100 HashMap
> adjacencyMap; //邻接表101 102 public Graph(int vexnum)103 {104 this.vertex = new Vertex[vexnum];105 for(int i = 0; i < this.vertex.length; i++)106 this.vertex[i] = new Vertex((char)('a' + i));107 this.vexnum = vexnum;108 }109 110 public void createGraph()111 {112 //各点与其邻接点的映射113 this.adjacencyMap = new HashMap<>();114 115 116 //无向图117 //a的邻接点118 /*LinkedList
aNeighbors = new LinkedList<>();119 aNeighbors.add(vertex[1]);120 aNeighbors.add(vertex[2]);121 //b的邻接点 122 LinkedList
bNeighbors = new LinkedList<>();123 bNeighbors.add(vertex[0]);124 bNeighbors.add(vertex[3]);125 bNeighbors.add(vertex[4]);126 //c的邻接点127 LinkedList
cNeighbors = new LinkedList<>();128 cNeighbors.add(vertex[0]);129 cNeighbors.add(vertex[3]);130 //d的邻接点131 LinkedList
dNeighbors = new LinkedList<>();132 dNeighbors.add(vertex[1]);133 dNeighbors.add(vertex[2]);134 dNeighbors.add(vertex[4]);135 //e的邻接点136 LinkedList
eNeighbors = new LinkedList<>();137 eNeighbors.add(vertex[1]);138 eNeighbors.add(vertex[3]);139 //建立映射140 adjacencyMap.put(vertex[0], aNeighbors);141 adjacencyMap.put(vertex[1], bNeighbors);142 adjacencyMap.put(vertex[2], cNeighbors);143 adjacencyMap.put(vertex[3], dNeighbors);144 adjacencyMap.put(vertex[4], eNeighbors);*/145 146 147 //有向图148 //a的邻接点149 LinkedList
aNeighbors = new LinkedList<>();150 aNeighbors.add(vertex[2]);151 //b的邻接点152 LinkedList
bNeighbors = new LinkedList<>();153 bNeighbors.add(vertex[0]);154 //c的邻接点155 LinkedList
cNeighbors = new LinkedList<>();156 cNeighbors.add(vertex[3]);157 //d的邻接点158 LinkedList
dNeighbors = new LinkedList<>();159 dNeighbors.add(vertex[1]);160 dNeighbors.add(vertex[4]);161 //e的邻接点162 LinkedList
eNeighbors = new LinkedList<>();163 eNeighbors.add(vertex[1]);164 //建立映射165 adjacencyMap.put(vertex[0], aNeighbors);166 adjacencyMap.put(vertex[1], bNeighbors);167 adjacencyMap.put(vertex[2], cNeighbors);168 adjacencyMap.put(vertex[3], dNeighbors);169 adjacencyMap.put(vertex[4], eNeighbors);170 }171 172 public int locateVex(char name)173 {174 int i;175 for(i = 0; /*i != this.vertex.length && */name != this.vertex[i].name; i++);176 return i;177 }178 }

 

转载于:https://www.cnblogs.com/Huayra/p/10867398.html

你可能感兴趣的文章
IOS上iframe的滚动条失效的解决办法
查看>>
C++_012C++11的语法新特性
查看>>
Git学习笔记:常用命令总结
查看>>
iOS - OC 与 Swift 互相操作
查看>>
sort、qsort排序
查看>>
修改时无论改成什么,值总是默认为1
查看>>
Android自动化测试01-环境安装连接问题及解决
查看>>
zencart后台修改首页meta_title、meta_keywords、meta_description
查看>>
SecureCRT 常用命令大全
查看>>
Android 通过触摸动态地在屏幕上画矩形
查看>>
序列化 反序列化
查看>>
html基础内容样式
查看>>
java for语句(翻译自Java Tutorials)
查看>>
iOS开发之SceneKit框架--实战地月系统围绕太阳旋转
查看>>
java设计模式2--工厂模式
查看>>
在Mac OS X中配置Apache + PHP + MySQL
查看>>
今天天气怎么样
查看>>
free函数
查看>>
bootstrap中点击左边展开
查看>>
【转】暴露问题是对项目验收最起码的尊重!
查看>>