《算法》第四章部分程序 part 5
▶ 书中第四章部分程序,加上自己补充的代码,图的深度优先遍历
● 无向图的广度优先遍历,有向 / 无向图代码仅若干方法名不同
package package01; import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.Graph;
import edu.princeton.cs.algs4.Stack;
import edu.princeton.cs.algs4.Queue; public class class01
{
private static final int INFINITY = Integer.MAX_VALUE;
private boolean[] marked; // 顶点是否已被遍历
private int[] edgeTo; // 每个顶点在 s - v 路径中的父顶点
private int[] distTo; // 每个顶点在 s - v 路径中的路径长 public class01(Graph G, int s)
{
marked = new boolean[G.V()];
distTo = new int[G.V()];
edgeTo = new int[G.V()];
for (int v = 0; v < G.V(); v++)
distTo[v] = INFINITY;
nonRecursiveBFS(G, s);
//assert check(G, s);
} private void nonRecursiveBFS(Graph G, int s)
{
Queue<Integer> q = new Queue<Integer>();
distTo[s] = 0;
marked[s] = true;
for (q.enqueue(s); !q.isEmpty();)
{
int v = q.dequeue();
for (int w : G.adj(v))
{
if (!marked[w])
{
edgeTo[w] = v;
distTo[w] = distTo[v] + 1;
marked[w] = true;
q.enqueue(w);
}
}
}
} private void nonRecursiveBFS(Graph G, Iterable<Integer> sources) // 以迭代器元素为起点列表进行遍历
{
Queue<Integer> q = new Queue<Integer>();
for (int s : sources)
{
marked[s] = true;
distTo[s] = 0;
q.enqueue(s);
}
for (;!q.isEmpty();)
{
int v = q.dequeue();
for (int w : G.adj(v))
{
if (!marked[w])
{
edgeTo[w] = v;
distTo[w] = distTo[v] + 1;
marked[w] = true;
q.enqueue(w);
}
}
}
} public boolean marked(int v)
{
return marked[v];
} public int distTo(int v)
{
return distTo[v];
} public Iterable<Integer> pathTo(int v)
{
if (!marked(v))
return null;
Stack<Integer> path = new Stack<Integer>();
int x;
for (x = v; distTo[x] != 0; x = edgeTo[x])
path.push(x);
path.push(x);
return path;
} private boolean check(Graph G, int s)
{
if (distTo[s] != 0)
{
StdOut.println("\n<check> error distance of s.\n");
return false;
}
for (int v = 0; v < G.V(); v++)
{
for (int w : G.adj(v))
{
if (marked(v) != marked(w)) // 检查边正确性
{
StdOut.println("edge " + v + "-" + w);
StdOut.println("marked(" + v + ") = " + marked(v));
StdOut.println("marked(" + w + ") = " + marked(w));
return false;
}
if (marked(v) && (distTo[w] > distTo[v] + 1)) // 检查顶点 v 相连的顶点的距离正确性
{
StdOut.println("edge " + v + "-" + w);
StdOut.println("distTo[" + v + "] = " + distTo[v]);
StdOut.println("distTo[" + w + "] = " + distTo[w]);
return false;
}
}
}
for (int w = 0; w < G.V(); w++)
{
if (!marked(w) || w == s)
continue;
int v = edgeTo[w];
if (distTo[w] != distTo[v] + 1) // 逐边检查距离正确性
{
StdOut.println("shortest path edge " + v + "-" + w);
StdOut.println("distTo[" + v + "] = " + distTo[v]);
StdOut.println("distTo[" + w + "] = " + distTo[w]);
return false;
}
}
return true;
} public static void main(String[] args)
{
In in = new In(args[0]);
int s = Integer.parseInt(args[1]);
Graph G = new Graph(in);
class01 search = new class01(G, s);
for (int v = 0; v < G.V(); v++)
{
if (search.marked(v))
{
StdOut.printf("%d to %d (%d): ", s, v, search.distTo(v));
for (int x : search.pathTo(v))
{
if (x == s)
StdOut.print(x);
else
StdOut.print("-" + x);
}
StdOut.println();
}
else
StdOut.printf("%d to %d (-): not connected\n", s, v);
}
}
}
● 有向图广度优先遍历
package package01; import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.Digraph;
import edu.princeton.cs.algs4.Stack;
import edu.princeton.cs.algs4.Queue; public class class01
{
private static final int INFINITY = Integer.MAX_VALUE;
private boolean[] marked;
private int[] edgeTo;
private int[] distTo; public class01(Digraph G, int s)
{
marked = new boolean[G.V()];
distTo = new int[G.V()];
edgeTo = new int[G.V()];
for (int v = 0; v < G.V(); v++)
distTo[v] = INFINITY;
nonRecursiveBFS(G, s);
} private void nonRecursiveBFS(Digraph G, int s)
{
Queue<Integer> q = new Queue<Integer>();
distTo[s] = 0;
marked[s] = true;
for (q.enqueue(s); !q.isEmpty();)
{
int v = q.dequeue();
for (int w : G.adj(v))
{
if (!marked[w])
{
edgeTo[w] = v;
distTo[w] = distTo[v] + 1;
marked[w] = true;
q.enqueue(w);
}
}
}
} private void nonRecursiveBFS(Digraph G, Iterable<Integer> sources)
{
Queue<Integer> q = new Queue<Integer>();
for (int s : sources)
{
marked[s] = true;
distTo[s] = 0;
q.enqueue(s);
}
for (;!q.isEmpty();)
{
int v = q.dequeue();
for (int w : G.adj(v))
{
if (!marked[w])
{
edgeTo[w] = v;
distTo[w] = distTo[v] + 1;
marked[w] = true;
q.enqueue(w);
}
}
}
} public boolean marked(int v)
{
return marked[v];
} public int distTo(int v)
{
return distTo[v];
} public Iterable<Integer> pathTo(int v)
{
if (!marked(v))
return null;
Stack<Integer> path = new Stack<Integer>();
int x;
for (x = v; distTo[x] != 0; x = edgeTo[x])
path.push(x);
path.push(x);
return path;
} public static void main(String[] args)
{
In in = new In(args[0]);
int s = Integer.parseInt(args[1]);
Digraph G = new Digraph(in);
class01 search = new class01(G, s);
for (int v = 0; v < G.V(); v++)
{
if (search.marked(v))
{
StdOut.printf("%d to %d (%d): ", s, v, search.distTo(v));
for (int x : search.pathTo(v))
{
if (x == s)
StdOut.print(x);
else
StdOut.print("->" + x);
}
StdOut.println();
}
else
StdOut.printf("%d to %d (-): not connected\n", s, v);
}
}
}
《算法》第四章部分程序 part 5的更多相关文章
- 《算法》第四章部分程序 part 19
▶ 书中第四章部分程序,包括在加上自己补充的代码,有边权有向图的邻接矩阵,FloydWarshall 算法可能含负环的有边权有向图任意两点之间的最短路径 ● 有边权有向图的邻接矩阵 package p ...
- 《算法》第四章部分程序 part 18
▶ 书中第四章部分程序,包括在加上自己补充的代码,在有权有向图中寻找环,Bellman - Ford 算法求最短路径,套汇算法 ● 在有权有向图中寻找环 package package01; impo ...
- 《算法》第四章部分程序 part 16
▶ 书中第四章部分程序,包括在加上自己补充的代码,Dijkstra 算法求有向 / 无向图最短路径,以及所有顶点对之间的最短路径 ● Dijkstra 算法求有向图最短路径 package packa ...
- 《算法》第四章部分程序 part 15
▶ 书中第四章部分程序,包括在加上自己补充的代码,Kruskal 算法和 Boruvka 算法求最小生成树 ● Kruskal 算法求最小生成树 package package01; import e ...
- 《算法》第四章部分程序 part 14
▶ 书中第四章部分程序,包括在加上自己补充的代码,两种 Prim 算法求最小生成树 ● 简单 Prim 算法求最小生成树 package package01; import edu.princeton ...
- 《算法》第四章部分程序 part 10
▶ 书中第四章部分程序,包括在加上自己补充的代码,包括无向图连通分量,Kosaraju - Sharir 算法.Tarjan 算法.Gabow 算法计算有向图的强连通分量 ● 无向图连通分量 pack ...
- 《算法》第四章部分程序 part 9
▶ 书中第四章部分程序,包括在加上自己补充的代码,两种拓扑排序的方法 ● 拓扑排序 1 package package01; import edu.princeton.cs.algs4.Digraph ...
- 《算法》第四章部分程序 part 17
▶ 书中第四章部分程序,包括在加上自己补充的代码,无环图最短 / 最长路径通用程序,关键路径方法(critical path method)解决任务调度问题 ● 无环图最短 / 最长路径通用程序 pa ...
- 《算法》第四章部分程序 part 13
▶ 书中第四章部分程序,包括在加上自己补充的代码,图的前序.后序和逆后续遍历,以及传递闭包 ● 图的前序.后序和逆后续遍历 package package01; import edu.princeto ...
- 《算法》第四章部分程序 part 12
▶ 书中第四章部分程序,包括在加上自己补充的代码,图的几种补充数据结构,包括无向 / 有向符号图,有权边结构,有边权有向图 ● 无向符号图 package package01; import edu. ...
随机推荐
- python "爬虫+有道词典"实现一个简单翻译程序
抓包软件使用的是Fiddler4 新版的查询接口 比较负责,引入了salt和sign http://fanyi.youdao.com/translate?smartresult=dict&sm ...
- ubuntu16.04安装chrome
方法1: 到 https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb 下载最新的安装文件. 然后使用cd命令 ...
- 峰Redis学习(3)Redis 数据结构(字符串、哈希)
第一节:Redis 数据类型介绍 五种数据类型: 字符串(String) 字符串列表(list) 有序字符串集合(sorted set) 哈希(hash) 字符串集合(set) 第二节:Redis ...
- Rabbitmq 安装&启动
安装socat [root@Aliyun software]# yum -y install epel-release [root@Aliyun software]# yum -y install s ...
- docker容器里面安装ssh
docker容器里面安装ssh https://blog.csdn.net/chengxuyuanyonghu/article/details/76619097
- 廖雪峰Java2面向对象编程-1面向对象-1面向对象基础
1.对象的概念 面向对象编程:Object-Oriented Programming 对现实世界建立计算机模型的一种编程方法. 现实世界 计算机模型 Java代码 人 类/class class Pe ...
- ServiceLoader
先从业务场景分析,要完成数据的分析处理功能.根据数据的不同种类,先调用groovy或者python脚本等中的一种处理数据,处理完数据的后需要流程相同. 要支持处理能力的动态扩展,也就是框架完成后,可以 ...
- Linux下的文件操作——基于文件描述符的文件操作(2)
文件描述符的复制 MMAP文件映射 ftruncate修改文件大小 文件描述符的复制 系统调用函数dup和dup2可以实现文件描述符的复制,经常用来重定向进程的stdin(0), stdout(1 ...
- vue 路由 以及默认路由跳转
https://router.vuejs.org/ vue路由配置: 1.安装 npm install vue-router --save / cnpm install vue-router --sa ...
- h5标签兼容
<!--[if lt IE 9]> <script src="//cdn.bootcss.com/respond.js/1.4.2/respond.js"> ...