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的更多相关文章

  1. 【拓扑排序】【线段树】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 ...

  2. topological sort~~~~初学

    今天讲了topological sort 问题: 判环:记录入队的点数,若<n则有环,可证: 算法:o(n):queue or  stack,而不是o(n^2)枚举 #. 关系运算图(vijos ...

  3. 拓扑排序(Topological Sort)

    Graph 拓扑排序(Topological Sort) 假设一个应用场景:你用 C 编写了一个爬虫工具,其中有很多自定义的库:queue.c.queue.h.stack.c.stack.h.heap ...

  4. 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. ...

  5. 6-16 Topological Sort(25 分)

    Write a program to find the topological order in a digraph. Format of functions: bool TopSort( LGrap ...

  6. [Algorithms] Topological Sort

    Topological sort is an important application of DFS in directed acyclic graphs (DAG). For each edge ...

  7. [MIT6.006] 14. Depth-First Search (DFS), Topological Sort 深度优先搜索,拓扑排序

    一.深度优先搜索 它的定义是:递归探索图,必要时要回溯,同时避免重复. 关于深度优先搜索的伪代码如下: 左边DFS-Visit(V, Adj.s)是只实现visit所有连接某个特定点(例如s)的其他点 ...

  8. Leetcode: Alien Dictionary && Summary: Topological Sort

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

  9. 拓扑排序 Topological Sort

    2018-05-02 16:26:07 在计算机科学领域,有向图的拓扑排序或拓扑排序是其顶点的线性排序,使得对于从顶点u到顶点v的每个有向边uv,u在排序中都在v前.例如,图形的顶点可以表示要执行的任 ...

随机推荐

  1. fetch请求get方式以及post提交参数为formdata类型的数据

    1.请求方式post,请求函数参数 _requestData(callback,_cityDt){ const switchIp = "http://192.168.43.103/api/p ...

  2. angular 引入ocLazyLoad实现js、controller懒加载

    项目之前是直接在index.html中引用了controller.js,其中包含了所有的controller.现引入ocLazyLoad实现按需加载,到指定页面再加载指定js.controller 1 ...

  3. springcloud-hystrix断路器对微服务的容错处理

    使用Hystrix实现微服务的容错处理 1.实现容错的手段 如果服务提供者响应的速度特别慢,那么消费者对提供者的请求就会强制等待,直到提供者响应或者超时.在高负载的情况下,如果不做任何处理,此类问题可 ...

  4. nginx 基本操作及配置

    基本操作: 1.启动nginx {global}/nginx 例如:/usr/local/Cellar/nginx/1.13.12/bin/nginx 2.重启nginx {global}/nginx ...

  5. Oracle通用维、父子维相互转换

    所谓通用维即维度层级1.2.3均作为字段展示为列,父子维即维度id+父级维度+维度层级字段 通用维 lvl_id1 lvl_name1 lvl_id2 lvl_name2 lvl_id3 lvl_na ...

  6. PHP设置凌晨时间戳

    这种需求应是很常见的,但一直没有时间整理. 一天可以领取2次奖励,今天领完了那就等明天再来. 这里面涉及到一个很重要的一点就是凌晨12点的时间戳,以前一直在前端去做判断.最近发现在后端用PHP获取凌晨 ...

  7. 使用idea搭建maven项目,结果spring-mybatis.xml文件报红“Cannot resolve file 'jdbc.properties' less... (Ctrl+F1) Inspection info:Spring XML model validation”

    错误如下图: 解决方案: 暂时只是使用右下角的highlightinglevel来屏蔽,拖动到最左边就行了

  8. 【性能测试】LoadRunner11安装(包含破解、汉化)

    LoadRunner安装(包含破解.汉化) 安装LoadRunner a.以解压包的方式打开[性能测试工具LR11.00].loadrunner-11.iso文件,运行“setup.ext”(花费时间 ...

  9. Flex 弹性盒基本语法

    网页布局(layout)是 CSS 的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂 ...

  10. RuntimeError: Model class app_anme.models.User doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.---python学习错误记录

    untimeError: Model class app_anme.models.User doesn't declare an explicit app_label and isn't in an ...