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前.例如,图形的顶点可以表示要执行的任 ...
随机推荐
- IDEA外部工具配置-OpenJML篇
帮助文档 jetbrains帮助文档:https://www.jetbrains.com/help/idea/settings-tools-external-tools.html 使用external ...
- java的抽象方法为什么不能是static、final、private?
1.java的抽象方法为什么不能用static修饰?类抽象方法? 如上代码,在抽象类中定义static属性是没有问题的,但是定义抽象方法时是不能定义为静态(static)的,否则编译器会报错:The ...
- php 基于tcpdf插件生成pdf
之前在公司做了个项目,,需要导出pdf合同,,在网上找了很久,选择用了tcpdf插件,,具体的插件网上可以搜到,中间遇到了很多的坑,慢慢的填. 先下好插件放到指定文件夹下 然后使用tcpdf插件里ht ...
- 【PAT】反转链表
PAT 乙级 1025 给定一个常数 K 以及一个单链表 L,请编写程序将 L 中每 K 个结点反转.例如:给定 L 为 1 → 2 → 3 → 4 → 5 → 6,K 为 3,则输出应该为 3 → ...
- python+selenium+xpath 爬取天眼查工商基本信息
# -*- coding:utf-8 -*-# author: kevin# CreateTime: 2018/8/16# software-version: python 3.7 import ti ...
- 【Monkey】Monkey获取包名的方式
获取包名的方式: 1.有源码:查看AndroidManifest.xml 2.只有apk: 2.1 aapt dump xmltree ColaBox.apk AndroidManifest.xml ...
- Daily record-December
December 11. All circles have the same shape. 所有圆的形状都是相同的.2. She first drew a circle on the board. 她 ...
- Vue中的钩子
每个Vue实例被创建后都要经历初始化过程.在这个过程中也会运行一些叫做生命周期钩子的函数,方便用户在不同阶段进行不同的代码实现. 1.Created 在实例创建完成后立即执行的函数. <!DOC ...
- JAVAEE第四周
一.定义 Async函数是Generator函数的语法糖,但相较来说比Generator函数更强大一些.主要用于1解决一次异步调用异步函数的问题,即当第一个异步调用结束后,再调用第二个异步函数:等第二 ...
- 解决Warning: mysql_connect(): Headers and client library minor version mismatch. 警告
php -i|grep Client 查询当前Client 版本,结果如下: Client API version => 5.6.31Client API library version =&g ...