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前.例如,图形的顶点可以表示要执行的任 ...
随机推荐
- MemSQL Start[c]UP 2.0 - Round 1E. Three strings
题意:给3个字符串,问从1到min(l1,l2,l3)的长度的子串,找到从该位置长度为l,三个子串相同的三元组的个数 题解:把3个子串用分隔符串起来.然后分开统计每个节点在三个串中出现次数.最后乘起来 ...
- document.body.scrollTop和document.documentElement.scrollTop 以及值为0的问题
转自http://wo13145219.iteye.com/blog/2001598 一.先遇到document.body.scrollTop值为0的问题 做页面的时候可能会用到位置固定的层,读取do ...
- Model First 开发方式
概述 在项目一开始,没有数据库时,可以借助 EF 设计模型,然后根据模型同步完成数据库中表的创建,这就是 Model First 开发方式. 总结一点就是:现有模型再有表. 创建 Model Firs ...
- TCP三次握手的思考?
大家都知道TCP有三次握手的过程,今天我就仔细想了想为什么TCP要有三次握手 先贴一张三次握手的示意图,说明一点是在三次握手中A是在第二次握手后申请缓存资源,B是在第一次握手后申请. 其实这个问题就是 ...
- mysql插件的初始化
- AES
ES算法之理论与编程结合篇 1 前言 AES是现在使用最多的对称密钥分组密码算法,在逆向的过程中经常碰到,这几天处于离职期,有点时间,于是乎想细细的来研究一下它的原理,也算是离职的一个纪念吧. 网上的 ...
- mysql排序之ORDER BY IF、ORDER BY配合IN、TIMESTAMPDIFF、TIMESTAMPADD、FIELD
1.order by if 排序 SELECT * FROM pet ORDER BY if (species='snake',0,1),species;--species为snake的行数放置到了查 ...
- with的上下文管理
1.with with语句是在Python2.6中出现的新语句.在Python2.6以前要正确的处理涉及到异常的资源管理时,需要使用try/finally代码结构.如要实现文件在操作出现异常时也能正确 ...
- 8ci
- python 生成器(generator)的生成方式
generator包括生成器和带yield的generator函数. 写了一个生成杨辉三角的小例子: # -*- coding:utf-8 -*- def triangles(): l = [1] w ...