Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// +++++++++++++++++++
Problem Description
// +++++++++++++++++++

Given a directed graph, design an algorithm to find out whether there is a route between two nodes
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package Q4_01_Route_Between_Nodes.v2;


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

public class Graph {
private List<Node> vertices;

public Graph() {
vertices = new ArrayList<>();
}

public void addNode(Node newNode) {
vertices.add(newNode);
}

public List<Node> getNodes() {
return vertices;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package Q4_01_Route_Between_Nodes.v2;


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

class Node {
private List<Node> 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<Node> getAdjacentNodes() {
return adjacentNodes;
}

public String getVertex() {
return vertex;
}

public int getAdjacentCount() {
return adjacentNodes.size();
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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<Node> 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<Node> 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<Node> 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;
}
}