Last active
December 22, 2017 05:29
-
-
Save clarkdo/85cf61996ddd5ea145e3fbf215843edc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.LinkedList; | |
public class DepthSearch { | |
private static final String START = "B"; | |
private static final String END = "E"; | |
public static void main(String[] args) { | |
// this graph is directional | |
Graph graph = new Graph(); | |
graph.addEdge("A", "B"); | |
graph.addEdge("A", "C"); | |
graph.addEdge("B", "A"); | |
graph.addEdge("B", "D"); | |
graph.addEdge("B", "E"); // this is the only one-way connection | |
graph.addEdge("B", "F"); | |
graph.addEdge("C", "A"); | |
graph.addEdge("C", "E"); | |
graph.addEdge("C", "F"); | |
graph.addEdge("D", "B"); | |
graph.addEdge("E", "C"); | |
graph.addEdge("E", "F"); | |
graph.addEdge("F", "B"); | |
graph.addEdge("F", "C"); | |
graph.addEdge("F", "E"); | |
LinkedList<String> visited = new LinkedList(); | |
visited.add(START); | |
new DepthSearch().depthFirst(graph, visited); | |
} | |
private void depthFirst(Graph graph, LinkedList<String> visited) { | |
LinkedList<String> nodes = graph.adjacentNodes(visited.getLast()); | |
// examine adjacent nodes | |
for (String node : nodes) { | |
if (visited.contains(node)) { | |
continue; | |
} | |
if (node.equals(END)) { | |
visited.add(node); | |
printPath(visited); | |
visited.removeLast(); | |
break; | |
} | |
} | |
for (String node : nodes) { | |
if (visited.contains(node) || node.equals(END)) { | |
continue; | |
} | |
visited.addLast(node); | |
depthFirst(graph, visited); | |
visited.removeLast(); | |
} | |
} | |
private void printPath(LinkedList<String> visited) { | |
for (String node : visited) { | |
System.out.print(node); | |
System.out.print(" "); | |
} | |
System.out.println(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.LinkedHashSet; | |
import java.util.LinkedList; | |
import java.util.Map; | |
import java.util.Set; | |
public class Graph { | |
private Map<String, LinkedHashSet<String>> map = new HashMap(); | |
public void addEdge(String node1, String node2) { | |
LinkedHashSet<String> adjacent = map.get(node1); | |
if(adjacent==null) { | |
adjacent = new LinkedHashSet(); | |
map.put(node1, adjacent); | |
} | |
adjacent.add(node2); | |
} | |
public void addTwoWayVertex(String node1, String node2) { | |
addEdge(node1, node2); | |
addEdge(node2, node1); | |
} | |
public boolean isConnected(String node1, String node2) { | |
Set adjacent = map.get(node1); | |
if(adjacent==null) { | |
return false; | |
} | |
return adjacent.contains(node2); | |
} | |
public LinkedList<String> adjacentNodes(String last) { | |
LinkedHashSet<String> adjacent = map.get(last); | |
if(adjacent==null) { | |
return new LinkedList(); | |
} | |
return new LinkedList<String>(adjacent); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
public class Search { | |
public static void main(String[] args) { | |
// this graph is directional | |
Graph graph = new Graph(); | |
graph.addEdge("A", "B"); | |
graph.addEdge("A", "C"); | |
graph.addEdge("B", "A"); | |
graph.addEdge("B", "D"); | |
graph.addEdge("B", "E"); // this is the only one-way connection | |
graph.addEdge("B", "F"); | |
graph.addEdge("C", "A"); | |
graph.addEdge("C", "E"); | |
graph.addEdge("C", "F"); | |
graph.addEdge("D", "B"); | |
graph.addEdge("E", "C"); | |
graph.addEdge("E", "F"); | |
graph.addEdge("F", "B"); | |
graph.addEdge("F", "C"); | |
graph.addEdge("F", "E"); | |
new Search().search(graph, "B", "E"); | |
System.out.printf(String.valueOf(Search.path)); | |
} | |
public static final List<List<String>> path = new ArrayList<>(); | |
public void search(Graph graph, String start, String end) { | |
LinkedList<String> visited = new LinkedList<>(); | |
visited.add(start); | |
this.depthFirst(graph, visited, end); | |
} | |
public void depthFirst(Graph graph, LinkedList<String> visited, String end) { | |
LinkedList<String> nodes = graph.adjacentNodes(visited.getLast()); | |
for (String node : nodes) { | |
if (visited.contains(node)) { | |
continue; | |
} | |
if (end.equals(node)) { | |
LinkedList<String> fork = (LinkedList<String>)visited.clone(); | |
fork.addLast(node); | |
path.add(fork); | |
continue; | |
} else { | |
visited.addLast(node); | |
depthFirst(graph, visited, end); | |
} | |
visited.removeLast(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment