topological sort
A topological sort
of a dag G is a linear ordering of all its vertices such that if G contains an
edge(u,v) then u appears before in the ordering. (If the graph contains a cycle,
then no linear ordering is possible.)


package element_graph;
import java.util.LinkedList;
public class deapth_first_search {
public static int time = 0;
private static class vertex{
private LinkedList<vertex> link;
private String name;
private String color;
private vertex p;
private int d,f;
public vertex(String na,LinkedList<vertex> lin){
name = na;
link = lin;
color = "white";
p = null;
d = 0; //discover time
f = 0;
}
}
public static void DFS(vertex v){
if(v.color == "white"){
time = time + 1;
v.d = time; //discover time
v.color = "gray";
for (vertex u : v.link) {
if(u.color == "white"){
u.p = v;
DFS(u);
}
}
v.color = "black";
time = time +1;
v.f = time;
System.out.println(v.name+v.f);
}
}
public static void printpath(vertex s,vertex v){
if(v == s){
System.out.println(s.name);
}
else if(v.p == null){ //will not get s
System.out.println("no way");
}
else{
printpath(s,v.p);
System.out.println(v.name+v.f);
}
}
public static void main(String[] args) {
LinkedList<vertex> sl = new LinkedList<vertex>();
LinkedList<vertex> rl = new LinkedList<vertex>();
LinkedList<vertex> vl = new LinkedList<vertex>();
LinkedList<vertex> wl = new LinkedList<vertex>();
LinkedList<vertex> tl = new LinkedList<vertex>();
LinkedList<vertex> xl = new LinkedList<vertex>();
LinkedList<vertex> ul = new LinkedList<vertex>();
LinkedList<vertex> yl = new LinkedList<vertex>();
vertex sv = new vertex("s",sl);
vertex rv = new vertex("r",rl);
vertex vv = new vertex("v",vl);
vertex wv = new vertex("w",wl);
vertex tv = new vertex("t",tl);
vertex xv = new vertex("x",xl);
vertex uv = new vertex("u",ul);
vertex yv = new vertex("y",yl);
sl.add(rv);
sl.add(wv);
//rl.add(sv);
rl.add(vv);
vl.add(rv);
wl.add(xv);
wl.add(tv);
//wl.add(sv);
tl.add(xv);
tl.add(uv);
//tl.add(wv);
//xl.add(tv);
xl.add(uv);
xl.add(yv);
//xl.add(wv);
//xl.add(tv);
//xl.add(xv);
//xl.add(yv);
//xl.add(uv);
//xl.add(xv);
DFS(sv);
// printpath(sv,tv);
}
}
topological sort的更多相关文章
- 【拓扑排序】【线段树】Gym - 101102K - Topological Sort
Consider a directed graph G of N nodes and all edges (u→v) such that u < v. It is clear that this ...
- topological sort~~~~初学
今天讲了topological sort 问题: 判环:记录入队的点数,若<n则有环,可证: 算法:o(n):queue or stack,而不是o(n^2)枚举 #. 关系运算图(vijos ...
- 拓扑排序(Topological Sort)
Graph 拓扑排序(Topological Sort) 假设一个应用场景:你用 C 编写了一个爬虫工具,其中有很多自定义的库:queue.c.queue.h.stack.c.stack.h.heap ...
- Some facts about topological sort
Definition: a topological sort of a DAG G is a sort such that for all edge (i,j) in G, i precedes j. ...
- 6-16 Topological Sort(25 分)
Write a program to find the topological order in a digraph. Format of functions: bool TopSort( LGrap ...
- [Algorithms] Topological Sort
Topological sort is an important application of DFS in directed acyclic graphs (DAG). For each edge ...
- [MIT6.006] 14. Depth-First Search (DFS), Topological Sort 深度优先搜索,拓扑排序
一.深度优先搜索 它的定义是:递归探索图,必要时要回溯,同时避免重复. 关于深度优先搜索的伪代码如下: 左边DFS-Visit(V, Adj.s)是只实现visit所有连接某个特定点(例如s)的其他点 ...
- Leetcode: Alien Dictionary && Summary: Topological Sort
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
- 拓扑排序 Topological Sort
2018-05-02 16:26:07 在计算机科学领域,有向图的拓扑排序或拓扑排序是其顶点的线性排序,使得对于从顶点u到顶点v的每个有向边uv,u在排序中都在v前.例如,图形的顶点可以表示要执行的任 ...
随机推荐
- 【A tour of go】练习题
练习:循环与函数 (1)题目 为了练习函数与循环,我们来实现一个平方根函数:用牛顿法实现平方根函数. 计算机通常使用循环来计算 x 的平方根.从某个猜测的值 z 开始,我们可以根据 z² 与 x 的近 ...
- 跟踪mqttv3源码(二)
对于spring-mqtt.xml中的标签: <int-mqtt:message-driven-channel-adapter> <int-mqtt:outbound-channel ...
- 根据cid获取哔哩哔哩弹幕
def biliget(cid): headers = { "Accept": "*/*", "Accept-Language": &quo ...
- 不安全代码只会在使用 /unsafe 编译的情况下出现
在你的项目属性页面里面,把是否包含unsafe代码的选项选上
- Qt 按名称查找子节点
TreeItem* TreeModel::GetItem(QStringList& list, TreeItem* parent ,int deep) { ).toString()) { if ...
- 微信公众号手机无法直接下载APK文件是怎么回事
现在微信分享的功能很多,从分享的链接下载apk安卓包是很正常的,但是微信不让下载apk包,只能通过浏览器来下载,但是这要给用户一个提示吧,不然用户不知道 下面我们来实现,引导用户通过浏览器来下载apk ...
- 【转载】如何查看Mysql是否已经安装
原文地址: https://jingyan.baidu.com/article/fd8044fa2ecaf35030137a42.html MySQL是关系型数据库管理系统,是目前最流行的关系型数据库 ...
- winfrom程序文本框第一次选中问题
想实现这样的功能: 就是在panel中的文本框,当第一次点击文本框时,全选文本框的内容:再次选择时,可以全选,也可以部分选中, 可是文本框总是从左全部选中,还不能从右边选择,在Enter或Down事件 ...
- R语言最优化(多维)
线性搜索的最速上升法 #### max.search <- function(f, x, y, tol=1e-9, a.max = 2^5){ if(sum(abs(y)) == 0) retu ...
- Struts框架原理及应用
Struts 2框架本身大致可以分为3个部分:核心控制器FilterDispatcher.业务控制器Action和用户实现的企业业务逻辑组件. 核心控制器FilterDispatcher是Struts ...