Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. package cpecmu.cpe218.sp2019.hw1.submit;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.List;
  7. import java.util.Scanner;
  8.  
  9. import cpecmu.cpe218.sp2019.Graph;
  10. import cpecmu.cpe218.sp2019.GraphAdjList;
  11. import cpecmu.cpe218.sp2019.Pair;
  12. import cpecmu.cpe218.sp2019.hw1.DAGger;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. import java.util.Queue;
  16. import java.util.ArrayList;
  17. import java.util.LinkedList;
  18. import java.util.Stack;
  19.  
  20. public class DAGgerImpl implements DAGger {
  21.  
  22. /** input file name */
  23. protected static final String inFile = "hw1tests/dagger03.in";
  24.  
  25. @Override
  26. public <V> Pair<Boolean, List<V>> hasTopoOrCycle(Graph<V> g) {
  27. Map<V,Boolean> visited = new HashMap();
  28. Stack<V> stack = new Stack<V>();
  29.  
  30. for(V v:g.vertices()) {
  31. visited.put(v, false);
  32. }
  33.  
  34. //findSCC
  35. for(V v : g.vertices()){
  36. if(visited.get(v)==false){
  37. fillOrder(g,visited,v,stack);
  38. }
  39. }
  40. Graph <V> revG = revGraph(g);
  41. //mark all v not visited for sec DFS
  42. for(V v:g.vertices()) {
  43. visited.put(v, false);
  44. }
  45.  
  46. while(stack.empty()==false){
  47. V pop = stack.pop();
  48. if(visited.get(pop)==false){
  49. DFS(revG,visited,pop);
  50.  
  51. System.out.println();
  52.  
  53. }
  54. }
  55.  
  56.  
  57. return new Pair<>(true,null);
  58.  
  59.  
  60. }
  61.  
  62. public <V> Graph<V> revGraph(Graph<V> g){
  63. Graph<V> revG = new GraphAdjList();
  64. for(V v : g.vertices()){
  65. revG.addVertex(v);
  66.  
  67. }
  68. for(V v: g.vertices()){
  69. for(V w: g.adjacentFrom(v)){
  70. revG.addEdge(w, v);
  71. }
  72. }
  73. /*
  74. for(V v: revG.vertices()){
  75. System.out.println(v + " ->" + revG.adjacentFrom(v));
  76. }
  77. */
  78. return revG;
  79. }
  80. public <V> void fillOrder(Graph<V> g,Map<V,Boolean> visited,V v,Stack stack){
  81. visited.put(v, true);
  82. for(V w: g.adjacentFrom(v)){
  83. //System.out.print(visited.get(w)+" ");
  84. if(visited.get(w)==false){
  85.  
  86. fillOrder(g,visited,w,stack);
  87.  
  88. }
  89. }
  90. stack.push(v);
  91. }
  92. public <V> void DFS(Graph<V> g, Map<V,Boolean> visited ,V v){
  93.  
  94. visited.put(v, true);
  95. System.out.print(v + " ");
  96.  
  97.  
  98. for(V w : g.adjacentFrom(v)){
  99. if(visited.get(w)==false){
  100.  
  101. DFS(g,visited,w);
  102.  
  103.  
  104. }
  105. }
  106.  
  107.  
  108.  
  109.  
  110. }
  111.  
  112. public static void main(String[] args) {
  113. try (FileReader fr = new FileReader(inFile);
  114. Scanner s = new Scanner(fr)) {
  115. Graph<String> g = new GraphAdjList<>();
  116. // read graph
  117. int n = s.nextInt();
  118. for (int i = 0; i < n; i++)
  119. g.addVertex(s.next());
  120. int m = s.nextInt();
  121. for (int i = 0; i < m; i++)
  122. g.addEdge(s.next(), s.next());
  123.  
  124. // invoke algorithm
  125. DAGger sbm = new DAGgerImpl();
  126. Pair<Boolean, List<String>> res = sbm.hasTopoOrCycle(g);
  127. System.out.println("Has " + (res.fst() ? "DAG" : "cycle"));
  128. System.out.println(res.snd());
  129. }
  130. catch (FileNotFoundException e) {
  131. e.printStackTrace();
  132. }
  133. catch (IOException e) {
  134. e.printStackTrace();
  135. }
  136. }
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement