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. MATLAB 曲线形状,粗细,颜色使用大全

    通过改变R-G-B 的值改变线条的颜色: $$\tt\Large plot(x,y,'Color',[R~~G~~B]);$$ 通过改变$c\in[1,+\infty)$的值改变线条的粗细 $$\tt ...

  2. Jupyter Notebook 修改默认打开的文件夹的位置

    初次使用Jupyter Notebook,确实好用啊!!,又好看又好用,不过还是遇到了一个问题,安装好之后,打开Jupyter Notebook 的时候,默认的文件夹的位置是C盘下面的XXX目录,但是 ...

  3. VUE初体验篇-安装

    现代前端框架大行其道,讲前端思想从操作dom的阶段,升级到操作数据的阶段.vue作为三大前端框架之一,其中平缓的学习曲线,让好多前端新手非常喜欢,应用也越来越广泛.虽然其他两个框架有facebook, ...

  4. 织梦默认编辑器换成kindEditor实操教程

    织梦默认编辑是CKeditor,要想换成kindEditor编辑器,按如下步骤操作. 先看效果图: 首先下载打包好的 kindEditor_for_dedeCMS.ZIP,下载地址:https://d ...

  5. 自己常用vs code 插件

    subline   快捷键配置插件 Auto Close Tag — 自动闭合HTML标签 Auto Rename Tag — 修改HTML标签时,自动修改匹配的标签 background — 背景 ...

  6. vue-实例生命周期钩子(不太明白)

    每个 Vue 应用都是通过用 Vue 函数创建一个新的 Vue 实例开始的: var vm = new Vue({ // 选项}) 每个 Vue 实例在被创建时都要经过一系列的初始化过程——例如,需要 ...

  7. 2015-112 ado.net2

    CRUD:create read update delete 七. 数据绑定数据列的转换 在gridview中添加<OnRowDataBound="GridView1_RowDataB ...

  8. jieba库与词云的使用——以孙子兵法为例

    1.打开cmd安装jieba库和 matplotlib. 2.打开python,输入代码.代码如下: from wordcloud import WordCloud import matplotlib ...

  9. windows异步通知I/O模型

    回声服务器端: #include <stdio.h> #include <stdlib.h> #include <WinSock2.h> #define BUF_S ...

  10. 浅谈C中操作字符串函数的用法(一)

    按照内核string.h中函数的顺序进行大概的介绍,若干函数会给出一个简单的例子.有不足之处还希望各位看到的留言告知. 一.memcpy: 函数原型:extern void * memcpy(void ...