diff --git a/src/ArraySorting.java b/src/ArraySorting.java index 6c453a6..af9bf5c 100644 --- a/src/ArraySorting.java +++ b/src/ArraySorting.java @@ -36,7 +36,7 @@ public class ArraySorting { return mid; } - // If the element is smaller than mid, hen it can only be present in left subarray + // If the element is smaller than mid, then it can only be present in left subarray if (array[mid] > targetValue) { return binarySearch(array, 1, mid - 1, targetValue); } diff --git a/src/Edge.java b/src/Edge.java new file mode 100644 index 0000000..067edd5 --- /dev/null +++ b/src/Edge.java @@ -0,0 +1,11 @@ +public class Edge { + Node source; + Node destination; + int weight; + + public Edge(Node source, Node destination, int weight) { + this.source = source; + this.destination = destination; + this.weight = weight; + } +} diff --git a/src/Graph.java b/src/Graph.java new file mode 100644 index 0000000..59c6213 --- /dev/null +++ b/src/Graph.java @@ -0,0 +1,31 @@ +import java.util.ArrayList; +import java.util.List; + +public class Graph { + List graph; + + public Graph() { + graph = new ArrayList(); + } + + public List getGraph() { + System.out.println(graph.toString()); + return graph; + } + + public void insertEdge(Edge edge) { + graph.add(edge); + } + + public int findRoute(String src, String dst) { + for(int i = 0; i < graph.size(); i++) { + String source = graph.get(i).source.getName(); + String destination = graph.get(i).destination.getName(); + if(source.equals(src) && destination.equals(dst)) { + return graph.get(i).weight; + } + } + + return 0; + } +} diff --git a/src/Main.java b/src/Main.java index af0a72e..1f7adbb 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,8 +1,74 @@ import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; public class Main { public static void main(String[] args) { + // Graphs + Graph graph = new Graph(); + + Node washington = new Node("Washington"); + Node indiana = new Node("Indiana"); + Node newYork = new Node("New York"); + Node california = new Node("California"); + Node florida = new Node("Florida"); + Edge ws2in = new Edge(washington, indiana, 5); + Edge ws2cl = new Edge(washington, california, 3); + washington.addEdge(ws2cl); + washington.addEdge(ws2in); + Edge in2ws = new Edge(indiana, washington, 5); + Edge in2ny = new Edge(indiana, newYork, 4); + Edge in2fl = new Edge(indiana, florida, 6); + indiana.addEdge(in2fl); + indiana.addEdge(in2ny); + indiana.addEdge(in2ws); + Edge ny2in = new Edge(newYork, indiana, 4); + Edge ny2fl = new Edge(newYork, florida, 7); + newYork.addEdge(ny2fl); + newYork.addEdge(ny2in); + Edge cl2ws = new Edge(california, washington, 3); + Edge cl2fl = new Edge(california, florida, 8); + california.addEdge(cl2ws); + california.addEdge(cl2fl); + Edge fl2cl = new Edge(florida, california, 8); + Edge fl2in = new Edge(florida, indiana, 6); + Edge fl2ny = new Edge(florida, newYork, 7); + florida.addEdge(fl2cl); + florida.addEdge(fl2in); + florida.addEdge(fl2ny); + + graph.insertEdge(ws2in); + graph.insertEdge(ws2cl); + graph.insertEdge(in2ws); + graph.insertEdge(in2ny); + graph.insertEdge(in2fl); + graph.insertEdge(ny2in); + graph.insertEdge(ny2fl); + graph.insertEdge(cl2ws); + graph.insertEdge(cl2fl); + graph.insertEdge(fl2cl); + graph.insertEdge(fl2in); + graph.insertEdge(fl2ny); + + List edges = graph.getGraph(); + for(int i = 0; i < edges.size(); i++) { + System.out.println("Route: " + (i + 1) + " " + edges.get(i).source.getName() + " to " + edges.get(i).destination.getName()); + } + + System.out.println(); + System.out.println("<------------------------------>"); + System.out.println(); + Scanner scanner = new Scanner(System.in); + System.out.println("Which route do you need to know?"); + System.out.print("Source: "); + String source = scanner.nextLine(); + System.out.print("Destination: "); + String destination = scanner.nextLine(); + System.out.println("<------------------------------>"); + int value = graph.findRoute(source, destination); + System.out.println("Route Cost: " + value); + // END: Graphs // System.out.println(addTotals(2.5, 2.5)); // System.out.println(addTotals(5, 5)); diff --git a/src/Node.java b/src/Node.java index dcaa80d..c9b2877 100644 --- a/src/Node.java +++ b/src/Node.java @@ -1,9 +1,32 @@ -public class Node { +import java.util.ArrayList; +import java.util.List; + +interface GraphNode { + String name = ""; + List edges = new ArrayList(); +} + +public class Node implements GraphNode { int element; Node next; Node previous; + String name; + List edges; public Node(int element) { this.element = element; } + + public Node(String name) { + this.name = name; + edges = new ArrayList(); + } + + public void addEdge(Edge edge) { + edges.add(edge); + } + + public String getName() { + return name; + } }