diff --git a/Java/Ch 04. Trees and Graphs/Q4_01_Route_Between_Nodes/Problem.txt b/Java/Ch 04. Trees and Graphs/Q4_01_Route_Between_Nodes/Problem.txt new file mode 100644 index 000000000..fc33b630c --- /dev/null +++ b/Java/Ch 04. Trees and Graphs/Q4_01_Route_Between_Nodes/Problem.txt @@ -0,0 +1,5 @@ +// +++++++++++++++++++ +Problem Description +// +++++++++++++++++++ + +Given a directed graph, design an algorithm to find out whether there is a route between two nodes \ No newline at end of file diff --git a/Java/Ch 04. Trees and Graphs/Q4_01_Route_Between_Nodes/v2/Graph.java b/Java/Ch 04. Trees and Graphs/Q4_01_Route_Between_Nodes/v2/Graph.java new file mode 100644 index 000000000..3d560b1d1 --- /dev/null +++ b/Java/Ch 04. Trees and Graphs/Q4_01_Route_Between_Nodes/v2/Graph.java @@ -0,0 +1,21 @@ +package Q4_01_Route_Between_Nodes.v2; + + +import java.util.ArrayList; +import java.util.List; + +public class Graph { + private List vertices; + + public Graph() { + vertices = new ArrayList<>(); + } + + public void addNode(Node newNode) { + vertices.add(newNode); + } + + public List getNodes() { + return vertices; + } +} \ No newline at end of file diff --git a/Java/Ch 04. Trees and Graphs/Q4_01_Route_Between_Nodes/v2/Node.java b/Java/Ch 04. Trees and Graphs/Q4_01_Route_Between_Nodes/v2/Node.java new file mode 100644 index 000000000..00358d944 --- /dev/null +++ b/Java/Ch 04. Trees and Graphs/Q4_01_Route_Between_Nodes/v2/Node.java @@ -0,0 +1,32 @@ +package Q4_01_Route_Between_Nodes.v2; + + +import java.util.ArrayList; +import java.util.List; + +class Node { + private List adjacentNodes; + private String vertex; + public Question.State state; + + public Node(String vertex) { + this.vertex = vertex; + adjacentNodes = new ArrayList<>(); + } + + public void addAdjacent(Node node) { + this.adjacentNodes.add(node); + } + + public List getAdjacentNodes() { + return adjacentNodes; + } + + public String getVertex() { + return vertex; + } + + public int getAdjacentCount() { + return adjacentNodes.size(); + } +} diff --git a/Java/Ch 04. Trees and Graphs/Q4_01_Route_Between_Nodes/v2/Problem_v2.txt b/Java/Ch 04. Trees and Graphs/Q4_01_Route_Between_Nodes/v2/Problem_v2.txt new file mode 100644 index 000000000..69a9a8c1e --- /dev/null +++ b/Java/Ch 04. Trees and Graphs/Q4_01_Route_Between_Nodes/v2/Problem_v2.txt @@ -0,0 +1,6 @@ +// +++++++++++++++++++ +Problem Description +// +++++++++++++++++++ + +Given a directed graph, design an algorithm to find out whether there is a route between two nodes. +This is an implementation of the same problem using List \ No newline at end of file diff --git a/Java/Ch 04. Trees and Graphs/Q4_01_Route_Between_Nodes/v2/Question.java b/Java/Ch 04. Trees and Graphs/Q4_01_Route_Between_Nodes/v2/Question.java new file mode 100644 index 000000000..c26e45db4 --- /dev/null +++ b/Java/Ch 04. Trees and Graphs/Q4_01_Route_Between_Nodes/v2/Question.java @@ -0,0 +1,66 @@ +package Q4_01_Route_Between_Nodes.v2; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +public class Question { + public enum State { + Unvisited, Visited; + } + + public static void main(String a[]) { + Graph g = createNewGraph(); + List nodes = g.getNodes(); + Node start = nodes.get(4); + Node end = nodes.get(5); + System.out.println(search(g, start, end)); + } + + public static Graph createNewGraph() { + Graph g = new Graph(); + List temp = new ArrayList<>(); + + temp.add(new Node("a")); + temp.add(new Node("b")); + temp.add(new Node("c")); + temp.add(new Node("d")); + temp.add(new Node("e")); + temp.add(new Node("f")); + + temp.get(0).addAdjacent(temp.get(1)); + temp.get(0).addAdjacent(temp.get(2)); + temp.get(0).addAdjacent(temp.get(3)); + temp.get(3).addAdjacent(temp.get(4)); + temp.get(4).addAdjacent(temp.get(5)); + + for (int i = 0; i < temp.size(); i++) { + g.addNode(temp.get(i)); + } + return g; + } + + public static boolean search(Graph g, Node start, Node end) { + LinkedList q = new LinkedList<>(); + for (Node u : g.getNodes()) { + u.state = State.Unvisited; + } + q.add(start); + Node u; + while (!q.isEmpty()) { + u = q.removeFirst(); + if (u != null) { + for (Node v : u.getAdjacentNodes()) { + if (v.state == State.Unvisited) { + if (v == end) { + return true; + } + q.add(v); + } + } + u.state = State.Visited; + } + } + return false; + } +}