zoukankan      html  css  js  c++  java
  • 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.)

      1 package element_graph;
      2 
      3 import java.util.LinkedList;
      4 
      5 
      6 
      7 
      8 
      9 
     10 
     11 public class deapth_first_search {
     12     public static int time = 0;
     13     private static class vertex{
     14         private LinkedList<vertex> link;
     15         private String name;
     16         private String color;
     17         private vertex p;
     18         private int d,f;
     19         public vertex(String na,LinkedList<vertex> lin){
     20             name = na;
     21             link = lin;
     22             color = "white";
     23             p = null;
     24             d = 0;   //discover time
     25             f = 0;
     26         }
     27     }
     28     public static void DFS(vertex v){
     29         if(v.color == "white"){
     30             time = time + 1;
     31             v.d = time;   //discover time
     32             v.color = "gray";
     33             for (vertex u : v.link) {
     34                 if(u.color == "white"){
     35                     u.p = v;
     36                     DFS(u);
     37                 }
     38             }
     39             v.color = "black";
     40             time = time +1;
     41             v.f = time;
     42             System.out.println(v.name+v.f);
     43         }
     44     }
     45     public static void printpath(vertex s,vertex v){
     46         if(v == s){
     47             System.out.println(s.name);
     48         }
     49         else if(v.p == null){  //will not get s 
     50             System.out.println("no way");
     51         }
     52         else{
     53             printpath(s,v.p);
     54             System.out.println(v.name+v.f);
     55         }
     56     }
     57      public static void main(String[] args) {
     58             LinkedList<vertex> sl = new LinkedList<vertex>();
     59         
     60             LinkedList<vertex> rl = new LinkedList<vertex>();
     61             
     62             LinkedList<vertex> vl = new LinkedList<vertex>();
     63             
     64             LinkedList<vertex> wl = new LinkedList<vertex>();
     65             
     66             LinkedList<vertex> tl = new LinkedList<vertex>();
     67             
     68             LinkedList<vertex> xl = new LinkedList<vertex>();
     69             
     70             LinkedList<vertex> ul = new LinkedList<vertex>();
     71             
     72             LinkedList<vertex> yl = new LinkedList<vertex>();
     73             
     74             vertex sv = new vertex("s",sl);
     75             vertex rv = new vertex("r",rl);
     76             vertex vv = new vertex("v",vl);
     77             vertex wv = new vertex("w",wl);
     78             vertex tv = new vertex("t",tl);
     79             vertex xv = new vertex("x",xl);
     80             vertex uv = new vertex("u",ul);
     81             vertex yv = new vertex("y",yl);
     82             sl.add(rv);
     83             sl.add(wv);
     84             //rl.add(sv);
     85             rl.add(vv);
     86             vl.add(rv);
     87             wl.add(xv);
     88             wl.add(tv);
     89             //wl.add(sv);
     90             tl.add(xv);
     91             tl.add(uv);
     92             //tl.add(wv);
     93             //xl.add(tv);
     94             xl.add(uv);
     95             xl.add(yv);
     96             //xl.add(wv);
     97             //xl.add(tv);
     98             //xl.add(xv);
     99             //xl.add(yv);
    100             //xl.add(uv);
    101             //xl.add(xv);
    102              DFS(sv);
    103             // printpath(sv,tv);
    104             
    105         }
    106 }
  • 相关阅读:
    uniapp 常用的基础知识
    uniapp接口请求(第一种)
    系统对象的使用——Cookie,ViewState,Session,Application
    网页跳转
    存储过程的使用——游标
    存储过程的使用——循环,条件语句
    存储过程的使用——定义变量
    页面读取英语单词
    加密
    XMind: ZEN 快捷键
  • 原文地址:https://www.cnblogs.com/wujunde/p/7147820.html
Copyright © 2011-2022 走看看