Adding in graph theory work

This commit is contained in:
Frank
2025-09-01 18:38:29 -06:00
parent 1955c8b659
commit d40e0a6e35
5 changed files with 133 additions and 2 deletions

View File

@@ -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);
}

11
src/Edge.java Normal file
View File

@@ -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;
}
}

31
src/Graph.java Normal file
View File

@@ -0,0 +1,31 @@
import java.util.ArrayList;
import java.util.List;
public class Graph {
List<Edge> graph;
public Graph() {
graph = new ArrayList<Edge>();
}
public List<Edge> 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;
}
}

View File

@@ -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<Edge> 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));

View File

@@ -1,9 +1,32 @@
public class Node {
import java.util.ArrayList;
import java.util.List;
interface GraphNode {
String name = "";
List<Edge> edges = new ArrayList<Edge>();
}
public class Node implements GraphNode {
int element;
Node next;
Node previous;
String name;
List<Edge> edges;
public Node(int element) {
this.element = element;
}
public Node(String name) {
this.name = name;
edges = new ArrayList<Edge>();
}
public void addEdge(Edge edge) {
edges.add(edge);
}
public String getName() {
return name;
}
}