Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added the BFS solution for Q4_07_Build_Order #216

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
bin/
.DS_Store
.gradle/
gradle/
Java/gradlew
Java/gradlew.bat
29 changes: 29 additions & 0 deletions Java/Ch 04. Trees and Graphs/Q4_07_Build_Order/BFS/Graph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package Q4_07_Build_Order.BFS;

import java.util.ArrayList;
import java.util.HashMap;

public class Graph {
private ArrayList<Project> nodes = new ArrayList<Project>();
private HashMap<String, Project> map = new HashMap<String, Project>();

public Project getOrCreateNode(String name) {
if (!map.containsKey(name)) {
Project node = new Project(name);
nodes.add(node);
map.put(name, node);
}

return map.get(name);
}

public void addEdge(String startName, String endName) {
Project start = getOrCreateNode(startName);
Project end = getOrCreateNode(endName);
start.addNeighbor(end);
}

public ArrayList<Project> getNodes() {
return nodes;
}
}
53 changes: 53 additions & 0 deletions Java/Ch 04. Trees and Graphs/Q4_07_Build_Order/BFS/Project.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package Q4_07_Build_Order.BFS;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Project {
private List<Project> children = new ArrayList<Project>();

private List<Project> parents = new ArrayList<Project>();

private HashMap<String, Project> map = new HashMap<String, Project>();
private String name;

private boolean processed = false;

public Project(String n) {
name = n;
}

public String getName() {
return name;
}

public void addNeighbor(Project node) {
if (!map.containsKey(node.getName())) {
children.add(node);
map.put(node.getName(), node);
node.addParent(this);
}
}

public List<Project> getChildren() {
return children;
}

public boolean isProcessed() {
return this.processed;
}

public void setProcessed(boolean processed) {
this.processed = processed;
}

public List<Project> getParents() {
return this.parents;
}

public void addParent(Project project) {
this.parents.add(project);
}

}
92 changes: 92 additions & 0 deletions Java/Ch 04. Trees and Graphs/Q4_07_Build_Order/BFS/Question.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package Q4_07_Build_Order.BFS;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

public class Question {
/* Build the graph, adding the edge (a, b) if b is dependent on a.
* Assumes a pair is listed in “build order”. The pair (a, b) in
* dependencies indicates that b depends on a and a must be built
* before b. */
public static Graph buildGraph(String[] projects, String[][] dependencies) {
Graph graph = new Graph();
for (String project : projects) {
graph.getOrCreateNode(project);
}

for (String[] dependency : dependencies) {
String first = dependency[0];
String second = dependency[1];
graph.addEdge(first, second);
}

return graph;
}

public static List<Project> orderProjects(List<Project> projects) {

LinkedList<Project> queue = new LinkedList<>();
for (Project project : projects) {
if (project.getParents().size() == 0) {
queue.add(project);
}
}

List<Project> result = new ArrayList<>();
while(!queue.isEmpty()) {
Project r = queue.removeFirst();
if(!r.isProcessed() && r.getParents().stream().allMatch(Project::isProcessed)) {
r.setProcessed(true);
result.add(r);
for (Project project : r.getChildren()) {
if(!project.isProcessed()) {
queue.add(project);
}
}
}


}
return result;
}

public static List<Project> findBuildOrder(String[] projects, String[][] dependencies) {
Graph graph = buildGraph(projects, dependencies);
return orderProjects(graph.getNodes());
}

public static List<String> buildOrderWrapper(String[] projects, String[][] dependencies) {
List<Project> buildOrder = findBuildOrder(projects, dependencies);
if (buildOrder == null) return null;
List<String> buildOrderString = buildOrder.stream().map(Project::getName).collect(Collectors.toList());
return buildOrderString;
}

public static void main(String[] args) {
String[] projects = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};
String[][] dependencies = {
{"a", "b"},
{"b", "c"},
{"a", "c"},
{"a", "c"},
{"d", "e"},
{"b", "d"},
{"e", "f"},
{"a", "f"},
{"h", "i"},
{"h", "j"},
{"i", "j"},
{"g", "j"}};
List<String> buildOrder = buildOrderWrapper(projects, dependencies);
if (buildOrder.isEmpty()) {
System.out.println("Circular Dependency.");
} else {
for (String s : buildOrder) {
System.out.println(s);
}
}
}

}