Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. package dependentNodes.withoutDfs;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Stack;
  6.  
  7. class Graph{
  8. private ArrayList<Project> nodes = new ArrayList<Project>();
  9. HashMap<String,Project> map = new HashMap<String, Project>();
  10.  
  11. public Project getOrCreateNode(String name){
  12. if(!map.containsKey(name)){
  13. Project proj = new Project(name);
  14. map.put(name, proj);
  15. nodes.add(proj);
  16. }
  17. return map.get(name);
  18. }
  19. public ArrayList<Project> getNodes(){
  20. return this.nodes;
  21. }
  22. public void addEdge(String name1, String name2){
  23. Project p1 = getOrCreateNode(name1);
  24. Project p2 = getOrCreateNode(name2);
  25. p1.addChild(p2);
  26.  
  27. }
  28. }
  29.  
  30. enum State{
  31. BLANK,PARTIAL,COMPLETE;
  32. }
  33.  
  34. class Project{
  35. private String name;
  36. private ArrayList<Project> children = new ArrayList<Project>();
  37. State state = State.BLANK;
  38.  
  39. public Project(String name){
  40. this.name = name;
  41. }
  42. public String getName() {
  43. return name;
  44. }
  45. public void setName(String name) {
  46. this.name = name;
  47. }
  48. public ArrayList<Project> getChildren() {
  49. return children;
  50. }
  51. public void setChildren(ArrayList<Project> children) {
  52. this.children = children;
  53. }
  54.  
  55. public void addChild(Project child){
  56. this.children.add(child);
  57. }
  58. }
  59.  
  60. public class BuildOrderDfs {
  61. public Stack<Project> findBuildOrder(String[] projects, String [][] dependencies){
  62. Graph g = buildGraph(projects, dependencies);
  63. return buildOrder(g);
  64. }
  65. public Graph buildGraph(String [] projects, String [][] dependencies){
  66. Graph g = new Graph();
  67. for(String proj: projects)
  68. g.getOrCreateNode(proj);
  69. for (String [] dependency : dependencies){
  70. String proj1 = dependency[0];
  71. String proj2 = dependency[1];
  72. g.addEdge(proj1,proj2);
  73. }
  74. return g;
  75. }
  76. public Stack<Project> buildOrder(Graph g){
  77. //make a stack to hold the projects
  78. Stack<Project> stack = new Stack<Project>();
  79. ArrayList<Project> projects = g.getNodes();
  80.  
  81. //dfs on the projects
  82. for (Project proj: projects){
  83. if(!DFS(proj,stack))
  84. return null;
  85. }
  86. return stack;
  87. }
  88. public boolean DFS(Project proj, Stack<Project> stack){
  89. //base case
  90. if(proj == null)
  91. return true;
  92. if(proj.state == State.COMPLETE) //already visited. so no need to visit again!
  93. return true;
  94. //cycle is detected
  95. if(proj.state == State.PARTIAL)
  96. return false;
  97. proj.state = State.PARTIAL;
  98. ArrayList<Project> children = proj.getChildren();
  99. for (Project child : children){
  100. if(!DFS(child,stack))
  101. return false; //return false if any of the child has a cycle
  102. }
  103. proj.state = State.COMPLETE; //this node is visited . hence mark is complete
  104. stack.push(proj);
  105. return true;
  106.  
  107. }
  108.  
  109. public static void main (String [] args){
  110. BuildOrderDfs obj = new BuildOrderDfs();
  111. String [] projects = new String[8];
  112. projects[0] = "a";
  113. projects[1] = "b";
  114. projects[2] = "c";
  115. projects[3] = "d";
  116. projects[4] = "e";
  117. projects[5] = "f";
  118. projects[6] = "g";
  119. projects[7] = "h";
  120. String [][] dependencies = new String[9][2];
  121. dependencies[0][0] = "f";
  122. dependencies[0][1] = "c";
  123. dependencies[1][0] = "c";
  124. dependencies[1][1] = "a";
  125. dependencies[2][0] = "a";
  126. dependencies[2][1] = "e";
  127. dependencies[3][0] = "f";
  128. dependencies[3][1] = "a";
  129. dependencies[4][0] = "f";
  130. dependencies[4][1] = "b";
  131. dependencies[5][0] = "b";
  132. dependencies[5][1] = "a";
  133. dependencies[6][0] = "b";
  134. dependencies[6][1] = "e";
  135. dependencies[7][0] = "b";
  136. dependencies[7][1] = "h";
  137. dependencies[8][0] = "d";
  138. dependencies[8][1] = "g";
  139. Stack<Project> stack = obj.findBuildOrder(projects, dependencies);
  140. while (!stack.isEmpty()){
  141. System.out.println(" "+stack.peek().getName());
  142. stack.pop();
  143. }
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement